-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathgrid.php
568 lines (469 loc) · 15.3 KB
/
grid.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/*
OpenJS Grid
This is openGrid version 2.0
*/
Class Grid {
var $data;
var $joins;
var $fields;
var $where;
var $table;
var $groupBy;
var $having;
var $limit;
var $order_by;
var $sort;
var $security;
var $set;
var $sql;
function __construct($table, $options) {
$this->table = $table;
// save
if( isset($options['save']) && isset($_POST['save']) && $options['save'] == "true") {
echo $this->save();
// delete
} else if( isset($options['delete']) && isset($_POST['delete']) && $options['delete'] == "true") {
echo $this->delete();
// delete
} else if( isset($options['select']) && isset($_POST['select']) && $options['select'] == "true") {
$this->select = true;
// select boxes
} else if(isset($options['select']) && isset($_POST['select'])) {
$this->joins = array();
$this->where = "";
$this->fields = array();
$call = $options['select'];
if(is_array($call)) {
call_user_func($call,$this);
} else {
$call($this);
}
// load
} else {
if(isset($options['where'])) $this->where = $options['where'];
if(isset($options['fields'])) $this->fields = $options['fields'];
if(isset($options['joins'])) $this->joins = $options['joins'];
if(isset($options['groupBy'])) $this->groupBy = $options['groupBy'];
if(isset($options['having'])) $this->having = $options['having'];
$this->load()->render();
}
}
function save() {
$saveArray = $this->getSaveArray();
// we need a primary key for editing
$primaryKey = $this->getPrimaryKey();
// die here if a primary is not found
if(empty($primaryKey)) die("Primary Key for table {$this->table} Not set! For inline editing you must have a primary key on your table.");
// go through each row and perform an update
foreach($saveArray as $rowId=>$row) {
$setArray = array();
foreach($row as $key=>$value) {
// don't update this row if you have security set
// idea from youtube user jfuruskog
if(!is_array($this->security) || in_array($key,$this->security)) {
// dont save fields that weren't saveable. i.e. joined fields
if(in_array($key,$_POST['saveable'])) {
$key = mysql_real_escape_string($key);
$value = mysql_real_escape_string($value);
$setArray[] = "`$key`='$value'";
}
}
}
$sql = "UPDATE {$this->table} SET ".implode(",",$setArray)." WHERE `$primaryKey` = '$rowId'";
$res = mysql_query($sql);
// die with messages if fail
$this->dieOnError($sql);
}
return (bool) $res;
}
// use this to write your own custom save function for the data
function getSaveArray() {
return $_POST['json'];
}
// adds a new row based on the editable fields
function add() {
// if didn't pass a set param, just add a new row
if(empty($this->set)) {
mysql_query("INSERT INTO {$this->table} VALUES ()");
// if you passed a set param then use that in the insert
} else {
mysql_query("INSERT INTO {$this->table} SET {$this->set}");
}
// we return the primary key so that we can order by it in the jS
echo $this->getPrimaryKey();
}
function delete() {
$post = $this->_safeMysql();
$primaryKey = $this->getPrimaryKey();
return mysql_query("DELETE FROM {$this->table} WHERE `$primaryKey` = '$post[id]'");
}
function select($selects) {
foreach($selects as $s) {
echo function_exists($s);
}
}
// will build an id, value array to be used to make a select box
function makeSelect($value,$display) {
// build sql if they are there
$where = $this->where ? "WHERE {$this->where}":"";
$order_by = $this->order_by ? "ORDER BY {$this->order_by}":"";
$sort = $this->sort ? "{$this->sort}":"";
$limit = $this->limit ? "LIMIT {$this->limit}":"";
$table = $this->table;
// bring all the joins togther if sent
if(is_array($this->joins)) {
$joins = implode(" ",$this->joins);
} else {
$joins = "";
}
// we only are selecting 2 columns, the one to use as the ID and the one for the display
$colsArray = array($value,$display);
$newColsArray = array();
$usedCols = array();
// loop through each complex field
if($this->fields && is_array($this->fields)) {
foreach($this->fields as $as=>$field) {
// find which column this is to replace (replace in terms of the column for its complex counterpart)
foreach($colsArray as $col) {
// replace your alias with the complex field
if($col == $as) {
// field from OTHER table
$newColsArray[] = "$field as `$as`";
// mark as used
$usedCols[] = $col;
} else {
// field from THIS table that aren't in the fields array
if(!isset($this->fields[$col]) && !in_array($col,$usedCols)) {
$newColsArray[] = "`$table`.`$col`";
$usedCols[] = $col;
}
}
}
}
} else {
// add safe tics
foreach($colsArray as $key=>$col) {
$newColsArray[] = "`$table`.`$col`";
}
}
// put it back
$colsArray = $newColsArray;
// get group and having
$groupBy = $this->groupBy ? "GROUP BY ".$this->groupBy : "";
$having = $this->having ? "HAVING ".$this->having : "";
// bring it all together again
$cols = implode(",",$colsArray);
// setup the sql - bring it all together
$sql = "
SELECT $cols
FROM `$table`
$joins
$where
$groupBy
$having
$order_by $sort
$limit
";
// run sql, build id/value json
$rows = $this->_queryMulti($sql);
$this->dieOnError($sql);
// setup rows to feed back to JS
foreach($rows as $row) {
$data[$row[$value]] = $row[$display];
}
// set our data so we can get it later
$this->data = $data;
return $data;
}
// loads data into the grid
function load() {
$post = $this->_safeMysql();
// setup variables from properties
$joins = $this->joins;
$fields = $this->fields;
$where = $this->where;
$table = $this->table;
// we need to break this up for use
$colsArray = explode(",",$post['cols']);
// get an array of saveable fields
$saveable = $colsArray;
// bug #1# @[email protected]
if(is_array($fields)) {
foreach($fields as $field=>$detail) {
foreach($saveable as $k=>$f) {
if( $f == $field ) {
unset($saveable[$k]);
}
}
}
}
// were gonna use this one because this allows us to order by a column that we didnt' pass
$order_by = isset($post['orderBy']) ? $post['orderBy'] : $colsArray[0];
// save variables for easier use throughout the code
$sort = isset($post['sort']) ? $post['sort'] : "asc";
$nRowsShowing = isset($post['nRowsShowing']) ? $post['nRowsShowing'] : 10;
$page = isset($post['page']) ? $post['page'] : 1;
$startRow = ($page - 1) * $nRowsShowing;
// bring all the joins togther if sent
if((bool)$joins && is_array($joins)) {
$joins = implode(" ",$joins);
} else {
$joins = "";
}
// if there are specific fields to add
// replace the specefied alias with its complex field
$colsArrayForWhere = array();
$newColsArray = array();
$usedCols = array();
$groupFunctions = array(
"AVG",
"BIT_AND",
"BIT_OR",
"BIT_XOR",
"COUNT",
"GROUP_CONCAT",
"ROUND",
"MAX",
"MIN",
"STD",
"STDDEV_POP",
"STDDEV_SAMP",
"STDDEV",
"SUM",
"VAR_POP",
"VAR_SAMP",
"VARIANCE"
);
if($fields && is_array($fields)) {
foreach($fields as $as=>$field) {
// find which column this is to replace
foreach($colsArray as $col) {
// replace your alias with the complex field
if($col == $as && !in_array($col,$usedCols)) {
// field from OTHER table
$newColsArray[] = "$field as `$as`";
// we can't search by group functions
preg_match('/^\w+/i',$field,$needle);
if(!in_array(strtoupper($needle[0]),$groupFunctions)) {
$colsArrayForWhere[] = $field;
}
// mark as used
$usedCols[] = $col;
} else {
// field from THIS non joined table that aren't in the fields array
if(!isset($fields[$col]) && !in_array($col,$usedCols)) {
$newColsArray[] = "`$table`.`$col`";
$colsArrayForWhere[] = "`$table`.`$col`";
$usedCols[] = $col;
// add fields that aren't in the <table> but you want passed anyway
} else if(!in_array($as,$usedCols)){
// were just using field & as because you should have back ticked and chosen your table in your call
$newColsArray[] = "$field as `$as`";
// we can't search by group functions
preg_match('/^\w+/i',$field,$needle);
if(isset($needle[0])) {
if(!in_array(strtoupper($needle[0]),$groupFunctions)) {
$colsArrayForWhere[] = $field;
}
}
$usedCols[] = $as;
}
}
}
}
} else {
// add safe tics
foreach($colsArray as $key=>$col) {
$newColsArray[] = "`$table`.`$col`";
$colsArrayForWhere[] = "`$table`.`$col`";
}
}
// put it back
$colsArray = $newColsArray;
// get primary key
$primaryKey = $this->getPrimaryKey();
// if primary key isn't in the list. add it.
if($primaryKey && !in_array($primaryKey,$usedCols)) {
$colsArray[] = $table.".".$primaryKey;
}
// with the cols array, if requested
$colData = array();
if(isset($post['maxLength']) && $post['maxLength'] == "true") {
foreach($colsArray as $col) {
// if there is no as (we can't determine length on aliased fields)
if(stripos($col," as ") === false) {
$col = str_replace("`","",$col);
list($aTable,$field) = explode(".",$col);
if(!$aTable) $aTable = $this->table;
$colDataSql = mysql_query("SHOW columns FROM $aTable WHERE Field = '$field'");
while($row = mysql_fetch_assoc($colDataSql)) {
$type = $row['Type'];
}
preg_match('/\(([^\)]+)/',$type,$matches);
$colData[$field] = array("maxLength"=>$matches[1]);
}
}
}
// shrink to comma list
$post['cols'] = implode(",",$colsArray);
// add dateRange to where
if(!empty($post['dateRangeFrom']) || !empty($post['dateRangeTo'])) {
// if one or the other is empty - use today otherwise parse into mysql date the date that was passed
$dateFrom = empty($post['dateRangeFrom']) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',strtotime($post['dateRangeFrom']));
$dateTo = empty($post['dateRangeTo']) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',strtotime($post['dateRangeTo']));
// if they are = we want just this day (otherwise it would be blank)
if($dateFrom == $dateTo) {
$dateWhere = "DATE($table.$post[dateRange]) = DATE('$dateFrom')";
// we actually want a range
} else {
$dateWhere = "`$table`.`$post[dateRange]` BETWEEN '$dateFrom' AND '$dateTo'";
}
// add this to the global where statement
if(empty($where)) {
$where = $dateWhere;
} else {
$where = "($dateWhere) && $where";
}
}
// specific where setup for searching
if(isset($post['search']) && $post['search']) {
// if there is a search term, add the custom where first, then the search
$where = !$where ? " WHERE " : " WHERE ($where) && ";
// if you are searching, at a like to all the columns
$where .= "(".implode(" LIKE '%$post[search]%' || ",$colsArrayForWhere) . " LIKE '%$post[search]%')";
} else {
// add the where keyword if there is no search term
if($where) {
$where = "WHERE $where";
}
}
// get group and having
$groupBy = $this->groupBy ? "GROUP BY ".$this->groupBy : "";
$having = $this->having ? "HAVING ".$this->having : "";
if($startRow < 0) $startRow = 1;
// we need this seperate so we can not have a limit at all
$limit = "LIMIT $startRow,$nRowsShowing";
// if were searching, see if we want all results or not
if(isset($_POST['pager']) && $_POST['pager'] == "false" || (!empty($_POST['search']) && isset($_POST['pageSearchResults']))) {
$limit = "";
}
// setup the sql - bring it all together
$order = strpos($order_by,".") === false ? "`$order_by`" : $order_by;
$sql = "
SELECT $post[cols]
FROM `$table`
$joins
$where
$groupBy
$having
ORDER BY $order $sort
$limit
";
$this->sql = $sql;
// execute the sql, get back a multi dimensial array
$rows = $this->_queryMulti($sql);
// die with messages if fail
$this->dieOnError($sql);
// form an array of the data to send back
$data = array();
$data['rows'] = array();
foreach($rows as $i=>$row) {
foreach($row as $col=>$cell) {
// use primary key if possible, other wise use index
$key = $primaryKey ? $row[$primaryKey] : $i;
// primary key has an _ infront becuase of google chrome re ordering JSON objects
//http://code.google.com/p/v8/issues/detail?id=164
$data['rows']["_".$key][$col] = utf8_encode($cell);
}
}
// if were searching and we dont want all the results - dont run a 2nd query
if(isset($_POST['pager']) && $_POST['pager'] == "false" || (!empty($_POST['search']) && isset($_POST['pageSearchResults']))) {
$data['nRows'] = count($rows);
$startRow = 0;
$nRowsShowing = $data['nRows'];
} else {
if(!$this->limit) {
// use the same query for counting less the limit
$sql2 = preg_replace('/LIMIT[\s\d,]+$/','',$sql);
// find the total results to send back
$res = mysql_query($sql2);
$data['nRows'] = mysql_num_rows($res);
} else {
$data['nRows'] = $this->limit;
}
}
$data['order_by'] = $order_by;
$data['sort'] = $sort;
$data['page'] = $page;
$data['start'] = $startRow + 1;
$data['end'] = $startRow + $nRowsShowing;
$data['colData'] = $colData;
$data['saveable'] = $saveable;
$this->data = $data;
return $this;
}
// renders the json data out
function render($data=NULL) {
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if($data) $this->data = $data;
echo json_encode($this->data);
}
// incomplete
// will allow this whole thing to run off an entire custom query
// as in not just setting props
function loadWithSql($sql) {
$sql = preg_replace('/[ORDER BY|order by]+[\s]+[^\n\r]+/','',$sql);
$sql = preg_replace('/[LIMIT|limit]+[\s\d,]+$/','',$sql);
echo $sql;
}
// using the current table will get the primary key column name
// does not work for combined primary keys
function getPrimaryKey($table=NULL) {
if(!$table) $table = $this->table;
$primaryKey = mysql_query("SHOW KEYS FROM `$table` WHERE Key_name = 'PRIMARY'");
$primaryKey = mysql_fetch_assoc($primaryKey);
return $primaryKey['Column_name'];
}
// if there is a mysql error it will die with that error
function dieOnError($sql) {
if($e=mysql_error()) {
//var_dump($sql);
die($e);
}
}
// runs a query, always returns a multi dimensional array of results
function _queryMulti($sql) {
$array = array();
$res = mysql_query($sql);
if((bool)$res) {
// if there is only 1 field, just return and array with that field as each value
if(mysql_num_fields($res) > 1) {
while($row = mysql_fetch_assoc($res)) $array[] = $row;
} else if(mysql_num_fields($res) == 1) {
while($row = mysql_fetch_assoc($res)) {
foreach($row as $item) $array[] = $item;
}
}
$error = mysql_error();
if($error) echo $error;
}
return $array;
}
// safeify post
function _safeMysql($post=NULL) {
if(!isset($post)) $post = $_POST;
$postReturn = array();
foreach($post as $key=>$value) {
if(!is_array($value)) {
$postReturn[$key] = mysql_real_escape_string(urldecode($value));
} else if(is_array($value)) {
$postReturn[$key] = $value;
}
}
return $postReturn;
}
}
?>