forked from sanex3339/Yii2-Simple-Filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleFilter.php
234 lines (204 loc) · 5.8 KB
/
SimpleFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace sanex\simplefilter;
use Yii;
use yii\base\UnknownPropertyException;
use yii\helpers\Url;
use yii\web\Session;
class SimpleFilter extends \yii\base\Module
{
/*
*
* setParams() properties
*
*/
/**
* @var object Model object
*/
public $model;
/**
* @var object yii\db\ActiveQuery object
*/
public $query;
/**
* @var bool Use or not ajax for updating data
*/
public $useAjax = true;
/**
* @var bool Ue or not cache
*/
public $useCache = false;
/**
* @var bool Return data as model or as dataProvider
*/
public $useDataProvider = false;
/*
*
* renderAjaxView() properties
*
*/
/**
* @var string Alias to ajax view file
*/
public $ajaxViewFile;
/**
* @var array Custom parameters what was send to ajax view
*/
public $ajaxViewParams = [];
/*
*
* other properties
*
*/
/**
* @var array setFilter() properties
*/
public $filter;
/**
* @var string Current controller route
*/
public $controllerRoute;
/**
* @var object Session object
*/
public $session;
/**
* @var array Temporary session data
*/
private $tempSessionData;
/**
* @var array Available public methods parameters
*/
private $availableParameters = [
'setFilter' => [
'property',
'caption',
'values',
'class'
],
'setParams' => [
'model',
'query',
'useAjax',
'useCache',
'useDataProvider'
]
];
/**
* module init method
*/
public function init()
{
parent::init();
$this->session = new Session;
$this->session->open();
}
/**
* set class properties with given parameters
* get current controller route in which module was called
* put that properties into session
*
* @param array $params
* @throws UnknownPropertyException
* @return void
*/
public function setParams(Array $params = [])
{
if ($params) {
foreach ($params as $key => $value) {
if (in_array($key, $this->availableParameters['setParams'])) {
$this->{$key} = $value;
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $key);
}
}
if (!$this->model) {
throw new UnknownPropertyException("The parameter `model` is required", 1);
}
} else {
throw new UnknownPropertyException('Filter parameters must be set', 1);
}
$this->getControllerRoute();
$this->setSessionData(['model', 'query', 'useCache', 'useDataProvider']);
}
/**
* set $filter property and run action 'filter/set-filter'
*
* @param array $filter
* @return mixed
* @throws UnknownPropertyException
*/
public function setFilter(Array $filter = [])
{
if ($filter) {
foreach ($filter as $filterGroup) {
foreach ($filterGroup as $key => $value) {
if (!in_array($key, $this->availableParameters['setFilter'])) {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $key);
}
if (!is_string($value) && !is_array($value)) {
throw new UnknownPropertyException('setFilter() values must be a string or array');
}
if (!is_array($filterGroup['values'])) {
throw new UnknownPropertyException('setFilter() `values` parameter must be an array');
}
}
}
$this->filter = $filter;
} else {
throw new UnknownPropertyException('Filter parameters must be set', 1);
}
return $this->runAction('filter/set-filter');
}
/**
* send $ajaxViewFile and $ajaxViewParams to module controller and run action 'filter/show-data-get'
*
* @param $ajaxViewFile
* @param array $ajaxViewParams
* @return mixed
*/
public function renderAjaxView($ajaxViewFile, Array $ajaxViewParams = [])
{
if (!is_string($ajaxViewFile)) {
throw new UnknownPropertyException('$ajaxViewFile must be a string');
}
$this->ajaxViewFile = $ajaxViewFile;
$this->ajaxViewParams = $ajaxViewParams;
$this->setSessionData(['ajaxViewFile', 'ajaxViewParams']);
return $this->runAction('filter/show-data-get');
}
/**
* encrypt data with Yii2 cookie validation key
*
* @param $data
* @return string
*/
private function encryptData($data)
{
return $encryptedData = Yii::$app->getSecurity()->encryptByKey(
trim(base64_encode(serialize($data))), Yii::$app->request->cookieValidationKey
);
}
/**
* get route for controller in which was called module
*
* @return void
*/
private function getControllerRoute()
{
$this->controllerRoute = str_replace('index.php', '', Url::to(['/' . Yii::$app->controller->getRoute()]));
$this->setSessionData(['controllerRoute']);
}
/**
* call encrypt method for given class properties and put them into session
*
* @param array $properties
* @return void
*/
private function setSessionData(Array $properties = [])
{
foreach ($properties as $property) {
$this->tempSessionData[$property] = $this->$property;
}
$this->session['SimpleFilter'] = $this->encryptData($this->tempSessionData);
}
}