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