diff --git a/application/libraries/Datatables.php b/Datatables.php similarity index 83% rename from application/libraries/Datatables.php rename to Datatables.php index c6de11f..0226caa 100644 --- a/application/libraries/Datatables.php +++ b/Datatables.php @@ -1,18 +1,5 @@ - - * @author Vincent Bambico - * Yusuf Ozdemir - * @link http://ellislab.com/forums/viewthread/160896/ - */ +ci =& get_instance(); + $db = \Config\Database::connect(); + + $this->db = $db; + $this->request = \Config\Services::request(); } /** @@ -50,8 +43,8 @@ public function __construct() */ public function set_database($db_name) { - $db_data = $this->ci->load->database($db_name, TRUE); - $this->ci->db = $db_data; + $db = \Config\Database::connect($db_name); + $this->db = $db; } /** @@ -71,7 +64,7 @@ public function select($columns, $backtick_protect = TRUE) $this->select[$column] = trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$1', $val)); } - $this->ci->db->select($columns, $backtick_protect); + $this->builder->select($columns, $backtick_protect); return $this; } @@ -84,7 +77,7 @@ public function select($columns, $backtick_protect = TRUE) public function distinct($column) { $this->distinct = $column; - $this->ci->db->distinct($column); + $this->builder->distinct($column); return $this; } @@ -97,18 +90,20 @@ public function distinct($column) public function group_by($val) { $this->group_by[] = $val; - $this->ci->db->group_by($val); + $this->builder->groupBy($val); return $this; } /** - * Generates the FROM portion of the query + * Generates the TABLE portion of the query * * @param string $table * @return mixed */ - public function from($table) + public function table($table) { + // $this->builder->table($table); + $this->builder = $this->db->table($table); $this->table = $table; return $this; } @@ -124,7 +119,7 @@ public function from($table) public function join($table, $fk, $type = NULL) { $this->joins[] = array($table, $fk, $type); - $this->ci->db->join($table, $fk, $type); + $this->builder->join($table, $fk, $type); return $this; } @@ -139,7 +134,7 @@ public function join($table, $fk, $type = NULL) 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); + $this->builder->where($key_condition, $val, $backtick_protect); return $this; } @@ -154,7 +149,7 @@ public function where($key_condition, $val = NULL, $backtick_protect = TRUE) 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); + $this->builder->orWhere($key_condition, $val, $backtick_protect); return $this; } @@ -169,7 +164,7 @@ public function or_where($key_condition, $val = NULL, $backtick_protect = TRUE) public function where_in($key_condition, $val = NULL) { $this->where_in[] = array($key_condition, $val); - $this->ci->db->where_in($key_condition, $val); + $this->builder->whereIn($key_condition, $val); return $this; } @@ -198,7 +193,7 @@ public function filter($key_condition, $val = NULL, $backtick_protect = TRUE) public function like($key_condition, $val = NULL, $side = 'both') { $this->like[] = array($key_condition, $val, $side); - $this->ci->db->like($key_condition, $val, $side); + $this->builder->like($key_condition, $val, $side); return $this; } @@ -213,7 +208,7 @@ public function like($key_condition, $val = NULL, $side = 'both') public function or_like($key_condition, $val = NULL, $side = 'both') { $this->or_like[] = array($key_condition, $val, $side); - $this->ci->db->or_like($key_condition, $val, $side); + $this->builder->orLike($key_condition, $val, $side); return $this; } @@ -282,11 +277,11 @@ public function generate($output = 'json', $charset = 'UTF-8') */ private function get_paging() { - $iStart = $this->ci->input->post('start'); - $iLength = $this->ci->input->post('length'); + $iStart = $this->request->getPost('start'); + $iLength = $this->request->getPost('length'); if($iLength != '' && $iLength != '-1') - $this->ci->db->limit($iLength, ($iStart)? $iStart : 0); + $this->builder->limit($iLength, ($iStart)? $iStart : 0); } /** @@ -297,15 +292,15 @@ private function get_paging() private function get_ordering() { - $Data = $this->ci->input->post('columns'); + $Data = $this->request->getPost('columns'); - if ($this->ci->input->post('order')) - foreach ($this->ci->input->post('order') as $key) + if ($this->request->getPost('order')) + foreach ($this->request->getPost('order') as $key) if($this->check_cType()) - $this->ci->db->order_by($Data[$key['column']]['data'], $key['dir']); + $this->builder->orderBy($Data[$key['column']]['data'], $key['dir']); else - $this->ci->db->order_by($this->columns[$key['column']] , $key['dir']); + $this->builder->orderBy($this->columns[$key['column']] , $key['dir']); } @@ -317,11 +312,11 @@ private function get_ordering() private function get_filtering() { - $mColArray = $this->ci->input->post('columns'); + $mColArray = $this->request->getPost('columns'); $sWhere = ''; - $search = $this->ci->input->post('search'); - $sSearch = $this->ci->db->escape_like_str(trim($search['value'])); + $search = $this->request->getPost('search'); + $sSearch = trim($search['value']); $columns = array_values(array_diff($this->columns, $this->unset_columns)); if($sSearch != '') @@ -336,12 +331,12 @@ private function get_filtering() $sWhere = substr_replace($sWhere, '', -3); if($sWhere != '') - $this->ci->db->where('(' . $sWhere . ')'); + $this->builder->where('(' . $sWhere . ')'); // TODO : sRangeSeparator foreach($this->filter as $val) - $this->ci->db->where($val[0], $val[1], $val[2]); + $this->builder->where($val[0], $val[1], $val[2]); } /** @@ -351,7 +346,7 @@ private function get_filtering() */ private function get_display_result() { - return $this->ci->db->get($this->table); + return $this->builder->get(); } /** @@ -372,7 +367,7 @@ private function produce_output($output, $charset) $iFilteredTotal = $this->get_total_results(TRUE); } - foreach($rResult->result_array() as $row_key => $row_val) + foreach($rResult->getResultArray() as $row_key => $row_val) { $aaData[$row_key] = ($this->check_cType())? $row_val : array_values($row_val); @@ -398,7 +393,7 @@ private function produce_output($output, $charset) { $sOutput = array ( - 'draw' => intval($this->ci->input->post('draw')), + 'draw' => intval($this->request->getPost('draw')), 'recordsTotal' => $iTotal, 'recordsFiltered' => $iFilteredTotal, 'data' => $aaData @@ -420,38 +415,42 @@ private function produce_output($output, $charset) */ private function get_total_results($filtering = FALSE) { + $this->builder->from($this->table, true); + if($filtering) $this->get_filtering(); foreach($this->joins as $val) - $this->ci->db->join($val[0], $val[1], $val[2]); + $this->builder->join($val[0], $val[1], $val[2]); foreach($this->where as $val) - $this->ci->db->where($val[0], $val[1], $val[2]); + $this->builder->where($val[0], $val[1], $val[2]); foreach($this->or_where as $val) - $this->ci->db->or_where($val[0], $val[1], $val[2]); + $this->builder->orWhere($val[0], $val[1], $val[2]); foreach($this->where_in as $val) - $this->ci->db->where_in($val[0], $val[1]); + $this->builder->whereIn($val[0], $val[1]); foreach($this->group_by as $val) - $this->ci->db->group_by($val); + $this->builder->groupBy($val); foreach($this->like as $val) - $this->ci->db->like($val[0], $val[1], $val[2]); + $this->builder->like($val[0], $val[1], $val[2]); foreach($this->or_like as $val) - $this->ci->db->or_like($val[0], $val[1], $val[2]); + $this->builder->orLike($val[0], $val[1], $val[2]); if(strlen($this->distinct) > 0) { - $this->ci->db->distinct($this->distinct); - $this->ci->db->select($this->columns); + $this->builder->distinct(); + $this->builder->select($this->select); } - $query = $this->ci->db->get($this->table, NULL, NULL, FALSE); - return $query->num_rows(); + // echo $this->builder->getCompiledSelect(); die(); + + $query = $this->builder->get(); + return $this->builder->countAll(); } /** @@ -508,7 +507,7 @@ private function exec_replace($custom_val, $row_data) */ private function check_cType() { - $column = $this->ci->input->post('columns'); + $column = $this->request->getPost('columns'); if(is_numeric($column[0]['data'])) return FALSE; else @@ -633,8 +632,6 @@ private function jsonify($result = FALSE) */ public function last_query() { - return $this->ci->db->last_query(); + return $this->builder->last_query(); } } -/* End of file Datatables.php */ -/* Location: ./application/libraries/Datatables.php */ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fa2d85f --- /dev/null +++ b/readme.md @@ -0,0 +1,13 @@ +### Ignited Datatables CodeIgniter 4 +This is was adjusting version for CodeIgniter 4 + +Changelog: +- Change "from" method name to "table" + +This adjustment is make change how to write the code. For this version, the table method should be called first, like so: + + + + $dt = new Datatables(); + $dt->table('table_name')->select('id, name'); + return $dt->generate(); diff --git a/readme.txt b/readme.txt deleted file mode 100644 index 2e7d63e..0000000 --- a/readme.txt +++ /dev/null @@ -1,54 +0,0 @@ -********************** -* Ignited Datatables * -********************** - -Ignited Datatables 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 - -Fork : https://github.com/IgnitedDatatables/Ignited-Datatables -Wiki : https://github.com/IgnitedDatatables/Ignited-Datatables/wiki -Discuss: http://ellislab.com/forums/viewthread/160896/ -Contact: Vincent Bambico - Yusuf Ozdemir - -============ -Requirements -============ -jQuery 1.5+ -DataTables 1.10+ -CodeIgniter "Reactor" - -======= -Install -======= -To install the library, copy the libraries/datatables.php file into your application/libraries folder. - -======= -License -======= -DON'T BE A DICK PUBLIC LICENSE - -Version 1, December 2009 - -Copyright (C) 2009 Philip Sturgeon -Everyone is permitted to copy and distribute verbatim or modified -copies of this license document, and changing it is allowed as long -as the name is changed. - -DON'T BE A DICK PUBLIC LICENSE -TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - -1. Do whatever you like with the original work, just don't be a dick. - -Being a dick includes - but is not limited to - the following instances: - -1a. Outright copyright infringement - Don't just copy this and change the name. -1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick. -1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick. - -2. If you become rich through modifications, related works/services, or supporting the original work, -share the love. Only a dick would make loads off this work and not buy the original works -creator(s) a pint. - -3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes -you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.