Integrate Flexigrid using CakePHP

Create a component

i.e FlexigridComponent

code :

class FlexigridComponent extends Component
{
var $name = ‘FlexigridComponent’;

function create($options)
{
if (!isset($options['sortby']))
$options['sortby'] = ‘id’;
if (!isset($options['sortorder']))
$options['sortorder'] = ‘asc’;
$flexigrid =
“$(document).ready(function()
{
$(\”#”.$options['id'].”\”).flexigrid
(
{
url: ‘”.$options['url'].”‘,
dataType: ‘json’,
method: ‘POST’,
colModel : [";
foreach ($options['columns'] as $column)
{
$flexigrid .= “{display: ‘”.$column['display'].”‘, name : ‘”.$column['name'].”‘, width : ‘”.$column['width'].”‘, sortable : “.$column['sortable'].”, align: ‘”.$column['align'].”‘”;
if(isset($column['hide']))
$flexigrid .= “, hide: ‘”.$column['hide'].”‘”;
$flexigrid .= “},”;
}
$flexigrid = substr($flexigrid, 0, -1);
$flexigrid .= “],
buttons : [";

foreach ($options['buttons'] as $button)
{
if ($button['name'] == ‘separator’)
$flexigrid .= “{separator: true},”;
else
{
$flexigrid .= “{name: ‘”.$button['name'].”‘, bclass: ‘”.$button['cssclass'].”‘”;
if(isset($button['onpress']))
$flexigrid .= “, onpress : “.$button['onpress'];
$flexigrid .= “},”;
}
}
$flexigrid = substr($flexigrid, 0, -1);

$flexigrid .= ”
],”;

if(isset($options['searchitems']))
{
$flexigrid .= “searchitems : [";
foreach ($options['searchitems'] as $searchitem)
{
$flexigrid .= “{display: ‘”.$searchitem['display'].”‘, name : ‘”.$searchitem['name'].”‘},”;
}
$flexigrid .= “    ],”;
}
$flexigrid .= ”
sortname: \”".$options['sortby'].”\”,
sortorder: \”".$options['sortorder'].”\”,
usepager: “.$options['usepager'].”,
showToggleBtn: false,
useRp: true,
rp: “.$options['rp'].”,
resizable: “.$options['resizable'].”,
showTableToggleBtn: false,
width: ‘auto’,
height: “.$options['height'].”,
showCheckbox: “.$options['showCheckbox'].”
}
);

});
flexcolumns = ‘”;

foreach ($options['columns'] as $column)
$flexigrid .= $column['name'].’, ‘;

$flexigrid = substr($flexigrid, 0, -2);
$flexigrid .= “‘;”;
return $flexigrid;
}
}

 

Now inherit component in your controller

and write below code in it function of controller for which you want to display flexigrid

code:

$this->set(‘flexigrid’,$this->Flexigrid->create(array(‘id’ => ‘<DIV>’,
‘title’ => ‘<TITLE>’,
‘url’ => ‘<URL>’,
‘width’ => ‘auto’,
‘height’ => ’346′,
‘showCheckbox’ => “false”,
‘usepager’ => ‘true’,
‘rp’ => 10,
‘resizable’=>’true’,
‘columns’ => array(array(‘display’ => ‘Field1′,
‘name’    => ‘Field1′,
‘width’   => ’30′,
‘sortable’ => ‘false’,
‘align’    => ‘left’),
array(‘display’ => ‘Field2′,
‘name’    => ‘Field2′,
‘width’   => ’300′,
‘sortable’ => ‘false’,
‘align’    => ‘left’),
array(‘display’ => ‘Field3′,
‘name’    => ‘Field3′,
‘width’   => ’10′,
‘sortable’ => ‘false’,
‘align’    => ‘left’,
‘hide’    => true),
array(‘display’ => ‘Field4′,
‘name’    => ‘Field4′,
‘width’   => ’100′,
‘sortable’ => ‘false’,
‘align’    => ‘left’),
array(‘display’ => ‘Field5′,
‘name’    => ‘Field5′,
‘width’   => ’75′,
‘sortable’ => ‘false’,
‘align’    => ‘left’),
array(‘display’ => ‘Field6′,
‘name’    => ‘Field6′,
‘width’   => ’200′,
‘sortable’ => ‘false’,
‘align’    => ‘left’),
array(‘display’ => ‘Field7′,
‘name’    => ‘Field7′,
‘width’   => ’200′,
‘sortable’ => ‘false’,
‘align’    => ‘left’)),
‘buttons’ => array(array(‘name’ => ‘Edit’,
‘cssclass’    => ‘edit’
),
array(‘name’ => ‘Delete’,
‘cssclass’    => ‘delete’,
‘onpress’   => ‘doDelete’),
array(‘name’ => ‘Add’,
‘cssclass’    => ‘add’,
‘onpress’   => ‘doAdd’),
array(‘name’ => ‘separator’))
)));

 

 

After it add below code in your ctp file

code:

<?php echo $flexigrid; ?>

<div id=”<DIV>”></div>

 

Thats all.

Your flexigrid is ready. Besure your url return data in JSON  or change data type from component.

Namespaces in PHP

After long waiting -  Namespaces  are introduced in PHP.

Let we start with Why we need Namespaces  ?

Just think A child got admission in new school, He made 2 new friends in 1st week let name 1st as ‘chirag’ and 2nd as ‘jatin’, He use to tell his parents story about his friends i.e chirag do this, jatin do that bla bla bla. After a month there is huge difference in his friend circle, now no. of friends in his circle increased from 2 to 20, And among those 20 there are 5 ‘chirag’, Now what …How he will clear his parents  that the story he is saying is about which ‘chirag’ ?  Smart kid he started using last name.

I used above example to illustrates  the concept of namespaces. A namespace is named container used for identifiers like class name and function name used in programs. Namespaces group them together and remove the possibility of name clashes.  In the above example last name of student served as namespaces.

Large applications often have hundreds or thousands of components, which result  in  large number of name clashes. Namespaces allow you to group components and avoid this kind of  problem.

Before namespaces were available, We normally use to prefix each components class with component name such as a class register in user component named as user_register Some time this result into very long naming convention. This practice result into long  and sometimes confusing class names.

How we can define Namespace

code in Namespace is defined using a single namespace keyword at the top  PHP file. It must be the first command (with the exception of declare) and no non-PHP code, HTML, or white-space can prefix the command.

The code following this line will be assigned to the ‘MyFirstNamespace’ namespace. It is not possible to nest namespaces or define more than one namespace for the same code block (only the last will be recognized). You can define different  code for namespace in the same file, e.g.

<?php
namespace MyFirstNamespace;
// PHP code for the MyFirstNamespacenamespace
namespace MyFirstNamespace2;
// PHP code for the MyFirstNamespace2namespace
// Alternative syntax
namespace MyFirstNamespace3{
	// PHP code for the MyFirstNamespace3
}
?>

Its possible, Even though I would advice against it, Use single namespace per file.

Calling Namespace Code

In a file named namespacelib1, we will define a constant, a function, and a class within the App\NamespaceLib1  namespace:

namespacelib1.php


<?php
namespace App\NamespaceLib1;
const CONST1 = 'App\NamespaceLib1\CONST1';
function Function1() {
	return __FUNCTION__;
}
class Class1 {
	static function FirstFunction() {
		return __METHOD__;
	}
}
?>

We can now include this code in another PHP file, e.g.

namespacelib2.php


<?php
header('Content-type: text/plain');
require_once('namespacelib1.php');
echo \App\NamespaceLib1 \CONST1 . "\n";
echo \App\NamespaceLib1 \Function1() . "\n";
echo \App\NamespaceLib1 \Class1::FirstFunction() . "\n";
?>

No namespace is defined in NamespaceLib2.php so the code exists in the global space. Any direct reference to CONST1 , Function1 or Class1will fail because they exist in the App\NamespaceLib1 namespace. To call code in NamespaceLib1.php, we can add a prefix of \App\NamespaceLib1 to define fully-qualified names. The following result is output when we load namespacelib2.php:

App\NamespaceLib1\CONST1
App\NamespaceLib1\Function1
App\NamespaceLib1\Class1::FirstFunction

Fully-qualified names can become quite long and there are few obvious benefits over defining long class names such as App-NamespaceLib1-Class1.

In next article we will discuss how Exactly Namespace work in PHP.