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