This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
Advanced.php
662 lines (633 loc) · 25 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
<?php // vim:ts=4:sw=4:et:fdm=marker
/*
* Undocumented
*
* @link http://agiletoolkit.org/
*//*
==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=*/
/**
* 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
*
**/
class Grid_Advanced extends Grid_Basic {
protected $no_records_message="No matching records to display";
public $last_column;
public $sortby='0';
public $sortby_db=null;
public $buttonset=null;
private $totals_title_field=null;
private $totals_title="";
public $totals_t=null;
public $totals_value_na = '-';
public $data=null;
private $record_order=null;
public $title_col=array();
public $paginator=null;
/**
* $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"
* 2) OBSOLTE! wrap: possible values are true|false; if true, 'wrap' is added
* use style/white-space property or simply format_wrap()
*
* All the rest are not checked and 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();
public $js_widget='ui.atk4_grid';
public $js_widget_arguments=array();
public $default_controller='MVCGrid';
function init(){
parent::init();
//$this->add('Reloadable');
//$this->api->addHook('pre-render',array($this,'precacheTemplate'));
if(isset($_GET[$this->name.'_sort'])){
$this->sortby=$this->memorize('sortby',$_GET[$this->name.'_sort']);
}else{
$this->sortby=$this->recall('sortby','0');
}
}
function importFields($model,$fields=undefined){
$this->add('Controller_MVCGrid')->importFields($model,$fields);
}
function defaultTemplate(){
return array('grid');
}
/**
* Returns the ID value of the expanded content on the basis of GET parameters
*/
function getExpanderId(){
return $_GET['expanded'].'_expandedcontent_'.$_GET['id'];
}
function getColumn($column){
$this->last_column=$column;
return $this;
}
function hasColumn($column){
if(!is_string($column))throw $this->exception('WRong');
return isset($this->columns[$column]);
}
function removeColumn($name){
unset($this->columns[$name]);
if($this->last_column==$name)$this->last_column=null;
return $this;
}
function addButton($label){
if(!$this->buttonset)
$this->buttonset=$this->add('ButtonSet',null,'grid_buttons');
return $this->buttonset
->add('Button','gbtn'.count($this->elements))
->setLabel($label);
}
function addQuickSearch($fields,$class='QuickSearch',$options=null){
return $this->add($class,$options,'quick_search')
->useWith($this)
->useFields($fields);
}
function makeSortable($db_sort=null){
// Sorting
$reverse=false;
if($db_sort[0]=='-'){
$reverse=true;
$db_sort=substr($db_sort,1);
}
if(!$db_sort)$db_sort=$this->last_column;
if($this->sortby==$this->last_column){
// we are already sorting by this column
$info=array('1',$reverse?0:("-".$this->last_column));
$this->sortby_db=$db_sort;
}elseif($this->sortby=="-".$this->last_column){
// We are sorted reverse by this column
$info=array('2',$reverse?$this->last_column:'0');
$this->sortby_db="-".$db_sort;
}else{
// we are not sorted by this column
$info=array('0',$reverse?("-".$this->last_column):$this->last_column);
}
$this->columns[$this->last_column]['sortable']=$info;
return $this;
}
function makeTitle(){
$this->title_col[]=$this->last_column;
return $this;
}
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);
}
return parent::render();
}
function format_number($field){
}
function format_shorttext($field){
$text=$this->current_row[$field];
if(strlen($text)>60) {
// Not sure about multi-byte support and execution speed for this
$a = explode(PHP_EOL,wordwrap($text,28,PHP_EOL,true),2);
$b = explode(PHP_EOL,wordwrap(strrev($text),28,PHP_EOL,true),2);
$text = $a[0] . ' ~~~ ' . strrev($b[0]);
}
$this->current_row[$field]=$text;
$this->tdparam[$this->getCurrentIndex()][$field]['title']=htmlspecialchars($this->current_row[$field.'_original']);
}
function format_html($field){
$this->current_row_html[$field] = $this->current_row[$field];
}
function init_money($field){
@$this->columns[$field]['thparam'].=' style="text-align: right"';
}
function init_real($field){
@$this->columns[$field]['thparam'].=' style="text-align: right"';
}
function init_fullwidth($field){
@$this->columns[$field]['thparam'].=' style="width: 100%"';
}
function format_fullwidth($field){}
function format_money($field){
$m=(float)$this->current_row[$field];
$this->current_row[$field]=number_format($m,2);
$this->setTDParam($field,'style/color',$m<0?'red':null);
$this->setTDParam($field,'align','right');
}
function format_real($field){
$m=(float)$this->current_row[$field];
$this->current_row[$field]=number_format($m,2);
$this->setTDParam($field,'align','right');
}
function format_totals_number($field){
return $this->format_number($field);
}
function format_totals_money($field){
return $this->format_money($field);
}
function format_totals_real($field){
return $this->format_real($field);
}
function format_totals_text($field){
// This method is mainly for totals title displaying
if($field==$this->totals_title_field){
$this->setTDParam($field,'style/font-weight','bold');
//$this->current_row[$field]=$this->totals_title.':';
}
else $this->current_row[$field]=$this->totals_value_na;
}
function format_time($field){
$this->current_row[$field]=date($this->api->getConfig('locale/time','H:i:s'),
strtotime($this->current_row[$field]));
}
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]));
}
function format_datetime($field){
if(!$this->current_row[$field])$this->current_row[$field]='-'; else
if($this->current_row[$field] instanceof MongoDate){
$this->current_row[$field]=date($this->api->getConfig('locale/datetime','d/m/Y H:i:s'),
$this->current_row[$field]->sec);
}else{
$this->current_row[$field]=date($this->api->getConfig('locale/datetime','d/m/Y H:i:s'),
strtotime($this->current_row[$field]));
}
}
function format_timestamp($field){
if(!$this->current_row[$field])$this->current_row[$field]='-';
else{
$format=$this->api->getConfig('locale/timestamp',$this->api->getConfig('locale/datetime','d/m/Y H:i:s'));
$this->current_row[$field]=date($format,strtotime($this->current_row[$field]));
}
}
function format_nowrap($field){
$this->tdparam[$this->getCurrentIndex()][$field]['style']='white-space: nowrap';
}
function format_wrap($field){
$this->tdparam[$this->getCurrentIndex()][$field]['style']='white-space: wrap';
}
function format_template($field){
if(!($t=$this->columns[$field]['template'])){
throw new BaseException('use setTemplate() for field '.$field);
}
$this->current_row_html[$field]=$t
->set($this->current_row)
->trySet('_value_',$this->current_row[$field])
->render();
}
function format_widget($field, $widget, $params=array(), $widget_json=null){
$class=$this->name.'_'.$field.'_expander';
$params=array(
'class'=>$class."_".$field." $widget lister_cell"
)+$params;
$this->js(true)->_tag('.'.$class.'_'.$field)->_load($widget)->activate($widget_json);
/*
$this->api->add('jUI')->addWidget($widget)->activate('.'.$class.'_'.$field,$widget_json);
*/
$this->tdparam[$this->getCurrentIndex()][$field]=$params;
if(!$this->current_row[$field]){
$this->current_row[$field]=$this->columns[$field]['descr'];
}
}
function format_expander_widget($field,$column){
return $this->format_expander($field,$column);
}
function format_expander($field, $column){
$class=$this->name.'_'.$field.'_expander';
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)
// ->
//
@$this->current_row_html[$field]='<input type="checkbox" class="button_'.$field.' '.$class.'"
id="'.$this->name.'_'.$field.'_'.$this->prepareIdField($this->model->id).'"
rel="'.$this->api->url($column['page']?$column['page']:'./'.$field,
array('expander'=>$field,
'cut_page'=>1,
'expanded'=>$this->name,
// TODO: id is obsolete
'id'=>$this->model->id,
$this->columns[$field]['refid'].'_id'=>$this->model->id
)
).'"
/><label for="'.$this->name.'_'.$field.'_'.$this->prepareIdField($this->model->id).'">'.
$this->current_row[$field].'</label>';
}
function prepareIdField($val){
return $this->api->normalizeName($val);
}
function init_expander_widget($field){
return $this->init_expander($field);
}
function init_expander($field){
@$this->columns[$field]['thparam'].=' style="width: 40px; text-align: center"';
$this->js(true)->find('.button_'.$field)->button();
if(!isset($this->columns[$field]['refid'])){
// TODO: test
/*
$refid=$this->getController();
if($refid)$refid=$refid->getModel();
*/
$refid=$this->model;
//if($refid)$refid=$refid->entity_code;
if($refid)$refid=$refid->getEntityCode();//Get Protected property Model::entity_code
if($refid){
$this->columns[$field]['refid']=$refid;
}else{
if($this->dq)
$refid=$this->dq->args['table'];
if(!$refid)$refid=preg_replace('/.*_/','',$this->api->page);
$this->columns[$field]['refid']=$refid;
}
}
$class=$this->name.'_'.$field.'_expander';
$this->js(true)->_selector('.'.$class)->_load('ui.atk4_expander')->atk4_expander();
}
function format_inline($field, $idfield='id'){
/**
* Formats the InlineEdit: field that on click should substitute the text
* in the columns of the row by the edit controls
*
* The point is to set an Id for each column of the row. To do this, we should
* set a property showing that id should be added in prerender
*/
}
function format_nl2br($field) {
$this->current_row[$field] = nl2br($this->current_row[$field]);
}
function format_order($field, $idfield='id'){
$n=$this->name.'_'.$field.'_'.$this->current_row[$idfield];
$this->tdparam[$this->getCurrentIndex()][$field]=array(
'id'=>$n,
'style'=>array(
'cursor'=>'hand'
)
);
$this->current_row[$field]=$this->record_order->getCell($this->current_id);
}
function init_link($field){
$this->setTemplate('<a href="<?'.'$_link?'.'>"><?'.'$'.$field.'?'.'></a>');
}
function format_password($field){
$this->current_row[$field]='***';
}
function format_link($field){
$this->current_row['_link']=$this->api->url('./'.$field,array('id'=>$this->current_id));
if(!$this->current_row[$field]){
$this->current_row[$field]=$this->columns[$field]['descr'];
}
return $this->format_template($field);
/*
$this->current_row[$field]='<a href="'.$this->api->url($field,
array('id'=>$this->current_id)).'">'.
$this->columns[$field]['descr'].'</a>';
*/
}
function _performDelete($id){
if($this->model){
return $this->model->delete($id);
}
$this->dq->where('id',$id)->do_delete();
}
function format_delete($field){
if(!$this->model)throw new BaseException('delete column requires $dq to be set');
return $this->format_confirm($field);
}
public $_url=array();
function init_button($field){
$this->_url[$field]=$this->api->url();
@$this->columns[$field]['thparam'].=' style="width: 40px; text-align: center"';
$this->js(true)->find('.button_'.$field)->button();
}
function setButtonClass($class){
$this->columns[$this->last_column]['button_class']=$class;
}
function init_delete($field){
$this->columns[$field]['button_class']='red';
$g=$this;
if($id=@$_GET[$this->name.'_'.$field]){
// this was clicked
$this->_performDelete($id);
$this->js()->univ()->successMessage('Deleted Successfully')->getjQuery()->reload()->execute();
}
$this->api->addHook('post-init',array($this,'_move_delete'),array($field));
/*function() use($g,$field){
});
*/
return $this->init_confirm($field);
}
function _move_delete($grid,$field){
if($this->hasColumn($field))$this->addOrder()->move($field,'last')->now();
}
function init_confirm($field){
$this->_url[$field]=$this->api->url();
@$this->columns[$field]['thparam'].=' style="width: 40px; text-align: center"';
$this->js(true)->find('.button_'.$field)->button();
}
function init_prompt($field){
@$this->columns[$field]['thparam'].=' style="width: 40px; text-align: center"';
$this->js(true)->find('.button_'.$field)->button();
}
function format_button($field){
$url=clone $this->_url[$field];
$this->current_row_html[$field]='<button type="button" class="'.$this->columns[$field]['button_class'].'button_'.$field.'" '.
'onclick="$(this).univ().ajaxec(\''.$url->set(
array($field=>$this->current_id,$this->name.'_'.$field=>$this->current_id)).'\')">'.
(isset($this->columns[$field]['icon'])?$this->columns[$field]['icon']:'').
$this->columns[$field]['descr'].'</button>';
}
function format_confirm($field){
$url=clone $this->_url[$field];
$this->current_row_html[$field]='<button type="button" class="'.$this->columns[$field]['button_class'].' button_'.$field.'" '.
'onclick="$(this).univ().confirm(\'Are you sure?\').ajaxec(\''.($u=$url->set(
array($field=>$this->current_id,$this->name.'_'.$field=>$this->current_id))).'\')">'.
(isset($this->columns[$field]['icon'])?$this->columns[$field]['icon']:'').
$this->columns[$field]['descr'].'</button>';
}
function format_prompt($field){
$this->current_row_html[$field]='<button type="button" class="'.$this->columns[$field]['button_class'].'button_'.$field.'" '.
'onclick="value=prompt(\'Enter value: \');$(this).univ().ajaxec(\''.$this->api->url(null,
array($field=>$this->current_id,$this->name.'_'.$field=>$this->current_id)).'&value=\'+value)">'.
(isset($this->columns[$field]['icon'])?$this->columns[$field]['icon']:'').
$this->columns[$field]['descr'].'</button>';
}
function init_boolean($field){
@$this->columns[$field]['thparam'].=' style="text-align: center"';
}
function format_boolean($field){
if ($this->current_row[$field] && $this->current_row[$field]!=='N') {
$this->current_row_html[$field]='<div align=center><span class="ui-icon ui-icon-check">yes</span></div>';
} else {
$this->current_row_html[$field]='';
}
}
function format_checkbox($field){
$this->current_row_html[$field] = '<input type="checkbox" id="cb_'.
$this->current_id.'" name="cb_'.$this->current_id.
'" value="'.$this->current_id.'"'.
($this->current_row['selected']=='Y'?" checked ":" ").//'" onclick="'.
//$this->onClick($field).
'/>';
$this->setTDParam($field,'width','10');
$this->setTDParam($field,'align','center');
}
function format_image($field){
$this->current_row_html[$field]='<img src="'.$this->current_row[$field].'"/>';
}
function staticSortCompare($row1,$row2){
if($this->sortby[0]=='-'){
return strcmp($row2[substr($this->sortby,1)],$row1[substr($this->sortby,1)]);
}
return strcmp($row1[$this->sortby],$row2[$this->sortby]);
}
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);
}
function getIterator(){
$i=parent::getIterator();
if($this->sortby){
$desc=false;
$order=$this->sortby_db;
if($this->sortby_db[0]=='-'){
$desc=true;
$order=substr($order,1);
}
$this->applySorting($i,$order,$desc);
}
return $i;
}
function setTemplate($template){
// This allows you to use Template
$this->columns[$this->last_column]['template']=$this->add('SMlite')
->loadTemplateFromString($template);
return $this;
}
function getFieldStyle($field,$id){
/**
* Returns the structured string with row styles. Used along with getRowContent()
* in row redrawing
*/
$style=$this->tdparam[$this->getCurrentIndex()][$field];
$tdparam=null;
if(is_array($style)){
// now we should convert style elements' names to JS compatible
$tdparam=array();
foreach($style as $key=>$value){
switch($key){
//case 'background-color':$tdparam[]="$key:$value";break;
case 'style': case 'css':
// style is a nested array
foreach($value as $k=>$v){
$tdparam[]="$k::$v";
}
break;
default:
$tdparam[]="$key::$value";
}
}
}
return (is_array($tdparam)?join($tdparam,'<s>'):'');
}
function getRowTitle(){
$title=' ';
foreach($this->title_col as $col){
$title.=$this->current_row[$col];
}
if($title==' '){
return "Row #".$this->current_id;
}
return substr($title,1);
}
function applyTDParams($field,$totals=false){
// setting cell parameters (tdparam)
$tdparam=@$this->tdparam[$this->getCurrentIndex()][$field];
$tdparam_str='';
if(is_array($tdparam)){
// wrap is replaced by style property
unset($tdparam['wrap']);
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.'" ';
if($totals)$this->totals_t->set("tdparam_$field",trim($tdparam_str));
else $this->row_t->trySetHTML("tdparam_$field",trim($tdparam_str));
}
}
function setTotalsTitle($field,$title="Total:"){
$this->totals_title_field=$field;
$this->totals_title=$title;
return $this;
}
function formatTotalsRow(){
foreach($this->columns as $tmp=>$column){
$formatters = explode(',',$column['type']);
$all_failed=true;
foreach($formatters as $formatter){
if(method_exists($this,$m="format_totals_".$formatter)){
$all_failed=false;
$this->$m($tmp);
}
}
// setting cell parameters (tdparam)
$this->applyTDParams($tmp,true);
if($all_failed)$this->current_row[$tmp]=$this->totals_value_na;
}
}
function updateTotals(){
parent::updateTotals();
// set title
foreach($this->current_row as $key=>$val){
if (!empty($this->totals_title_field) && $key==$this->totals_title_field) {
$this->totals[$key]=$this->totals_title;
}
}
}
/**
* Adds paginator to the grid
* @param $ipp row count per page
*/
function addPaginator($ipp=25,$args=null){
// adding ajax paginator
if ($this->paginator) {
return $this->paginator;
}
$this->paginator=$this->add('Paginator',$args);
$this->paginator->ipp($ipp);
return $this;
}
/** OBSOLETE, simply put grid inside a container */
public function setWidth( $width ){
$this->template->set('container_style', 'margin: 0 auto; width:'.$width.((!is_numeric($width))?'':'px'));
return $this;
}
public function setNoRecords($message){
$this->no_records_message=$message;
return $this;
}
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];
}
public function setTDParam($field,$path,$value){
// 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;
}
/**
* Adds column ordering object. With it you can reorder your columns.
*/
function addOrder(){
return $this->add('Order','columns')
->useArray($this->columns)
;
}
/**
* Adds column with checkboxes on the basis of Model definition
* @field - should be Form_Field object or jQuery selector of 1 field
* When passing it as jQuery selector don't forget 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();
}
/* to reuse td params */
function getAllTDParams(){
return $this->tdparam;
}
}