Completed
Push — master ( a6625c...029209 )
by Henry
08:49
created

includes/Admin/Controller/Group.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 group request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class Group 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 10
	public function process(string $action = null) : string
32
	{
33 10
		$postArray = $this->_normalizePost($this->_sanitizePost());
34 10
		$validateArray = $this->_validatePost($postArray);
35
36
		/* validate post */
37
38 10
		if ($validateArray)
39
		{
40 6
			return $this->_error(
41
			[
42 6
				'route' => $this->_getErrorRoute($postArray),
43 6
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle create */
48
49 4
		if ($action === 'create')
50
		{
51
			$createArray =
52
			[
53 1
				'name' => $postArray['name'],
54 1
				'alias' => $postArray['alias'],
55 1
				'description' => $postArray['description'],
56 1
				'categories' => $postArray['categories'],
57 1
				'articles' => $postArray['articles'],
58 1
				'extras' => $postArray['extras'],
59 1
				'comments' => $postArray['comments'],
60 1
				'groups' => $postArray['groups'],
61 1
				'users' => $postArray['users'],
62 1
				'modules' => $postArray['modules'],
63 1
				'settings' => $postArray['settings'],
64 1
				'filter' => $postArray['filter'],
65 1
				'status' => $postArray['status']
66
			];
67 1
			if ($this->_create($createArray))
68
			{
69 1
				return $this->_success(
70
				[
71 1
					'route' => $this->_getSuccessRoute($postArray),
72 1
					'timeout' => 2
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79 3
		if ($action === 'update')
80
		{
81
			$updateFullArray =
82
			[
83 2
				'name' => $postArray['name'],
84 2
				'alias' => $postArray['alias'],
85 2
				'description' => $postArray['description'],
86 2
				'categories' => $postArray['categories'],
87 2
				'articles' => $postArray['articles'],
88 2
				'extras' => $postArray['extras'],
89 2
				'comments' => $postArray['comments'],
90 2
				'groups' => $postArray['groups'],
91 2
				'users' => $postArray['users'],
92 2
				'modules' => $postArray['modules'],
93 2
				'settings' => $postArray['settings'],
94 2
				'filter' => $postArray['filter'],
95 2
				'status' => $postArray['status']
96
			];
97
			$updateLiteArray =
98
			[
99 2
				'name' => $postArray['name'],
100 2
				'alias' => $postArray['alias'],
101 2
				'description' => $postArray['description']
102
			];
103 2
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
104
			{
105 2
				return $this->_success(
106
				[
107 2
					'route' => $this->_getSuccessRoute($postArray),
108 2
					'timeout' => 2
109
				]);
110
			}
111
		}
112
113
		/* handle error */
114
115 1
		return $this->_error(
116
		[
117 1
			'route' => $this->_getErrorRoute($postArray)
118
		]);
119
	}
120
121
	/**
122
	 * sanitize the post
123
	 *
124
	 * @since 4.0.0
125
	 *
126
	 * @return array
127
	 */
128
129 10
	protected function _sanitizePost() : array
130
	{
131 10
		$aliasFilter = new Filter\Alias();
132 10
		$numberFilter = new Filter\Number();
133 10
		$textFilter = new Filter\Text();
134 10
		$toggleFilter = new Filter\Toggle();
135
136
		/* sanitize post */
137
138
		return
139
		[
140 10
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
141 10
			'name' => $textFilter->sanitize($this->_request->getPost('name')),
142 10
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
143 10
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
144 10
			'categories' => json_encode($this->_request->getPost('categories')),
145 10
			'articles' => json_encode($this->_request->getPost('articles')),
146 10
			'extras' => json_encode($this->_request->getPost('extras')),
147 10
			'comments' => json_encode($this->_request->getPost('comments')),
148 10
			'groups' => json_encode($this->_request->getPost('groups')),
149 10
			'users' => json_encode($this->_request->getPost('users')),
150 10
			'modules' => json_encode($this->_request->getPost('modules')),
151 10
			'settings' => $numberFilter->sanitize($this->_request->getPost('settings')),
152 10
			'filter' => $toggleFilter->sanitize($this->_request->getPost('filter')),
153 10
			'status' => $toggleFilter->sanitize($this->_request->getPost('status'))
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 10
	protected function _validatePost(array $postArray = []) : array
168
	{
169 10
		$nameValidator = new Validator\Name();
170 10
		$aliasValidator = new Validator\Alias();
171 10
		$groupModel = new Admin\Model\Group();
172 10
		$validateArray = [];
173
174
		/* validate post */
175
176 10
		if (!$postArray['name'])
177
		{
178 4
			$validateArray[] = $this->_language->get('name_empty');
179
		}
180 6
		else if (!$nameValidator->validate($postArray['name']))
181
		{
182 1
			$validateArray[] = $this->_language->get('name_incorrect');
183
		}
184 10
		if (!$postArray['alias'])
185
		{
186 4
			$validateArray[] = $this->_language->get('alias_empty');
187
		}
188 6
		else if (!$aliasValidator->validate($postArray['alias']))
189
		{
190 1
			$validateArray[] = $this->_language->get('alias_incorrect');
191
		}
192 5
		else if (!$groupModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
193
		{
194 1
			$validateArray[] = $this->_language->get('alias_exists');
195
		}
196 10
		return $validateArray;
197
	}
198
199
	/**
200
	 * create the group
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
		$groupModel = new Admin\Model\Group();
212 1
		return $groupModel->createByArray($createArray);
213
	}
214
215
	/**
216
	 * update the group
217
	 *
218
	 * @since 4.0.0
219
	 *
220
	 * @param int $groupId identifier of the group
221
	 * @param array $updateArray array of the update
222
	 *
223
	 * @return bool
224
	 */
225
226 2
	protected function _update(int $groupId = null, array $updateArray = []) : bool
227
	{
228 2
		$groupModel = new Admin\Model\Group();
229 2
		return $groupModel->updateByIdAndArray($groupId, $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('groupsEdit') && $postArray['id'])
245
		{
246 1
			return 'admin/view/groups#row-' . $postArray['id'];
247
		}
248 2
		if ($this->_registry->get('groupsEdit') && $postArray['alias'])
249
		{
250 1
			$groupModel = new Admin\Model\Group();
251 1
			$groupId = $groupModel->getByAlias($postArray['alias'])?->id;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_OBJECT_OPERATOR
Loading history...
252 1
			if ($groupId)
253
			{
254 1
				return 'admin/view/groups#row-' . $groupId;
255
			}
256
			return 'admin/view/groups';
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 7
	protected function _getErrorRoute(array $postArray = []) : string
272
	{
273 7
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
274
		{
275 2
			return 'admin/edit/groups/' . $postArray['id'];
276
		}
277 5
		if ($this->_registry->get('groupsNew'))
278
		{
279 4
			return 'admin/new/groups';
280
		}
281 1
		return 'admin';
282
	}
283
}
284