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 extra request |
12
|
|
|
* |
13
|
|
|
* @since 4.0.0 |
14
|
|
|
* |
15
|
|
|
* @package Redaxscript |
16
|
|
|
* @category Controller |
17
|
|
|
* @author Henry Ruhs |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class Extra 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
|
|
|
public function process(string $action = null) : string |
33
|
|
|
{ |
34
|
|
|
$postArray = $this->_normalizePost($this->_sanitizePost()); |
35
|
|
|
$validateArray = $this->_validatePost($postArray); |
|
|
|
|
36
|
|
|
$myUser = $this->_registry->get('myUser'); |
37
|
|
|
$now = $this->_registry->get('now'); |
38
|
|
|
|
39
|
|
|
/* validate post */ |
40
|
|
|
|
41
|
|
|
if ($validateArray) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
return $this->_error( |
44
|
|
|
[ |
45
|
|
|
'route' => $this->_getErrorRoute($postArray), |
|
|
|
|
46
|
|
|
'message' => $validateArray |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* handle create */ |
51
|
|
|
|
52
|
|
|
if ($action === 'create') |
53
|
|
|
{ |
54
|
|
|
$createArray = |
55
|
|
|
[ |
56
|
|
|
'title' => $postArray['title'], |
57
|
|
|
'alias' => $postArray['alias'], |
58
|
|
|
'author' => $myUser, |
59
|
|
|
'text' => $postArray['text'], |
60
|
|
|
'language' => $postArray['language'], |
61
|
|
|
'sibling' => $postArray['sibling'], |
62
|
|
|
'category' => $postArray['category'], |
63
|
|
|
'article' => $postArray['article'], |
64
|
|
|
'headline' => $postArray['headline'], |
65
|
|
|
'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
66
|
|
|
'rank' => $postArray['rank'], |
67
|
|
|
'access' => $postArray['access'], |
68
|
|
|
'date' => $postArray['date'] ? : $now |
69
|
|
|
]; |
70
|
|
|
if ($this->_create($createArray)) |
71
|
|
|
{ |
72
|
|
|
return $this->_success( |
73
|
|
|
[ |
74
|
|
|
'route' => $this->_getSuccessRoute($postArray), |
75
|
|
|
'timeout' => 2 |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* handle update */ |
81
|
|
|
|
82
|
|
|
if ($action === 'update') |
83
|
|
|
{ |
84
|
|
|
$updateArray = |
85
|
|
|
[ |
86
|
|
|
'title' => $postArray['title'], |
87
|
|
|
'alias' => $postArray['alias'], |
88
|
|
|
'author' => $myUser, |
89
|
|
|
'text' => $postArray['text'], |
90
|
|
|
'language' => $postArray['language'], |
91
|
|
|
'sibling' => $postArray['sibling'], |
92
|
|
|
'category' => $postArray['category'], |
93
|
|
|
'article' => $postArray['article'], |
94
|
|
|
'headline' => $postArray['headline'], |
95
|
|
|
'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
96
|
|
|
'rank' => $postArray['rank'], |
97
|
|
|
'access' => $postArray['access'], |
98
|
|
|
'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
|
|
|
$htmlFilter = new Filter\Html(); |
132
|
|
|
|
133
|
|
|
/* sanitize post */ |
134
|
|
|
|
135
|
|
|
return |
136
|
|
|
[ |
137
|
|
|
'id' => $numberFilter->sanitize($this->_request->getPost('id')), |
|
|
|
|
138
|
|
|
'title' => $this->_request->getPost('title'), |
139
|
|
|
'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')), |
|
|
|
|
140
|
|
|
'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')), |
|
|
|
|
141
|
|
|
'language' => $specialFilter->sanitize($this->_request->getPost('language')), |
|
|
|
|
142
|
|
|
'sibling' => $this->_request->getPost('sibling'), |
143
|
|
|
'category' => $this->_request->getPost('category'), |
144
|
|
|
'article' => $this->_request->getPost('article'), |
145
|
|
|
'headline' => $numberFilter->sanitize($this->_request->getPost('headline')), |
|
|
|
|
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
|
|
|
$extraModel = new Admin\Model\Extra(); |
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 (!$extraModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias'])) |
184
|
|
|
{ |
185
|
|
|
$validateArray[] = $this->_language->get('alias_exists'); |
186
|
|
|
} |
187
|
|
|
if (!$postArray['text']) |
188
|
|
|
{ |
189
|
|
|
$validateArray[] = $this->_language->get('extra_empty'); |
190
|
|
|
} |
191
|
|
|
return $validateArray; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* create the extra |
196
|
|
|
* |
197
|
|
|
* @since 4.0.0 |
198
|
|
|
* |
199
|
|
|
* @param array $createArray array of the create |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
|
204
|
|
|
protected function _create(array $createArray = []) : bool |
205
|
|
|
{ |
206
|
|
|
$extraModel = new Admin\Model\Extra(); |
207
|
|
|
return $extraModel->createByArray($createArray); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* update the extra |
212
|
|
|
* |
213
|
|
|
* @since 4.0.0 |
214
|
|
|
* |
215
|
|
|
* @param int $extraId identifier of the extra |
216
|
|
|
* @param array $updateArray array of the update |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
|
221
|
|
|
protected function _update(int $extraId = null, array $updateArray = []) : bool |
222
|
|
|
{ |
223
|
|
|
$extraModel = new Admin\Model\Extra(); |
224
|
|
|
return $extraModel->updateByIdAndArray($extraId, $updateArray); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* get success route |
229
|
|
|
* |
230
|
|
|
* @since 4.0.0 |
231
|
|
|
* |
232
|
|
|
* @param array $postArray array of the post |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
|
237
|
|
|
protected function _getSuccessRoute(array $postArray = []) : string |
238
|
|
|
{ |
239
|
|
|
if ($this->_registry->get('extrasEdit') && $postArray['id']) |
240
|
|
|
{ |
241
|
|
|
return 'admin/view/extras#row-' . $postArray['id']; |
242
|
|
|
} |
243
|
|
|
if ($this->_registry->get('extrasEdit') && $postArray['alias']) |
244
|
|
|
{ |
245
|
|
|
$extraModel = new Admin\Model\Extra(); |
246
|
|
|
return 'admin/view/extras#row-' . $extraModel->getByAlias($postArray['alias'])->id; |
247
|
|
|
} |
248
|
|
|
return 'admin'; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* get error route |
253
|
|
|
* |
254
|
|
|
* @since 4.0.0 |
255
|
|
|
* |
256
|
|
|
* @param array $postArray array of the post |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
|
261
|
|
|
protected function _getErrorRoute(array $postArray = []) : string |
262
|
|
|
{ |
263
|
|
|
if ($this->_registry->get('extrasEdit') && $postArray['id']) |
264
|
|
|
{ |
265
|
|
|
return 'admin/edit/extras/' . $postArray['id']; |
266
|
|
|
} |
267
|
|
|
if ($this->_registry->get('extrasNew')) |
268
|
|
|
{ |
269
|
|
|
return 'admin/new/extras'; |
270
|
|
|
} |
271
|
|
|
return 'admin'; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
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.