How to add image browse and image upload button in ckeditor

Others

Posted on 27 Aug 15 by Vickey

Ckeditor is a most populer text editor.We can change a textarea in to editor very easyly.But in some cases we need some extra functionality in our editor and we can fulfill these needs by adding some extra plugin.For example Color Button,Mathematical Formulas etc. you can use these addon or plugins by putting these in your ckeditor's plugin directory.If you want to insert an image in your editor you need to add image url but you can not upload an image with ckeditor.

 But by using some plugins you can do this. so lets add image upload functionality in ckeditor

1. Download Imageupload plugin

2. Download imgbrowse plugin

3. Extract these in to ckeditor plugins directory

4. Open your ckeditor config.js file and add extraplugin code

CKEDITOR.editorConfig = function( config ) {

	config.filebrowserBrowseUrl='/ckeditor/plugins/imagebrows/imgbrowse.html';
	config.filebrowserImageUploadUrl='/ckeditor/plugins/imgupload/imgupload.php';
};

5. Now open /ckeditor/plugins/imagebrows/imgbrowse.php and /ckeditor/plugins/imgupload/imgupload.php files and give your upload directory path

protected $root = '/upload_dir/';

$upload_dir = '/upload_dir/';

and you have done

 

How to set template by using library in codeigniter

Codeigniter

Posted on 25 Aug 15 by Vickey

By using library we can set themes in a codeigniter app. In codeigniter by defualt we load header and footer in every action.

public function index()
{
    $this->load->view('header');
    $this->load->view('index');
    $this->load->view('footer');
}

But if we use a library for this we can make it easy. so lets create a easy theme for codeigniter

1.Go to /application/libraries directory and create a file 'Layout.php' and add the below code

    class Layout 
    {
        var $codeigniter;
        function __construct() 
        {
            $this->codeigniter =& get_instance();
        }
        
        function load($template, $page_content = null, $data = null) 
        {
            
            if ( ! is_null( $page_content )) 
            {
                
                if (file_exists(APPPATH.'views/'.$template.'/'.$page_content )) 
                {
                    $page_content_path = $template.'/'.$page_content;
                }
                else if (file_exists(APPPATH.'views/'.$template.'/'.$page_content.'.php' )) 
                {
                    $page_content_path = $template.'/'.$page_content.'.php';
                }
                else if (file_exists(APPPATH.'views/'.$page_content )) 
                {
                    $page_content_path = $page_content;
                }
                else if (file_exists(APPPATH.'views/'.$page_content.'.php' )) 
                {
                    $page_content_path = $page_content.'.php';
                }
                else
                {
                    show_error('Requested view can not be load: ' . $template.'/'.$page_content.'.php');
                }
                 
                $content = $this->codeigniter->load->view($page_content_path, $data, TRUE);
                 
                if (is_null($data)) 
                {
                    $data = array('content' => $content);
                }
                else if (is_array($data))
                {
                    $data['content'] = $content;
                }
                else if (is_object($data))
                {
                    $data->body = $content;
                }
            }
            $this->codeigniter->load->view('layouts/'.$template, $data);
            
        }
        
    }

2. go to directory /application/config and open file 'autoload.php' and here set your template library

$autoload['libraries'] = array('Layout','database');

it will load your layout library

3. Now go to /application/views and create two file 'header.php' and 'footer.php' and put your html code accordingly

4. Under application/views  create a new directory 'layouts' and create a file 'main.php' under this and add the below code in the file

<?php $this->load->view('header'); ?>
<?php echo $content; ?>
<?php $this->load->view('footer'); ?>

you have created your first theme you can use this by using the below code

public function index()
{
    $data = array('title' => 'Your title',);
    $this->layout->load('main', 'content', $data);
}

here content is respected view of this action and main is the template name

How to use wepay payment gateway in cakephp

Cakephp

Posted on 22 Aug 15 by Vickey

Wepay is a best payment gateway if you want to use croudfunding. In we pay payment gateway we can hold payment and capture it later when we want.For using we pay you need to create wepay stage account and download sdk from wepay site.In this post I am integration wepay in cakephp so for using wepay with cakephp follow these steps

1.Login in wepay and download sdk for php also take your  WEPAY CLIENT ID, WEPAY CLIENT SECRET,WEPAY ACCESS TOKEN and WEPAY ACCOUNT ID and define these in your bootstrap file or in app controller

define(WEPAY_CLIENT_ID,'tertre');
define(WEPAY_CLIENT_SECRET,'tedgt435ertrtre');
define(WEPAY_ACCESS_TOKEN,'gdfgterte5455');
define(WEPAY_ACCOUNT_ID,'345345');

2. Put your wepay sdk in app/Vender directory and import it in your controller

App::import('Vendor', 'wepay/wepay');

Now you are ready to use wepay code in your actions here are some example code for make payment with wepay

In the below code payment will be on hold beacuse I used 'auto_capture'     => FALSE if you do not want it on hold you can use TRUE

App::import('Vendor', 'wepay/wepay');
public function start_payment()
{
    $client_id = WEPAY_CLIENT_ID;
    $client_secret = WEPAY_CLIENT_SECRET;
    $access_token = WEPAY_ACCESS_TOKEN;
    $account_id = WEPAY_ACCOUNT_ID;
    
    $amount=10;
    $paymentReferenceId=22; // IF YOU ARE UPDATING YOUR DATABASE IT WILL BE THE PRIMARY KEY
    
    Wepay::useStaging($client_id, $client_secret);
    $wepay = new WePay($access_token);
    $response = $wepay->request('checkout/create', array(
    
	'account_id'        => $account_id,
	'amount'            => $amount,
	'short_description' => 'We pay testing',
	'reference_id' => $paymentReferenceId,
	'type'              => 'GOODS',
	'mode'=>'iframe',
	'fee_payer' => "Payee", 
	'redirect_uri' => "PUT HERE YOUR REDIRECT URL", 
	'callback_uri' => "GIVE YOUR IPN URL", 
	'auto_capture'     => FALSE
	
    ));
    $html='';
    $html	.='<div id="paymentBox"></div>';		
    $html .='<script type="text/javascript">';
    $html .='WePay.iframe_checkout("paymentBox", "'.$response->checkout_uri.'");';
    $html .='</script>';
    
    //$this->redirect($response->checkout_uri); // YOU WILL REDIRECT ON WEPAY FOR MAKE PAYMENT
}

and this code for ipn  if you want any responce after payment or you want to save payment record in your database

public function ipn(){
    
    $client_id = WEPAY_CLIENT_ID;
    $client_secret = WEPAY_CLIENT_SECRET;
    $access_token = WEPAY_ACCESS_TOKEN;
    $account_id = WEPAY_ACCOUNT_ID;
    
    Wepay::useStaging($client_id, $client_secret);
    $wepay = new WePay($access_token);
    $checkout = $wepay->request('checkout', array(
	'checkout_id' => $_POST['checkout_id'], 
    ));
    
    $reference_Id	=	$_POST['reference_id'];
    $checkout_id	=	$_POST['checkout_id'];
    $processing_fee	=	$checkout->fee;
    
    // SAVE checkout_id TO DATABASE
   
    
}

you can capture your payment by the below code

public function capture_funds(){
    
    $client_id = WEPAY_CLIENT_ID;
    $client_secret = WEPAY_CLIENT_SECRET;
    $access_token = WEPAY_ACCESS_TOKEN;
    $account_id = WEPAY_ACCOUNT_ID;
    
    //// FETCH RECORD FROM DATABASE
    $checkout_id='FETCH RECORD FROM DATABASE';
    
    Wepay::useStaging($client_id, $client_secret);
    $wepay = new WePay($access_token);
    $response = $wepay->request('checkout/capture', array(
	'checkout_id' => $checkout_id 
    ));
    
    
}

I hope it will help you for any question or query please comment

Thanks

Preview an image before upload on server

Jquery

Posted on 10 Aug 15 by Vickey

Html5 provide us an easy way to preview an image before upload it with the help of File API. File API is used for create a thumbnail preview of a image file before sending it to the server.With the help of File API you can get the file size and extension on the client side.Here I have an example code for preview a image before upload.

javascript Code

       <script>
       $( document ).ready(function() {
            $("#imagefile").change(function ()
              {
                     $("#img").show();
                     $("#img").attr("src",'http://blog.teamtreehouse.com/wp-content/uploads/2015/05/InternetSlowdown_Day.gif');
                     if (typeof(FileReader)!="undefined"){
                       
                         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png)$/;
                         $($(this)[0].files).each(function () {
                             var getfile = $(this);
                             if (regex.test(getfile[0].name.toLowerCase())) {
                                 var reader = new FileReader();
                                 reader.onload = function (e) {
                                     $("#img").attr("src",e.target.result);
                                 }
                                 reader.readAsDataURL(getfile[0]);
                             } else {
                                 alert(getfile[0].name + " is not image file.");
                                 return false;
                             }
                         });
                     }
                     else {
                         alert("Browser does not supportFileReader.");
                     }
            });
        });
    </script>

Html Code

       <div>
              <input id="imagefile" type="file" multiple="multiple" />
              <div id="showfile"><img src="" width="200" height="200" id="img" style="display: none;"></div>
       </div>

 

How to retrieving data in objects not array in cakephp

Cakephp

Posted on 06 Aug 15 by Vickey

Cakephp by defualt return data as an array.For example if I want fetch a user's record from database i will use the below function and it will return
data in array form

public function index() {
        
        $data=$this->User->findById(4);
        pr($data); die;
        
}
Array
(
    [User] => Array
        (
            [id] => 4
            [name] => Kamryn Smitham
            [email] => ischmidt@hotmail.com
            [password] => qui
            [remember_token] => 
            [created_at] => 2015-05-08 09:22:18
            [updated_at] => 2015-05-08 09:22:18
        )

)

but in some cases we need data in objects so for get data in objects form we use 'Set::map()' function

public function index() {
        
        $data=$this->User->findById(4);
        $data = Set::map($data);
        pr($data); die;
        
}
stdClass Object
(
    [_name_] => User
    [id] => 4
    [name] => Kamryn Smitham
    [email] => ischmidt@hotmail.com
    [password] => qui
    [remember_token] => 
    [created_at] => 2015-05-08 09:22:18
    [updated_at] => 2015-05-08 09:22:18
)

here we get data as objects

What is the use of namespace in php

php

Posted on 06 Aug 15 by Vickey

The word 'namespace' come in PHP version 5.3 by using namespace we can define more than one classes with the same name.If we have two or more class with the same name for example if we have a third party class which we are using in our project and we have also a core class with the same name then
when we create the object of class it will return a fatal error 'Fatal error: Cannot redeclare class'

class Login
{
    public function do_login()
    {
        return 'I am login as admin';
    }
}

class Login
{
    public function do_login()
    {
        return 'I am login as User';
    }
}

class Login
{
    public function do_login()
    {
        return 'Login By Facebook';
    }
}

$myobj = new Login();
echo $myobj->do_login();

But we can fix this error by using namespace. we will create a different namespace for each class

namespace Myproject\Admin;
class Login
{
    public function do_login()
    {
        return 'I am login as admin';
    }
}


namespace Myproject\User;
class Login
{
    public function do_login()
    {
        return 'I am login as User';
    }
}



namespace Myproject\FacebookLogin;
class Login
{
    public function do_login()
    {
        return 'Login By Facebook';
    }
}

$myobj = new Login();
echo $myobj->do_login();

now it will not give error but it will use the last class and you will see it will print 'Login By Facebook' but if want to use the all classes then there is need to use namespace
in this case we will create class object by using name space

$myobj = new \Myproject\Admin\Login();
echo $myobj->do_login();


$myobj = new \Myproject\User\Login();
echo $myobj->do_login();

$myobj = new \Myproject\FacebookLogin\Login();
echo $myobj->do_login();

or we can use this also

use Myproject\Admin\Login as admin_login;
$myobj = new admin_login();
echo $myobj->do_login();

 

How get an address from latitude and longitude by using google api

php

Posted on 05 Aug 15 by Vickey

By using google api we can get latitude and longitude of a given location and we can also get the location details if we have latitude and longitude of that location.

With the help of below code you can get location details if you have latitude and longitude of a location

echo getAddressByLatLong('-4734774834','005347587');

public function getAddressByLatLong($loc_latitude='',loc_longitude='')
{
    $loc_latitude=trim($loc_latitude);
    $loc_longitude=trim($loc_longitude);
    $api_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$loc_latitude.','.$loc_longitude.'&sensor=false';
    
    $location_record = @file_get_contents($api_url);
    $location_data=json_decode($location_record);

    $location_status = $location_data->status;
    if($location_status=="OK")
        return $location_data->results[0]->formatted_address;
    else
        return false;
}

 

Change the default template engine in yii2

Yii

Posted on 03 Aug 15 by Vickey

Yii use php as the template engine by default but we can change different template engine for example we can use smarty, twig and others
In this post I will use smarty as the template engine with yii. I am using yii2 and i am working on Ubuntu operating System with xampp. so lets start

1. Go to Yii root directory and you will see a file here name as 'composer.json', open this and search the like given below

  "require": {
  
        "php": ">=5.4.0",
        some code here
        some code here
        some code here
        some code here
        
        
    },

now add the below code
      

  "yiisoft/yii2-smarty": "*"


you should see something like this
    

      "require": {
  
        "php": ">=5.4.0",
        some code here,
        some code here,
        some code here,
        some code here,
        
        "yiisoft/yii2-smarty": "*"
        
    },

now go to directory name as  'config' and open web.php file and search text 'components'   and add the below code
    

    'components' => [
    
    
    
            'view' => [
                'renderers' => [
                        'tpl' => [
                        'class' => 'yii\smarty\ViewRenderer',
                    ],
                ],
            ],


 Now open you terminal and locate in yii root folder
                   

cd /opt/lampp/htdocs/yiiroot

 and update composer

sudo /opt/lampp/bin/php /usr/local/bin/composer update

( in window you need different command   'composer update')
            
after installation complete go to your controller and change your render view name
            
 return $this->render('index.tpl');

 now in your views create a file index.tpl and put your smarty code here and open your webpage you see your new tpl file content
            
            
            
            
            
            
            
            
    
    

How to write ajax responce in window popup

Js

Posted on 15 Jul 15 by Vickey

We have many popup plugins and library for open a popup up with the dynamic content. but we can use widnow popup also, which is browser property. we can open our ajax responce in window popup

function openPopup(){

    $.ajax({
    
        type: "POST",
        url: "url here",
        success: function(response){
	  var win=window.open('about:blank','popup','width=600,height=200')
	  with(win.document)
	  {
	    open();
	    write(response);
	    close();
	  }
        }
    });
}

 

What is function overriding vs overloading in php oops

Object Oriented Programming

Posted on 19 Jun 15 by Vickey

Function Overriding

In the sence of Overriding is to replace parent property in child.In Overriding if we have defined a function in base class and we want to change the
its basic functionality in child class then we can do this and this is called function Overriding for example

class BaseController {
{
    public function dbConnection(){

        $host = "localhost";
        $username = "root";
        $password = "root";
        $connection = new mysqli($host, $username, $password);

    }
}



class UsersController extends BaseController {

    public function dbConnection(){

        $host = "192.168.2.00";
        $username = "root";
        $password = "PASSWORD123";
        $connection = new mysqli($host, $username, $password);

    }
}

as you cann see in base class i have a method dbConnection with code for make database connection but in child class
i have change the host and password for database connection

Function Overloading

In programming the function Overloading means is create two function with same name with different functionality by passing different arguments.
but in php we can not create two function with the same name so if we want to implement overloading in php we can take help of _call function
Here is a simple example of function overloading by using of _call function

class Calculate{

    public function __call($name, $arguments){

        if($name='sum'){

            if(count($arguments) === 2 ){
                return $this->sum1($arguments[0],$arguments[1]);
            }
            if(count($arguments) === 3){
                return $this->sum2($arguments[0], $arguments[1],$arguments[2]);
            }
        }
    }
    public function sum1($a,$b){
        echo $a+$b;
    }

    public function sum2($a,$b,$c){
        echo $a+$b+$c;
    }

}
$obj=new Calculate();
echo $obj->sum(1,2);
echo "
";
echo $obj->sum(1,2,3);

here _call magic function manage the overloading by calling different function on basis of number of arguments