1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Admin\Controller; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Admin; |
5
|
|
|
use Redaxscript\Filter; |
6
|
|
|
use Redaxscript\Validator; |
7
|
|
|
use function json_encode; |
8
|
|
|
use function strtotime; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* children class to process the admin category request |
12
|
|
|
* |
13
|
|
|
* @since 4.0.0 |
14
|
|
|
* |
15
|
|
|
* @package Redaxscript |
16
|
|
|
* @category Controller |
17
|
|
|
* @author Henry Ruhs |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class Category extends ControllerAbstract |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* process the class |
24
|
|
|
* |
25
|
|
|
* @since 4.0.0 |
26
|
|
|
* |
27
|
|
|
* @param string $action action to process |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
|
32
|
11 |
|
public function process(string $action = null) : string |
33
|
|
|
{ |
34
|
11 |
|
$postArray = $this->_normalizePost($this->_sanitizePost()); |
35
|
11 |
|
$validateArray = $this->_validatePost($postArray); |
36
|
11 |
|
$myName = $this->_registry->get('myName'); |
37
|
11 |
|
$now = $this->_registry->get('now'); |
38
|
|
|
|
39
|
|
|
/* validate post */ |
40
|
|
|
|
41
|
11 |
|
if ($validateArray) |
42
|
|
|
{ |
43
|
7 |
|
return $this->_error( |
44
|
|
|
[ |
45
|
7 |
|
'route' => $this->_getErrorRoute($postArray), |
46
|
7 |
|
'message' => $validateArray |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* handle create */ |
51
|
|
|
|
52
|
4 |
|
if ($action === 'create') |
53
|
|
|
{ |
54
|
|
|
$createArray = |
55
|
|
|
[ |
56
|
1 |
|
'title' => $postArray['title'], |
57
|
1 |
|
'alias' => $postArray['alias'], |
58
|
1 |
|
'author' => $myName, |
59
|
1 |
|
'description' => $postArray['description'], |
60
|
1 |
|
'keywords' => $postArray['keywords'], |
61
|
1 |
|
'robots' => $postArray['robots'], |
62
|
1 |
|
'language' => $postArray['language'], |
63
|
1 |
|
'template' => $postArray['template'], |
64
|
1 |
|
'sibling' => $postArray['sibling'], |
65
|
1 |
|
'parent' => $postArray['parent'], |
66
|
1 |
|
'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
67
|
1 |
|
'rank' => $postArray['rank'], |
68
|
1 |
|
'access' => $postArray['access'], |
69
|
1 |
|
'date' => $postArray['date'] ? : $now |
70
|
|
|
]; |
71
|
1 |
|
if ($this->_create($createArray)) |
72
|
|
|
{ |
73
|
1 |
|
return $this->_success( |
74
|
|
|
[ |
75
|
1 |
|
'route' => $this->_getSuccessRoute($postArray), |
76
|
1 |
|
'timeout' => 2 |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* handle update */ |
82
|
|
|
|
83
|
3 |
|
if ($action === 'update') |
84
|
|
|
{ |
85
|
|
|
$updateArray = |
86
|
|
|
[ |
87
|
2 |
|
'title' => $postArray['title'], |
88
|
2 |
|
'alias' => $postArray['alias'], |
89
|
2 |
|
'author' => $myName, |
90
|
2 |
|
'description' => $postArray['description'], |
91
|
2 |
|
'keywords' => $postArray['keywords'], |
92
|
2 |
|
'robots' => $postArray['robots'], |
93
|
2 |
|
'language' => $postArray['language'], |
94
|
2 |
|
'template' => $postArray['template'], |
95
|
2 |
|
'sibling' => $postArray['sibling'], |
96
|
2 |
|
'parent' => $postArray['parent'], |
97
|
2 |
|
'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
98
|
2 |
|
'rank' => $postArray['rank'], |
99
|
2 |
|
'access' => $postArray['access'], |
100
|
2 |
|
'date' => $postArray['date'] ? : $now |
101
|
|
|
]; |
102
|
2 |
|
if ($this->_update($postArray['id'], $updateArray)) |
103
|
|
|
{ |
104
|
2 |
|
return $this->_success( |
105
|
|
|
[ |
106
|
2 |
|
'route' => $this->_getSuccessRoute($postArray), |
107
|
2 |
|
'timeout' => 2 |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/* handle error */ |
113
|
|
|
|
114
|
1 |
|
return $this->_error( |
115
|
|
|
[ |
116
|
1 |
|
'route' => $this->_getErrorRoute($postArray) |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* sanitize the post |
122
|
|
|
* |
123
|
|
|
* @since 4.0.0 |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
|
128
|
11 |
|
protected function _sanitizePost() : array |
129
|
|
|
{ |
130
|
11 |
|
$aliasFilter = new Filter\Alias(); |
131
|
11 |
|
$numberFilter = new Filter\Number(); |
132
|
11 |
|
$specialFilter = new Filter\Special(); |
133
|
11 |
|
$textFilter= new Filter\Text(); |
134
|
11 |
|
$toggleFilter = new Filter\Toggle(); |
135
|
|
|
|
136
|
|
|
/* sanitize post */ |
137
|
|
|
|
138
|
|
|
return |
139
|
|
|
[ |
140
|
11 |
|
'id' => $numberFilter->sanitize($this->_request->getPost('id')), |
141
|
11 |
|
'title' => $textFilter->sanitize($this->_request->getPost('title')), |
142
|
11 |
|
'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')), |
143
|
11 |
|
'description' => $textFilter->sanitize($this->_request->getPost('description')), |
144
|
11 |
|
'keywords' => $textFilter->sanitize($this->_request->getPost('keywords')), |
145
|
11 |
|
'robots' => $numberFilter->sanitize($this->_request->getPost('robots')), |
146
|
11 |
|
'language' => $specialFilter->sanitize($this->_request->getPost('language')), |
147
|
11 |
|
'template' => $specialFilter->sanitize($this->_request->getPost('template')), |
148
|
11 |
|
'sibling' => $numberFilter->sanitize($this->_request->getPost('sibling')), |
149
|
11 |
|
'parent' => $numberFilter->sanitize($this->_request->getPost('parent')), |
150
|
11 |
|
'status' => $toggleFilter->sanitize($this->_request->getPost('status')), |
151
|
11 |
|
'rank' => $numberFilter->sanitize($this->_request->getPost('rank')), |
152
|
11 |
|
'access' => json_encode($this->_request->getPost('access')), |
153
|
11 |
|
'date' => strtotime($this->_request->getPost('date')) |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* validate the post |
159
|
|
|
* |
160
|
|
|
* @since 4.0.0 |
161
|
|
|
* |
162
|
|
|
* @param array $postArray array of the post |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
|
167
|
11 |
|
protected function _validatePost(array $postArray = []) : array |
168
|
|
|
{ |
169
|
11 |
|
$nameValidator = new Validator\Name(); |
170
|
11 |
|
$aliasValidator = new Validator\Alias(); |
171
|
11 |
|
$categoryModel = new Admin\Model\Category(); |
172
|
11 |
|
$validateArray = []; |
173
|
|
|
|
174
|
|
|
/* validate post */ |
175
|
|
|
|
176
|
11 |
|
if (!$postArray['title']) |
177
|
|
|
{ |
178
|
6 |
|
$validateArray[] = $this->_language->get('title_empty'); |
179
|
|
|
} |
180
|
5 |
|
else if (!$nameValidator->validate($postArray['title'])) |
181
|
|
|
{ |
182
|
1 |
|
$validateArray[] = $this->_language->get('title_incorrect'); |
183
|
|
|
} |
184
|
11 |
|
if (!$postArray['alias']) |
185
|
|
|
{ |
186
|
4 |
|
$validateArray[] = $this->_language->get('alias_empty'); |
187
|
|
|
} |
188
|
7 |
|
else if (!$aliasValidator->validate($postArray['alias']) || $aliasValidator->matchSystem($postArray['alias'])) |
189
|
|
|
{ |
190
|
2 |
|
$validateArray[] = $this->_language->get('alias_incorrect'); |
191
|
|
|
} |
192
|
5 |
|
else if (!$categoryModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias'])) |
193
|
|
|
{ |
194
|
1 |
|
$validateArray[] = $this->_language->get('alias_exists'); |
195
|
|
|
} |
196
|
11 |
|
return $validateArray; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* create the category |
201
|
|
|
* |
202
|
|
|
* @since 4.0.0 |
203
|
|
|
* |
204
|
|
|
* @param array $createArray array of the create |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
|
209
|
1 |
|
protected function _create(array $createArray = []) : bool |
210
|
|
|
{ |
211
|
1 |
|
$categoryModel = new Admin\Model\Category(); |
212
|
1 |
|
return $categoryModel->createByArray($createArray); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* update the category |
217
|
|
|
* |
218
|
|
|
* @since 4.0.0 |
219
|
|
|
* |
220
|
|
|
* @param int $categoryId identifier of the category |
221
|
|
|
* @param array $updateArray array of the update |
222
|
|
|
* |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
|
226
|
2 |
|
protected function _update(int $categoryId = null, array $updateArray = []) : bool |
227
|
|
|
{ |
228
|
2 |
|
$categoryModel = new Admin\Model\Category(); |
229
|
2 |
|
return $categoryModel->updateByIdAndArray($categoryId, $updateArray); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* get success route |
234
|
|
|
* |
235
|
|
|
* @since 4.0.0 |
236
|
|
|
* |
237
|
|
|
* @param array $postArray array of the post |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
|
242
|
3 |
|
protected function _getSuccessRoute(array $postArray = []) : string |
243
|
|
|
{ |
244
|
3 |
|
if ($this->_registry->get('categoriesEdit') && $postArray['id']) |
245
|
|
|
{ |
246
|
1 |
|
return 'admin/view/categories#row-' . $postArray['id']; |
247
|
|
|
} |
248
|
2 |
|
if ($this->_registry->get('categoriesEdit') && $postArray['alias']) |
249
|
|
|
{ |
250
|
1 |
|
$categoryModel = new Admin\Model\Category(); |
251
|
1 |
|
$categoryId = $categoryModel->getByAlias($postArray['alias'])?->id; |
|
|
|
|
252
|
1 |
|
if ($categoryId) |
253
|
|
|
{ |
254
|
1 |
|
return 'admin/view/categories#row-' . $categoryId; |
255
|
|
|
} |
256
|
|
|
return 'admin/view/categories'; |
257
|
|
|
} |
258
|
1 |
|
return 'admin'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* get error route |
263
|
|
|
* |
264
|
|
|
* @since 4.0.0 |
265
|
|
|
* |
266
|
|
|
* @param array $postArray array of the post |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
|
271
|
8 |
|
protected function _getErrorRoute(array $postArray = []) : string |
272
|
|
|
{ |
273
|
8 |
|
if ($this->_registry->get('categoriesEdit') && $postArray['id']) |
274
|
|
|
{ |
275
|
1 |
|
return 'admin/edit/categories/' . $postArray['id']; |
276
|
|
|
} |
277
|
7 |
|
if ($this->_registry->get('categoriesNew')) |
278
|
|
|
{ |
279
|
6 |
|
return 'admin/new/categories'; |
280
|
|
|
} |
281
|
1 |
|
return 'admin'; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|