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