This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
Advanced.php
1352 lines (1202 loc) · 33.8 KB
/
Advanced.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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php // vim:ts=4:sw=4:et:fdm=marker
/**
* This is a Basic Grid implementation, which produces fully
* functional HTML grid capable of filtering, sorting, paginating
* and using multiple column formatters.
*
* @link http://agiletoolkit.org/doc/grid
*
* Use:
* $grid=$this->add('Grid');
* $grid->setModel('User');
*
* @license See http://agiletoolkit.org/about/license
*//*
==ATK4===================================================
This file is part of Agile Toolkit 4
http://agiletoolkit.org/
(c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
Distributed under Affero General Public License v3 and
commercial license.
See LICENSE or LICENSE_COM for more information
=====================================================ATK4=*/
class Grid_Advanced extends Grid_Basic
{
/** Sorting */
public $sortby = '0';
public $sortby_db = null;
/** For totals */
private $totals_title_field = null;
private $totals_title = "";
/** Static data source? */
public $data = null;
/**
* Paginator object
*
* @see addPaginator()
*/
public $paginator = null;
/**
* Paginator class name
*
* @see enablePaginator()
* */
public $paginator_class = 'Paginator';
/**
* QuickSearch object
*
* @see addQuickSearch()
*/
public $quick_search = null;
/**
* QuickSearch class name
*
* @see enableQuickSearch()
* */
public $quick_search_class = 'QuickSearch';
/**
* $tdparam property is an array with cell parameters specified in td tag.
* This should be a hash: 'param_name'=>'param_value'
* Following parameters treated and processed in a special way:
* 1) 'style': nested array, style parameter.
* items of this nested array converted to a form of style:
* style="param_name: param_value; param_name: param_value"
*
* All the rest are not checked and simply converted to a form of
* param_name="param_value"
*
* This is a tree-like array with the following structure:
* array(
* [level1] => dataset_row = array(
* [level2] => field = array(
* [level3] => tdparam_elements = array(
* param_name => param_value
* )
* )
* )
* )
*/
protected $tdparam = array();
// JavaScript widget
public $js_widget = 'ui.atk4_grid';
public $js_widget_arguments = array();
/** @private used in button formatters to share URL between methods */
public $_url = array();
/**
* Initialization
*
* @return void
*/
function init()
{
parent::init();
// sorting support
$this->sortby =
isset($_GET[$this->name.'_sort'])
? $this->memorize('sortby', $_GET[$this->name.'_sort'])
: $this->recall('sortby', '0');
}
// {{{ Misc features
/**
* Add default paginator to the grid
*
* @param int $rows row count per page
* @param array $options optional options array
*
* @return $this
*/
function enablePaginator($rows = 25, $options = null)
{
$this->addPaginator($rows, $options);
return $this;
}
/**
* Adds paginator to the grid
*
* @param int $rows row count per page
* @param array $options optional options array
* @param string $class optional paginator class name
*
* @return Paginator
*
* @todo decide, maybe we need to add $spot optional template spot like in addQuickSearch()
*/
function addPaginator($rows = 25, $options = null, $class = null)
{
// add only once
// @todo decide, maybe we should destroy and recreate to keep last one
if ($this->paginator) {
return $this->paginator;
}
$this->paginator = $this->add($class ?: $this->paginator_class, $options);
$this->paginator->setRowsPerPage($rows);
return $this->paginator;
}
/**
* Adds default QuickSearch to the grid
*
* @param array $fields array of fieldnames used in quick search
* @param array $options optional options array
*
* @return $this
*/
function enableQuickSearch($fields, $options = null)
{
$this->addQuickSearch($fields, $options);
return $this;
}
/**
* Adds QuickSearch to the grid
*
* @param array $fields array of fieldnames used in quick search
* @param array $options optional options array
* @param string $class optional quick search object class
* @param string $spot optional template spot
*
* @return QuickSearch
*/
function addQuickSearch($fields, $options = null, $class = null, $spot = null)
{
// add only once
// @todo decide, maybe we should destroy and recreate to keep last one
if ($this->quick_search) {
return $this->quick_search;
}
$this->quick_search = $this->add($class ?: $this->quick_search_class, $options, $spot ?: 'quick_search')
->useWith($this)
->useFields($fields);
return $this->quick_search;
}
/**
* Adds column ordering object
*
* With it you can reorder your columns
*
* @return Order
*/
function addOrder()
{
return $this->add('Order', 'columns')
->useArray($this->columns)
;
}
/**
* Adds column with checkboxes on the basis of Model definition
*
* @param mixed $field should be Form_Field object or jQuery selector of
* 1 field. When passing it as jQuery selector don't
* forget to use hash sign like "#myfield"
*/
function addSelectable($field)
{
$this->js_widget = null;
$this->js(true)
->_load('ui.atk4_checkboxes')
->atk4_checkboxes(array('dst_field' => $field));
$this->addColumn('checkbox', 'selected');
$this->addOrder()
->useArray($this->columns)
->move('selected', 'first')
->now();
}
// }}}
// {{{ Sorting
/**
* Returns data source iterator
*
* @return mixed
*/
function getIterator()
{
$iter = parent::getIterator();
// sorting support
if ($this->sortby) {
$desc = ($this->sortby_db[0] == '-');
$order = ltrim($this->sortby_db, '-');
$this->applySorting($iter, $order, $desc);
}
return $iter;
}
/**
* Make sortable
*
* @param string $db_sort
*
* @return $this
*/
function makeSortable($db_sort = null)
{
// reverse sorting
$reverse = false;
if ($db_sort[0] == '-') {
$reverse = true;
$db_sort = substr($db_sort, 1);
}
// used db field
if (!$db_sort) {
$db_sort = $this->last_column;
}
switch ((string)$this->sortby) {
// we are already sorting by this column
case $this->last_column:
$info = array('1', $reverse ? 0 : ("-".$this->last_column));
$this->sortby_db = $db_sort;
break;
// We are sorted reverse by this column
case "-" . $this->last_column:
$info = array('2', $reverse ? $this->last_column : '0');
$this->sortby_db = "-" . $db_sort;
break;
// we are not sorted by this column
default:
$info = array('0', $reverse ? ("-" . $this->last_column) : $this->last_column);
}
$this->columns[$this->last_column]['sortable'] = $info;
return $this;
}
/**
* Compare two strings and return:
* < 0 if str1 is less than str2;
* > 0 if str1 is greater than str2,
* and 0 if they are equal.
*
* Note that this comparison is case sensitive
*
* @param string $str1
* @param string $str2
*
* @return int
*/
function staticSortCompare($str1, $str2)
{
if ($this->sortby[0] == '-') {
return strcmp(
$str2[substr($this->sortby, 1)],
$str1[substr($this->sortby, 1)]
);
}
return strcmp(
$str1[$this->sortby],
$str2[$this->sortby]
);
}
/**
* Apply sorting on particular field
*
* @param Iterator $i
* @param string $field
* @param string $desc
*
* @return void
*/
function applySorting($i, $field, $desc)
{
if ($i instanceof DB_dsql) {
$i->order($field, $desc);
} elseif ($i instanceof SQL_Model) {
$i->setOrder($field, $desc);
} elseif ($i instanceof Model) {
$i->setOrder($field, $desc);
}
}
// }}}
// {{{ Rendering
/**
* Render grid
*
* @return void
*/
function render()
{
if ($this->js_widget) {
$fn = str_replace('ui.', '', $this->js_widget);
$this->js(true)
->_load($this->js_widget)
->$fn($this->js_widget_arguments);
}
parent::render();
}
/**
* Render Totals row
*
* @return void
*/
function renderTotalsRow()
{
parent::renderTotalsRow();
}
// }}}
// {{{ Formatting
/**
* Additional formatting for Totals row
*
* Extends CompleteLister formatTotalsRow method.
*
* Note: in this method you should only add *additional* formatting of
* totals row because standard row formatting will be already applied by
* calling parent::formatTotalsRow().
*
* @return void
*/
function formatTotalsRow()
{
// call CompleteLister formatTotalsRow method
parent::formatTotalsRow();
// additional formatting of totals row
$totals_columns = array_keys($this->totals) ?: array();
foreach ($this->columns as $field=>$column) {
// process formatters (additional to default formatters)
$this->executeFormatters($field, $column, 'format_totals_', true);
// totals title displaying
if ($field == $this->totals_title_field) {
$this->setTDParam($field, 'style/font-weight', 'bold');
}
// apply TD parameters to all columns
$this->applyTDParams($field, $this->totals_t);
}
// set title
if ($this->totals_title_field && $this->totals) {
$this->current_row_html[$this->totals_title_field] = sprintf(
$this->totals_title,
$this->current_row['row_count'],
$this->current_row['plural_s']
);
}
}
/**
* Returns ID of record
*
* @param string $idfield ID field name
*
* @return mixed
*/
public function getCurrentIndex($idfield = 'id')
{
// TODO: think more to optimize this method
if (is_array($this->data)) {
return array_search(current($this->data), $this->data);
}
// else it is dsql dataset...
return $this->current_row[$idfield];
}
/**
* Sets TD params
*
* @param string $field
* @param string $path
* @param string $value
*
* @return void
*/
public function setTDParam($field, $path, $value)
{
// if value is null, then do nothing
if ($value === null) {
return;
}
// adds a parameter. nested ones can be specified like 'style/color'
$path = explode('/', $path);
$current_position = &$this->tdparam[$this->getCurrentIndex()][$field];
if (!is_array($current_position)) {
$current_position = array();
}
foreach ($path as $part) {
if (array_key_exists($part, $current_position)) {
$current_position = &$current_position[$part];
} else {
$current_position[$part] = array();
$current_position = &$current_position[$part];
}
}
$current_position = $value;
}
/**
* Apply TD parameters to appropriate template
*
* You can pass row template to use too. That's useful to set up totals rows, for example.
*
* @param string $field Fieldname
* @param SQLite $row_template Optional row template
*
* @return void
*/
function applyTDParams($field, &$row_template = null)
{
// data row template by default
if (!$row_template) {
$row_template = &$this->row_t;
}
// setting cell parameters (tdparam)
$tdparam = @$this->tdparam[$this->getCurrentIndex()][$field];
$tdparam_str = '';
if (is_array($tdparam)) {
if (is_array($tdparam['style'])) {
$tdparam_str .= 'style="';
foreach ($tdparam['style'] as $key=>$value) {
$tdparam_str .= $key . ':' . $value . ';';
}
$tdparam_str .= '" ';
unset($tdparam['style']);
}
//walking and combining string
foreach ($tdparam as $id=>$value) {
$tdparam_str .= $id . '="' . $value . '" ';
}
// set TD param to appropriate row template
$row_template->set("tdparam_$field", trim($tdparam_str));
}
}
// }}}
// {{{ Totals
/**
* Sets totals title field and text
*
* @param string $field
* @param string $title
*
* @return $this
*/
function setTotalsTitle($field, $title = "Total: %s row%s")
{
$this->totals_title_field = $field;
$this->totals_title = $title;
return $this;
}
/**
* Add current rendered row values to totals
*
* Called before each formatRow() call.
*
* @return void
*/
function updateTotals()
{
parent::updateTotals();
}
/**
* Calculate grand totals of all rows
*
* Called one time on rendering phase - before renderRows() call.
*
* @return void
*/
function updateGrandTotals()
{
parent::updateGrandTotals();
}
/**
* Additional formatting of number fields for totals row
*
* @param string $field
*
* @return void
*/
function format_totals_number($field) {}
/**
* Additional formatting of money fields for totals row
*
* @param string $field
*
* @return void
*/
function format_totals_money($field) {}
/**
* Additional formatting of real number fields for totals row
*
* @param string $field
*
* @return void
*/
function format_totals_real($field) {}
/**
* Additional formatting of expander fields for totals row
*
* Basically we remove everything from such field
*
* @param string $field field name
* @param array $column column configuration
*
* @return void
*/
function format_totals_expander($field, $column) {
@$this->current_row_html[$field] = '';
}
/**
* Additional formatting of custom template fields for totals row
*
* Basically we remove everything from such field
*
* @param string $field field name
* @param array $column column configuration
*
* @return void
*/
function format_totals_template($field, $column) {
@$this->current_row_html[$field] = '';
}
/**
* Additional formatting of checkbox fields column for totals row
*
* Basically we remove everything from such field
*
* @param string $field field name
* @param array $column column configuration
*
* @return void
*/
function format_totals_checkbox($field, $column) {
@$this->current_row_html[$field] = '';
}
/**
* Additional formatting of delete button fields for totals row
*
* Basically we remove everything from such field
*
* @param string $field field name
* @param array $column column configuration
*
* @return void
*/
function format_totals_delete($field, $column) {
@$this->current_row_html[$field] = '';
}
// }}}
// {{{ Expander
/**
* Initialize expander
*
* @param string $field field name
*
* @return void
*/
function init_expander($field)
{
// set column style
@$this->columns[$field]['thparam'] .= ' style="width:40px; text-align:center"';
// set column refid - referenced model table for example
if (!isset($this->columns[$field]['refid'])) {
if ($this->model) {
$refid = $this->model->table;
} elseif ($this->dq) {
$refid = $this->dq->args['table'];
} else {
$refid = preg_replace('/.*_/', '', $this->api->page);
}
$this->columns[$field]['refid'] = $refid;
}
// initialize button widget on page load
$class = $this->name.'_'.$field.'_expander';
$this->js(true)->find('.'.$class)->button();
// initialize expander
$this->js(true)
->_selector('.'.$class)
->_load('ui.atk4_expander')
->atk4_expander();
}
/**
* Format expander
*
* @param string $field field name
* @param array $column column configuration
*
* @return void
*/
function format_expander($field, $column)
{
if (!@$this->current_row[$field]) {
$this->current_row[$field] = $column['descr'];
}
// TODO:
// reformat this using Button, once we have more advanced system to
// bypass rendering of sub-elements.
// $this->current_row[$field] = $this->add('Button',null,false)
$key = $this->name . '_' . $field . '_';
$id = $key . $this->api->normalizeName($this->model->id);
$class = $key . 'expander';
@$this->current_row_html[$field] =
'<input type="checkbox" '.
'class="'.$class.'" '.
'id="'.$id.'" '.
'rel="'.$this->api->url(
$column['page'] ?: './'.$field,
array(
'expander' => $field,
'expanded' => $this->name,
'cut_page' => 1,
// TODO: id is obsolete
//'id' => $this->model->id,
$this->columns[$field]['refid'].'_id' => $this->model->id
)
).'" '.
'/>'.
'<label for="'.$id.'">' . $this->current_row[$field] . '</label>';
}
// }}}
// {{{ Formatters
/**
* Format field as HTML without encoding. Use with care.
*
* @param string $field
*
* @return void
*/
function format_html($field)
{
$this->current_row_html[$field] = $this->current_row[$field];
}
/**
* Format field as number
*
* @param string $field
*
* @return void
*/
function format_number($field) {}
/**
* Initialize column as real number
*
* @param string $field
*
* @return void
*/
function init_float($field)
{
@$this->columns[$field]['thparam'] .= ' style="text-align: right"';
}
/**
* Format field as real number with 2 digit precision
*
* @param string $field
*
* @return void
*/
function format_float($field)
{
$precision = 2;
$m = (float) $this->current_row[$field];
$this->current_row[$field] =
is_null($this->current_row[$field])
? '-'
: number_format($m, $precision);
$this->setTDParam($field, 'align', 'right');
$this->setTDParam($field, 'style/white-space', 'nowrap');
}
function init_real($field)
{
return $this->init_float($field);
}
function format_real($field)
{
return $this->format_float($field);
}
/**
* Initialize column as money
*
* @param string $field
*
* @return void
*/
function init_money($field)
{
@$this->columns[$field]['thparam'] .= ' style="text-align: right"';
}
/**
* Format field as money with 2 digit precision
*
* @param string $field
*
* @return void
*/
function format_money($field)
{
// use real number formatter
$this->format_real($field);
// negative values show in red color
if ($this->current_row[$field] < 0) {
$this->setTDParam($field, 'style/color', 'red');
}
}
/**
* Initialize column as boolean
*
* @param string $field
*
* @return void
*/
function init_boolean($field)
{
@$this->columns[$field]['thparam'] .= ' style="text-align: center"';
}
/**
* Format field as boolean
*
* @param string $field
*
* @return void
*/
function format_boolean($field)
{
if ($this->current_row[$field] && $this->current_row[$field] !== 'N') {
$this->current_row_html[$field] =
'<div align=center>'.
'<i class="icon-check">'.$this->api->_('yes').'</i>'.
'</div>';
} else {
$this->current_row_html[$field] = '';
}
}
/**
* Format field as object
*
* @param string $field
*
* @return void
*/
function format_object($field)
{
$this->current_row[$field] = (string)$this->current_row[$field];
return $this->format_shorttext($field);
}
/**
* Format field as date
*
* @param string $field
*
* @return void
*/
function format_date($field)
{
if (!$this->current_row[$field]) {
$this->current_row[$field] = '-';
} else {
$this->current_row[$field] = date(
$this->api->getConfig('locale/date', 'd/m/Y'),
strtotime($this->current_row[$field])
);
}
}
/**
* Format field as time
*
* @param string $field
*
* @return void
*/
function format_time($field)
{
$this->current_row[$field] = date(
$this->api->getConfig('locale/time', 'H:i:s'),
strtotime($this->current_row[$field])
);
}
/**
* Format field as datetime
*
* @param string $field
*
* @return void
*/
function format_datetime($field)
{
$d = $this->current_row[$field];
if (!$d) {
$this->current_row[$field] = '-';
} else {
if ($d instanceof MongoDate) {
$this->current_row[$field] = date(
$this->api->getConfig('locale/datetime', 'd/m/Y H:i:s'),
$d->sec
);
} elseif (is_numeric($d)) {
$this->current_row[$field] = date(
$this->api->getConfig('locale/datetime', 'd/m/Y H:i:s'),
$d
);
} else {
$d = strtotime($d);
$this->current_row[$field] = $d
? date(
$this->api->getConfig('locale/datetime', 'd/m/Y H:i:s'),
$d
)
: '-';
}
}
}
/**
* Format field as timestamp
*
* @param string $field
*
* @return void
*/
function format_timestamp($field)
{
$this->format_datetime($field);
}
/**
* Initialize column as fullwidth
*
* @param string $field
*
* @return void
*/
function init_fullwidth($field)
{
@$this->columns[$field]['thparam'] .= ' style="width: 100%"';
}
/**
* Format field as full width field
*
* @param string $field
*
* @return void
*/
function format_fullwidth($field){}
/**
* Format field as no-wrap field
*
* @param string $field
*
* @return void
*/
function format_nowrap($field)
{
$this->setTDParam($field, 'class', 'atk-text-nowrap');
}
/**
* Format field as wrap field
*
* @param string $field
*
* @return void
*/
function format_wrap($field)
{
$this->setTDParam($field, 'class', 'atk-text-wrap');
}
/**
* Format shorttext field
*
* @param string $field
*
* @return void
*/
function format_shorttext($field)