-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatatables.php
148 lines (121 loc) · 3.57 KB
/
datatables.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Datatables
{
var $ci;
var $imported;
public function __construct()
{
$this->ci=& get_instance();
}
public function generate($table, $columns, $index, $joins, $where, $search)
{
$sLimit = $this->get_paging();
$sOrder = $this->get_ordering($columns);
$sWhere = $this->get_filtering($columns, $where, $search);
$rResult = $this->get_display_data($table, $columns, $sWhere, $sOrder, $sLimit, $joins, $where);
$rResultFilterTotal = $this->get_data_set_length();
$aResultFilterTotal = $rResultFilterTotal->result_array();
$iFilteredTotal = $aResultFilterTotal[0]["FOUND_ROWS()"];
$rResultTotal = $this->get_total_data_set_length($table, $index, $sWhere, $joins, $where);
$aResultTotal = $rResultTotal->result_array();
$iTotal = $aResultTotal[0]["COUNT($index)"];
return $this->produce_output($columns, $iTotal, $iFilteredTotal, $rResult);
}
protected function get_paging()
{
if($this->ci->input->post("iDisplayStart") && $this->ci->input->post("iDisplayLength") !== "-1")
$sLimit = "LIMIT " . $this->ci->input->post("iDisplayStart"). ", " .$this->ci->input->post("iDisplayLength");
else
{
$iDisplayLength = $this->ci->input->post("iDisplayLength");
if(empty($iDisplayLength)){
$sLimit = "LIMIT " . "0,10";
}else{
$sLimit = "LIMIT " . "0,". $this->ci->input->post("iDisplayLength");
}
}
return $sLimit;
}
protected function get_ordering($columns)
{
$sOrder = "";
if($this->ci->input->post("iSortCol_0"))
{
$sOrder = "ORDER BY ";
for($i = 0; $i < intval($this->ci->input->post("iSortingCols")); $i++)
$sOrder .= $columns[intval($this->ci->input->post("iSortCol_" . $i))] . " " . $this->ci->input->post("sSortDir_" . $i) . ", ";
$sOrder = substr_replace($sOrder, "", -2);
}
return $sOrder;
}
protected function get_filtering($columns, $where, $search)
{
$sWhere = "";
if($this->ci->input->post("sSearch") != '')
{
$sWhere = "WHERE ";
for($i = 0; $i < count($columns); $i++)
$sWhere .= $columns[$i] . " LIKE '%" . $this->ci->input->post("sSearch") . "%' OR ";
$sWhere = substr_replace($sWhere, "", -3);
}
if($sWhere == ''){
if($where !== ''){
$where = 'WHERE ' . $where;
}
}
return $sWhere . $where;
}
protected function get_display_data($table, $columns, $sWhere, $sOrder, $sLimit, $joins, $where)
{
return $this->ci->db->query
("
SELECT SQL_CALC_FOUND_ROWS " . implode(", ", $columns) . "
FROM $table
$joins
$sWhere
$sOrder
$sLimit
");
}
protected function get_data_set_length()
{
return $this->ci->db->query("SELECT FOUND_ROWS()");
}
protected function get_total_data_set_length($table, $index, $sWhere, $joins, $where)
{
return $this->ci->db->query
("
SELECT COUNT(" . $index . ")
FROM $table
$joins
$sWhere
");
}
protected function produce_output($columns, $iTotal, $iFilteredTotal, $rResult)
{
$aaData = array();
foreach($rResult->result_array() as $row_key => $row_val)
{
foreach($row_val as $col_key => $col_val)
{
if($row_val[$col_key] == "version")
$aaData[$row_key][$col_key] = ($aaData[$row_key][$col_key] == 0)? "-" : $col_val;
else
{
switch($row_val[$col_key])
{
default: $aaData[$row_key][] = $col_val; break;
}
}
}
}
$sOutput = array
(
"sEcho" => intval($this->ci->input->post("sEcho")),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => $aaData
);
return json_encode($sOutput);
}
}