|
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-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\actions; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\base\FilterStorage; |
|
14
|
|
|
use hipanel\grid\RepresentationCollectionFinder; |
|
15
|
|
|
use hiqdev\higrid\representations\RepresentationCollection; |
|
16
|
|
|
use hiqdev\higrid\representations\RepresentationCollectionInterface; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\helpers\ArrayHelper; |
|
19
|
|
|
use yii\helpers\Inflector; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class IndexAction. |
|
23
|
|
|
*/ |
|
24
|
|
|
class IndexAction extends SearchAction |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string view to render |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_view; |
|
30
|
|
|
|
|
31
|
|
|
public function setView($value) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->_view = $value; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getView() |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->_view === null) { |
|
39
|
|
|
$this->_view = lcfirst(Inflector::id2camel($this->id)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->_view; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array The map of filters for the [[hipanel\base\FilterStorage|FilterStorage]] |
|
47
|
|
|
*/ |
|
48
|
|
|
public $filterStorageMap = []; |
|
49
|
|
|
|
|
50
|
|
|
protected function getDefaultRules() |
|
51
|
|
|
{ |
|
52
|
|
|
return array_merge([ |
|
|
|
|
|
|
53
|
|
|
'html | pjax' => [ |
|
54
|
|
|
'save' => false, |
|
55
|
|
|
'flash' => false, |
|
56
|
|
|
'success' => [ |
|
57
|
|
|
'class' => RenderAction::class, |
|
58
|
|
|
'view' => $this->getView(), |
|
59
|
|
|
'data' => $this->data, |
|
60
|
|
|
'params' => function () { |
|
61
|
|
|
return [ |
|
62
|
|
|
'model' => $this->getSearchModel(), |
|
63
|
|
|
'dataProvider' => $this->getDataProvider(), |
|
64
|
|
|
'representationCollection' => $this->ensureRepresentationCollection(), |
|
65
|
|
|
'uiModel' => $this->getUiModel(), |
|
66
|
|
|
]; |
|
67
|
|
|
}, |
|
68
|
|
|
], |
|
69
|
|
|
], |
|
70
|
|
|
], parent::getDefaultRules()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getUiModel() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->controller->indexPageUiOptionsModel; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Method tries to guess representation collection class name and create object |
|
80
|
|
|
* Creates empty collection when no specific representation exists. |
|
81
|
|
|
* |
|
82
|
|
|
* @return RepresentationCollection|RepresentationCollectionInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function ensureRepresentationCollection() |
|
85
|
|
|
{ |
|
86
|
|
|
return RepresentationCollectionFinder::forCurrentRoute()->findOrFail(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getDataProvider() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->dataProvider === null) { |
|
95
|
|
|
$request = Yii::$app->request; |
|
96
|
|
|
|
|
97
|
|
|
$formName = $this->getSearchModel()->formName(); |
|
|
|
|
|
|
98
|
|
|
$requestFilters = $request->get($formName) ?: $request->get() ?: $request->post(); |
|
99
|
|
|
|
|
100
|
|
|
// Don't save filters for ajax requests, because |
|
101
|
|
|
// the request is probably triggered with select2 or smt similar |
|
102
|
|
|
if ($request->getIsPjax() || !$request->getIsAjax()) { |
|
103
|
|
|
$filterStorage = new FilterStorage(['map' => $this->filterStorageMap]); |
|
104
|
|
|
|
|
105
|
|
|
if ($request->getIsPost() && $request->post('clear-filters')) { |
|
106
|
|
|
$filterStorage->clearFilters(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$filterStorage->set($requestFilters); |
|
110
|
|
|
|
|
111
|
|
|
// Apply filters from storage only when request does not contain any data |
|
112
|
|
|
if (empty($requestFilters)) { |
|
113
|
|
|
$requestFilters = $filterStorage->get(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$search = ArrayHelper::merge($this->findOptions, $requestFilters); |
|
118
|
|
|
|
|
119
|
|
|
$this->returnOptions[$this->controller->modelClassName()] = ArrayHelper::merge( |
|
120
|
|
|
ArrayHelper::remove($search, 'return', []), |
|
121
|
|
|
ArrayHelper::remove($search, 'rename', []) |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
if ($formName !== '') { |
|
125
|
|
|
$search = [$formName => $search]; |
|
126
|
|
|
} |
|
127
|
|
|
$this->dataProvider = $this->getSearchModel()->search($search, $this->dataProviderOptions); |
|
128
|
|
|
|
|
129
|
|
|
// Set sort |
|
130
|
|
|
if ($this->getUiModel()->sort) { |
|
131
|
|
|
$this->dataProvider->setSort(['defaultOrder' => [$this->getUiModel()->sortAttribute => $this->getUiModel()->sortDirection]]); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Set pageSize |
|
135
|
|
|
if ($this->getUiModel()->per_page) { |
|
136
|
|
|
$this->dataProvider->setPagination(['pageSize' => $this->getUiModel()->per_page]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this->dataProvider; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.