-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_bridge.php
420 lines (363 loc) · 15.2 KB
/
data_bridge.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* ORMDatatableBridge
*
* A bridge between Idiorm ORM and the popular Datatables javascript library
* that provides the ability to quickly generated the required JSON for
* using Datatable AJAX tables.
*
* https://github.com/tegansnyder/idiorm-datatable-json-bridge
*
* BSD Licensed.
*
* ORMDatatableBridge notice:
* Copyright (c) 2016, Tegan Snyder
* All rights reserved.
*
* Idiorm notice:
* Copyright (c) 2010, Jamie Matthews
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Tegan Snyder <tsnyder@tegdesign.com>
*/
class ORMDatatableBridge extends ORM {
public $cnt_query = false;
/**
* @param null|integer $id
* @return Model
*/
public function get_datatable($options = array()) {
$limit = $this->_limit;
$offset = $this->_offset;
$this->_limit = null;
$this->_offset = null;
$this->cnt_query = true;
$cnt = parent::find_array();
if (isset($cnt[0]['_dt_record_cnt'])) {
$cnt = $cnt[0]['_dt_record_cnt'];
} else {
// @todo throw error
$cnt = 0;
}
$this->_limit = $limit;
$this->_offset = $offset;
$this->cnt_query = false;
$results = $this->_create_json(parent::find_array(), $cnt, $options);
return $results;
}
/**
* Method to create an instance of the model class
* associated with this wrapper and populate
* it with the supplied Idiorm instance.
*
* @param array $data
* @param array $options
* @return string
*/
protected function _create_json($data, $cnt, $options) {
if ($data === false) {
return false;
}
$json = array();
$json['recordsTotal'] = $cnt;
$json['recordsFiltered'] = $cnt;
$json['data'] = array();
$x = 0;
foreach ($data as $row) {
// lets see if we have a row id we are using
if (isset($options['DT_RowId']['type'])) {
if ($options['DT_RowId']['type'] == 'fixed') {
// fixed keys will most likely not be used as they increment by the x
// value of the loop, the ideal identifier would be dynamic
if (isset($options['DT_RowId']['key'])) {
$row['DT_RowId'] = $options['DT_RowId']['key'] . $x;
} else {
// @todo exception handler
}
} elseif ($options['DT_RowId']['type'] == 'dynamic') {
// dynmaic keys increment by the value of the key and the value
// or another identifier like an id column in the dataset
if (isset($options['DT_RowId']['key'])) {
if (isset($row[$options['DT_RowId']['key']])) {
$row['DT_RowId'] = $row[$options['DT_RowId']['key']];
} else {
// @todo exception handler
}
}
}
if (isset($options['DT_RowId']['prepend'])) {
if (!empty($row['DT_RowId'])) {
$row['DT_RowId'] = $options['DT_RowId']['prepend'] . $row['DT_RowId'];
}
}
}
/**
* http://datatables.net/manual/server-side#Returned-data
* Add the data contained in the object to the row using the jQuery data() method to set the data,
* which can also then be used for later retrieval (for example on a click event).
*/
if (isset($options['DT_RowData'])) {
if (is_array($options['DT_RowData'])) {
foreach ($options['DT_RowData'] as $key => $val) {
// lets see if we are including row data via template
$templated_vals = $this->getTemplatedValues($val);
foreach ($templated_vals as $t_val) {
if (isset($row[$t_val])) {
$val = str_replace('{{' . $t_val . '}}', $row[$t_val], $val);
}
}
$row['DT_RowData'][$key] = $val;
}
}
}
/**
* the ability to add dynamic columns to the result set is very important
* for instance you may want a column called `actions` that has an edit or a delete button
* this module provides a very simple way to add these
*/
if (isset($options['dynamic_columns'])) {
foreach ($options['dynamic_columns'] as $col) {
if (isset($col['key']) && isset($col['column_template'])) {
$templated_vals = $this->getTemplatedValues($col['column_template']);
foreach ($templated_vals as $t_val) {
if (isset($row[$t_val])) {
$col['column_template'] = str_replace('{{' . $t_val . '}}', $row[$t_val], $col['column_template']);
}
}
$row[$col['key']] = $col['column_template'];
} else {
// @todo exception handler
}
}
}
// the ability to wrap columns dyanamically with html
if (isset($options['wrap_columns'])) {
foreach ($options['wrap_columns'] as $col) {
if (isset($col['key']) && isset($col['column_template'])) {
$templated_vals = $this->getTemplatedValues($col['column_template']);
foreach ($templated_vals as $t_val) {
if (isset($row[$t_val])) {
$col['column_template'] = str_replace('{{' . $t_val . '}}', $row[$t_val], $col['column_template']);
} elseif ($t_val == 'column_data') {
$col['column_template'] = str_replace('{{' . $t_val . '}}', $row[$col['key']], $col['column_template']);
}
}
$row[$col['key']] = $col['column_template'];
} else {
// @todo exception handler
}
}
}
// ability to wrap an entire column with custom HTML
if (isset($options['wrap_all']['columns'])) {
$new_row = array();
foreach ($row as $key => $val) {
if ($key == 'DT_RowId' || $key == 'DT_RowData') {
continue;
}
$new_row[$key] = str_replace('{{column_data}}', $val, $options['wrap_all']['columns']);
$new_row[$key] = str_replace('{{col_name}}', $key, $new_row[$key]);
}
foreach ($new_row as $key => $val) {
$row[$key] = $val;
}
}
// ability to rename columns for display purposes, note this happens after all processing
$new_row = array();
if (isset($options['column_display_names'])) {
foreach ($row as $row_name => $v) {
$row_renamed = false;
foreach ($options['column_display_names'] as $orig_col_name => $col_rename_to) {
if ($row_name == $orig_col_name) {
$row_renamed = true;
$new_row[$col_rename_to] = $v;
}
}
if (!$row_renamed) {
$new_row[$row_name] = $v;
}
}
if (!empty($new_row)) {
$row = $new_row;
}
}
// ability to reorder array keys to new positions
// note if the column display names option is used above then you must use the new names
// given to the columns
$new_row = array();
$old_row = $row;
if (isset($options['column_order'])) {
foreach ($options['column_order'] as $reordered_column) {
foreach ($row as $row_name => $v) {
if ($row_name == $reordered_column) {
$new_row[$reordered_column] = $v;
unset($old_row[$row_name]);
}
}
}
if (!empty($new_row)) {
$new_row = array_merge($new_row, $old_row);
$row = $new_row;
}
}
// hide any colums
if (isset($options['hide_columns'])) {
foreach ($options['hide_columns'] as $hide_col) {
foreach ($row as $row_name => $v) {
if ($row_name == $hide_col) {
unset($row[$hide_col]);
}
}
}
}
array_push($json['data'], $row);
$x = $x + 1;
}
if (isset($options['include_columns'])) {
if (isset($json['data'][0])) {
$columns = array();
foreach ($json['data'][0] as $col => $val) {
$columns[]['data'] = $col;
}
if (!empty($columns)) {
$new_json = array();
$new_json['records'] = $json;
$new_json['columns'] = $columns;
$json = $new_json;
}
}
}
$json = json_encode($json);
return $json;
}
/**
* This function only produces the COUNT query wrapped in `_dt_record_cnt`
* The parent function produces the main select queries
*/
protected function _build_select() {
if (!$this->cnt_query) {
return parent::_build_select();
}
// If the query is raw we need to remove the LIMIT and OFFSET and
// replace the param values before proceeding.
if ($this->_is_raw_query) {
$this->_values = $this->_raw_parameters;
$query = $this->_raw_query;
$new_vals = array();
if (is_array($this->_values)) {
foreach ($this->_values as $k => $v) {
// @todo find a better way to do this for raw_queries
if ($k == 'limit') {
$query = str_ireplace('LIMIT :limit', '', $query);
continue;
}
if ($k == 'offset') {
$query = str_ireplace('OFFSET :offset', '', $query);
continue;
}
if (!is_numeric($v)) {
$v = '"' . $v . '"';
}
$query = str_replace(':' . $k, $v, $query);
}
}
$query = "SELECT COUNT(*) as _dt_record_cnt FROM (" . $query . ") dt_bridge_cnt";
return $query;
}
// Build and return the full SELECT statement by concatenating
// the results of calling each separate builder method.
$query = $this->_join_if_not_empty(" ", array(
$this->_build_select_start(),
$this->_build_join(),
$this->_build_where(),
$this->_build_group_by(),
$this->_build_having(),
$this->_build_order_by(),
$this->_build_limit(),
$this->_build_offset(),
));
$query = "SELECT COUNT(*) as _dt_record_cnt FROM (" . $query . ") dt_bridge_cnt";
return $query;
}
/**
* Execute the SELECT query that has been built up by chaining methods
* on this class. Return an array of rows as associative arrays.
*/
protected function _run() {
// allow parent method to run
if (!$this->cnt_query) {
// need a way to make sure this is set
if ($this->_is_raw_query) {
if (is_array($this->_raw_parameters)) {
foreach ($this->_raw_parameters as $k => $v) {
if (!is_numeric($v)) {
$v = '"' . $v . '"';
}
$this->_raw_query = str_replace(':' . $k, $v, $this->_raw_query);
}
}
}
return parent::_run();
}
// we are not caching the COUNT - @todo - implement caching
$query = $this->_build_select();
parent::_execute($query, $this->_values, $this->_connection_name);
$statement = parent::get_last_statement();
$rows = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$rows[] = $row;
}
// reset Idiorm bound values
$this->_values = array();
return $rows;
}
/**
* @param string $str
* @return array
*/
private function getTemplatedValues($str) {
$templated_vals = array();
// extract a list of strings between {{tags}}
preg_match_all('/{{.*?}}/is', $str, $matches);
$matches = $matches[0];
foreach ($matches as $m) {
$m = str_replace('{{', '', $m);
$m = str_replace('}}', '', $m);
$templated_vals[] = $m;
}
return $templated_vals;
}
/**
* Factory method, a repeat of content in parent::for_table, so that
* created class is ORMDatatableBridge, not ORM
*
* @param string $table_name
* @param string $connection_name
* @return ORMDatatableBridge
*/
public static function for_table($table_name, $connection_name = parent::DEFAULT_CONNECTION) {
self::_setup_db($connection_name);
return new self($table_name, array(), $connection_name);
}
}