Friday, September 12, 2014

Cakephp : Reading an element content from with in a component

If you ever need to read the content (rendered of otherwise) of an element from component this is how to do it.
This is based on V 2.3

//vie file in /View/Elements/templates/prolfile.ctp

$view = new View();
$view->set(compact('data', $data)); //$data will be pass in to the element.  
$view->viewPath = 'Elements'; 

$content = $view->element('templates/profile');



Friday, June 14, 2013

Installing CakePhp DebugKit on Croogo.

This is a method to install the default debugkit for cakephp in to the croogo cms.

Copy debug_kit to app/Plugin/DebugKit
Create these in app/Plugin/DebugKit
- DIR : Config
- File : app/Plugin/DebugKit\Config\plugin.json
- File : app/Plugin/DebugKit\Config\bootstrap.php


plugin.json (modify author details)
{
 "name": "DebugKit",
 "description": "DebugKit",

 "author": "Author Name",
 "authorEmail": "author@example.com",
 "authorUrl": "http://example.com",

 "dependencies": {
  "plugins": [
    "extensions"
  ]
 }
}

bootstrap.php
Croogo::hookComponent('*', 'DebugKit.Toolbar');

Activate DebugKit plugin Croogo Admin -> Extensions -> Plugins

Thanks to Rachman Chavik from CakePHP & Croogo IRC channel

Sunday, August 19, 2012

Simple script to auto load css/js from webroot - CakePhp

This is very simple script to auto-load all css/js files from the default webroot/css, webroot/js folders.

Script isnt flexible and feel free to build on it and use it in your apps

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class AutoloadHelper extends AppHelper {
   
    var $helpers = array('Javascript', 'Html');
    
    function js()
    {
     $this->walker('js');
    }
    
    function css()
    {
     $this->walker('css');
    }


    private function walker( $subpath = "")
    {
     $path = WWW_ROOT .$subpath;
     
     $dir = new Folder($path);
     $files = $dir->find('.*\.'. $subpath);
     
     
     foreach($files as $file)
     {
      if($subpath == 'css')
      {
       echo $this->Html->css($file);
      }else
      {
       echo $this->Html->script($file);
      }
     }
    }
}



in your layout
Autoload->css();?>
Autoload->js();?>

Tuesday, August 7, 2012

Jquery UI autocomplete with Ajax / POST

Jquery UI has some great widgets, one of them is the autocomplete widget.
Default documentation will show you how to provide an array of tags as the source which to search from.
As useful as that is, we as developers mostly get our data from the server end, and this is one way send a request over ajax / post and get a list of results.

$("#search").autocomplete({
  source : function(request, response) 
  {
   $.post("/manager/Assets/ajax_adpsearch", 
   {
    term : request.term
   }, function(data) 
   {
    response(data)
   }, 'json');
  },
  select : function(event, ui) 
  {
   var selectedObj = ui.item;
   console.log(selectedObj.id) //selected id
  }
 });

On the server side simply construct an associative array and json encode it.

 $data = array( 
     "id" => 1, "value" => "Apple"
     ,"id" => 2, "value" => "Mango"
     ,"id" => 3, "value" => "Orange"
     ,"id" => 4, "value" => "Green Apple"
)
json_encode($data)

Sending additional data.

Some times you want to send more than just id and value pairs. In my most recent project I had a server side function that converted a given string to a SEO friendly URL format. you can put them in to the json array and access them with the array key name
 $data = array( 
     "id" => 1, "value" => "Apple" , "data" => "Fresh"
     ,"id" => 2, "value" => "Mango" , "data" => "Rotten"
     ,"id" => 3, "value" => "Orange" , "data" => "Rotten"
     ,"id" => 4, "value" => "Green Apple" , "data" => "Fresh"
)
json_encode($data)
 select : function(event, ui) 
 {
  var selectedObj = ui.item;
  console.log(selectedObj.data) // fresh or rotten
 }





Cakephp Model association on a non primary key

Issue:
City -> belongsTo -> Country

City:
id | name | country_code

Country:
id | name | code


Your city is linked to country through City.country_code = Country.code.
Now if you do the usual $belongsTo

public $belongsTo = array(
  "Country"=>array(
   'className'    => 'Country'
   )
 );

You'll find cake will join the models on PK fields
LEFT JOIN `db`.`countries` AS `Country` ON (`City`.`country_id` = `Country`.`id`)

To solve this use "condition"
public $belongsTo = array(

 "Country"=>array(
 'className'    => 'Country'
 ,'foreignKey' => false
 ,'conditions' => array('City.country_code = Country.code')
   )
  );

Saturday, June 30, 2012

Quick way to generate your DOB arrays

A quick way to generate arrays for DOB drop down fields

$days = array();
$mons = array();
$years = array();

$days = array();
$mons = array();
$years = array();

for($i=1;$i<=31;$i++)
{
 $days[$i] = date("d", mktime(0, 0, 0,0, $i));
}

for($i=1;$i<=12;$i++)
{
 $mons[$i] = date("M", mktime(0, 0, 0, $i));
}

for($i=1950;$i<=1988;$i++)
{
 $years[$i] = $i;
}


Monday, March 5, 2012

Multi column layouts and floating , #jquery

Have you ever tried to have a dynamic multi column layout, and float the divs nicely under neither each other?
This morning I was tempting to dynamically build a 2 column  menu for a restaurant,  however  I couldn't get my left column divs to sit under the one above.


Jquery to the rescues,  Masonry is a simple, very easy to use plugin that will do this for you.

http://masonry.desandro.com/