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 | /** |
||
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 |
||
186 |