-
Notifications
You must be signed in to change notification settings - Fork 0
/
KalipsoTable.php
235 lines (187 loc) · 6.17 KB
/
KalipsoTable.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* KalipsoTable Server-side Processing Class
* @author KalipsoCollective
* @package KN
* @subpackage KN Helper
*/
declare(strict_types=1);
namespace KN;
class KalipsoTable {
/**
* Database connection instance.
* (PDO)
*/
protected $db;
/**
* Table name or SQL query to be processed.
*/
protected $from;
/**
* Output data
*/
protected $data = [];
/**
* Response init
* @return this
*/
public function __construct() {
$this->data = [
'current_page' => 1,
'total_page' => 0,
'record_count' => 0,
'filtered_count' => 0,
'records' => []
];
return $this;
}
/**
* @param instance $instance pdo connection data
* @return this
*/
public function db($instance) {
$this->db = $instance;
return $this;
}
/**
* @param string $from SQL or table name
*
*/
public function from($from) {
$this->from = $from;
return $this;
}
/**
* Create query and processing
* useful arguments;
* - primary: bool
* - formatter: function
* - exclude: bool -> exclude this column in where queries
* @param array $args column arguments
*/
public function process($args) {
/**
* Record length
*/
$perPage = (isset($_GET['per_page']) !== false ? (int) $_GET['per_page'] : 10);
if ($perPage <= 0) $perPage = 10;
/**
* Page number
*/
$page = (isset($_GET['page']) !== false ? (int) $_GET['page'] : 1);
if ($page <= 0) $page = 1;
/**
* Order
*/
$order = (isset($_GET['order']) !== false ? strip_tags($_GET['order']) : 'id,desc');
if (strpos($order, ',') !== false) $order = explode(',', $order, 2);
else $order = ['id', 'desc'];
$order[1] = strtolower($order[1]);
if ($order[1] !== 'asc' AND $order[1] !== 'desc')
$order[1] = 'desc';
if (! strlen($order[0]))
$order[0] = 'id';
/**
* Search
*/
$search = (isset($_GET['search']) !== false ? @json_decode(urldecode($_GET['search']), true) : []);
if (! is_array($search)) $search = [];
if (count($search)) {
foreach ($search as $col => $keyword) {
$search[$col] = trim(strip_tags($keyword));
if (! strlen($search[$col])) {
unset($search[$col]);
}
}
}
/**
* Full search
*/
$fullSearch = (isset($_GET['full_search']) !== false ? trim(strip_tags($_GET['full_search'])) : null);
if (is_string($fullSearch) AND ! strlen($fullSearch)) $fullSearch = null;
$where = [];
$whereString = '';
if (! is_null($fullSearch)) {
$columns = $args;
foreach ($columns as $col => $details) {
if (isset($details['exclude']) !== false AND $details['exclude'])
unset($columns[$col]);
}
foreach (array_keys($columns) as $column) {
$where[] = $column . ' LIKE "%' . $fullSearch . '%"';
}
$whereString = '(' . implode(' OR ', $where) . ')';
}
$where = [];
if (count($search)) {
foreach ($search as $column => $keyword) {
$where[] = $column . ' LIKE "%' . $keyword . '%"';
}
$whereString .= ($whereString !== '' ? ' AND ' : ' ') . '(' . implode(' OR ', $where) . ')';
}
if ($whereString !== '') {
$whereString = ' WHERE ' . trim($whereString);
}
// Get total record
$totalSql = 'SELECT COUNT(id) AS total FROM ' . $this->from . $whereString;
$total = $this->db->query($totalSql);
if ($total === false) {
$err = $this->db->errorInfo();
die('<pre>#' . $err[1] . ': ' . $err[2] . '</pre>');
} else {
// $total->setFetchMode(\PDO::FETCH_OBJ);
$total = $total->fetch();
$total = (int) $total->total;
$this->data['record_count'] = $total;
}
if (strpos($order[0], 'id') !== false OR strpos($order[0], '_order') !== false) {
$order[0] = 'CAST('.$order[0].' AS unsigned)';
}
// Get results
$resultSql = 'SELECT * FROM ' . $this->from .
$whereString . ' ORDER BY ' . $order[0] . ' ' . $order[1] . '
LIMIT ' . $perPage . ' OFFSET ' . (($page > 0 ? $page : 1) - 1) * $perPage;
$result = $this->db->query($resultSql);
if ($result === false) {
$err = $this->db->errorInfo();
die('<pre>#' . $err[1] . ': ' . $err[2] . '</pre>');
} else {
// $total->setFetchMode(\PDO::FETCH_OBJ);
$result = $result->fetchAll();
}
foreach ($result as $index => $data) {
$record = (object)[];
foreach ($args as $column => $details) {
$record->{$column} = null;
if (isset($details['formatter']) !== false) {
$record->{$column} = $details['formatter']($data);
} elseif (isset($data->{$column}) !== false) {
$record->{$column} = $data->{$column};
}
}
$this->data['records'][$index] = $record;
}
$this->data['filtered_count'] = count($this->data['records']);
$this->data['current_page'] = $page;
$this->data['total_page'] = ceil($total / $perPage);
return $this;
}
/**
* Output method;
* true: direct write with json header,
* false: return as array
* @param boolean $direct output method
* @return array|void
*/
public function output($direct = false) {
if ($direct) {
ob_clean();
ob_start();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($this->data);
exit;
} else {
return $this->data;
}
}
}