|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\actions; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\base\FilterStorage; |
|
14
|
|
|
use hipanel\grid\RepresentationCollectionFinder; |
|
15
|
|
|
use hipanel\widgets\SynchronousCountEnabler; |
|
16
|
|
|
use hiqdev\higrid\representations\RepresentationCollection; |
|
17
|
|
|
use hiqdev\higrid\representations\RepresentationCollectionInterface; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\grid\GridView; |
|
20
|
|
|
use yii\helpers\ArrayHelper; |
|
21
|
|
|
use yii\helpers\Inflector; |
|
22
|
|
|
use yii\web\Controller; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class IndexAction. |
|
26
|
|
|
*/ |
|
27
|
|
|
class IndexAction extends SearchAction |
|
28
|
|
|
{ |
|
29
|
|
|
public const VARIANT_PAGER_RESPONSE = 'pager'; |
|
30
|
|
|
|
|
31
|
|
|
public const VARIANT_SUMMARY_RESPONSE = 'summary'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string view to render |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_view; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* GET AJAX answer options for `VariantAction`, for example: |
|
40
|
|
|
* ``` |
|
41
|
|
|
* [ |
|
42
|
|
|
* 'headerValue1' => fn(VariantAction $action): string => 'response1', |
|
43
|
|
|
* 'headerValue2' => fn(VariantAction $action): string => 'response2', |
|
44
|
|
|
* ], |
|
45
|
|
|
* ``` |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
public array $responseVariants = []; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var RepresentationCollectionFinder |
|
52
|
|
|
*/ |
|
53
|
|
|
private $representationCollectionFinder; |
|
54
|
|
|
|
|
55
|
|
|
public function setView($value) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->_view = $value; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getView() |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->_view === null) { |
|
63
|
|
|
$this->_view = lcfirst(Inflector::id2camel($this->id)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->_view; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function __construct(string $id, Controller $controller, RepresentationCollectionFinder $representationCollectionFinder, array $config = []) |
|
70
|
|
|
{ |
|
71
|
|
|
parent::__construct($id, $controller, $config); |
|
72
|
|
|
$this->representationCollectionFinder = $representationCollectionFinder; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var array The map of filters for the [[hipanel\base\FilterStorage|FilterStorage]] |
|
77
|
|
|
*/ |
|
78
|
|
|
public $filterStorageMap = []; |
|
79
|
|
|
|
|
80
|
|
|
protected function getDefaultRules() |
|
81
|
|
|
{ |
|
82
|
|
|
return ArrayHelper::merge([ |
|
83
|
|
|
'html | pjax' => [ |
|
84
|
|
|
'save' => false, |
|
85
|
|
|
'flash' => false, |
|
86
|
|
|
'success' => [ |
|
87
|
|
|
'class' => RenderAction::class, |
|
88
|
|
|
'view' => $this->getView(), |
|
89
|
|
|
'data' => $this->data, |
|
90
|
|
|
'params' => function () { |
|
91
|
|
|
return [ |
|
92
|
|
|
'model' => $this->getSearchModel(), |
|
93
|
|
|
'dataProvider' => $this->getDataProvider(), |
|
94
|
|
|
'representationCollection' => $this->ensureRepresentationCollection(), |
|
95
|
|
|
'uiModel' => $this->getUiModel(), |
|
96
|
|
|
]; |
|
97
|
|
|
}, |
|
98
|
|
|
], |
|
99
|
|
|
], |
|
100
|
|
|
'GET ajax' => [ |
|
101
|
|
|
'class' => VariantsAction::class, |
|
102
|
|
|
'variants' => array_merge([ |
|
103
|
|
|
self::VARIANT_PAGER_RESPONSE => fn(VariantsAction $action): string => SynchronousCountEnabler::widget([ |
|
104
|
|
|
'dataProvider' => $action->parent->getDataProvider(), |
|
105
|
|
|
'content' => fn(GridView $grid): string => $grid->renderPager(), |
|
106
|
|
|
]), |
|
107
|
|
|
self::VARIANT_SUMMARY_RESPONSE => fn(VariantsAction $action): string => SynchronousCountEnabler::widget([ |
|
108
|
|
|
'dataProvider' => $action->parent->getDataProvider(), |
|
109
|
|
|
'content' => fn(GridView $grid): string => $grid->renderSummary(), |
|
110
|
|
|
]), |
|
111
|
|
|
], $this->responseVariants), |
|
112
|
|
|
], |
|
113
|
|
|
], parent::getDefaultRules()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getUiModel() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->controller->indexPageUiOptionsModel; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Method tries to guess representation collection class name and create object |
|
123
|
|
|
* Creates empty collection when no specific representation exists. |
|
124
|
|
|
* |
|
125
|
|
|
* @return RepresentationCollection|RepresentationCollectionInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function ensureRepresentationCollection() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->representationCollectionFinder->findOrFallback(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getDataProvider() |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->dataProvider === null) { |
|
138
|
|
|
$request = Yii::$app->request; |
|
139
|
|
|
|
|
140
|
|
|
$formName = $this->getSearchModel()->formName(); |
|
141
|
|
|
$requestFilters = $request->get($formName) ?: $request->get() ?: $request->post(); |
|
142
|
|
|
|
|
143
|
|
|
// Don't save filters for ajax requests, because |
|
144
|
|
|
// the request is probably triggered with select2 or smt similar |
|
145
|
|
|
if ($request->getIsPjax() || !$request->getIsAjax()) { |
|
146
|
|
|
$filterStorage = new FilterStorage(['map' => $this->filterStorageMap]); |
|
147
|
|
|
|
|
148
|
|
|
if ($request->getIsPost() && $request->post('clear-filters')) { |
|
149
|
|
|
$filterStorage->clearFilters(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$filterStorage->set($requestFilters); |
|
153
|
|
|
|
|
154
|
|
|
// Apply filters from storage only when request does not contain any data |
|
155
|
|
|
if (empty($requestFilters)) { |
|
156
|
|
|
$requestFilters = $filterStorage->get(); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$search = ArrayHelper::merge($this->findOptions, $requestFilters); |
|
161
|
|
|
|
|
162
|
|
|
$this->returnOptions[$this->controller->modelClassName()] = ArrayHelper::merge( |
|
163
|
|
|
ArrayHelper::remove($search, 'return', []), |
|
164
|
|
|
ArrayHelper::remove($search, 'rename', []) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
if ($formName !== '') { |
|
168
|
|
|
$search = [$formName => $search]; |
|
169
|
|
|
} |
|
170
|
|
|
$this->dataProvider = $this->getSearchModel()->search($search, $this->dataProviderOptions); |
|
171
|
|
|
|
|
172
|
|
|
// Set sort |
|
173
|
|
|
if ($this->getUiModel()->sort) { |
|
174
|
|
|
$this->dataProvider->setSort(['defaultOrder' => [$this->getUiModel()->sortAttribute => $this->getUiModel()->sortDirection]]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// Set pageSize |
|
178
|
|
|
if ($this->getUiModel()->per_page) { |
|
179
|
|
|
$this->dataProvider->setPagination(['pageSize' => $this->getUiModel()->per_page]); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->dataProvider; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|