-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathTbExtendedGridView.php
1445 lines (1285 loc) · 40.6 KB
/
TbExtendedGridView.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
/**
* ## TbExtendedGridView class file
*
* @author Antonio Ramirez <[email protected]>
* @copyright Copyright © Clevertech 2012-
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
Yii::import('booster.widgets.TbGridView');
/**
*## TbExtendedGridView is an extended version of TbGridView.
*
* Features are:
* - Display an extended summary of the records shown. The extended summary can be configured to any of the
* {@link TbOperation} type of widgets.
* - Automatic chart display (using TbHighCharts widget), where user can 'switch' between views.
* - Selectable cells
* - Sortable rows
*
* @property CActiveDataProvider $dataProvider the data provider for the view.
* @property TbDataColumn[] $columns
*
* @package booster.widgets.grids
*/
class TbExtendedGridView extends TbGridView {
/**
* @var bool $fixedHeader if set to true will keep the header fixed position
*/
public $fixedHeader = false;
/**
* @var integer $headerOffset, when $fixedHeader is set to true, headerOffset will position table header top position
* at $headerOffset. If you are using bootstrap and has navigation top fixed, its height is 40px, so it is recommended
* to use $headerOffset=40;
*/
public $headerOffset = 0;
/**
* @var string the template to be used to control the layout of various sections in the view.
* These tokens are recognized: {extendedSummary}, {summary}, {items} and {pager}. They will be replaced with the
* extended summary, summary text, the items, and the pager.
*/
public $template = "{summary}\n{items}\n{pager}\n{extendedSummary}";
/**
* @var array $extendedSummary displays an extended summary version.
* There are different types of summary types,
* please, see {@link TbSumOperation}, {@link TbSumOfTypeOperation},{@link TbPercentOfTypeGooglePieOperation}
* {@link TbPercentOfTypeOperation} and {@link TbPercentOfTypeEasyPieOperation}.
*
* The following is an example, please review the different types of TbOperation classes to find out more about
* its configuration parameters.
*
* <pre>
* 'extendedSummary' => array(
* 'title' => '', // the extended summary title
* 'columns' => array( // the 'columns' that will be displayed at the extended summary
* 'id' => array( // column name "id"
* 'class' => 'TbSumOperation', // what is the type of TbOperation we are going to display
* 'label' => 'Sum of Ids' // label is name of label of the resulted value (ie Sum of Ids:)
* ),
* 'results' => array( // column name "results"
* 'class' => 'TbPercentOfTypeGooglePieOperation', // the type of TbOperation
* 'label' => 'How Many Of Each? ', // the label of the operation
* 'types' => array( // TbPercentOfTypeGooglePieOperation "types" attributes
* '0' => array('label' => 'zeros'), // a value of "0" will be labelled "zeros"
* '1' => array('label' => 'ones'), // a value of "1" will be labelled "ones"
* '2' => array('label' => 'twos')) // a value of "2" will be labelled "twos"
* )
* )
* ),
* </pre>
*/
public $extendedSummary = array();
/**
* @var string $extendedSummaryCssClass is the class name of the layer containing the extended summary
*/
public $extendedSummaryCssClass = 'extended-summary';
/**
* @var array $extendedSummaryOptions the HTML attributes of the layer containing the extended summary
*/
public $extendedSummaryOptions = array();
/**
* @var array $componentsAfterAjaxUpdate has scripts that will be executed after components have updated.
* It is used internally to render scripts required for components to work correctly. You may use it for your own
* scripts, just make sure it is of type array.
*/
public $componentsAfterAjaxUpdate = array();
/**
* @var array $componentsReadyScripts hold scripts that will be executed on document ready.
* It is used internally to render scripts required for components to work correctly. You may use it for your own
* scripts, just make sure it is of type array.
*/
public $componentsReadyScripts = array();
/**
* @var array $chartOptions if configured, the extended view will display a highcharts chart.
*/
public $chartOptions = array();
/**
* @var bool $sortableRows. If true the rows at the table will be sortable.
*/
public $sortableRows = false;
/**
* @var string Database field name for row sorting
*/
public $sortableAttribute = 'sort_order';
/**
* @var boolean Save sort order by ajax defaults to false
* @see bootstrap.action.TbSortableAction for an easy way to use with your controller
*/
public $sortableAjaxSave = false;
/**
* @var string Name of the action to call and sort values
* @see bootstrap.action.TbSortableAction for an easy way to use with your controller
*
* <pre>
* 'sortableAction' => 'module/controller/sortable' | 'controller/sortable'
* </pre>
*
* The widget will make use of the string to create the URL and then append $sortableAttribute
* @see $sortableAttribute
*/
public $sortableAction;
/**
* @var string a javascript function that will be invoked after a successful sorting is done.
* The function signature is <code>function(id, position)</code> where 'id' refers to the ID of the model id key,
* 'position' the new position in the list.
*/
public $afterSortableUpdate;
/**
* @var bool whether to allow selecting of cells
*/
public $selectableCells = false;
/**
* @var string the filter to use to allow selection. For example, if you set the "htmlOptions" property of a column to have a
* "class" of "tobeselected", you could set this property as: "td.tobeselected" in order to allow selection to
* those columns with that class only.
*/
public $selectableCellsFilter = 'td';
/**
* @var string a javascript function that will be invoked after a selection is done.
* The function signature is <code>function(selected)</code> where 'selected' refers to the selected columns.
*/
public $afterSelectableCells;
/**
* @var array the configuration options to display a TbBulkActions widget
* @see TbBulkActions widget for its configuration
*/
public $bulkActions = array();
/**
* @var string the aligment of the bulk actions. It can be 'left' or 'right'.
*/
public $bulkActionAlign = 'right';
/**
* @var TbBulkActions component that will display the bulk actions to the grid
*/
protected $bulk;
/**
* @var boolean $displayExtendedSummary a helper property that is set to true if we have to render the
* extended summary
*/
protected $displayExtendedSummary;
/**
* @var boolean $displayChart a helper property that is set to true if we have to render a chart.
*/
protected $displayChart;
/**
* @var TbOperation[] $extendedSummaryTypes hold the current configured TbOperation that will process column values.
*/
protected $extendedSummaryTypes = array();
/**
* @var array $extendedSummaryOperations hold the supported operation types
*/
protected $extendedSummaryOperations = array(
'TbSumOperation',
'TbCountOfTypeOperation',
'TbPercentOfTypeOperation',
'TbPercentOfTypeEasyPieOperation',
'TbPercentOfTypeGooglePieOperation'
);
/**
*### .init()
*
* Widget initialization
*/
public function init(){
if ($this->shouldEnableExtendedSummary())
$this->prepareDisplayingExtendedSummary();
if ($this->shouldEnableChart() && $this->hasData())
$this->enableChart();
if ($this->shouldEnableBulkActions())
$this->enableBulkActions();
$this->fillSelectionChangedProperty();
parent::init();
}
/**
*### .renderContent()
*
* Renders grid content
*/
public function renderContent()
{
parent::renderContent();
$this->registerCustomClientScript();
}
/**
*### .renderKeys()
*
* Renders the key values of the data in a hidden tag.
*/
public function renderKeys()
{
$data = $this->dataProvider->getData();
if (!$this->sortableRows || (isset($data[0]) && !isset($data[0]->attributes[(string)$this->sortableAttribute]))) {
parent::renderKeys();
}
echo CHtml::openTag(
'div',
array(
'class' => 'keys',
'style' => 'display:none',
'title' => Yii::app()->getRequest()->getUrl(),
)
);
foreach ($data as $d) {
echo CHtml::tag(
'span',
array('data-order' => $this->getAttribute($d, $this->sortableAttribute)),
CHtml::encode($this->getPrimaryKey($d))
);
}
echo "</div>\n";
return true;
}
/**
*### .getAttribute()
*
* Helper function to get an attribute from the data
*
* @param CActiveRecord $data
* @param string $attribute the attribute to get
*
* @return mixed the attribute value null if none found
*/
protected function getAttribute($data, $attribute)
{
if ($this->dataProvider instanceof CActiveDataProvider && $data->hasAttribute($attribute)) {
return $data->{$attribute};
}
if ($this->dataProvider instanceof CArrayDataProvider || $this->dataProvider instanceof CSqlDataProvider) {
if (is_object($data) && isset($data->{$attribute})) {
return $data->{$attribute};
}
if (isset($data[$attribute])) {
return $data[$attribute];
}
}
return null;
}
/**
*### .getPrimaryKey()
*
* Helper function to return the primary key of the $data
* IMPORTANT: composite keys on CActiveDataProviders will return the keys joined by comma
*
* @param CActiveRecord $data
*
* @return null|string
*/
protected function getPrimaryKey($data)
{
if ($this->dataProvider instanceof CActiveDataProvider) {
$key = $this->dataProvider->keyAttribute === null ? $data->getPrimaryKey() : $data->{$this->dataProvider->keyAttribute};
return is_array($key) ? implode(',', $key) : $key;
}
if (($this->dataProvider instanceof CArrayDataProvider || $this->dataProvider instanceof CSqlDataProvider) && !empty($this->dataProvider->keyField)) {
return is_object($data) ? $data->{$this->dataProvider->keyField}
: $data[$this->dataProvider->keyField];
}
return null;
}
/**
*### .renderTableHeader()
*
* Renders grid header
*/
public function renderTableHeader() {
$this->renderChart();
parent::renderTableHeader();
}
/**
*### .renderTableFooter()
*
* Renders the table footer.
*/
public function renderTableFooter()
{
$hasFilter = $this->filter !== null && $this->filterPosition === self::FILTER_POS_FOOTER;
$hasFooter = $this->getHasFooter();
if ($this->bulk !== null || $hasFilter || $hasFooter) {
echo "<tfoot>\n";
if ($hasFooter) {
echo "<tr>\n";
/** @var $column CDataColumn */
foreach ($this->columns as $column) {
$column->renderFooterCell();
}
echo "</tr>\n";
}
if ($hasFilter) {
$this->renderFilter();
}
if ($this->bulk !== null) {
$this->renderBulkActions();
}
echo "</tfoot>\n";
}
}
/**
*### .renderBulkActions()
*/
public function renderBulkActions() {
Booster::getBooster()->registerAssetJs('jquery.saveselection.gridview.js');
$this->componentsAfterAjaxUpdate[] = "$.fn.yiiGridView.afterUpdateGrid('".$this->id."');";
echo '<tr><td colspan="' . count($this->columns) . '">';
$this->bulk->renderButtons();
echo '</td></tr>';
}
/**
*### .renderChart()
*
* Renders chart
* @throws CException
*/
public function renderChart() {
if (!$this->displayChart || $this->dataProvider->getItemCount() <= 0) {
return;
}
if (!isset($this->chartOptions['data']['series'])) {
throw new CException(Yii::t(
'zii',
'You need to set the "series" attribute in order to render a chart'
));
}
$configSeries = $this->chartOptions['data']['series'];
if (!is_array($configSeries)) {
throw new CException(Yii::t('zii', '"chartOptions.series" is expected to be an array.'));
}
if (!isset($this->chartOptions['config'])) {
$this->chartOptions['config'] = array();
}
// ****************************************
// render switch buttons
$buttons = Yii::createComponent(
array(
'class' => 'booster.widgets.TbButtonGroup',
'toggle' => 'radio',
'buttons' => array(
array(
'label' => Yii::t('zii', 'Grid'),
'url' => '#',
'htmlOptions' => array('class' => 'active ' . $this->getId() . '-grid-control grid')
),
array(
'label' => Yii::t('zii', 'Chart'),
'url' => '#',
'htmlOptions' => array('class' => $this->getId() . '-grid-control chart')
),
),
'htmlOptions' => array('style' => 'margin-bottom:5px')
)
);
echo '<div>';
$buttons->init();
$buttons->run();
echo '</div>';
$chartId = preg_replace('[-\\ ?]', '_', 'exgvwChart' . $this->getId()); // cleaning out most possible characters invalid as javascript variable identifiers.
$this->componentsReadyScripts[] = '$(document).on("click",".' . $this->getId() . '-grid-control", function() {
$(this).parent().find("input[type=\"radio\"]").parent().toggleClass("active");
if ($(this).hasClass("grid") && $("#' . $this->getId() . ' #' . $chartId . '").is(":visible"))
{
$("#' . $this->getId() . ' #' . $chartId . '").hide();
$("#' . $this->getId() . ' table.items").show();
}
if ($(this).hasClass("chart") && $("#' . $this->getId() . ' table.items").is(":visible"))
{
$("#' . $this->getId() . ' table.items").hide();
$("#' . $this->getId() . ' #' . $chartId . '").show();
}
return false;
});';
$this->componentsAfterAjaxUpdate[] = '
if($("label.grid.'.$this->getId().'-grid-control").hasClass("active")) {
$("#' . $this->getId() . ' #' . $chartId . '").hide();
$("#' . $this->getId() . ' table.items").show();
} else {
$("#' . $this->getId() . ' table.items").hide();
$("#' . $this->getId() . ' #' . $chartId . '").show();
}
';
// end switch buttons
// ****************************************
// render Chart
// chart options
$data = $this->dataProvider->getData();
$count = count($data);
$seriesData = array();
$cnt = 0;
foreach ($configSeries as $set) {
$seriesData[$cnt] = array('name' => isset($set['name']) ? $set['name'] : null, 'data' => array());
for ($row = 0; $row < $count; ++$row) {
$column = $this->getColumnByName($set['attribute']);
if (!is_null($column) && $column->value !== null) {
$seriesData[$cnt]['data'][] = $this->evaluateExpression(
$column->value,
array('data' => $data[$row], 'row' => $row)
);
} else {
$value = CHtml::value($data[$row], $set['attribute']);
$seriesData[$cnt]['data'][] = is_numeric($value) ? (float)$value : $value;
}
}
++$cnt;
}
$xAxisData = array();
$xAxisData[] = array('categories'=>array());
if(!empty($this->chartOptions['data']['xAxis'])){
$xAxis = $this->chartOptions['data']['xAxis'];
$categories = $xAxis['categories'];
if(is_array($categories)) {
$xAxisData['categories'] = $categories;
} else { // field name
for ($row = 0; $row < $count; ++$row) {
$column = $this->getColumnByName($categories);
if (!is_null($column) && $column->value !== null) {
$xAxisData['categories'][] = $this->evaluateExpression(
$column->value,
array('data' => $data[$row], 'row' => $row)
);
} else {
$value = CHtml::value($data[$row], $categories);
$xAxisData['categories'][] = $value;
}
}
}
}
// ****************************************
// render chart
$options = CMap::mergeArray(
$this->chartOptions['config'],
array('series' => $seriesData, 'xAxis' => $xAxisData)
);
$this->chartOptions['htmlOptions'] = isset($this->chartOptions['htmlOptions'])
? $this->chartOptions['htmlOptions'] : array();
// sorry but use a class to provide styles, we need this
if(empty($this->chartOptions['htmlOptions']['style']))
$this->chartOptions['htmlOptions']['style'] = 'width: 100%; height: 100%;';
else
$this->chartOptions['htmlOptions']['style'] = $this->chartOptions['htmlOptions']['style'].'; width: 100%; height: 100%;';
// build unique ID
// important!
echo '<div>';
if ($this->ajaxUpdate !== false) {
if (isset($options['chart']) && is_array($options['chart'])) {
$options['chart']['renderTo'] = $chartId;
} else {
$options['chart'] = array('renderTo' => $chartId);
}
$jsOptions = CJSON::encode($options);
if (isset($this->chartOptions['htmlOptions']['data-config'])) {
unset($this->chartOptions['htmlOptions']['data-config']);
}
echo "<div id='{$chartId}' " . CHtml::renderAttributes(
$this->chartOptions['htmlOptions']
) . " data-config='{$jsOptions}'></div>";
/* fix for chart dimensions changing after ajax */
$this->componentsAfterAjaxUpdate[] = "
$('#".$chartId."').width($('#".$this->id." table').width());
$('#".$chartId."').height($('#".$this->id." table').height() + 150);
highchart{$chartId} = new Highcharts.Chart($('#{$chartId}').data('config'));
";
}
$configChart = array(
'class' => 'booster.widgets.TbHighCharts',
'id' => $chartId,
'options' => $options,
'htmlOptions' => $this->chartOptions['htmlOptions']
);
$chart = Yii::createComponent($configChart);
$chart->init();
$chart->run();
echo '</div>';
// end chart display
// ****************************************
// check if the chart should appear by default
if(isset($this->chartOptions['defaultView']) && $this->chartOptions['defaultView'] === true) {
$this->componentsReadyScripts[] = '
$(".' . $this->getId() . '-grid-control.grid").removeClass("active");
$(".' . $this->getId() . '-grid-control.chart").addClass("active");
$("#' . $this->getId() . ' table.items").hide();
$("#' . $this->getId() . ' #' . $chartId . '").show();
';
} else {
$this->componentsReadyScripts[] = '
$(".' . $this->getId() . '-grid-control.grid").addClass("active");
$(".' . $this->getId() . '-grid-control.chart").removeClass("active");
$("#' . $this->getId() . ' table.items").show();
$("#' . $this->getId() . ' #' . $chartId . '").hide();
';
}
}
/**
*### .renderTableRow()
*
* Renders a table body row.
*
* This method is a copy-paste from CGridView.renderTableRow(),
* because we cannot override *just* the `renderDataCell` routine (it's polymorphic)
* and Yii doesn't have a seam in place for us to do this.
* @see https://github.com/yiisoft/yii/pull/3571
*
* Only meaningful change in here is the replacement of the `$column->renderDataCell($row)`
* with our own method.
*
* @param integer $row the row number (zero-based).
*
* @deprecated This method will be removed after Yii 1.1.17 release.
*/
public function renderTableRow($row)
{
$htmlOptions = array();
if ($this->rowHtmlOptionsExpression !== null) {
$data = $this->dataProvider->data[$row];
$options = $this->evaluateExpression(
$this->rowHtmlOptionsExpression,
array('row' => $row, 'data' => $data)
);
if (is_array($options)) {
$htmlOptions = $options;
}
}
if ($this->rowCssClassExpression !== null) {
$data = $this->dataProvider->data[$row];
$class = $this->evaluateExpression($this->rowCssClassExpression, array('row' => $row, 'data' => $data));
} elseif (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0) {
$class = $this->rowCssClass[$row % $n];
}
if (!empty($class)) {
if (isset($htmlOptions['class'])) {
$htmlOptions['class'] .= ' ' . $class;
} else {
$htmlOptions['class'] = $class;
}
}
echo CHtml::openTag('tr', $htmlOptions);
foreach ($this->columns as $column)
$this->renderDataCellProcessingSummariesIfNeeded($column, $row);
echo CHtml::closeTag('tr');
}
/**
*### .renderExtendedSummary()
*
* Renders summary
*/
public function renderExtendedSummary()
{
if (!isset($this->extendedSummaryOptions['class'])) {
$this->extendedSummaryOptions['class'] = $this->extendedSummaryCssClass;
} else {
$this->extendedSummaryOptions['class'] .= ' ' . $this->extendedSummaryCssClass;
}
echo '<div ' . CHtml::renderAttributes($this->extendedSummaryOptions) . '></div>';
}
/**
*### .renderExtendedSummaryContent()
*
* Renders summary content. Will be appended to
*/
public function renderExtendedSummaryContent()
{
if ($this->dataProvider->getItemCount() <= 0)
return;
if (empty($this->extendedSummaryTypes))
return;
echo '<div id="' . $this->id . '-extended-summary" style="display:none">';
if (isset($this->extendedSummary['title'])) {
echo '<h3>' . $this->extendedSummary['title'] . '</h3>';
}
foreach ($this->extendedSummaryTypes as $summaryType) {
/** @var $summaryType TbOperation */
$summaryType->run();
echo '<br/>';
}
echo '</div>';
}
/**
*### .registerCustomClientScript()
*
* This script must be run at the end of content rendering not at the beginning as it is common with normal CGridViews
*/
public function registerCustomClientScript()
{
/** @var $cs CClientScript */
$cs = Yii::app()->getClientScript();
$fixedHeaderJs = '';
if ($this->fixedHeader) {
Booster::getBooster()->registerAssetJs('jquery.stickytableheaders' . (!YII_DEBUG ? '.min' : '') . '.js');
$fixedHeaderJs = "$('#{$this->id} table.items').stickyTableHeaders({fixedOffset:{$this->headerOffset}});";
$this->componentsAfterAjaxUpdate[] = $fixedHeaderJs;
}
if ($this->sortableRows) {
$afterSortableUpdate = '';
if ($this->afterSortableUpdate !== null) {
if (!($this->afterSortableUpdate instanceof CJavaScriptExpression) && strpos(
$this->afterSortableUpdate,
'js:'
) !== 0
) {
$afterSortableUpdate = new CJavaScriptExpression($this->afterSortableUpdate);
} else {
$afterSortableUpdate = $this->afterSortableUpdate;
}
}
$this->selectableRows = 1;
$cs->registerCoreScript('jquery.ui');
Booster::getBooster()->registerAssetJs('jquery.sortable.gridview.js');
if ($this->sortableAjaxSave && $this->sortableAction !== null) {
$sortableAction = Yii::app()->createUrl(
$this->sortableAction,
array('sortableAttribute' => $this->sortableAttribute)
);
} else {
$sortableAction = '';
}
$afterSortableUpdate = CJavaScript::encode($afterSortableUpdate);
if (Yii::app()->request->enableCsrfValidation)
{
$csrfTokenName = Yii::app()->request->csrfTokenName;
$csrfToken = Yii::app()->request->csrfToken;
$csrf = "{'$csrfTokenName':'$csrfToken' }";
} else
$csrf = '{}';
$this->componentsReadyScripts[] = "$.fn.yiiGridView.sortable('{$this->id}', '{$sortableAction}', {$afterSortableUpdate}, $csrf);";
$this->componentsAfterAjaxUpdate[] = "$.fn.yiiGridView.sortable('{$this->id}', '{$sortableAction}', {$afterSortableUpdate}, $csrf);";
}
if ($this->selectableCells) {
$afterSelectableCells = '';
if ($this->afterSelectableCells !== null) {
if (!($this->afterSelectableCells instanceof CJavaScriptExpression) && strpos($this->afterSelectableCells,'js:') !== 0) {
$afterSelectableCells = new CJavaScriptExpression($this->afterSelectableCells);
} else {
$afterSelectableCells = $this->afterSelectableCells;
}
}
$cs->registerCoreScript('jquery.ui');
Booster::getBooster()->registerAssetJs('jquery.selectable.gridview.js');
$afterSelectableCells = CJavaScript::encode($afterSelectableCells);
$this->componentsReadyScripts[] = "$.fn.yiiGridView.selectable('{$this->id}','{$this->selectableCellsFilter}',{$afterSelectableCells});";
$this->componentsAfterAjaxUpdate[] = "$.fn.yiiGridView.selectable('{$this->id}','{$this->selectableCellsFilter}', {$afterSelectableCells});";
}
$cs->registerScript(
__CLASS__ . '#' . $this->id . 'Ex',
'
var $grid = $("#' . $this->id . '");
' . $fixedHeaderJs . '
if ($(".' . $this->extendedSummaryCssClass . '", $grid).length)
{
$(".' . $this->extendedSummaryCssClass . '", $grid).html($("#' . $this->id . '-extended-summary", $grid).html());
}
' . (count($this->componentsReadyScripts) ? implode(PHP_EOL, $this->componentsReadyScripts) : '') . '
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
var qs = $.deparam.querystring(options.url);
if (qs.hasOwnProperty("ajax") && qs.ajax == "' . $this->id . '")
{
if (typeof (options.realsuccess) == "undefined" || options.realsuccess !== options.success)
{
options.realsuccess = options.success;
options.success = function(data)
{
if (options.realsuccess) {
options.realsuccess(data);
var $data = $("<div>" + data + "</div>");
// we need to get the grid again... as it has been updated
if ($(".' . $this->extendedSummaryCssClass . '", $("#' . $this->id . '")))
{
$(".' . $this->extendedSummaryCssClass . '", $("#' . $this->id . '")).html($("#' . $this->id . '-extended-summary", $data).html());
}
' . (count($this->componentsAfterAjaxUpdate) ? implode(
PHP_EOL,
$this->componentsAfterAjaxUpdate
) : '') . '
}
}
}
}
});'
);
}
/**
*### .parseColumnValue()
*
* @param CGridColumn $column
* @param string $value Value of the $column rendered by $column->renderDataCell($row).
*/
protected function processColumnValue($column, $value)
{
if (!($column instanceof CDataColumn))
return;
if (!array_key_exists($column->name, $this->extendedSummary['columns']))
return;
$config = $this->extendedSummary['columns'][$column->name];
$config['column'] = $column;
$this->getSummaryOperationInstance($column->name, $config)
->processValue($value);
}
/**
*### .getSummaryOperationInstance()
*
* Each type of 'extended' summary
*
* @param string $name the name of the column
* @param array $config the configuration of the column at the extendedSummary
*
* @return mixed
* @throws CException
*/
protected function getSummaryOperationInstance($name, $config)
{
if (!isset($config['class'])) {
throw new CException(Yii::t(
'zii',
'Column summary configuration must be an array containing a "type" element.'
));
}
if (!in_array($config['class'], $this->extendedSummaryOperations)) {
throw new CException(Yii::t(
'zii',
'"{operation}" is an unsupported class operation.',
array('{operation}' => $config['class'])
));
}
// name of the column should be unique
if (!isset($this->extendedSummaryTypes[$name])) {
$this->extendedSummaryTypes[$name] = Yii::createComponent($config);
$this->extendedSummaryTypes[$name]->init();
}
return $this->extendedSummaryTypes[$name];
}
/**
*### .getColumnByName()
*
* Helper function to get a column by its name
*
* @param string $name
*
* @return TbDataColumn|null
*/
protected function getColumnByName($name)
{
foreach ($this->columns as $column) {
if (strcmp($column->name, $name) === 0) {
return $column;
}
}
return null;
}
/**
* @return bool
*/
private function shouldEnableExtendedSummary()
{
return preg_match(
'/extendedsummary/i',
$this->template
) && !empty($this->extendedSummary) && isset($this->extendedSummary['columns']);
}
private function prepareDisplayingExtendedSummary()
{
$this->template .= "\n{extendedSummaryContent}";
$this->displayExtendedSummary = true;
}
private function shouldEnableChart()
{
return !empty($this->chartOptions) && (bool)@$this->chartOptions['data'];
}
/**
* @return int
*/
private function hasData()
{
return $this->dataProvider->getItemCount();
}
private function enableChart()
{
$this->displayChart = true;
}
/**
* @return bool
*/
private function shouldEnableBulkActions()
{
return $this->bulkActions !== array() && isset($this->bulkActions['actionButtons']);
}
private function enableBulkActions()
{
if (!isset($this->bulkActions['class'])) {
$this->bulkActions['class'] = 'booster.widgets.TbBulkActions';
}
$this->bulk = Yii::createComponent($this->bulkActions, $this);
$this->bulk->init();
}
private function fillSelectionChangedProperty()
{
$this->selectionChanged = $this->makeDefaultSelectionChangedJavascript();
}
/**
* @return string
*/
private function makeDefaultSelectionChangedJavascript()
{
return 'js:function(id) {
$("#"+id+" input[type=checkbox]").change();
}';
}
/**
* This method will become `renderDataCell` after Yii 1.1.17 will be released
* @see https://github.com/yiisoft/yii/pull/3571
*
* @param CGridColumn $column
* @param integer $row
*/
private function renderDataCellProcessingSummariesIfNeeded($column, $row)
{
$value = $this->getRenderedDataCellValue($column, $row);
if ($this->isExtendedSummaryEnabled())
$this->processColumnValue($column, $value);
echo $value;
}
/**
* @param CGridColumn $column
* @param integer $row
*
* @return string
*/
private function getRenderedDataCellValue($column, $row)
{
ob_start();
$column->renderDataCell($row);
return ob_get_clean();
}
/**
* @return bool
*/
private function isExtendedSummaryEnabled()
{
return $this->displayExtendedSummary && !empty($this->extendedSummary['columns']);
}
}
/**
*## TbOperation class
*
* Abstract class where all types of operations extend from
*
* @package booster.widgets.grids.operations
*/
abstract class TbOperation extends CWidget
{
/**
* @var string $template the template to display label and value of the operation at the summary
*/
public $template = '{label}: {value}';
/**
* @var int $value the resulted value of operation
*/
public $value = 0;
/**
* @var string $label the label of the calculated value
*/
public $label;
/**
* @var TbDataColumn $column
*/
public $column;
/**
* Widget initialization
* @throws CException
*/
public function init()