forked from kartik-v/yii2-dynagrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynaGrid.php
1265 lines (1173 loc) · 42.4 KB
/
DynaGrid.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
/**
* @package yii2-dynagrid
* @author Kartik Visweswaran <[email protected]>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015 - 2018
* @version 1.4.8
*/
namespace kartik\dynagrid;
use kartik\base\Config;
use kartik\dialog\Dialog;
use kartik\dynagrid\models\DynaGridConfig;
use kartik\dynagrid\models\DynaGridSettings;
use kartik\grid\GridView;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;
use yii\data\DataProviderInterface;
use yii\data\Sort;
use yii\data\SqlDataProvider;
use yii\db\ActiveQuery;
use yii\db\ActiveQueryInterface;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Inflector;
use yii\helpers\Json;
/**
* Enhance GridView by allowing you to dynamically edit grid configuration. The dynagrid allows you to set your own grid
* theme, pagesize, and column order/display settings. The widget allows you to manage the order and visibility of
* columns dynamically at runtime. It also allows you to save this configuration or retrieve the saved configuration
* to/from session, cookie, or database.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class DynaGrid extends Widget
{
use DynaGridTrait;
/**
* Session storage
*/
const TYPE_SESSION = 'session';
/**
* Cookie storage
*/
const TYPE_COOKIE = 'cookie';
/**
* Database storage
*/
const TYPE_DB = 'db';
/**
* Fix column to the left
*/
const ORDER_FIX_LEFT = 'fixleft';
/**
* Fix column to the right
*/
const ORDER_FIX_RIGHT = 'fixright';
/**
* Fix column to the middle
*/
const ORDER_MIDDLE = 'middle';
/**
* @var string the module identifier if this widget is part of a module. If not set, the module identifier will
* be auto derived based on the \yii\base\Module::getInstance method. This can be useful, if you are setting
* multiple module identifiers for the same module in your Yii configuration file. To specify children or grand
* children modules you can specify the module identifiers relative to the parent module (e.g. `admin/content`).
*/
public $moduleId;
/**
* @var string the type of storage for the dynagrid configuration.
* - [[DynaGrid::TYPE_SESSION]]: Save the config in a session variable for the current session.
* - [[DynaGrid::TYPE_COOKIE]]: Save the config in a cookie for retrieval. You need to setup the
* [[Module::cookieSettings]] property to control the cookie expiry and other settings.
* - [[DynaGrid::TYPE_DB]]: Save the config to a database. You need to setup the [[Module::dbSettings]]
* property to setup the database table and attributes for storage.
*/
public $storage;
/**
* @var string the initial grid theme to be set
*/
public $theme;
/**
* @var boolean whether settings are stored specific to each user
*/
public $userSpecific;
/**
* @var boolean whether to update only the name, when editing and saving a filter or sort. This is applicable
* only for [[$storage]] set to [[Dynagrid::TYPE_DB]]. If set to `false`, it will also overwrite the current
* `filter` or `sort` settings.
*/
public $dbUpdateNameOnly = false;
/**
* @var boolean whether to show the personalize button group. Defaults to `true`.
*/
public $showPersonalize = true;
/**
* @var boolean whether to show the filter save widget button. Defaults to `true`.
*/
public $showFilter = true;
/**
* @var boolean whether to show the sort save widget button. Defaults to `true`.
*/
public $showSort = true;
/**
* @var boolean whether to enable multiple sort. Defaults to `true`.
*/
public $enableMultiSort = true;
/**
* @var boolean whether to allow setup of the pagination. Defaults to `true`.
*/
public $allowPageSetting = true;
/**
* @var boolean whether to allow display/setup of the theme. Defaults to `true`.
*/
public $allowThemeSetting = true;
/**
* @var boolean whether to allow display/setup of the filter in the personalize grid form
*/
public $allowFilterSetting = true;
/**
* @var boolean whether to allow display/setup of the sort in the personalize grid form
*/
public $allowSortSetting = true;
/**
* @var array widget options for \kartik\widgets\GridView that will be rendered by the DynaGrid widget
*/
public $gridOptions;
/**
* @var boolean whether the DynaGrid configuration button class should match the grid panel style.
*/
public $matchPanelStyle;
/**
* @var array the HTML attributes for the dynagrid personalize toggle button which will render the DynaGrid
* configuration form within a Bootstrap Modal container.
*/
public $toggleButtonGrid;
/**
* @var array the HTML attributes for the filter configuration button which will render the Filter settings form
* within a Bootstrap Modal container.
*/
public $toggleButtonFilter;
/**
* @var array the HTML attributes for the sort configuration button which will render the Sort settings form
* within a Bootstrap Modal container.
*/
public $toggleButtonSort;
/**
* @var array HTML options for the DynaGrid widget
*/
public $options;
/**
* @var array the sortable widget options
*/
public $sortableOptions;
/**
* @var array the HTML attributes for the sortable columns header
*/
public $sortableHeader = ['class' => 'alert alert-info dynagrid-sortable-header'];
/**
* @var array the grid columns configuration
*/
public $columns;
/**
* @var string the message to display after applying and submitting the configuration and until refreshed grid is
* reloaded
*/
public $submitMessage;
/**
* @var string the message to display after deleting the configuration and
* until refreshed grid is reloaded
*/
public $deleteMessage;
/**
* @var array HTML attributes for the submission message container
*/
public $messageOptions;
/**
* @var string the confirmation warning message before deleting a personalization configuration or setting.
*/
public $deleteConfirmation;
/**
* @var array configuration settings for the Krajee dialog widget that will be used to render alerts and
* confirmation dialog prompts
* @see http://demos.krajee.com/dialog
*/
public $krajeeDialogSettings = [];
/**
* @var array the HTML attributes for the save/apply action button. If this is set to `false`, it will not be
* displayed. The following special variables are supported:
* - `icon`: _string_, the glyphicon class suffix for the button. Defaults to `save`.
* - `label`: _string_, the label for the action button. Defaults to empty string.
* - `title`: _string_, the title for the action button. Defaults to `Save grid settings`.
*/
public $submitButtonOptions = [];
/**
* @var array|boolean the HTML attributes for the reset action button. If this is set to `false`, it will not be
* displayed. The following special variables are supported:
* - `icon`: _string_, the glyphicon class suffix for the button. Defaults to `repeat`.
* - `label`: _string_, the label for the action button. Defaults to empty string.
* - `title`: _string_, the title for the action button. Defaults to `Abort any changes and reset settings`.
*/
public $resetButtonOptions = [];
/**
* @var array|boolean the HTML attributes for the delete/trash action button. If this is set to `false`, it will
* not be displayed. The following special variables are supported:
* - `icon`: _string_, the glyphicon class suffix for the button. Defaults to `trash`.
* - `label`: _string_, the label for the action button. Defaults to empty string.
* - `title`: _string_, the title for the action button. Defaults to `Remove saved grid settings`.
*/
public $deleteButtonOptions = [];
/**
* @var string the icon that will be displayed for each VISIBLE column heading in the column reordering pane.
* This is not HTML encoded.
*/
public $iconVisibleColumn = '<i class="glyphicon glyphicon-eye-open"></i>';
/**
* @var string the icon that will be displayed for each HIDDEN column heading in the column reordering pane.
* This is not HTML encoded.
*/
public $iconHiddenColumn = '<i class="glyphicon glyphicon-eye-close"></i>';
/**
* @var array the cached columns configuration
*/
protected $_columns = [];
/**
* @var array the user configured visible widget columns
*/
protected $_visibleColumns = [];
/**
* @var array the hidden widget columns for user configuration
*/
protected $_hiddenColumns = [];
/**
* @var array the stored visible keys
*/
protected $_visibleKeys = [];
/**
* @var integer the grid pagesize
*/
protected $_pageSize;
/**
* @var integer the grid filter id
*/
protected $_filterId = null;
/**
* @var integer the grid sort id
*/
protected $_sortId = null;
/**
* @var array the dynagrid detail configuration settings (for filter and sort)
*/
protected $_detailConfig = [];
/**
* @var Module the current module
*/
protected $_module;
/**
* @var string request param name which will show the grid configuration submitted
*/
protected $_requestSubmit;
/**
* @var DynaGridConfig model
*/
protected $_model;
/**
* @var boolean flag to check if the grid configuration form has been submitted
*/
protected $_isSubmit = false;
/**
* @var boolean flag to check if the pjax is enabled for the grid
*/
protected $_isPjax;
/**
* @var string the identifier for the grid settings modal dialog
*/
protected $_gridModalId;
/**
* @var string the identifier for the filter settings modal dialog
*/
protected $_filterModalId;
/**
* @var string the identifier for the sort settings modal dialog
*/
protected $_sortModalId;
/**
* @var string the unique element identifier for the hidden filter input
*/
protected $_filterKey;
/**
* @var string the unique element identifier for the hidden sort input
*/
protected $_sortKey;
/**
* @var string the identifier for pjax container
*/
protected $_pjaxId;
/**
* @var DynaGridStore the storage instance
*/
protected $_store;
/**
* Is column visible
*
* @param mixed $column
*
* @return mixed
*/
protected static function isVisible($column)
{
return is_array($column) && !ArrayHelper::getValue($column, 'visible', true) ? false : true;
}
/**
* Get the default action button option settings
*
* @param string $type the button type
*
* @return array the button settings
*/
protected static function getDefaultButtonOptions($type)
{
if ($type === 'submit') {
return [
'type' => 'button',
'icon' => 'save',
'label' => Yii::t('kvdynagrid', 'Apply'),
'title' => Yii::t('kvdynagrid', 'Save grid settings'),
'class' => 'btn btn-primary',
'data-pjax' => false,
];
}
if ($type === 'reset') {
return [
'type' => 'reset',
'icon' => 'repeat',
'label' => Yii::t('kvdynagrid', 'Reset'),
'title' => Yii::t('kvdynagrid', 'Abort any changes and reset settings'),
'class' => 'btn btn-default',
'data-pjax' => false,
];
}
if ($type === 'delete') {
return [
'type' => 'button',
'icon' => 'trash',
'label' => Yii::t('kvdynagrid', 'Trash'),
'title' => Yii::t('kvdynagrid', 'Remove saved grid settings'),
'class' => 'btn btn-danger',
'data-pjax' => false,
];
}
return [];
}
/**
* Gets the columns for the dynagrid
*
* @return array
*/
public function getColumns()
{
return $this->gridOptions['columns'];
}
/**
* @inheritdoc
*/
public function run()
{
$this->initWidget();
echo Html::tag('div', GridView::widget($this->gridOptions), $this->options);
parent::run();
}
/**
* Initialize the module based on module identifier
*/
protected function initModule()
{
if (!isset($this->moduleId)) {
$this->_module = Module::getInstance();
if (isset($this->_module)) {
$this->moduleId = $this->_module->id;
return;
}
$this->moduleId = Module::MODULE;
}
$this->_module = Config::getModule($this->moduleId, Module::className());
}
/**
* Initializes widget settings and options
*
* @throws InvalidConfigException
*/
protected function initWidget()
{
if (empty($this->options['id'])) {
throw new InvalidConfigException(
"You must setup a unique identifier for DynaGrid within \"options['id']\"."
);
}
$this->initModule();
$this->_gridModalId = $this->options['id'] . '-grid-modal';
$this->_filterModalId = $this->options['id'] . '-filter-modal';
$this->_sortModalId = $this->options['id'] . '-sort-modal';
$this->_filterKey = $this->options['id'] . '-filter-key';
$this->_sortKey = $this->options['id'] . '-sort-key';
$this->_pjaxId = $this->options['id'] . '-pjax';
foreach ($this->_module->dynaGridOptions as $key => $setting) {
if (is_array($setting) && !empty($setting) && !empty($this->$key)) {
$this->$key = ArrayHelper::merge($setting, $this->$key);
} elseif (!isset($this->$key)) {
$this->$key = $setting;
}
}
if (empty($this->columns) || !is_array($this->columns)) {
throw new InvalidConfigException("The 'columns' configuration must be setup as a valid array.");
}
if (empty($this->gridOptions['dataProvider']) && empty($this->gridOptions['filterModel'])) {
throw new InvalidConfigException(
"You must setup either the gridOptions['filterModel'] or gridOptions['dataProvider']."
);
}
if (!empty($this->gridOptions['filterModel']) && !method_exists($this->gridOptions['filterModel'], 'search')) {
throw new InvalidConfigException(
"The gridOptions['filterModel'] must implement a 'search' method in order to apply saved filters."
);
}
if (empty($this->gridOptions['dataProvider'])) {
$this->initDataProvider($this->gridOptions['filterModel']);
}
/** @var DataProviderInterface $dataProvider */
$dataProvider = $this->gridOptions['dataProvider'];
if ($dataProvider->getSort() === false) {
$this->showSort = false;
$this->allowSortSetting = false;
}
if ($dataProvider->getPagination() === false) {
$this->allowPageSetting = false;
}
if (empty($this->gridOptions['filterModel'])) {
$this->showFilter = false;
$this->allowFilterSetting = false;
}
if (empty($this->theme)) {
$this->theme = $this->_module->defaultTheme;
}
if (!isset($this->_pageSize) || $this->_pageSize === null) {
$this->_pageSize = $this->_module->defaultPageSize;
}
$this->_requestSubmit = $this->options['id'] . '-dynagrid';
$this->_model = new DynaGridConfig(['moduleId' => $this->moduleId]);
$this->_isSubmit = !empty($_POST[$this->_requestSubmit]) && $this->_model->load(Yii::$app->request->post()) &&
$this->_model->validate();
$this->_store = new DynaGridStore(
[
'id' => $this->options['id'],
'moduleId' => $this->moduleId,
'storage' => $this->storage,
'userSpecific' => $this->userSpecific,
'dbUpdateNameOnly' => $this->dbUpdateNameOnly,
]
);
$this->prepareColumns();
$this->configureColumns();
$this->applyGridConfig();
$this->_isPjax = ArrayHelper::getValue($this->gridOptions, 'pjax', false);
if ($this->_isPjax) {
$this->gridOptions['pjaxSettings']['options']['id'] = $this->_pjaxId;
}
$this->initGrid();
}
/**
* Can the column be reordered
*
* @param mixed $column
*
* @return boolean
*/
protected function canReorder($column)
{
return is_array($column) && ArrayHelper::getValue($column, 'order', self::ORDER_MIDDLE) != self::ORDER_MIDDLE
? false : true;
}
/**
* Initialize the data provider
*
* @param Model $searchModel
*/
protected function initDataProvider($searchModel)
{
/** @noinspection PhpUndefinedMethodInspection */
$this->gridOptions['dataProvider'] = $searchModel->search(Yii::$app->request->getQueryParams());
}
/**
* Prepares the columns for the dynagrid
*/
protected function prepareColumns()
{
$this->_columns = $this->columns;
$columns = [];
foreach ($this->columns as $column) {
if (is_array($column)) {
unset($column['order']);
}
$columns[] = $column;
}
$this->gridOptions['columns'] = $columns;
}
/**
* Reconfigure columns with unique keys
*/
protected function configureColumns()
{
$columnsByKey = [];
foreach ($this->_columns as $column) {
$columnKey = $this->getColumnKey($column);
for ($j = 0; true; $j++) {
$suffix = ($j) ? '_' . $j : '';
$columnKey .= $suffix;
if (!array_key_exists($columnKey, $columnsByKey)) {
break;
}
}
$columnsByKey[$columnKey] = $column;
}
$this->_columns = $columnsByKey;
}
/**
* Generate an unique column key
*
* @param mixed $column
*
* @return mixed
*/
protected function getColumnKey($column)
{
if (!is_array($column)) {
$matches = $this->matchColumnString($column);
$columnKey = $matches[1];
} elseif (!empty($column['attribute'])) {
$columnKey = $column['attribute'];
} elseif (!empty($column['label'])) {
$columnKey = $column['label'];
} elseif (!empty($column['header'])) {
$columnKey = $column['header'];
} elseif (!empty($column['class'])) {
$columnKey = $column['class'];
} else {
$columnKey = null;
}
return hash('crc32', $columnKey);
}
/**
* Finds the matches for a string column format
*
* @param string $column
*
* @return mixed
* @throws InvalidConfigException
*/
protected function matchColumnString($column)
{
$matches = [];
if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/u', $column, $matches)) {
throw new InvalidConfigException(
"Invalid column configuration for '{$column}'. The column must be specified " .
"in the format of 'attribute', 'attribute:format' or 'attribute:format: label'."
);
}
return $matches;
}
/**
* Applies the current grid configuration
*/
protected function applyGridConfig()
{
$config = $this->getGridConfig();
if ($this->_isSubmit) {
$delete = ArrayHelper::getValue($_POST, 'deleteFlag', 0) == 1;
$this->saveGridConfig($config, $delete);
Yii::$app->controller->refresh();
} else {
$this->loadGridConfig($config);
$this->setWidgetColumns();
$this->loadAttributes($this->_model);
}
$this->applyFilter();
$this->applySort();
$this->applyPageSize();
$this->applyTheme();
$this->applyColumns();
}
/**
* Gets the current grid configuration
*
* @param boolean $current whether it is the currently set grid configuraton
*
* @return array
*/
protected function getGridConfig($current = false)
{
if ($current) {
return [
'page' => $this->_pageSize,
'keys' => $this->_visibleKeys,
'theme' => $this->theme,
'filter' => $this->_filterId,
'sort' => $this->_sortId,
];
}
return !$this->_isSubmit ? $this->_store->fetch() : [
'page' => $this->_model->pageSize,
'theme' => $this->_model->theme,
'keys' => explode(',', $_POST['visibleKeys']),
'filter' => $this->_model->filterId,
'sort' => $this->_model->sortId,
];
}
/**
* Update configuration
*
* @param array $config the dynagrid configuration
* @param boolean $delete the deletion flag
*/
protected function saveGridConfig($config, $delete)
{
if ($delete) {
$this->_store->delete();
} else {
$this->_store->save($config);
}
}
/**
* Load grid configuration from specific storage
*
* @param array $config the configuration to load
*
* @throws InvalidConfigException
*/
protected function loadGridConfig($config = [])
{
if ($config === false) {
$this->_visibleKeys = []; //take visible keys from grid config
$this->_pageSize = $this->_module->defaultPageSize; //take pagesize from module configuration
foreach ($this->_columns as $key => $column) {
if (static::canReorder($column) && static::isVisible($column)) {
$this->_visibleKeys[] = $key;
}
}
} else {
$this->parseData($config);
}
}
/**
* Parses the encoded grid configuration and fetches
* - grid master settings: the theme, pagesize, visible keys
* - grid detail settings: filter and sort configuration
*
* @param array $data the stored data to be parsed
*
* @return void
*/
protected function parseData($data)
{
if (!is_array($data) || empty($data)) {
return;
}
$this->_pageSize = ArrayHelper::getValue($data, 'page', $this->_module->defaultPageSize);
$this->theme = ArrayHelper::getValue($data, 'theme', $this->_module->defaultTheme);
if ($this->storage === self::TYPE_DB) {
$this->_filterId = $this->_store->fetch('filterAttr');
$this->_sortId = $this->_store->fetch('sortAttr');
} else {
$this->_filterId = ArrayHelper::getValue($data, DynaGridStore::STORE_FILTER, '');
$this->_sortId = ArrayHelper::getValue($data, DynaGridStore::STORE_SORT, '');
}
if (!empty($data['keys'])) {
$this->_visibleKeys = $data['keys'];
}
$this->parseDetailData(DynaGridStore::STORE_FILTER);
$this->parseDetailData(DynaGridStore::STORE_SORT);
}
/**
* Parses the grid detail configuration (for filter or sort).
*
* @param string $category one of 'filter' or 'sort'
*/
protected function parseDetailData($category)
{
$dtlKey = "_{$category}Id";
if (!empty($this->$dtlKey)) {
$store = new DynaGridStore(
[
'id' => $this->options['id'],
'moduleId' => $this->moduleId,
'storage' => $this->storage,
'userSpecific' => $this->userSpecific,
'dbUpdateNameOnly' => $this->dbUpdateNameOnly,
'category' => $category,
'dtlKey' => $this->$dtlKey,
]
);
$config = $store === null ? false : $store->fetch();
if ($config !== false) {
$this->_detailConfig[$category] = $config;
}
}
}
/**
* Sets widget columns for display in [[\kartik\sortable\Sortable]] widget
*/
protected function setWidgetColumns()
{
$this->_visibleColumns = $this->getSortableHeader($this->_model->getAttributeLabel('visibleColumns'));
$this->_hiddenColumns = $this->getSortableHeader($this->_model->getAttributeLabel('hiddenColumns'));
$visibleSettings = [];
// Ensure visible keys is not empty. If it is so, then grid will display all columns.
$this->_visibleKeys = array_filter($this->_visibleKeys);
$showAll = !is_array($this->_visibleKeys) || empty($this->_visibleKeys);
$indicator = Html::tag('span', $this->iconVisibleColumn, ['class' => 'icon-visible-column']) .
Html::tag('span', $this->iconHiddenColumn, ['class' => 'icon-hidden-column']);
foreach ($this->_columns as $key => $column) {
$order = ArrayHelper::getValue($column, 'order', self::ORDER_MIDDLE);
$disabled = ($order == self::ORDER_MIDDLE) ? false : true;
$widgetColumns = [
'content' => (empty($indicator) ? '' : $indicator . ' ') . $this->getColumnLabel($key, $column),
'options' => ['id' => $key],
];
if ($showAll && !$disabled) {
$visibleSettings[$key] = $widgetColumns;
} elseif (in_array($key, $this->_visibleKeys) && !$disabled) {
$visibleSettings[$key] = $widgetColumns;
} else {
$this->_hiddenColumns[] = $widgetColumns + ['disabled' => $disabled];
}
}
if ($showAll) {
$this->_visibleColumns = $visibleSettings;
$this->_visibleKeys = array_keys($this->_visibleColumns);
} else {
foreach ($this->_visibleKeys as $key) {
if (!empty($visibleSettings[$key])) {
$this->_visibleColumns[] = $visibleSettings[$key];
}
}
}
}
/**
* Generates the config for sortable widget header
*
* @param string $label
*
* @return array
*/
protected function getSortableHeader($label)
{
return [
[
'content' => $label,
'disabled' => true,
'options' => $this->sortableHeader,
],
];
}
/**
* Fetches the column label
*
* @param mixed $key the column key
* @param mixed $column the column object / configuration
*
* @return string
*/
protected function getColumnLabel($key, $column)
{
if (is_string($column)) {
$matches = $this->matchColumnString($column);
$attribute = $matches[1];
if (isset($matches[5])) {
return $matches[5];
} //header specified is in the format "attribute:format:label"
return $this->getAttributeLabel($attribute);
} else {
$label = $key;
if (is_array($column)) {
if (!empty($column['label'])) {
$label = $column['label'];
} elseif (!empty($column['header'])) {
$label = $column['header'];
} elseif (!empty($column['attribute'])) {
$label = $this->getAttributeLabel($column['attribute']);
} elseif (!empty($column['class'])) {
$class = explode('\\', $column['class']);
$label = Inflector::camel2words(end($class));
}
}
return trim(strip_tags(str_replace(['<br>', '<br/>'], ' ', $label)));
}
}
/**
* Generates the attribute label
*
* @param $attribute
*
* @return string
*/
protected function getAttributeLabel($attribute)
{
$provider = $this->gridOptions['dataProvider'];
/** @var Model $model */
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
/** @var ActiveQuery $query */
$query = $provider->query;
$model = new $query->modelClass;
return $model->getAttributeLabel($attribute);
}
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
return $model->getAttributeLabel($attribute);
} else {
return Inflector::camel2words($attribute);
}
}
/**
* Load configuration attributes into DynaGridConfig model
*
* @param DynaGridConfig $model
*/
protected function loadAttributes($model)
{
$model->id = $this->_requestSubmit;
$model->hiddenColumns = $this->_hiddenColumns;
$model->visibleColumns = $this->_visibleColumns;
$model->pageSize = $this->_pageSize;
$model->theme = $this->theme;
$model->filterId = $this->_filterId;
$model->sortId = $this->_sortId;
$model->widgetOptions = $this->sortableOptions;
$model->footer = $this->renderActionButton('delete') . $this->renderActionButton('reset') .
$this->renderActionButton('submit');
$themes = array_keys($this->_module->themeConfig);
$model->themeList = array_combine($themes, $themes);
}
/**
* Renders the action button
*
* @param string $type the button type
*
* @return string the rendered button
*/
protected function renderActionButton($type)
{
$tag = "{$type}ButtonOptions";
$options = $this->$tag;
if ($options === false) {
return '';
}
$defaultOptions = static::getDefaultButtonOptions($type);
if (!is_array($options)) {
$options = $defaultOptions;
} else {
$options = ArrayHelper::merge($defaultOptions, $options);
}
$icon = ArrayHelper::remove($options, 'icon', '');
$label = '';
if (!empty($icon)) {
$label = '<span class="glyphicon glyphicon-' . $icon . '"></span> ';
}
$label .= ArrayHelper::remove($options, 'label', '');
Html::addCssClass($options, "dynagrid-{$type}");
return Html::button($label, $options);
}
/**
* Applies the grid filter
*/
protected function applyFilter()
{
if (empty($this->gridOptions['filterModel'])) {
return;
}
$class = get_class($this->gridOptions['filterModel']);
/** @var Model $searchModel */
if (!empty($this->_detailConfig[DynaGridStore::STORE_FILTER]) && empty($_GET[$class])) {
$attributes = $this->_detailConfig[DynaGridStore::STORE_FILTER];
$searchModel = $this->gridOptions['filterModel'];
$searchModel->setAttributes($attributes);
$this->gridOptions['filterModel'] = $searchModel;
$this->initDataProvider($searchModel);
}
}
/**
* Applies the grid sort
*/
protected function applySort()
{
if (!empty($this->_detailConfig[DynaGridStore::STORE_SORT])) {
/** @var ActiveDataProvider $dataProvider */
$dataProvider = $this->gridOptions['dataProvider'];
$sort = $dataProvider->getSort();
if (!$sort instanceof Sort) {
return;
}
$sort->defaultOrder = $this->_detailConfig[DynaGridStore::STORE_SORT];
$dataProvider->setSort($sort);
$this->gridOptions['dataProvider'] = $dataProvider;
}
}
/**
* Applies the page size
*/
protected function applyPageSize()
{
if (isset($this->_pageSize) && $this->_pageSize !== '' && $this->allowPageSetting) {
/** @var \yii\data\BaseDataProvider $dataProvider */
$dataProvider = $this->gridOptions['dataProvider'];