Guest User

Component

a guest
Jan 18th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  * jQuery DataTables Component.
  4.  *
  5.  *
  6.  * PHP versions 5 - Known & Tested
  7.  *
  8.  * Copyright 2011, Simon Dann. (http://likepie.net)
  9.  *
  10.  * Licensed under The MIT License
  11.  * Redistributions of files must retain the above copyright notice.
  12.  *
  13.  * @copyright     Copyright 2011, Simon Dann. (http://likepie.net)
  14.  * @link          http://likepie.net/cakephp-jquery-datatables-componenthelper/ jQuery DataTables Component Project
  15.  * @package       CakePHP-Datatables  
  16.  * @since         CakePHP-Datatables v 1.0.0
  17.  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  18.  */
  19. class DatatablesComponent extends Component {
  20.  
  21.     public $sLimit = null;
  22.     public $sNames = null;
  23.     public $model = null;
  24.  
  25.    /**
  26.     * initialize function.
  27.     * loads any and all linked models to the controller, this is quite inefficient and dumb atm
  28.     * very lazy and should be fixed once someone comes up with a better solution.
  29.     * @since     CakePHP-Datatables v 1.0.0
  30.     */
  31.  
  32.     public function __construct(ComponentCollection $collection, $settings = array()) {
  33.         $this->_controller = $collection->getController();
  34.         parent::__construct($collection, $settings);
  35.     }
  36.  
  37.     public function initialize($controller) {
  38.         $this->Controller = $controller;
  39.        
  40.     }
  41.    
  42.    
  43.    /**
  44.     * output function.
  45.     * echos out a json encoded array for the dataTables jQuery font end to pick up
  46.     * yes once again lazyness has resulted in this being a simple echo and kill rather than
  47.     * being passed on to a view, which should probably be returned via a url with a .json at the end.
  48.     * @since     CakePHP-Datatables v 1.0.0
  49.     */
  50.     public function output ($dataTables = null){
  51.         if ($dataTables){          
  52.            
  53.             // A way of getting the names of all the columns.
  54.             if ( isset( $this->request->query['sNames'] ) ){
  55.                 $this->sNames = explode(',', $this->request->query['sNames'] );
  56.             }
  57.            
  58.             // Are we sorting columns
  59.             if ( isset( $this->request->query['iSortCol_0'] ) ){
  60.                 for ( $i=0 ; $i<intval( $this->request->query['iSortingCols'] ) ; $i++ ){
  61.                     if ( $this->request->query[ 'bSortable_'.intval( $this->request->query['iSortCol_'.$i] ) ] == "true" ){
  62.                         if (isset ($this->sNames)){
  63.                             $ordering[] = $dataTables['use'] . '.' . $this->sNames[ intval( $this->request->query['iSortCol_'.$i] ) ] . ' ' . $this->request->query['sSortDir_'.$i];
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.            
  69.             // If the user is searching
  70.             if ( $this->request->query['sSearch'] != "" ){
  71.                 for ( $i=0 ; $i<count($this->sNames) ; $i++ ){
  72.                     if ( $this->request->query['bSearchable_'.$i] == "true"){
  73.                         $conditions[$dataTables['use'] . '.' . $this->sNames[$i] . ' LIKE'] = '%' . $this->request->query['sSearch'] . '%';
  74.                     }
  75.                 }
  76.             }
  77.            
  78.             if (isset($conditions) && !empty($conditions)){
  79.                 $finalconditions = array('OR'=>$conditions);
  80.             }else{
  81.                 $finalconditions = '';
  82.             }
  83.            
  84.             $n = array(
  85.                 'conditions' => $finalconditions,
  86.                 'recursive' => 1,
  87.                 'fields' => $this->sNames,
  88.                 'order' => $ordering,
  89.                 'limit' => $this->request->query['iDisplayLength'],
  90.                 'offset'=> $this->request->query['iDisplayStart'],
  91.             );
  92.            
  93.             $rResult = $this->$dataTables['use']->find('all', $n);
  94.             $iTotal = $this->$dataTables['use']->find('count');
  95.             if ($this->request->query['sSearch']){
  96.                 $n['limit'] = null;
  97.                 $n['offset'] = null;
  98.                 $iFilteredTotal = $this->$dataTables['use']->find('count', array('conditions' => $finalconditions));
  99.             }else{
  100.                 $iFilteredTotal = $iTotal;
  101.             }
  102.            
  103.             $output = array(
  104.                 "aoColumns" => implode(',', $this->sNames),
  105.                 "sEcho" => intval($this->request->query['sEcho']),
  106.                 "iTotalRecords" => $iTotal,
  107.                 "iTotalDisplayRecords" => $iFilteredTotal,
  108.                 "aaData" => array()
  109.             );
  110.             foreach ($rResult as $record){
  111.                 $row = array();
  112.                 for ( $i=0 ; $i<count($this->sNames) ; $i++ ){
  113.                     $row[] = $this->filter($this->sNames[$i], $record, $dataTables['use']);
  114.                 }
  115.                 /* Add buttons as an additional column?
  116.                  * maybe in the form of:
  117.                  * $dataTables['buttons'] = array(
  118.                  *      'view' => '/view/%id%',
  119.                  *      'edit' => '/edit/%id$',
  120.                  * );
  121.                  * with urls being able to be hooked into cakes automagic black box :)
  122.                  */
  123.                 $output['aaData'][] = $row;
  124.             }
  125.             echo json_encode( $output );
  126.             die();
  127.         }else{
  128.             return false;
  129.         }
  130.     }
  131.        
  132.    /**
  133.     * filter function.
  134.     * Use this function to filter the output as provided by the find request, if you are using joins such as belongsTo,
  135.     * or virtual fields with weird __ in their names you can filter them here and fix the output
  136.     * the example below replaces public which is a boole from having a 0/1 output to having a no/yes output
  137.     * @since     CakePHP-Datatables v 1.0.0
  138.     */
  139.     public function filter($input, $record, $tName){
  140.  
  141.         switch ($input){
  142.  
  143.             case 'public':
  144.                 $output = 'No';
  145.                 if ($record[$tName]['public'] == '1'){
  146.                     $output = 'Yes';
  147.                 }
  148.                 return $output;
  149.                 break;
  150.             default:
  151.                 return $record[$tName][$input];
  152.                 break;
  153.         }
  154.     }
  155.  
  156. }
  157.  
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment