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
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* children class to process the admin module request |
11
|
|
|
* |
12
|
|
|
* @since 4.0.0 |
13
|
|
|
* |
14
|
|
|
* @package Redaxscript |
15
|
|
|
* @category Controller |
16
|
|
|
* @author Henry Ruhs |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class Module extends ControllerAbstract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* process the class |
23
|
|
|
* |
24
|
|
|
* @since 4.0.0 |
25
|
|
|
* |
26
|
|
|
* @param string $action action to process |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
|
31
|
6 |
|
public function process(string $action = null) : string |
32
|
|
|
{ |
33
|
6 |
|
$postArray = $this->_normalizePost($this->_sanitizePost()); |
34
|
6 |
|
$validateArray = $this->_validatePost($postArray); |
|
|
|
|
35
|
|
|
|
36
|
|
|
/* validate post */ |
37
|
|
|
|
38
|
6 |
|
if ($validateArray) |
|
|
|
|
39
|
|
|
{ |
40
|
3 |
|
return $this->_error( |
41
|
|
|
[ |
42
|
3 |
|
'route' => $this->_getErrorRoute($postArray), |
|
|
|
|
43
|
3 |
|
'message' => $validateArray |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* handle update */ |
48
|
|
|
|
49
|
3 |
|
if ($action === 'update') |
50
|
|
|
{ |
51
|
|
|
$updateArray = |
52
|
|
|
[ |
53
|
2 |
|
'name' => $postArray['name'], |
54
|
2 |
|
'description' => $postArray['description'], |
55
|
2 |
|
'status' => $postArray['status'], |
56
|
2 |
|
'access' => $postArray['access'] |
57
|
|
|
]; |
58
|
2 |
|
if ($this->_update($postArray['id'], $updateArray)) |
59
|
|
|
{ |
60
|
2 |
|
return $this->_success( |
61
|
|
|
[ |
62
|
2 |
|
'route' => $this->_getSuccessRoute($postArray), |
63
|
2 |
|
'timeout' => 2 |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* handle error */ |
69
|
|
|
|
70
|
1 |
|
return $this->_error( |
71
|
|
|
[ |
72
|
1 |
|
'route' => $this->_getErrorRoute($postArray) |
|
|
|
|
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* sanitize the post |
78
|
|
|
* |
79
|
|
|
* @since 4.0.0 |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
|
84
|
6 |
|
protected function _sanitizePost() : array |
85
|
|
|
{ |
86
|
6 |
|
$numberFilter = new Filter\Number(); |
87
|
6 |
|
$textFilter = new Filter\Text(); |
88
|
6 |
|
$toggleFilter = new Filter\Toggle(); |
89
|
|
|
|
90
|
|
|
/* sanitize post */ |
91
|
|
|
|
92
|
|
|
return |
93
|
|
|
[ |
94
|
6 |
|
'id' => $numberFilter->sanitize($this->_request->getPost('id')), |
|
|
|
|
95
|
6 |
|
'name' => $textFilter->sanitize($this->_request->getPost('name')), |
|
|
|
|
96
|
6 |
|
'description' => $textFilter->sanitize($this->_request->getPost('description')), |
|
|
|
|
97
|
6 |
|
'status' => $toggleFilter->sanitize($this->_request->getPost('status')), |
|
|
|
|
98
|
6 |
|
'access' => json_encode($this->_request->getPost('access')) |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* validate the post |
104
|
|
|
* |
105
|
|
|
* @since 4.0.0 |
106
|
|
|
* |
107
|
|
|
* @param array $postArray array of the post |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
|
112
|
6 |
|
protected function _validatePost(array $postArray = []) : array |
113
|
|
|
{ |
114
|
6 |
|
$nameValidator = new Validator\Name(); |
115
|
6 |
|
$validateArray = []; |
116
|
|
|
|
117
|
|
|
/* validate post */ |
118
|
|
|
|
119
|
6 |
|
if (!$postArray['name']) |
120
|
|
|
{ |
121
|
3 |
|
$validateArray[] = $this->_language->get('name_empty'); |
122
|
|
|
} |
123
|
3 |
|
else if (!$nameValidator->validate($postArray['name'])) |
124
|
|
|
{ |
125
|
|
|
$validateArray[] = $this->_language->get('name_incorrect'); |
126
|
|
|
} |
127
|
6 |
|
return $validateArray; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* update the module |
132
|
|
|
* |
133
|
|
|
* @since 4.0.0 |
134
|
|
|
* |
135
|
|
|
* @param int $moduleId identifier of the module |
136
|
|
|
* @param array $updateArray array of the update |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
|
141
|
2 |
|
protected function _update(int $moduleId = null, array $updateArray = []) : bool |
142
|
|
|
{ |
143
|
2 |
|
$moduleModel = new Admin\Model\Module(); |
144
|
2 |
|
return $moduleModel->updateByIdAndArray($moduleId, $updateArray); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* get success route |
149
|
|
|
* |
150
|
|
|
* @since 4.0.0 |
151
|
|
|
* |
152
|
|
|
* @param array $postArray array of the post |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
|
157
|
2 |
|
protected function _getSuccessRoute(array $postArray = []) : string |
158
|
|
|
{ |
159
|
2 |
|
if ($this->_registry->get('modulesEdit')) |
160
|
|
|
{ |
161
|
1 |
|
if ($postArray['id']) |
162
|
|
|
{ |
163
|
1 |
|
return 'admin/view/modules#row-' . $postArray['id']; |
164
|
|
|
} |
165
|
|
|
return 'admin/view/modules'; |
166
|
|
|
} |
167
|
1 |
|
return 'admin'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* get error route |
172
|
|
|
* |
173
|
|
|
* @since 4.0.0 |
174
|
|
|
* |
175
|
|
|
* @param array $postArray array of the post |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
|
180
|
4 |
|
protected function _getErrorRoute(array $postArray = []) : string |
181
|
|
|
{ |
182
|
4 |
|
if ($this->_registry->get('modulesEdit')) |
183
|
|
|
{ |
184
|
3 |
|
if ($postArray['id']) |
185
|
|
|
{ |
186
|
2 |
|
return 'admin/edit/modules/' . $postArray['id']; |
187
|
|
|
} |
188
|
1 |
|
return 'admin/view/modules'; |
189
|
|
|
} |
190
|
1 |
|
return 'admin'; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
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.