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