Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Datagrid (source : Ignited Datatables)
- *
- * This is a wrapper class/library based on the native Datatables server-side implementation by Allan Jardine
- * found at http://datatables.net/examples/data_sources/server_side.html for CodeIgniter
- *
- * @package CodeIgniter
- * @subpackage libraries
- * @category library
- * @version 0.1
- * @author Vincent Bambico <metal.conspiracy@gmail.com>
- * Yusuf Ozdemir <yusuf@ozdemir.be>
- * @link http://codeigniter.com/forums/viewthread/160896/
- *
- * @modify Mustopa Amin for Datagrid
- */
- class Datagrid {
- /**
- * Global container variables for chained argument results
- *
- */
- protected $ci;
- protected $table;
- protected $select = array();
- protected $joins = array();
- protected $columns = array();
- protected $where = array();
- protected $filter = array();
- protected $add_columns = array();
- protected $edit_columns = array();
- protected $unset_columns = array();
- /**
- * Copies an instance of CI
- */
- public function __construct()
- {
- $this->ci =& get_instance();
- }
- /**
- * Generates the SELECT portion of the query
- *
- * @param string $columns
- * @param bool $backtick_protect
- * @return mixed
- */
- public function select($columns, $backtick_protect = TRUE)
- {
- foreach($this->explode(',', $columns) as $val)
- {
- $column = trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$2', $val));
- $this->columns[] = $column;
- $this->select[$column] = trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$1', $val));
- }
- $this->ci->db->select($columns, $backtick_protect);
- return $this;
- }
- /**
- * Generates the DISTINCT portion of the query
- *
- * @param string $column
- * @return mixed
- */
- public function distinct($column)
- {
- $this->distinct = $column;
- $this->ci->db->distinct($column);
- return $this;
- }
- /**
- * Generates a custom GROUP BY portion of the query
- *
- * @param string $val
- * @return mixed
- */
- public function group_by($val)
- {
- $this->group_by[] = $val;
- $this->ci->db->group_by($val);
- return $this;
- }
- /**
- * Generates the FROM portion of the query
- *
- * @param string $table
- * @return mixed
- */
- public function from($table)
- {
- $this->table = $table;
- $this->ci->db->from($table);
- return $this;
- }
- /**
- * Generates the JOIN portion of the query
- *
- * @param string $table
- * @param string $fk
- * @param string $type
- * @return mixed
- */
- public function join($table, $fk, $type = NULL)
- {
- $this->joins[] = array($table, $fk, $type);
- $this->ci->db->join($table, $fk, $type);
- return $this;
- }
- /**
- * Generates the WHERE portion of the query
- *
- * @param mixed $key_condition
- * @param string $val
- * @param bool $backtick_protect
- * @return mixed
- */
- public function where($key_condition, $val = NULL, $backtick_protect = TRUE)
- {
- $this->where[] = array($key_condition, $val, $backtick_protect);
- $this->ci->db->where($key_condition, $val, $backtick_protect);
- return $this;
- }
- /**
- * Generates the WHERE portion of the query
- *
- * @param mixed $key_condition
- * @param string $val
- * @param bool $backtick_protect
- * @return mixed
- */
- public function or_where($key_condition, $val = NULL, $backtick_protect = TRUE)
- {
- $this->or_where[] = array($key_condition, $val, $backtick_protect);
- $this->ci->db->or_where($key_condition, $val, $backtick_protect);
- return $this;
- }
- /**
- * Generates the Order portion of the query
- *
- * @param string $orderby
- * @param string $direction
- * @return mixed
- */
- public function order_by($orderby, $direction = '')
- {
- $this->ci->db->order_by($orderby, $direction);
- return $this;
- }
- /**
- * Generates the LIKE portion of the query
- *
- * @return mixed
- */
- protected function get_filtering()
- {
- $filterRules = ($this->ci->input->post('filterRules')) ? ($this->ci->input->post('filterRules')) : '';
- if (!empty($filterRules)){
- $filterRules = str_replace('\\','',$filterRules);
- $filterRules = json_decode($filterRules);
- //print_r ($filterRules);
- foreach($filterRules as $rule){
- $rule = get_object_vars($rule);
- $field = $rule['field'];
- $op = $rule['op'];
- $value = $rule['value'];
- if (!empty($value)){
- if ($op == 'contains'){
- $cond .= " and ($field like '%$value%')";
- $this->ci->db->like(array( $field => $value));
- } else if ($op == 'greater'){
- $cond .= " and $field>$value";
- $this->ci->db->where($field." >",$value);
- }
- else
- {
- $this->ci->db->like(array( $field => $value));
- }
- }
- }
- }
- }
- /**
- * Generates the LIMIT portion of the query
- *
- * @return mixed
- */
- protected function get_paging()
- {
- $page= ($this->ci->input->post('page')) ? intval($this->ci->input->post('page')) : 1;
- $rows= ($this->ci->input->post('rows')) ? intval($this->ci->input->post('rows')) : 20;
- $offset=($page-1)*$rows;
- $this->ci->db->limit($rows,$offset);
- }
- /**
- * Generates the ORDER BY portion of the query
- *
- * @return mixed
- */
- protected function get_ordering()
- {
- $sort =$this->ci->input->post('sort');
- $order =$this->ci->input->post('order');
- if($sort) $this->ci->db->order_by($sort,$order);
- }
- /**
- * Builds all the necessary query segments and performs the main query based on results set from chained statements
- *
- * @param string charset
- * @return string
- */
- public function generate($charset = 'UTF-8')
- {
- $this->get_paging();
- $this->get_ordering();
- $this->get_filtering();
- return $this->produce_output($charset);
- }
- /**
- * Compiles the select statement based on the other functions called and runs the query
- *
- * @return mixed
- */
- protected function get_display_result()
- {
- return $this->ci->db->get();
- }
- /**
- * Builds a JSON encoded string data
- *
- * @param string charset
- * @return string
- */
- protected function produce_output($charset)
- {
- $aaData = array();
- $rResult = $this->get_display_result();
- $iFilteredTotal = $this->get_total_results(TRUE);
- $sOutput = array
- (
- 'total' => $iFilteredTotal,
- 'rows' => $rResult->result_array(),
- );
- if(strtolower($charset) == 'utf-8')
- return json_encode($sOutput);
- else
- return $this->jsonify($sOutput);
- }
- /**
- * Get result count
- *
- * @return integer
- */
- protected function get_total_results($filtering = FALSE)
- {
- if($filtering)
- $this->get_filtering();
- foreach($this->joins as $val)
- $this->ci->db->join($val[0], $val[1], $val[2]);
- foreach($this->where as $val)
- $this->ci->db->where($val[0], $val[1], $val[2]);
- return $this->ci->db->count_all_results($this->table);
- }
- /**
- * Workaround for json_encode's UTF-8 encoding if a different charset needs to be used
- *
- * @param mixed result
- * @return string
- */
- protected function jsonify($result = FALSE)
- {
- if(is_null($result)) return 'null';
- if($result === FALSE) return 'false';
- if($result === TRUE) return 'true';
- if(is_scalar($result))
- {
- if(is_float($result))
- return floatval(str_replace(',', '.', strval($result)));
- if(is_string($result))
- {
- static $jsonReplaces = array(array('\\', '/', '\n', '\t', '\r', '\b', '\f', '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
- return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $result) . '"';
- }
- else
- return $result;
- }
- $isList = TRUE;
- for($i = 0, reset($result); $i < count($result); $i++, next($result))
- {
- if(key($result) !== $i)
- {
- $isList = FALSE;
- break;
- }
- }
- $json = array();
- if($isList)
- {
- foreach($result as $value)
- $json[] = $this->jsonify($value);
- return '[' . join(',', $json) . ']';
- }
- else
- {
- foreach($result as $key => $value)
- $json[] = $this->jsonify($key) . ':' . $this->jsonify($value);
- return '{' . join(',', $json) . '}';
- }
- }
- /**
- * Explode, but ignore delimiter until closing characters are found
- *
- * @param string $delimiter
- * @param string $str
- * @param string $open
- * @param string $close
- * @return mixed $retval
- */
- protected function explode($delimiter, $str, $open='(', $close=')')
- {
- $retval = array();
- $hold = array();
- $balance = 0;
- $parts = explode($delimiter, $str);
- foreach ($parts as $part)
- {
- $hold[] = $part;
- $balance += $this->balanceChars($part, $open, $close);
- if ($balance < 1)
- {
- $retval[] = implode($delimiter, $hold);
- $hold = array();
- $balance = 0;
- }
- }
- if (count($hold) > 0)
- $retval[] = implode($delimiter, $hold);
- return $retval;
- }
- /**
- * Return the difference of open and close characters
- *
- * @param string $str
- * @param string $open
- * @param string $close
- * @return string $retval
- */
- protected function balanceChars($str, $open, $close)
- {
- $openCount = substr_count($str, $open);
- $closeCount = substr_count($str, $close);
- $retval = $openCount - $closeCount;
- return $retval;
- }
- }
- /* End of file Datagrid.php */
- /* Location: ./application/libraries/Datagrid.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement