1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Libraries\Crud; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database\DataObjects\Result; |
19
|
|
|
use O2System\Framework\Libraries\Ui; |
20
|
|
|
use O2System\Html\Element; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Table |
24
|
|
|
* |
25
|
|
|
* @package O2System\Libraries\CRUD |
26
|
|
|
*/ |
27
|
|
|
class Table extends Ui\Contents\Table |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Table::$rows |
31
|
|
|
* |
32
|
|
|
* Table rows resource object. |
33
|
|
|
* |
34
|
|
|
* @var \O2System\Database\DataObjects\Result |
35
|
|
|
*/ |
36
|
|
|
public $rows; |
37
|
|
|
/** |
38
|
|
|
* Table::$columns |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $columns = []; |
43
|
|
|
/** |
44
|
|
|
* Table::$config |
45
|
|
|
* |
46
|
|
|
* Table configuration. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $config = [ |
51
|
|
|
'id' => true, |
52
|
|
|
'numbering' => true, |
53
|
|
|
'checkbox' => true, |
54
|
|
|
'images' => false, |
55
|
|
|
'actions' => true, |
56
|
|
|
'labels' => false, |
57
|
|
|
'responsive' => true, |
58
|
|
|
'nested' => false, |
59
|
|
|
'ordering' => false, |
60
|
|
|
'createTimestamp' => false, |
61
|
|
|
'updateTimestamp' => false, |
62
|
|
|
'status' => true, |
63
|
|
|
'heading' => null, |
64
|
|
|
'entries' => [ |
65
|
|
|
'minimum' => 10, |
66
|
|
|
'maximum' => 100, |
67
|
|
|
'step' => 10, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
protected $tools = [ |
71
|
|
|
'create' => [ |
72
|
|
|
'label' => true, |
73
|
|
|
'show' => true, |
74
|
|
|
'contextual' => 'default', |
75
|
|
|
'icon' => 'fa fa-file-o', |
76
|
|
|
'href' => false, |
77
|
|
|
], |
78
|
|
|
'update' => [ |
79
|
|
|
'label' => true, |
80
|
|
|
'show' => true, |
81
|
|
|
'contextual' => 'default', |
82
|
|
|
'icon' => 'fa fa-pencil', |
83
|
|
|
'href' => false, |
84
|
|
|
], |
85
|
|
|
'publish' => [ |
86
|
|
|
'label' => true, |
87
|
|
|
'show' => true, |
88
|
|
|
'contextual' => 'default', |
89
|
|
|
'icon' => 'fa fa-eye', |
90
|
|
|
'href' => false, |
91
|
|
|
], |
92
|
|
|
'unpublish' => [ |
93
|
|
|
'label' => true, |
94
|
|
|
'show' => true, |
95
|
|
|
'contextual' => 'default', |
96
|
|
|
'icon' => 'fa fa-eye-slash', |
97
|
|
|
'href' => false, |
98
|
|
|
], |
99
|
|
|
'delete' => [ |
100
|
|
|
'label' => true, |
101
|
|
|
'show' => true, |
102
|
|
|
'contextual' => 'default', |
103
|
|
|
'icon' => 'fa fa-trash', |
104
|
|
|
'href' => false, |
105
|
|
|
], |
106
|
|
|
'archive' => [ |
107
|
|
|
'label' => true, |
108
|
|
|
'show' => true, |
109
|
|
|
'contextual' => 'default', |
110
|
|
|
'icon' => 'fa fa-archive', |
111
|
|
|
'href' => false, |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
protected $options = [ |
115
|
|
|
'grid' => [ |
116
|
|
|
'label' => true, |
117
|
|
|
'show' => true, |
118
|
|
|
'contextual' => 'default', |
119
|
|
|
'icon' => 'fa fa-id-card', |
120
|
|
|
'href' => false, |
121
|
|
|
], |
122
|
|
|
'reset' => [ |
123
|
|
|
'label' => false, |
124
|
|
|
'show' => true, |
125
|
|
|
'contextual' => 'default', |
126
|
|
|
'icon' => 'fa fa-repeat', |
127
|
|
|
'href' => false, |
128
|
|
|
], |
129
|
|
|
'reload' => [ |
130
|
|
|
'label' => false, |
131
|
|
|
'show' => true, |
132
|
|
|
'contextual' => 'default', |
133
|
|
|
'icon' => 'fa fa-refresh', |
134
|
|
|
'href' => false, |
135
|
|
|
], |
136
|
|
|
'archive' => [ |
137
|
|
|
'label' => true, |
138
|
|
|
'show' => true, |
139
|
|
|
'contextual' => 'success', |
140
|
|
|
'icon' => 'fa fa-archive', |
141
|
|
|
'href' => false, |
142
|
|
|
], |
143
|
|
|
'help' => [ |
144
|
|
|
'label' => true, |
145
|
|
|
'show' => true, |
146
|
|
|
'contextual' => 'default', |
147
|
|
|
'icon' => 'fa fa-help', |
148
|
|
|
'href' => false, |
149
|
|
|
], |
150
|
|
|
]; |
151
|
|
|
protected $actions = [ |
152
|
|
|
'view' => [ |
153
|
|
|
'label' => false, |
154
|
|
|
'show' => false, |
155
|
|
|
'contextual' => 'default', |
156
|
|
|
'icon' => 'fa fa-eye', |
157
|
|
|
'href' => false, |
158
|
|
|
], |
159
|
|
|
'copy' => [ |
160
|
|
|
'label' => false, |
161
|
|
|
'show' => false, |
162
|
|
|
'contextual' => 'default', |
163
|
|
|
'icon' => 'fa fa-clone', |
164
|
|
|
'href' => false, |
165
|
|
|
], |
166
|
|
|
'edit' => [ |
167
|
|
|
'label' => false, |
168
|
|
|
'show' => false, |
169
|
|
|
'contextual' => 'default', |
170
|
|
|
'icon' => 'fa fa-pencil', |
171
|
|
|
'href' => false, |
172
|
|
|
], |
173
|
|
|
'delete' => [ |
174
|
|
|
'label' => false, |
175
|
|
|
'show' => false, |
176
|
|
|
'contextual' => 'default', |
177
|
|
|
'icon' => 'fa fa-trash', |
178
|
|
|
'href' => false, |
179
|
|
|
], |
180
|
|
|
'archive' => [ |
181
|
|
|
'label' => false, |
182
|
|
|
'show' => false, |
183
|
|
|
'contextual' => 'default', |
184
|
|
|
'icon' => 'fa fa-archive', |
185
|
|
|
'href' => false, |
186
|
|
|
], |
187
|
|
|
'export' => [ |
188
|
|
|
'label' => false, |
189
|
|
|
'show' => false, |
190
|
|
|
'contextual' => 'default', |
191
|
|
|
'icon' => 'fa fa-mail-forward', |
192
|
|
|
'href' => false, |
193
|
|
|
], |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
// ------------------------------------------------------------------------ |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Table::__construct |
200
|
|
|
* |
201
|
|
|
* @param array $config |
202
|
|
|
*/ |
203
|
|
|
public function __construct(array $config = []) |
204
|
|
|
{ |
205
|
|
|
parent::__construct(); |
206
|
|
|
|
207
|
|
|
language()->loadFile('crud/table'); |
208
|
|
|
presenter()->assets->loadJs('crud/table'); |
209
|
|
|
|
210
|
|
|
$this->attributes->addAttributeClass(['table-striped', 'table-hover', 'mb-0']); |
211
|
|
|
$this->setConfig($config); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// ------------------------------------------------------------------------ |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Table::setConfig |
218
|
|
|
* |
219
|
|
|
* Set table configuration. |
220
|
|
|
* |
221
|
|
|
* @param array $config |
222
|
|
|
* |
223
|
|
|
* @return static |
224
|
|
|
*/ |
225
|
|
|
public function setConfig(array $config) |
226
|
|
|
{ |
227
|
|
|
$this->config = array_merge($this->config, $config); |
228
|
|
|
|
229
|
|
|
if ($this->config[ 'responsive' ]) { |
230
|
|
|
$this->responsive(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// ------------------------------------------------------------------------ |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Table::setRows |
240
|
|
|
* |
241
|
|
|
* @param \O2System\Database\DataObjects\Result $rows |
242
|
|
|
* |
243
|
|
|
* @return static |
244
|
|
|
*/ |
245
|
|
|
public function setRows(Result $rows) |
246
|
|
|
{ |
247
|
|
|
$this->rows = $rows; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// ------------------------------------------------------------------------ |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Table::setColumns |
256
|
|
|
* |
257
|
|
|
* @param array $columns |
258
|
|
|
* |
259
|
|
|
* @return static |
260
|
|
|
*/ |
261
|
|
|
public function setColumns(array $columns) |
262
|
|
|
{ |
263
|
|
|
$defaultColumn = [ |
264
|
|
|
'field' => 'label', |
265
|
|
|
'label' => 'Label', |
266
|
|
|
'attr' => [ |
267
|
|
|
'name' => '', |
268
|
|
|
'id' => '', |
269
|
|
|
'class' => '', |
270
|
|
|
'width' => '', |
271
|
|
|
'style' => '', |
272
|
|
|
], |
273
|
|
|
'format' => 'txt', |
274
|
|
|
'show' => true, |
275
|
|
|
'sorting' => true, |
276
|
|
|
'hiding' => true, |
277
|
|
|
'options' => false, |
278
|
|
|
'nested' => false, |
279
|
|
|
]; |
280
|
|
|
|
281
|
|
|
$this->setPrependColumns(); |
282
|
|
|
|
283
|
|
|
foreach ($columns as $key => $column) { |
284
|
|
|
$column = array_merge($defaultColumn, $column); |
285
|
|
|
$this->columns[ $key ] = $column; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->setAppendColumns(); |
289
|
|
|
|
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
// ------------------------------------------------------------------------ |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Table::setPrependColumns |
297
|
|
|
*/ |
298
|
|
|
protected function setPrependColumns() |
299
|
|
|
{ |
300
|
|
|
$prependColumns = [ |
301
|
|
|
'numbering' => [ |
302
|
|
|
'field' => 'numbering', |
303
|
|
|
'label' => '#', |
304
|
|
|
'attr' => [ |
305
|
|
|
'width' => '5%', |
306
|
|
|
], |
307
|
|
|
'format' => 'number', |
308
|
|
|
'show' => true, |
309
|
|
|
'sorting' => true, |
310
|
|
|
'hiding' => false, |
311
|
|
|
'options' => false, |
312
|
|
|
'nested' => false, |
313
|
|
|
'content' => '', |
314
|
|
|
], |
315
|
|
|
'id' => [ |
316
|
|
|
'field' => 'id', |
317
|
|
|
'label' => 'ID', |
318
|
|
|
'attr' => [ |
319
|
|
|
'class' => 'text-right', |
320
|
|
|
'width' => '3%', |
321
|
|
|
], |
322
|
|
|
'format' => 'txt', |
323
|
|
|
'show' => true, |
324
|
|
|
'sorting' => true, |
325
|
|
|
'hiding' => true, |
326
|
|
|
'options' => false, |
327
|
|
|
'nested' => false, |
328
|
|
|
], |
329
|
|
|
'checkbox' => [ |
330
|
|
|
'field' => 'id', |
331
|
|
|
'label' => new Ui\Components\Form\Elements\Checkbox([ |
|
|
|
|
332
|
|
|
'data-toggle' => 'table-crud-checkbox', |
333
|
|
|
]), |
334
|
|
|
'attr' => [ |
335
|
|
|
'width' => '2%', |
336
|
|
|
], |
337
|
|
|
'format' => 'checkbox', |
338
|
|
|
'show' => true, |
339
|
|
|
'sorting' => false, |
340
|
|
|
'hiding' => false, |
341
|
|
|
'options' => false, |
342
|
|
|
'nested' => false, |
343
|
|
|
], |
344
|
|
|
'images' => [ |
345
|
|
|
'field' => 'images', |
346
|
|
|
'label' => null, |
347
|
|
|
'attr' => [ |
348
|
|
|
'width' => '5%', |
349
|
|
|
], |
350
|
|
|
'format' => 'image', |
351
|
|
|
'show' => true, |
352
|
|
|
'sorting' => false, |
353
|
|
|
'hiding' => true, |
354
|
|
|
'options' => false, |
355
|
|
|
'nested' => false, |
356
|
|
|
], |
357
|
|
|
]; |
358
|
|
|
|
359
|
|
|
foreach ($prependColumns as $key => $column) { |
360
|
|
|
if ($this->config[ $key ] === true) { |
361
|
|
|
$this->columns[ $key ] = $column; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// ------------------------------------------------------------------------ |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Table::setAppendColumns |
370
|
|
|
*/ |
371
|
|
|
protected function setAppendColumns() |
372
|
|
|
{ |
373
|
|
|
$appendColumns = [ |
374
|
|
|
'ordering' => [ |
375
|
|
|
'field' => 'ordering', |
376
|
|
|
'label' => '', |
377
|
|
|
'attr' => [ |
378
|
|
|
'class' => 'width-fit', |
379
|
|
|
'width' => '1%', |
380
|
|
|
], |
381
|
|
|
'format' => 'ordering', |
382
|
|
|
'show' => true, |
383
|
|
|
'sorting' => false, |
384
|
|
|
'filtering' => false, |
385
|
|
|
'hiding' => false, |
386
|
|
|
'options' => true, |
387
|
|
|
'nested' => false, |
388
|
|
|
], |
389
|
|
|
'status' => [ |
390
|
|
|
'field' => 'record_status', |
391
|
|
|
'label' => 'TABLE_LABEL_STATUS', |
392
|
|
|
'format' => 'status', |
393
|
|
|
'show' => false, |
394
|
|
|
'sorting' => false, |
395
|
|
|
'hiding' => true, |
396
|
|
|
'grouping' => true, |
397
|
|
|
'options' => true, |
398
|
|
|
'nested' => false, |
399
|
|
|
], |
400
|
|
|
'createTimestamp' => [ |
401
|
|
|
'field' => 'record_create_timestamp', |
402
|
|
|
'label' => 'TABLE_LABEL_CREATED_DATE', |
403
|
|
|
'format' => 'date', |
404
|
|
|
'show' => false, |
405
|
|
|
'hidden' => true, |
406
|
|
|
'sorting' => true, |
407
|
|
|
'hiding' => true, |
408
|
|
|
'grouping' => false, |
409
|
|
|
'options' => true, |
410
|
|
|
'nested' => false, |
411
|
|
|
], |
412
|
|
|
'updateTimestamp' => [ |
413
|
|
|
'field' => 'record_update_timestamp', |
414
|
|
|
'label' => 'TABLE_LABEL_UPDATED_DATE', |
415
|
|
|
'format' => 'date', |
416
|
|
|
'show' => false, |
417
|
|
|
'hidden' => true, |
418
|
|
|
'sorting' => true, |
419
|
|
|
'grouping' => false, |
420
|
|
|
'hiding' => true, |
421
|
|
|
'options' => true, |
422
|
|
|
'nested' => false, |
423
|
|
|
], |
424
|
|
|
'actions' => [ |
425
|
|
|
'field' => null, |
426
|
|
|
'label' => null, |
427
|
|
|
'format' => 'actions', |
428
|
|
|
'show' => true, |
429
|
|
|
'sorting' => false, |
430
|
|
|
'hiding' => false, |
431
|
|
|
'grouping' => false, |
432
|
|
|
'options' => false, |
433
|
|
|
'nested' => false, |
434
|
|
|
], |
435
|
|
|
]; |
436
|
|
|
|
437
|
|
|
foreach ($appendColumns as $key => $column) { |
438
|
|
|
if ($this->config[ $key ] === true) { |
439
|
|
|
$this->columns[ $key ] = $column; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
// ------------------------------------------------------------------------ |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Table::setActions |
448
|
|
|
* |
449
|
|
|
* @param array $actions |
450
|
|
|
* |
451
|
|
|
* @return static |
452
|
|
|
*/ |
453
|
|
|
public function setActions(array $actions) |
454
|
|
|
{ |
455
|
|
|
$this->actions = array_merge($this->actions, $actions); |
456
|
|
|
|
457
|
|
|
return $this; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
// ------------------------------------------------------------------------ |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Table::setTools |
464
|
|
|
* |
465
|
|
|
* @param array $tools |
466
|
|
|
* |
467
|
|
|
* @return static |
468
|
|
|
*/ |
469
|
|
|
public function setTools(array $tools) |
470
|
|
|
{ |
471
|
|
|
$this->tools = array_merge($this->tools, $tools); |
472
|
|
|
|
473
|
|
|
return $this; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
// ------------------------------------------------------------------------ |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Table::setOptions |
480
|
|
|
* |
481
|
|
|
* @param array $options |
482
|
|
|
* |
483
|
|
|
* @return static |
484
|
|
|
*/ |
485
|
|
|
public function setOptions(array $options) |
486
|
|
|
{ |
487
|
|
|
$this->options = array_merge($this->options, $options); |
488
|
|
|
|
489
|
|
|
return $this; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// ------------------------------------------------------------------------ |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Table::render |
496
|
|
|
* |
497
|
|
|
* @return string |
498
|
|
|
*/ |
499
|
|
|
public function render() |
500
|
|
|
{ |
501
|
|
|
if ($this->config[ 'ordering' ]) { |
502
|
|
|
$this->attributes->addAttributeClass('table-ordering'); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
// Render header |
506
|
|
|
$tr = $this->header->createRow(); |
507
|
|
|
|
508
|
|
|
foreach ($this->columns as $key => $column) { |
509
|
|
|
|
510
|
|
|
$th = $tr->createColumn('th'); |
511
|
|
|
|
512
|
|
|
$column[ 'attr' ][ 'data-column' ] = $key; |
513
|
|
|
|
514
|
|
|
foreach ($column[ 'attr' ] as $name => $value) { |
515
|
|
|
if ( ! empty($value)) { |
516
|
|
|
$th->attributes->addAttribute($name, $value); |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
if ($column[ 'sorting' ] === true) { |
521
|
|
|
$icon = new Ui\Contents\Icon('fa fa-sort'); |
522
|
|
|
$icon->attributes->addAttributeClass(['text-muted', 'float-right', 'mt-1']); |
523
|
|
|
|
524
|
|
|
$th->attributes->addAttributeClass('col-sortable'); |
525
|
|
|
$th->attributes->addAttribute('data-toggle', 'sort'); |
526
|
|
|
$th->childNodes->push($icon); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
if ($column[ 'show' ] === false) { |
530
|
|
|
$th->attributes->addAttributeClass('hidden'); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$th->textContent->push(language()->getLine($column[ 'label' ])); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
// Render tbody |
537
|
|
|
if ($this->rows->count() > 0) { |
538
|
|
|
$totalEntries = $this->rows->count(); |
539
|
|
|
$totalRows = $this->rows->countAll(); |
540
|
|
|
$currentPage = input()->get('page') ? input()->get('page') : 1; |
541
|
|
|
$startNumber = $currentPage == 1 ? 1 : $currentPage * $this->config[ 'entries' ][ 'minimum' ]; |
542
|
|
|
$totalPages = round($totalRows / $this->config[ 'entries' ][ 'minimum' ]); |
543
|
|
|
$totalPages = $totalPages == 0 && $totalRows > 0 ? 1 : $totalPages; |
544
|
|
|
|
545
|
|
|
$i = $startNumber; |
546
|
|
|
foreach ($this->rows as $row) { |
547
|
|
|
$row->numbering = $i; |
548
|
|
|
$tr = $this->body->createRow(); |
549
|
|
|
$tr->attributes->addAttribute('data-id', $row->id); |
550
|
|
|
|
551
|
|
|
foreach ($this->columns as $key => $column) { |
552
|
|
|
|
553
|
|
|
$column[ 'key' ] = $key; |
554
|
|
|
$td = $tr->createColumn(); |
555
|
|
|
|
556
|
|
|
$column[ 'attr' ][ 'data-column' ] = $key; |
557
|
|
|
|
558
|
|
|
foreach ($column[ 'attr' ] as $name => $value) { |
559
|
|
|
if ( ! empty($value)) { |
560
|
|
|
$td->attributes->addAttribute($name, $value); |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
if ($column[ 'show' ] === false) { |
565
|
|
|
$td->attributes->addAttributeClass('hidden'); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
$td->attributes->addAttributeClass('col-body-' . $key); |
569
|
|
|
|
570
|
|
|
$this->renderBodyColumn($td, $column, $row); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
$i++; |
574
|
|
|
} |
575
|
|
|
} else { |
576
|
|
|
$totalEntries = 0; |
577
|
|
|
$totalRows = 0; |
578
|
|
|
$currentPage = 1; |
579
|
|
|
$startNumber = 0; |
580
|
|
|
$totalPages = 0; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
$showEntries = input()->get('entries') ? input()->get('entries') : $this->config[ 'entries' ][ 'minimum' ]; |
584
|
|
|
|
585
|
|
|
// Build table card |
586
|
|
|
$card = new Ui\Components\Card(); |
587
|
|
|
$card->attributes->addAttribute('data-role', 'table-crud'); |
588
|
|
|
$card->attributes->addAttributeClass('card-table'); |
589
|
|
|
|
590
|
|
|
// Build table header tools |
591
|
|
|
$tools = new Element('div', 'cardHeaderTools'); |
592
|
|
|
$tools->attributes->addAttributeClass(['card-tools', 'float-left']); |
593
|
|
|
|
594
|
|
|
$buttons = new Ui\Components\Buttons\Group(); |
595
|
|
|
foreach ($this->tools as $key => $tool) { |
596
|
|
|
if ($tool[ 'show' ] === false) { |
597
|
|
|
continue; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
if ($tool[ 'label' ] === true) { |
601
|
|
|
$button = $buttons->createButton(language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
602
|
|
|
if ( ! empty($tool[ 'icon' ])) { |
603
|
|
|
$button->setIcon($tool[ 'icon' ]); |
604
|
|
|
} |
605
|
|
|
} else { |
606
|
|
|
$button = $buttons->createButton(new Ui\Contents\Icon($tool[ 'icon' ])); |
607
|
|
|
$button->attributes->addAttribute('data-toggle', 'tooltip'); |
608
|
|
|
$button->attributes->addAttribute('title', |
609
|
|
|
language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
if ( ! empty($tool[ 'href' ])) { |
613
|
|
|
$tool[ 'data-url' ] = $tool[ 'href' ]; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
if (isset($tool[ 'attr' ])) { |
617
|
|
|
foreach ($tool[ 'attr' ] as $name => $value) { |
618
|
|
|
$button->attributes->addAttribute($name, $value); |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
$button->setContextualClass($tool[ 'contextual' ]); |
623
|
|
|
$button->smallSize(); |
624
|
|
|
$button->attributes->addAttribute('data-action', $key); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
$tools->childNodes->push($buttons); |
628
|
|
|
|
629
|
|
|
$card->header->childNodes->push($tools); |
630
|
|
|
|
631
|
|
|
// Build table header options |
632
|
|
|
$options = new Element('div', 'cardHeaderOptions'); |
633
|
|
|
$options->attributes->addAttributeClass(['card-options', 'float-right']); |
634
|
|
|
|
635
|
|
|
$buttons = new Ui\Components\Buttons\Group(); |
636
|
|
|
foreach ($this->options as $key => $option) { |
637
|
|
|
if ($option[ 'show' ] === false) { |
638
|
|
|
continue; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
if ($option[ 'label' ] === true) { |
642
|
|
|
$button = $buttons->createButton(language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
643
|
|
|
if ( ! empty($option[ 'icon' ])) { |
644
|
|
|
$button->setIcon($option[ 'icon' ]); |
645
|
|
|
} |
646
|
|
|
} else { |
647
|
|
|
$button = $buttons->createButton(new Ui\Contents\Icon($option[ 'icon' ])); |
648
|
|
|
$button->attributes->addAttribute('data-toggle', 'tooltip'); |
649
|
|
|
$button->attributes->addAttribute('title', |
650
|
|
|
language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
if ( ! empty($option[ 'href' ])) { |
654
|
|
|
$option[ 'data-url' ] = $option[ 'href' ]; |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
if (isset($option[ 'attr' ])) { |
658
|
|
|
foreach ($option[ 'attr' ] as $name => $value) { |
659
|
|
|
$button->attributes->addAttribute($name, $value); |
660
|
|
|
} |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
$button->setContextualClass($option[ 'contextual' ]); |
664
|
|
|
$button->smallSize(); |
665
|
|
|
$button->attributes->addAttribute('data-action', $key); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
$options->childNodes->push($buttons); |
669
|
|
|
|
670
|
|
|
$card->header->childNodes->push($options); |
671
|
|
|
|
672
|
|
|
// Build table body |
673
|
|
|
$cardBody = $card->createBody(); |
674
|
|
|
$cardBodyRow = $cardBody->createRow(); |
675
|
|
|
|
676
|
|
|
$columnSearch = $cardBodyRow->createColumn(['class' => 'col-md-4']); |
677
|
|
|
$columnShow = $cardBodyRow->createColumn(['class' => 'col-md-8']); |
678
|
|
|
|
679
|
|
|
// Search |
680
|
|
|
$search = new Ui\Components\Form\Elements\Input([ |
681
|
|
|
'name' => 'query', |
682
|
|
|
'data-role' => 'table-crud-search', |
683
|
|
|
'placeholder' => language()->getLine('TABLE_LABEL_SEARCH'), |
684
|
|
|
'value' => input()->get('query'), |
685
|
|
|
]); |
686
|
|
|
|
687
|
|
|
$columnSearch->childNodes->push($search); |
688
|
|
|
|
689
|
|
|
// Show |
690
|
|
|
$inputGroup = new Ui\Components\Form\Input\Group(); |
691
|
|
|
|
692
|
|
|
$addOn = new Ui\Components\Form\Input\Group\AddOn(); |
693
|
|
|
$addOn->textContent->push(language()->getLine('TABLE_LABEL_SHOW')); |
694
|
|
|
|
695
|
|
|
$inputGroup->childNodes->push($addOn); |
696
|
|
|
|
697
|
|
|
$options = []; |
698
|
|
|
foreach (range((int)$this->config[ 'entries' ][ 'minimum' ], (int)$this->config[ 'entries' ][ 'maximum' ], |
699
|
|
|
(int)$this->config[ 'entries' ][ 'step' ]) as $entries |
700
|
|
|
) { |
701
|
|
|
$options[ $entries ] = $entries; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
$columnsDropdown = new Ui\Components\Dropdown(language()->getLine('TABLE_LABEL_COLUMNS')); |
705
|
|
|
$columnsDropdown->attributes->addAttribute('data-role', 'table-crud-columns'); |
706
|
|
|
|
707
|
|
|
foreach ($this->columns as $key => $column) { |
708
|
|
|
|
709
|
|
|
if ($column[ 'hiding' ] === false) { |
710
|
|
|
continue; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
$label = new Ui\Components\Form\Elements\Label(); |
714
|
|
|
$label->attributes->addAttributeClass('form-check-label'); |
715
|
|
|
$label->textContent->push(language()->getLine($column[ 'label' ])); |
716
|
|
|
|
717
|
|
|
$checkbox = new Ui\Components\Form\Elements\Input([ |
718
|
|
|
'type' => 'checkbox', |
719
|
|
|
'class' => 'form-check-input', |
720
|
|
|
'data-toggle' => 'table-crud-columns', |
721
|
|
|
'data-column' => $key, |
722
|
|
|
]); |
723
|
|
|
|
724
|
|
|
$checkbox->attributes->removeAttributeClass('form-control'); |
725
|
|
|
|
726
|
|
|
if ($column[ 'show' ] === true) { |
727
|
|
|
$checkbox->attributes->addAttribute('checked', 'checked'); |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
$label->childNodes->push($checkbox); |
731
|
|
|
|
732
|
|
|
$columnsDropdown->menu->createItem($label); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
$inputGroup->childNodes->push($columnsDropdown); |
736
|
|
|
|
737
|
|
|
$select = new Ui\Components\Form\Elements\Select(); |
738
|
|
|
$select->attributes->addAttribute('name', 'entries'); |
739
|
|
|
$select->createOptions($options, $showEntries); |
740
|
|
|
$inputGroup->childNodes->push($select); |
741
|
|
|
|
742
|
|
|
$addOn = new Ui\Components\Form\Input\Group\AddOn(); |
743
|
|
|
$addOn->textContent->push(language()->getLine('TABLE_LABEL_PAGE', [ |
744
|
|
|
$startNumber, |
745
|
|
|
$totalEntries, |
746
|
|
|
$totalRows, |
747
|
|
|
$currentPage, |
748
|
|
|
$totalPages, |
749
|
|
|
])); |
750
|
|
|
|
751
|
|
|
$inputGroup->childNodes->push($addOn); |
752
|
|
|
|
753
|
|
|
$input = new Ui\Components\Form\Elements\Input([ |
754
|
|
|
'name' => 'page', |
755
|
|
|
'placeholder' => language()->getLine('TABLE_LABEL_GOTO'), |
756
|
|
|
]); |
757
|
|
|
|
758
|
|
|
$inputGroup->childNodes->push($input); |
759
|
|
|
|
760
|
|
|
$pagination = new Ui\Components\Pagination($totalRows, $showEntries); |
|
|
|
|
761
|
|
|
$inputGroup->childNodes->push($pagination); |
762
|
|
|
|
763
|
|
|
$columnShow->childNodes->push($inputGroup); |
764
|
|
|
|
765
|
|
|
$card->textContent->push(parent::render()); |
766
|
|
|
|
767
|
|
|
return $card->render(); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
// ------------------------------------------------------------------------ |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Table::renderBodyColumn |
774
|
|
|
* |
775
|
|
|
* @param Element $td |
776
|
|
|
* @param array $column |
777
|
|
|
* @param \O2System\Database\DataObjects\Result\Row $row |
778
|
|
|
*/ |
779
|
|
|
protected function renderBodyColumn($td, array $column, Result\Row $row) |
780
|
|
|
{ |
781
|
|
|
switch ($column[ 'format' ]) { |
782
|
|
|
default: |
783
|
|
|
if (is_callable($column[ 'format' ])) { |
784
|
|
|
if (is_array($column[ 'field' ])) { |
785
|
|
|
foreach ($column[ 'field' ] as $field) { |
786
|
|
|
$args[ $field ] = $row->offsetGet($field); |
787
|
|
|
} |
788
|
|
|
$textContent = call_user_func_array($column[ 'format' ], $args); |
|
|
|
|
789
|
|
|
} elseif ($row->offsetExists($column[ 'field' ])) { |
790
|
|
|
$textContent = call_user_func_array($column[ 'format' ], |
791
|
|
|
[$row->offsetGet($column[ 'field' ])]); |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
$td->textContent->push($textContent); |
|
|
|
|
795
|
|
|
|
796
|
|
|
} elseif (strpos($column[ 'format' ], '{{') !== false) { |
797
|
|
|
$textContent = str_replace(['{{', '}}'], '', $column[ 'format' ]); |
798
|
|
|
if (is_array($column[ 'field' ])) { |
799
|
|
|
foreach ($column[ 'field' ] as $field) { |
800
|
|
|
if ($row->offsetExists($field)) { |
801
|
|
|
$textContent = str_replace('$' . $field, $row->offsetGet($field), $textContent); |
802
|
|
|
} else { |
803
|
|
|
$textContent = str_replace('$' . $field, '', $textContent); |
804
|
|
|
} |
805
|
|
|
} |
806
|
|
|
} elseif ($row->offsetExists($column[ 'field' ])) { |
807
|
|
|
$textContent = str_replace('$' . $column[ 'field' ], $row->offsetGet($column[ 'field' ]), |
808
|
|
|
$textContent); |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
$td->textContent->push($textContent); |
812
|
|
|
} |
813
|
|
|
break; |
814
|
|
|
|
815
|
|
|
case 'checkbox': |
816
|
|
|
$checkbox = new Ui\Components\Form\Elements\Checkbox([ |
|
|
|
|
817
|
|
|
'data-role' => 'table-crud-checkbox', |
818
|
|
|
]); |
819
|
|
|
$td->childNodes->push($checkbox); |
820
|
|
|
break; |
821
|
|
|
|
822
|
|
|
case 'number': |
823
|
|
|
case 'txt': |
824
|
|
|
case 'text': |
825
|
|
|
case 'price': |
826
|
|
|
case 'number': |
827
|
|
|
if ($row->offsetExists($column[ 'field' ])) { |
828
|
|
|
$td->textContent->push($row->offsetGet($column[ 'field' ])); |
829
|
|
|
} |
830
|
|
|
break; |
831
|
|
|
case 'date': |
832
|
|
|
if ($row->offsetExists($column[ 'field' ])) { |
833
|
|
|
print_out('date'); |
|
|
|
|
834
|
|
|
} |
835
|
|
|
break; |
836
|
|
|
|
837
|
|
|
case 'status': |
838
|
|
|
if ($row->offsetExists($column[ 'field' ])) { |
839
|
|
|
$options = [ |
840
|
|
|
'PUBLISH' => 'success', |
841
|
|
|
'UNPUBLISH' => 'warning', |
842
|
|
|
'DRAFT' => 'default', |
843
|
|
|
'ARCHIVE' => 'info', |
844
|
|
|
'DELETE' => 'danger', |
845
|
|
|
]; |
846
|
|
|
|
847
|
|
|
$badge = new Ui\Components\Badge( |
848
|
|
|
language()->getLine('TABLE_OPTION_' . $row->offsetGet($column[ 'field' ])), |
849
|
|
|
$options[ $row->offsetGet($column[ 'field' ]) ] |
850
|
|
|
); |
851
|
|
|
|
852
|
|
|
$td->childNodes->push($badge); |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
break; |
856
|
|
|
|
857
|
|
|
case 'actions': |
858
|
|
|
|
859
|
|
|
$buttons = new Ui\Components\Buttons\Group(); |
860
|
|
|
|
861
|
|
|
foreach ($this->actions as $key => $action) { |
862
|
|
|
|
863
|
|
|
if ($action[ 'show' ] === false) { |
864
|
|
|
continue; |
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
if ($action[ 'label' ] === true) { |
868
|
|
|
$button = $buttons->createButton(language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
869
|
|
|
if ( ! empty($action[ 'icon' ])) { |
870
|
|
|
$button->setIcon($action[ 'icon' ]); |
871
|
|
|
} |
872
|
|
|
} else { |
873
|
|
|
$button = $buttons->createButton(new Ui\Contents\Icon($action[ 'icon' ])); |
874
|
|
|
$button->attributes->addAttribute('data-toggle', 'tooltip'); |
875
|
|
|
$button->attributes->addAttribute('title', |
876
|
|
|
language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
if (isset($action[ 'attr' ])) { |
880
|
|
|
foreach ($action[ 'attr' ] as $name => $value) { |
881
|
|
|
$button->attributes->addAttribute($name, $value); |
882
|
|
|
} |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
$button->setContextualClass($action[ 'contextual' ]); |
886
|
|
|
$button->smallSize(); |
887
|
|
|
$button->attributes->addAttribute('data-action', $key); |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
$td->childNodes->push($buttons); |
891
|
|
|
|
892
|
|
|
break; |
893
|
|
|
} |
894
|
|
|
} |
895
|
|
|
} |