1 | <?php |
||
25 | /** |
||
26 | * Class IndexAction. |
||
27 | */ |
||
28 | class IndexAction extends SearchAction |
||
29 | { |
||
30 | /** |
||
31 | * @var string view to render |
||
32 | */ |
||
33 | protected $_view; |
||
34 | |||
35 | public array $responseVariants = []; |
||
|
|||
36 | |||
37 | /** |
||
38 | * @var RepresentationCollectionFinder |
||
39 | */ |
||
40 | private $representationCollectionFinder; |
||
41 | |||
42 | public function setView($value) |
||
43 | { |
||
44 | $this->_view = $value; |
||
45 | } |
||
46 | |||
47 | public function getView() |
||
48 | { |
||
49 | if ($this->_view === null) { |
||
50 | $this->_view = lcfirst(Inflector::id2camel($this->id)); |
||
51 | } |
||
52 | |||
53 | return $this->_view; |
||
54 | } |
||
55 | |||
56 | public function __construct(string $id, Controller $controller, RepresentationCollectionFinder $representationCollectionFinder, array $config = []) |
||
57 | { |
||
58 | parent::__construct($id, $controller, $config); |
||
59 | $this->representationCollectionFinder = $representationCollectionFinder; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @var array The map of filters for the [[hipanel\base\FilterStorage|FilterStorage]] |
||
64 | */ |
||
65 | public $filterStorageMap = []; |
||
66 | |||
67 | protected function getDefaultRules() |
||
68 | { |
||
69 | return ArrayHelper::merge([ |
||
70 | 'html | pjax' => [ |
||
71 | 'save' => false, |
||
72 | 'flash' => false, |
||
73 | 'success' => [ |
||
74 | 'class' => RenderAction::class, |
||
75 | 'view' => $this->getView(), |
||
76 | 'data' => $this->data, |
||
77 | 'params' => function () { |
||
78 | return [ |
||
79 | 'model' => $this->getSearchModel(), |
||
80 | 'dataProvider' => $this->getDataProvider(), |
||
81 | 'representationCollection' => $this->ensureRepresentationCollection(), |
||
82 | 'uiModel' => $this->getUiModel(), |
||
83 | ]; |
||
84 | }, |
||
85 | ], |
||
86 | ], |
||
87 | 'GET ajax' => [ |
||
88 | 'class' => VariantsAction::class, |
||
89 | 'variants' => array_merge([ |
||
90 | 'pager' => fn(VariantsAction $action): string => CountEnabler::widget([ |
||
91 | 'dataProvider' => $action->parent->getDataProvider(), |
||
92 | 'content' => fn(GridView $grid): string => $grid->renderPager(), |
||
93 | ]), |
||
94 | 'summary' => fn(VariantsAction $action): string => CountEnabler::widget([ |
||
95 | 'dataProvider' => $action->parent->getDataProvider(), |
||
96 | 'content' => fn(GridView $grid): string => $grid->renderSummary(), |
||
97 | ]), |
||
98 | ], $this->responseVariants), |
||
99 | ], |
||
100 | ], parent::getDefaultRules()); |
||
101 | } |
||
102 | |||
103 | public function getUiModel() |
||
104 | { |
||
105 | return $this->controller->indexPageUiOptionsModel; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Method tries to guess representation collection class name and create object |
||
110 | * Creates empty collection when no specific representation exists. |
||
111 | * |
||
112 | * @return RepresentationCollection|RepresentationCollectionInterface |
||
113 | */ |
||
114 | protected function ensureRepresentationCollection() |
||
115 | { |
||
116 | return $this->representationCollectionFinder->findOrFallback(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getDataProvider() |
||
123 | { |
||
124 | if ($this->dataProvider === null) { |
||
125 | $request = Yii::$app->request; |
||
126 | |||
127 | $formName = $this->getSearchModel()->formName(); |
||
128 | $requestFilters = $request->get($formName) ?: $request->get() ?: $request->post(); |
||
129 | |||
130 | // Don't save filters for ajax requests, because |
||
131 | // the request is probably triggered with select2 or smt similar |
||
132 | if ($request->getIsPjax() || !$request->getIsAjax()) { |
||
133 | $filterStorage = new FilterStorage(['map' => $this->filterStorageMap]); |
||
134 | |||
135 | if ($request->getIsPost() && $request->post('clear-filters')) { |
||
136 | $filterStorage->clearFilters(); |
||
137 | } |
||
138 | |||
139 | $filterStorage->set($requestFilters); |
||
140 | |||
141 | // Apply filters from storage only when request does not contain any data |
||
142 | if (empty($requestFilters)) { |
||
143 | $requestFilters = $filterStorage->get(); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | $search = ArrayHelper::merge($this->findOptions, $requestFilters); |
||
148 | |||
149 | $this->returnOptions[$this->controller->modelClassName()] = ArrayHelper::merge( |
||
150 | ArrayHelper::remove($search, 'return', []), |
||
151 | ArrayHelper::remove($search, 'rename', []) |
||
152 | ); |
||
153 | |||
154 | if ($formName !== '') { |
||
173 |