Completed
Pull Request — master (#145)
by Klochok
10:28
created

IndexAction::getDefaultRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.568
cc 1
nc 1
nop 0
crap 2
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 Closure;
14
use hipanel\base\FilterStorage;
15
use hipanel\grid\RepresentationCollectionFinder;
16
use hipanel\widgets\CountEnabler;
17
use hiqdev\higrid\representations\RepresentationCollection;
18
use hiqdev\higrid\representations\RepresentationCollectionInterface;
19
use Yii;
20
use yii\grid\GridView;
21
use yii\helpers\ArrayHelper;
22
use yii\helpers\Inflector;
23
use yii\web\Controller;
24
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 = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
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 !== '') {
155
                $search = [$formName => $search];
156
            }
157
            $this->dataProvider = $this->getSearchModel()->search($search, $this->dataProviderOptions);
158
159
            // Set sort
160
            if ($this->getUiModel()->sort) {
161
                $this->dataProvider->setSort(['defaultOrder' => [$this->getUiModel()->sortAttribute => $this->getUiModel()->sortDirection]]);
162
            }
163
164
            // Set pageSize
165
            if ($this->getUiModel()->per_page) {
166
                $this->dataProvider->setPagination(['pageSize' => $this->getUiModel()->per_page]);
167
            }
168
        }
169
170
        return $this->dataProvider;
171
    }
172
}
173