-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathStateRestore.php
More file actions
699 lines (596 loc) · 16.3 KB
/
Copy pathStateRestore.php
File metadata and controls
699 lines (596 loc) · 16.3 KB
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?php
namespace DataTables;
use DataTables\Database\Query;
class StateRestore extends Ext
{
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private properties
*/
/** @var string */
private $_columnDefault = 'defaultState';
/** @var string */
private $_columnId = 'id';
/** @var string */
private $_columnName = 'name';
/** @var string */
private $_columnPath = 'path';
/** @var string */
private $_columnShared = 'shared';
/** @var string */
private $_columnState = 'state';
/** @var string */
private $_columnTable = 'table';
/** @var string */
private $_columnUser = 'user';
/** @var Database */
private $_db;
/** @var array */
private $_result = [];
/** @var array */
private $_set = [];
/** @var string */
private $_table = '';
/** @var string */
private $_userId = '';
/** @var array */
private $_where = [];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
*/
/**
* Get / set the column name for the default state flag.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnDefault($_ = null)
{
return $this->_getSet($this->_columnDefault, $_);
}
/**
* Get / set the column name for the table's primary key.
*
* @param string $_ Primary key column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnId($_ = null)
{
return $this->_getSet($this->_columnId, $_);
}
/**
* Get / set the column name for the state name.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnName($_ = null)
{
return $this->_getSet($this->_columnName, $_);
}
/**
* Get / set the column name for the URL (path) of where the state applies.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnPath($_ = null)
{
return $this->_getSet($this->_columnPath, $_);
}
/**
* Get / set the column name for the shared flag.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnShared($_ = null)
{
return $this->_getSet($this->_columnShared, $_);
}
/**
* Get / set the column name for where the state itself is stored.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnState($_ = null)
{
return $this->_getSet($this->_columnState, $_);
}
/**
* Get / set the column name for where the name of the host DataTable is
* stored.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnTable($_ = null)
{
return $this->_getSet($this->_columnTable, $_);
}
/**
* Get / set the column name for where the user identifier is stored.
*
* @param string $_ Column name
*
* @return ($_ is null ? string : $this) Column name.
*/
public function columnUser($_ = null)
{
return $this->_getSet($this->_columnUser, $_);
}
/**
* Get the data constructed in this instance.
*
* This will get the PHP array of data that has been constructed for the
* command that has been processed by this instance. Therefore only useful after
* process has been called.
*
* @return array Processed data array.
*/
public function data()
{
return $this->_result;
}
/**
* Get / set the database connection instance.
*
* @param Database $_ Database instance
*
* @return ($_ is null ? Database : $this) Database instance.
*/
public function db($_ = null)
{
return $this->_getSet($this->_db, $_);
}
/**
* Get the JSON for the data constructed in this instance.
*
* Basically the same as the {@see StateRestore->data()} method, but in this
* case we echo, or return the JSON string of the data.
*
* @param bool $print Echo the JSON string out (true, default) or return it
* (false).
* @param int $options JSON encode option
* https://www.php.net/manual/en/json.constants.php
*
* @return ($print is false ? string : $this) JSON representation of the
* processed data if false is given as the first parameter.
*/
public function json($print = true, $options = 0)
{
if ($print) {
$json = json_encode($this->_result, $options);
header('Content-Type: application/json; charset=utf-8');
if ($json !== false) {
echo $json;
} else {
echo json_encode([
'error' => 'JSON encoding error: ' . json_last_error_msg(),
]);
}
return $this;
}
return json_encode($this->_result);
}
/**
* Process a request from the StateRestore client-side to get / set data.
*
* @param array $data Typically $_POST or $_GET as required by what is sent
* by StateRestore
*
* @return $this
*/
public function process($data)
{
if (!isset($data['action'])) {
$this->_result = ['error' => 'Unknown action'];
} elseif ($data['action'] === 'state-read') {
$this->_result = $this->_read($data);
} elseif ($data['action'] === 'state-create') {
$this->_result = $this->_create($data);
} elseif ($data['action'] === 'state-edit') {
$this->_result = $this->_edit($data);
} elseif ($data['action'] === 'state-remove') {
$this->_result = $this->_remove($data);
}
return $this;
}
/**
* Get / set extra column name / value properties to store in the db. These
* are _set only_ - they are not read back from the db when loading a state.
* They can however be used for auditing information or for extra query
* conditions.
*
* @param array $_ Column name / value pairs
*
* @return ($_ is null ? array : $this) Column values
*/
public function set($_ = null)
{
return $this->_getSet($this->_set, $_);
}
/**
* Get / set the table name that will be used for state storage in the
* database.
*
* @param string $_ Table name
*
* @return ($_ is null ? string : $this) Table name.
*/
public function table($_ = null)
{
return $this->_getSet($this->_table, $_);
}
/**
* Get / set the value for the current user identifier. This is usually a
* user id, but it could be any other unique identifier.
*
* @param string $_ User id
*
* @return ($_ is null ? string : $this) Column name.
*/
public function user($_ = null)
{
return $this->_getSet($this->_userId, $_);
}
/**
* Where condition to add to the query used to get data from the database.
*
* Can be used in two different ways:
*
* * Simple case: `where( field, value, operator )`
* * Complex: `where( fn )`
*
* The simple case is fairly self explanatory, a condition is applied to the
* data that looks like `field operator value` (e.g. `name = 'Allan'`). The
* complex case allows full control over the query conditions by providing a
* closure function that has access to the database Query that Editor is
* using, so you can use the `where()`, `or_where()`, `and_where()` and
* `where_group()` methods as you require.
*
* Please be very careful when using this method! If an edit made by a user
* using Editor removes the row from the where condition, the result is
* undefined (since Editor expects the row to still be available, but the
* condition removes it from the result set).
*
* @param string|\Closure(Query): void $key Single field name or a closure function
* @param string $value Single field value.
* @param string $op Condition operator: <, >, = etc
*
* @return ($key is null ? string[] : $this) Where condition array.
*/
public function where($key = null, $value = null, $op = '=')
{
if ($key === null) {
return $this->_where;
}
if ($key instanceof \Closure) {
$this->_where[] = $key;
} else {
$this->_where[] = [
'key' => $key,
'value' => $value,
'op' => $op,
];
}
return $this;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
/**
* Constructor.
*
* @param Database $db An instance of the DataTables Database class that we
* can use for the DB connection. Can be given here or with the 'db'
* method.
* @param string $table The table name in the database to read and write
* information from and to. Can be given here or with the 'table' method.
* @param string $pkey Primary key column name in the table given in the
* $table parameter. Can be given here or with the 'pkey' method.
*/
public function __construct($db = null, $table = null, $pkey = null)
{
// Set constructor parameters using the API - note that the get/set will
// ignore null values if they are used (i.e. not passed in)
$this->db($db);
$this->table($table);
$this->columnId($pkey);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private methods
*/
/**
* Check a property is available in the data object.
*
* @param mixed $data Data object to check
* @param mixed $prop Property name
*
* @return bool true if valid, false otherwise.
*/
private function _assert($data, $prop)
{
return isset($data[$prop]);
}
/**
* Validate submitted state data.
*
* @param mixed $data Data to validate
*
* @return bool|array{error: string} `true` if valid, or an
*/
private function _assertStateData($data)
{
if (!$this->_assert($data, 'isDefault')) {
return ['error' => 'Incomplete data - no default flag'];
}
if (!$this->_assert($data, 'isSharedOut')) {
return ['error' => 'Incomplete data - no share flag'];
}
if (!$this->_assert($data, 'name') || !$data['name']) {
return ['error' => 'Incomplete data - no name'];
}
if (!$this->_assert($data, 'state') || !json_validate($data['state'])) {
return ['error' => 'Incomplete data - no valid state'];
}
$validate = $this->_assertStateHost($data);
if ($validate !== true) {
return $validate;
}
return true;
}
/**
* Check the parameters that are submitted for host table information.
*
* @param mixed $data Data to validate
*
* @return bool|array{error: string} `true` if valid, or an
*/
private function _assertStateHost($data)
{
if (!$this->_assert($data, 'path') || !$data['path']) {
return ['error' => 'Incomplete data - no path'];
}
if (!$this->_assert($data, 'table') || !$data['table']) {
return ['error' => 'Incomplete data - table'];
}
return true;
}
/**
* Add a new state to the database.
*
* @param mixed $data State information
*
* @return array
*/
private function _create($data)
{
$validate = $this->_assertStateData($data);
if ($validate !== true) {
return $validate;
}
$set = [];
$set[$this->_columnDefault] = $this->_valueBoolean($data['isDefault']);
$set[$this->_columnName] = $data['name'];
$set[$this->_columnPath] = $data['path'];
$set[$this->_columnShared] = $this->_valueBoolean($data['isSharedOut']);
$set[$this->_columnState] = $data['state'];
$set[$this->_columnTable] = $data['table'];
if ($this->_userId) {
$set[$this->_columnUser] = $this->_userId;
}
// Dev defined values (server-side)
foreach ($this->_set as $key => $value) {
$set[$key] = $value;
}
if ($set[$this->_columnDefault]) {
$this->_removeDefault($data);
}
$res = $this->db()->insert($this->_table, $set, $this->_columnId);
$id = $res->insertId();
return $this->_read($data, $id);
}
/**
* Update a state on the database.
*
* @param mixed $data State information
*
* @return array
*/
private function _edit($data)
{
$validate = $this->_assertStateData($data);
if ($validate !== true) {
return $validate;
}
if (!isset($data['id'])) {
return [
'error' => 'Incomplete data - no id',
];
}
// Values to set
$set = [];
$set[$this->_columnDefault] = $this->_valueBoolean($data['isDefault']);
$set[$this->_columnName] = $data['name'];
$set[$this->_columnShared] = $this->_valueBoolean($data['isSharedOut']);
$set[$this->_columnState] = $data['state'];
// Dev defined values (server-side)
foreach ($this->_set as $key => $value) {
$set[$key] = $value;
}
// Conditions
$where = [];
$where[$this->_columnId] = $data['id'];
$where[$this->_columnTable] = $data['table'];
$where[$this->_columnPath] = $data['path'];
if ($this->_userId) {
$where[$this->_columnUser] = $this->_userId;
}
if ($set[$this->_columnDefault]) {
$this->_removeDefault($data);
}
$this->db()->update($this->_table, $set, $where);
return $this->_read($data, $data['id']);
}
/**
* Read the states from the db.
*
* @param array $data Submitted data
* @param string $id Limit the read to a specific ID
*
* @return array
*/
private function _read($data, $id = null)
{
$q = $this->db()
->query('select')
->table($this->_table)
->get($this->_columnId . ' as id');
if ($this->_columnDefault) {
$q->get($this->_columnDefault . ' as isDefault');
}
if ($this->_columnName) {
$q->get($this->_columnName . ' as name');
}
if ($this->_columnShared) {
$q->get($this->_columnShared . ' as isSharedOut');
}
if ($this->_columnState) {
$q->get($this->_columnState . ' as state');
}
if ($this->_columnUser) {
$q->get($this->_columnUser . ' as user');
}
// Must have the table and path, otherwise all states would be returned!
if (!isset($data['table']) || !isset($data['path'])) {
return [
'error' => 'Source table and path not set',
];
}
// Conditions
$q->where($this->_columnTable, $data['table']);
$q->where($this->_columnPath, $data['path']);
if ($id) {
$q->where($this->_columnId, $id);
}
// The user id is optional, but there can't be any separation of user
// states without it!
if ($this->_userId) {
$q->where(function ($r) {
$r->where($this->_columnUser, $this->_userId);
$r->or_where($this->_columnShared, 1);
});
}
// Dev set conditions
for ($i = 0; $i < count($this->_where); ++$i) {
if ($this->_where[$i] instanceof \Closure) {
$this->_where[$i]($q);
} else {
$q->where(
$this->_where[$i]['key'],
$this->_where[$i]['value'],
$this->_where[$i]['op']
);
}
}
$res = $q->exec();
$out = [];
// Map to the JSON structure that StateRestore expects
while ($row = $res->fetch()) {
$out[] = [
'id' => $row['id'],
'isDefault' => $row['isDefault'],
'isSharedIn' => $this->_userId && $row['user'] != $this->_userId ? true : false,
'isSharedOut' => $row['isSharedOut'],
'isStatic' => false,
'name' => $row['name'],
'state' => $row['state'],
];
}
return [
'data' => $out,
];
}
/**
* Delete states.
*
* @param mixed $data
*
* @return array
*/
private function _remove($data)
{
$validate = $this->_assertStateHost($data);
if ($validate !== true) {
return $validate;
}
if (!isset($data['ids']) || !is_array($data['ids'])) {
return [
'error' => 'Invalid submitted data',
];
}
$q = $this->_db
->query('delete')
->table($this->_table);
$q->where($this->_columnTable, $data['table']);
$q->where($this->_columnPath, $data['path']);
if ($this->_userId) {
$q->where($this->_columnUser, $this->_userId);
}
$q->where_in($this->_columnId, $data['ids']);
$q->exec();
return [
'data' => [],
];
}
/**
* If there is an existing default, remove it. The client-side will do this
* as well, so we don't need to worry about there being two default states
* shown, despite only returning a single record.
*
* @param mixed $data Submitted data
*
* @return void
*/
private function _removeDefault($data)
{
$validate = $this->_assertStateHost($data);
if ($validate !== true) {
return;
}
// Values to set
$set = [];
$set[$this->_columnDefault] = 0;
// Conditions
$where = [];
$where[$this->_columnDefault] = 1;
$where[$this->_columnTable] = $data['table'];
$where[$this->_columnPath] = $data['path'];
if ($this->_userId) {
$where[$this->_columnUser] = $this->_userId;
}
$this->db()->update($this->_table, $set, $where);
}
/**
* Convert a boolean HTTP value to a PHP boolean.
*
* @param mixed $data
*
* @return int
*/
private function _valueBoolean($data)
{
if ($data === 'true' || $data === 't' || $data === '1' || $data === 1 || $data === true) {
return 1;
}
return 0;
}
}