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