1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Filter |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\filter\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
13
|
|
|
use gplcart\core\models\UserRole; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles incoming requests and outputs data related to Filter module |
17
|
|
|
*/ |
18
|
|
|
class Filter extends Controller |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User role model instance |
23
|
|
|
* @var \gplcart\core\models\UserRole $role |
24
|
|
|
*/ |
25
|
|
|
protected $role; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* An array of filter data |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $data_filter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Filter constructor. |
35
|
|
|
* @param UserRole $role |
36
|
|
|
*/ |
37
|
|
|
public function __construct(UserRole $role) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->role = $role; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Displays the filter overview page |
46
|
|
|
*/ |
47
|
|
|
public function listFilter() |
48
|
|
|
{ |
49
|
|
|
$this->setTitleListFilter(); |
50
|
|
|
$this->setBreadcrumbListFilter(); |
51
|
|
|
|
52
|
|
|
$this->setData('roles', $this->role->getList()); |
53
|
|
|
$this->setData('filters', $this->getListFilter()); |
54
|
|
|
|
55
|
|
|
$this->outputListFilter(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set titles on the filter overview page |
60
|
|
|
*/ |
61
|
|
|
protected function setTitleListFilter() |
62
|
|
|
{ |
63
|
|
|
$this->setTitle($this->text('Filters')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set breadcrumbs on the filter overview page |
68
|
|
|
*/ |
69
|
|
|
protected function setBreadcrumbListFilter() |
70
|
|
|
{ |
71
|
|
|
$breadcrumbs = array(); |
72
|
|
|
|
73
|
|
|
$breadcrumbs[] = array( |
74
|
|
|
'text' => $this->text('Dashboard'), |
75
|
|
|
'url' => $this->url('admin') |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$breadcrumbs[] = array( |
79
|
|
|
'text' => $this->text('Modules'), |
80
|
|
|
'url' => $this->url('admin/module/list') |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$breadcrumbs[] = array( |
84
|
|
|
'text' => $this->text('Filters'), |
85
|
|
|
'url' => $this->url('admin/module/settings/filter') |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Render and output the filter overview page |
93
|
|
|
*/ |
94
|
|
|
protected function outputListFilter() |
95
|
|
|
{ |
96
|
|
|
$this->output('filter|list'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns an array of prepared filters |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getListFilter() |
104
|
|
|
{ |
105
|
|
|
$roles = $this->role->getList(); |
106
|
|
|
$filters = $this->filter->getHandlers(); |
107
|
|
|
|
108
|
|
|
foreach ($filters as $id => &$filter) { |
109
|
|
|
|
110
|
|
|
if (!isset($filter['module']) || $filter['module'] !== 'filter') { |
111
|
|
|
unset($filters[$id]); |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$names = array(); |
116
|
|
|
|
117
|
|
|
foreach ($filter['role_id'] as $role_id) { |
118
|
|
|
if (isset($roles[$role_id]['name'])) { |
119
|
|
|
$names[] = $roles[$role_id]['name']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$filter['role_name'] = $names; |
124
|
|
|
$filter['rendered_config'] = json_encode($filter['data'], JSON_PRETTY_PRINT); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $filters; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Displays the filter edit page |
132
|
|
|
* @param string $filter_id |
133
|
|
|
*/ |
134
|
|
|
public function editFilter($filter_id) |
135
|
|
|
{ |
136
|
|
|
$this->setFilterFilter($filter_id); |
137
|
|
|
$this->setTitleEditFilter(); |
138
|
|
|
$this->setBreadcrumbEditFilter(); |
139
|
|
|
|
140
|
|
|
$this->setData('filter', $this->data_filter); |
141
|
|
|
$this->setData('roles', $this->role->getList()); |
142
|
|
|
$this->setData('can_delete', $this->canDeleteFilter()); |
143
|
|
|
|
144
|
|
|
$this->submitEditFilter(); |
145
|
|
|
$this->setDataEditFilter(); |
146
|
|
|
$this->outputEditFilter(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets template data |
151
|
|
|
*/ |
152
|
|
|
protected function setDataEditFilter() |
153
|
|
|
{ |
154
|
|
|
$data = $this->getData('filter.data'); |
155
|
|
|
|
156
|
|
|
if (is_array($data)) { |
157
|
|
|
$this->setData('filter.data', json_encode($data, JSON_PRETTY_PRINT)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets the current filter |
163
|
|
|
* @param string $filter_id |
164
|
|
|
*/ |
165
|
|
|
protected function setFilterFilter($filter_id) |
166
|
|
|
{ |
167
|
|
|
$this->data_filter = $this->filter->get($filter_id); |
168
|
|
|
|
169
|
|
|
if (empty($this->data_filter['module']) || $this->data_filter['module'] !== 'filter') { |
170
|
|
|
$this->setHttpStatus(404); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set title on the filter edit page |
176
|
|
|
*/ |
177
|
|
|
protected function setTitleEditFilter() |
178
|
|
|
{ |
179
|
|
|
$title = $this->text('Edit %name', array('%name' => $this->data_filter['name'])); |
180
|
|
|
$this->setTitle($title); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set breadcrumbs on the filter edit page |
185
|
|
|
*/ |
186
|
|
|
protected function setBreadcrumbEditFilter() |
187
|
|
|
{ |
188
|
|
|
$breadcrumbs = array(); |
189
|
|
|
|
190
|
|
|
$breadcrumbs[] = array( |
191
|
|
|
'text' => $this->text('Dashboard'), |
192
|
|
|
'url' => $this->url('admin') |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$breadcrumbs[] = array( |
196
|
|
|
'text' => $this->text('Modules'), |
197
|
|
|
'url' => $this->url('admin/module/list') |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$breadcrumbs[] = array( |
201
|
|
|
'text' => $this->text('Filters'), |
202
|
|
|
'url' => $this->url('admin/module/settings/filter') |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Saves the submitted filter |
210
|
|
|
*/ |
211
|
|
|
protected function submitEditFilter() |
212
|
|
|
{ |
213
|
|
|
if ($this->isPosted('delete')) { |
214
|
|
|
$this->deleteFilter(); |
215
|
|
|
} else if ($this->isPosted('save') && $this->validateEditFilter()) { |
216
|
|
|
$this->updateFilter(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Validate submitted filter |
222
|
|
|
*/ |
223
|
|
|
protected function validateEditFilter() |
224
|
|
|
{ |
225
|
|
|
$this->setSubmitted('filter', null, false); |
226
|
|
|
$this->setSubmittedBool('status'); |
227
|
|
|
|
228
|
|
|
$this->validateElement('name', 'required'); |
229
|
|
|
$this->validateElement('description', 'length', array(0, 255)); |
230
|
|
|
|
231
|
|
|
$array = json_decode($this->getSubmitted('data'), true); |
232
|
|
|
|
233
|
|
|
if (is_array($array)) { |
234
|
|
|
$this->setSubmitted('data', $array); |
235
|
|
|
} else { |
236
|
|
|
$this->setError('data', $this->text('Invalid configuration')); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return !$this->hasErrors(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Update a filter |
244
|
|
|
*/ |
245
|
|
|
protected function updateFilter() |
246
|
|
|
{ |
247
|
|
|
$saved = $this->config->get('module_filter_filters', array()); |
248
|
|
|
$saved[$this->data_filter['filter_id']] = $this->getSubmitted(); |
249
|
|
|
$this->config->set('module_filter_filters', $saved); |
250
|
|
|
|
251
|
|
|
$this->redirect("admin/module/settings/filter", $this->text('Filter has been updated'), 'success'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Delete a saved filter |
256
|
|
|
*/ |
257
|
|
|
protected function deleteFilter() |
258
|
|
|
{ |
259
|
|
|
$saved = $this->config->get('module_filter_filters', array()); |
260
|
|
|
unset($saved[$this->data_filter['filter_id']]); |
261
|
|
|
$this->config->set('module_filter_filters', $saved); |
262
|
|
|
|
263
|
|
|
$this->redirect("admin/module/settings/filter", $this->text('Filter has been deleted'), 'success'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Whether the filter can be deleted |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
|
|
protected function canDeleteFilter() |
271
|
|
|
{ |
272
|
|
|
$saved = $this->config->get('module_filter_filters', array()); |
273
|
|
|
return isset($saved[$this->data_filter['filter_id']]) && $this->access('module_filter_delete'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Render and output the filter edit page |
278
|
|
|
*/ |
279
|
|
|
protected function outputEditFilter() |
280
|
|
|
{ |
281
|
|
|
$this->output('filter|edit'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|