Advertisement
gotopa

Datagrid.php

Jan 27th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.19 KB | None | 0 0
  1. <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Datagrid (source : Ignited Datatables)
  4. *
  5. * This is a wrapper class/library based on the native Datatables server-side implementation by Allan Jardine
  6. * found at http://datatables.net/examples/data_sources/server_side.html for CodeIgniter
  7. *
  8. * @package    CodeIgniter
  9. * @subpackage libraries
  10. * @category   library
  11. * @version    0.1
  12. * @author     Vincent Bambico <metal.conspiracy@gmail.com>
  13. *             Yusuf Ozdemir <yusuf@ozdemir.be>
  14. * @link       http://codeigniter.com/forums/viewthread/160896/
  15. *
  16. * @modify     Mustopa Amin for Datagrid
  17. */
  18.  
  19. class Datagrid  {
  20.  
  21.     /**
  22.     * Global container variables for chained argument results
  23.     *
  24.     */
  25.     protected $ci;
  26.     protected $table;
  27.     protected $select         = array();
  28.     protected $joins          = array();
  29.     protected $columns        = array();
  30.     protected $where          = array();
  31.     protected $filter         = array();
  32.     protected $add_columns    = array();
  33.     protected $edit_columns   = array();
  34.     protected $unset_columns  = array();
  35.  
  36.     /**
  37.     * Copies an instance of CI
  38.     */
  39.     public function __construct()
  40.     {
  41.       $this->ci =& get_instance();
  42.     }
  43.  
  44.     /**
  45.     * Generates the SELECT portion of the query
  46.     *
  47.     * @param string $columns
  48.     * @param bool $backtick_protect
  49.     * @return mixed
  50.     */
  51.     public function select($columns, $backtick_protect = TRUE)
  52.     {
  53.       foreach($this->explode(',', $columns) as $val)
  54.       {
  55.         $column = trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$2', $val));
  56.         $this->columns[] =  $column;
  57.         $this->select[$column] =  trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$1', $val));
  58.       }
  59.       $this->ci->db->select($columns, $backtick_protect);
  60.       return $this;
  61.     }
  62.  
  63.     /**
  64.     * Generates the DISTINCT portion of the query
  65.     *
  66.     * @param string $column
  67.     * @return mixed
  68.     */
  69.     public function distinct($column)
  70.     {
  71.       $this->distinct = $column;
  72.       $this->ci->db->distinct($column);
  73.       return $this;
  74.     }
  75.  
  76.     /**
  77.     * Generates a custom GROUP BY portion of the query
  78.     *
  79.     * @param string $val
  80.     * @return mixed
  81.     */
  82.     public function group_by($val)
  83.     {
  84.       $this->group_by[] = $val;
  85.       $this->ci->db->group_by($val);
  86.       return $this;
  87.     }
  88.  
  89.     /**
  90.     * Generates the FROM portion of the query
  91.     *
  92.     * @param string $table
  93.     * @return mixed
  94.     */
  95.     public function from($table)
  96.     {
  97.       $this->table = $table;
  98.       $this->ci->db->from($table);
  99.       return $this;
  100.     }
  101.    
  102.     /**
  103.     * Generates the JOIN portion of the query
  104.     *
  105.     * @param string $table
  106.     * @param string $fk
  107.     * @param string $type
  108.     * @return mixed
  109.     */
  110.     public function join($table, $fk, $type = NULL)
  111.     {
  112.       $this->joins[] = array($table, $fk, $type);
  113.       $this->ci->db->join($table, $fk, $type);
  114.       return $this;
  115.     }
  116.    
  117.     /**
  118.     * Generates the WHERE portion of the query
  119.     *
  120.     * @param mixed $key_condition
  121.     * @param string $val
  122.     * @param bool $backtick_protect
  123.     * @return mixed
  124.     */
  125.     public function where($key_condition, $val = NULL, $backtick_protect = TRUE)
  126.     {
  127.       $this->where[] = array($key_condition, $val, $backtick_protect);
  128.       $this->ci->db->where($key_condition, $val, $backtick_protect);
  129.       return $this;
  130.     }
  131.  
  132.     /**
  133.     * Generates the WHERE portion of the query
  134.     *
  135.     * @param mixed $key_condition
  136.     * @param string $val
  137.     * @param bool $backtick_protect
  138.     * @return mixed
  139.     */
  140.     public function or_where($key_condition, $val = NULL, $backtick_protect = TRUE)
  141.     {
  142.       $this->or_where[] = array($key_condition, $val, $backtick_protect);
  143.       $this->ci->db->or_where($key_condition, $val, $backtick_protect);
  144.       return $this;
  145.     }
  146.  
  147.     /**
  148.     * Generates the Order portion of the query
  149.     *
  150.     * @param string $orderby
  151.     * @param string $direction
  152.     * @return mixed
  153.     */
  154.     public function order_by($orderby, $direction = '')
  155.     {
  156.       $this->ci->db->order_by($orderby, $direction);
  157.       return $this;
  158.     }
  159.  
  160.     /**
  161.     * Generates the LIKE portion of the query
  162.     *
  163.     * @return mixed
  164.     */
  165.     protected function get_filtering()
  166.     {
  167.         $filterRules = ($this->ci->input->post('filterRules')) ? ($this->ci->input->post('filterRules')) : '';
  168.         if (!empty($filterRules)){
  169.             $filterRules = str_replace('\\','',$filterRules);
  170.             $filterRules = json_decode($filterRules);
  171.             //print_r ($filterRules);
  172.             foreach($filterRules as $rule){
  173.                 $rule = get_object_vars($rule);
  174.                 $field = $rule['field'];
  175.                 $op = $rule['op'];
  176.                 $value = $rule['value'];
  177.                 if (!empty($value)){
  178.                     if ($op == 'contains'){
  179.                         $cond .= " and ($field like '%$value%')";
  180.                         $this->ci->db->like(array( $field => $value));
  181.                     } else if ($op == 'greater'){
  182.                         $cond .= " and $field>$value";
  183.                         $this->ci->db->where($field." >",$value);
  184.                     }
  185.                     else
  186.                     {
  187.                         $this->ci->db->like(array( $field => $value));
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.     /**
  195.     * Generates the LIMIT portion of the query
  196.     *
  197.     * @return mixed
  198.     */
  199.     protected function get_paging()
  200.     {
  201.       $page=    ($this->ci->input->post('page')) ? intval($this->ci->input->post('page')) : 1;
  202.       $rows= ($this->ci->input->post('rows')) ? intval($this->ci->input->post('rows')) : 20;
  203.       $offset=($page-1)*$rows;
  204.       $this->ci->db->limit($rows,$offset);
  205.     }            
  206.  
  207.     /**
  208.     * Generates the ORDER BY portion of the query
  209.     *
  210.     * @return mixed
  211.     */
  212.     protected function get_ordering()
  213.     {
  214.       $sort     =$this->ci->input->post('sort');
  215.       $order    =$this->ci->input->post('order');
  216.       if($sort) $this->ci->db->order_by($sort,$order);
  217.     }
  218.  
  219.     /**
  220.     * Builds all the necessary query segments and performs the main query based on results set from chained statements
  221.     *
  222.     * @param string charset
  223.     * @return string
  224.     */
  225.     public function generate($charset = 'UTF-8')
  226.     {
  227.       $this->get_paging();
  228.       $this->get_ordering();
  229.       $this->get_filtering();
  230.       return $this->produce_output($charset);
  231.     }
  232.    
  233.     /**
  234.     * Compiles the select statement based on the other functions called and runs the query
  235.     *
  236.     * @return mixed
  237.     */
  238.     protected function get_display_result()
  239.     {
  240.       return $this->ci->db->get();
  241.     }
  242.  
  243.     /**
  244.     * Builds a JSON encoded string data
  245.     *
  246.     * @param string charset
  247.     * @return string
  248.     */
  249.     protected function produce_output($charset)
  250.     {
  251.       $aaData = array();
  252.       $rResult = $this->get_display_result();
  253.       $iFilteredTotal = $this->get_total_results(TRUE);
  254.      
  255.       $sOutput = array
  256.       (
  257.         'total'         => $iFilteredTotal,
  258.         'rows'          => $rResult->result_array(),
  259.       );
  260.  
  261.       if(strtolower($charset) == 'utf-8')
  262.         return json_encode($sOutput);
  263.       else
  264.         return $this->jsonify($sOutput);
  265.     }
  266.  
  267.     /**
  268.     * Get result count
  269.     *
  270.     * @return integer
  271.     */
  272.     protected function get_total_results($filtering = FALSE)
  273.     {
  274.       if($filtering)
  275.         $this->get_filtering();
  276.  
  277.       foreach($this->joins as $val)
  278.         $this->ci->db->join($val[0], $val[1], $val[2]);
  279.  
  280.       foreach($this->where as $val)
  281.         $this->ci->db->where($val[0], $val[1], $val[2]);
  282.  
  283.       return $this->ci->db->count_all_results($this->table);
  284.     }
  285.  
  286.     /**
  287.     * Workaround for json_encode's UTF-8 encoding if a different charset needs to be used
  288.     *
  289.     * @param mixed result
  290.     * @return string
  291.     */
  292.     protected function jsonify($result = FALSE)
  293.     {
  294.       if(is_null($result)) return 'null';
  295.       if($result === FALSE) return 'false';
  296.       if($result === TRUE) return 'true';
  297.  
  298.       if(is_scalar($result))
  299.       {
  300.         if(is_float($result))
  301.           return floatval(str_replace(',', '.', strval($result)));
  302.  
  303.         if(is_string($result))
  304.         {
  305.           static $jsonReplaces = array(array('\\', '/', '\n', '\t', '\r', '\b', '\f', '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  306.           return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $result) . '"';
  307.         }
  308.         else
  309.           return $result;
  310.       }
  311.  
  312.       $isList = TRUE;
  313.  
  314.       for($i = 0, reset($result); $i < count($result); $i++, next($result))
  315.       {
  316.         if(key($result) !== $i)
  317.         {
  318.           $isList = FALSE;
  319.           break;
  320.         }
  321.       }
  322.  
  323.       $json = array();
  324.  
  325.       if($isList)
  326.       {
  327.         foreach($result as $value)
  328.           $json[] = $this->jsonify($value);
  329.  
  330.         return '[' . join(',', $json) . ']';
  331.       }
  332.       else
  333.       {
  334.         foreach($result as $key => $value)
  335.           $json[] = $this->jsonify($key) . ':' . $this->jsonify($value);
  336.  
  337.         return '{' . join(',', $json) . '}';
  338.       }
  339.     }
  340.  
  341.     /**
  342.     * Explode, but ignore delimiter until closing characters are found
  343.     *
  344.     * @param string $delimiter
  345.     * @param string $str
  346.     * @param string $open
  347.     * @param string $close
  348.     * @return mixed $retval
  349.     */
  350.     protected function explode($delimiter, $str, $open='(', $close=')')
  351.     {
  352.       $retval = array();
  353.       $hold = array();
  354.       $balance = 0;
  355.       $parts = explode($delimiter, $str);
  356.  
  357.       foreach ($parts as $part)
  358.       {
  359.         $hold[] = $part;
  360.         $balance += $this->balanceChars($part, $open, $close);
  361.         if ($balance < 1)
  362.         {
  363.           $retval[] = implode($delimiter, $hold);
  364.           $hold = array();
  365.           $balance = 0;
  366.         }
  367.       }
  368.  
  369.       if (count($hold) > 0)
  370.         $retval[] = implode($delimiter, $hold);
  371.  
  372.       return $retval;
  373.     }
  374.    
  375.     /**
  376.     * Return the difference of open and close characters
  377.     *
  378.     * @param string $str
  379.     * @param string $open
  380.     * @param string $close
  381.     * @return string $retval
  382.     */
  383.     protected function balanceChars($str, $open, $close)
  384.     {
  385.       $openCount = substr_count($str, $open);
  386.       $closeCount = substr_count($str, $close);
  387.       $retval = $openCount - $closeCount;
  388.       return $retval;
  389.     }
  390.    
  391. }
  392.  
  393. /* End of file Datagrid.php */
  394. /* Location: ./application/libraries/Datagrid.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement