Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

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

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
use function strtotime;
9
10
/**
11
 * children class to process the admin category request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class Category 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)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
				'description' => $postArray['description'],
60
				'keywords' => $postArray['keywords'],
61
				'robots' => $postArray['robots'],
62
				'language' => $postArray['language'],
63
				'template' => $postArray['template'],
64
				'sibling' => $postArray['sibling'],
65
				'parent' => $postArray['parent'],
66
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
67
				'rank' => $postArray['rank'],
68
				'access' => $postArray['access'],
69
				'date' => $postArray['date'] ? : $now
70
			];
71
			if ($this->_create($createArray))
72
			{
73
				return $this->_success(
74
				[
75
					'route' => $this->_getSuccessRoute($postArray),
76
					'timeout' => 2
77
				]);
78
			}
79
		}
80
81
		/* handle update */
82
83
		if ($action === 'update')
84
		{
85
			$updateArray =
86
			[
87
				'title' => $postArray['title'],
88
				'alias' => $postArray['alias'],
89
				'author' => $myUser,
90
				'description' => $postArray['description'],
91
				'keywords' => $postArray['keywords'],
92
				'robots' => $postArray['robots'],
93
				'language' => $postArray['language'],
94
				'template' => $postArray['template'],
95
				'sibling' => $postArray['sibling'],
96
				'parent' => $postArray['parent'],
97
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
98
				'rank' => $postArray['rank'],
99
				'access' => $postArray['access'],
100
				'date' => $postArray['date'] ? : $now
101
			];
102
			if ($this->_update($postArray['id'], $updateArray))
103
			{
104
				return $this->_success(
105
				[
106
					'route' => $this->_getSuccessRoute($postArray),
107
					'timeout' => 2
108
				]);
109
			}
110
		}
111
112
		/* handle error */
113
114
		return $this->_error(
115
		[
116
			'route' => $this->_getErrorRoute($postArray)
117
		]);
118
	}
119
120
	/**
121
	 * sanitize the post
122
	 *
123
	 * @since 4.0.0
124
	 *
125
	 * @return array
126
	 */
127
128
	protected function _sanitizePost() : array
129
	{
130
		$numberFilter = new Filter\Number();
131
		$specialFilter = new Filter\Special();
132
		$aliasFilter = new Filter\Alias();
133
134
		/* sanitize post */
135
136
		return
137
		[
138
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
139
			'title' => $this->_request->getPost('title'),
140
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
141
			'description' => $this->_request->getPost('description'),
142
			'keywords' => $this->_request->getPost('keywords'),
143
			'robots' => $this->_request->getPost('robots'),
144
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
145
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
146
			'sibling' => $this->_request->getPost('sibling'),
147
			'parent' => $this->_request->getPost('parent'),
148
			'status' => $numberFilter->sanitize($this->_request->getPost('status')),
149
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
150
			'access' => json_encode($this->_request->getPost('access')),
151
			'date' => strtotime($this->_request->getPost('date'))
152
		];
153
	}
154
155
	/**
156
	 * validate the post
157
	 *
158
	 * @since 4.0.0
159
	 *
160
	 * @param array $postArray array of the post
161
	 *
162
	 * @return array
163
	 */
164
165
	protected function _validatePost(array $postArray = []) : array
166
	{
167
		$aliasValidator = new Validator\Alias();
168
		$categoryModel = new Admin\Model\Category();
169
		$validateArray = [];
170
171
		/* validate post */
172
173
		if (!$postArray['title'])
174
		{
175
			$validateArray[] = $this->_language->get('title_empty');
176
		}
177
		if (!$postArray['alias'])
178
		{
179
			$validateArray[] = $this->_language->get('alias_empty');
180
		}
181
		else if ($aliasValidator->validate($postArray['alias'], 'general') || $aliasValidator->validate($postArray['alias'], 'system'))
182
		{
183
			$validateArray[] = $this->_language->get('alias_incorrect');
184
		}
185
		else if (!$categoryModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
186
		{
187
			$validateArray[] = $this->_language->get('alias_exists');
188
		}
189
		return $validateArray;
190
	}
191
192
	/**
193
	 * create the category
194
	 *
195
	 * @since 4.0.0
196
	 *
197
	 * @param array $createArray array of the create
198
	 *
199
	 * @return bool
200
	 */
201
202
	protected function _create(array $createArray = []) : bool
203
	{
204
		$categoryModel = new Admin\Model\Category();
205
		return $categoryModel->createByArray($createArray);
206
	}
207
208
	/**
209
	 * update the category
210
	 *
211
	 * @since 4.0.0
212
	 *
213
	 * @param int $categoryId identifier of the category
214
	 * @param array $updateArray array of the update
215
	 *
216
	 * @return bool
217
	 */
218
219
	protected function _update(int $categoryId = null, array $updateArray = []) : bool
220
	{
221
		$categoryModel = new Admin\Model\Category();
222
		return $categoryModel->updateByIdAndArray($categoryId, $updateArray);
223
	}
224
225
	/**
226
	 * get success route
227
	 *
228
	 * @since 4.0.0
229
	 *
230
	 * @param array $postArray array of the post
231
	 *
232
	 * @return string
233
	 */
234
235
	protected function _getSuccessRoute(array $postArray = []) : string
236
	{
237
		if ($this->_registry->get('categoriesEdit') && $postArray['id'])
238
		{
239
			return 'admin/view/categories#row-' . $postArray['id'];
240
		}
241
		if ($this->_registry->get('categoriesEdit') && $postArray['alias'])
242
		{
243
			$categoryModel = new Admin\Model\Category();
244
			return 'admin/view/categories#row-' . $categoryModel->getByAlias($postArray['alias'])->id;
245
		}
246
		return 'admin';
247
	}
248
249
	/**
250
	 * get error route
251
	 *
252
	 * @since 4.0.0
253
	 *
254
	 * @param array $postArray array of the post
255
	 *
256
	 * @return string
257
	 */
258
259
	protected function _getErrorRoute(array $postArray = []) : string
260
	{
261
		if ($this->_registry->get('categoriesEdit') && $postArray['id'])
262
		{
263
			return 'admin/edit/categories/' . $postArray['id'];
264
		}
265
		if ($this->_registry->get('categoriesNew'))
266
		{
267
			return 'admin/new/categories';
268
		}
269
		return 'admin';
270
	}
271
}
272