Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Datatables.php #118

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 57 additions & 60 deletions application/libraries/Datatables.php → Datatables.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 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 2.0 <beta>
* @author Vincent Bambico <[email protected]>
* Yusuf Ozdemir <[email protected]>
* @link http://ellislab.com/forums/viewthread/160896/
*/
<?php namespace App\Libraries;

class Datatables
{
/**
* Global container variables for chained argument results
*
*/
private $ci;
private $db;
private $builder;
private $request;
private $table;
private $distinct;
private $group_by = array();
Expand All @@ -41,7 +31,10 @@ class Datatables
*/
public function __construct()
{
$this->ci =& get_instance();
$db = \Config\Database::connect();

$this->db = $db;
$this->request = \Config\Services::request();
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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']);

}

Expand All @@ -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 != '')
Expand All @@ -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]);
}

/**
Expand All @@ -351,7 +346,7 @@ private function get_filtering()
*/
private function get_display_result()
{
return $this->ci->db->get($this->table);
return $this->builder->get();
}

/**
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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();
54 changes: 0 additions & 54 deletions readme.txt

This file was deleted.