Completed
Push — master ( ba8c52...47b68a )
by Henry
07:01
created

Category::_getSuccessRoute()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.5222
c 0
b 0
f 0
cc 5
nc 3
nop 1
crap 30
1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
use Redaxscript\Validator;
7
8
/**
9
 * children class to process the admin category request
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Controller
15
 * @author Henry Ruhs
16
 */
17
18
class Category extends ControllerAbstract
19
{
20
	/**
21
	 * process the class
22
	 *
23
	 * @since 4.0.0
24
	 *
25
	 * @param string $action action to process
26
	 *
27
	 * @return string
28
	 */
29
30
	public function process(string $action = null) : string
31
	{
32
		$postArray = $this->_normalizePost($this->_sanitizePost());
33
		$validateArray = $this->_validatePost($postArray);
34
		$myUser = $this->_registry->get('myUser');
35
		$now = $this->_registry->get('now');
36
37
		/* validate post */
38
39
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
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...
40
		{
41
			return $this->_error(
42
			[
43
				'route' => $this->_getErrorRoute($postArray),
44
				'message' => $validateArray
45
			]);
46
		}
47
48
		/* handle create */
49
50
		if ($action === 'create')
51
		{
52
			$createArray =
53
			[
54
				'title' => $postArray['title'],
55
				'alias' => $postArray['alias'],
56
				'author' => $myUser,
57
				'description' => $postArray['description'],
58
				'keywords' => $postArray['keywords'],
59
				'robots' => $postArray['robots'],
60
				'language' => $postArray['language'],
61
				'template' => $postArray['template'],
62
				'sibling' => $postArray['sibling'],
63
				'parent' => $postArray['parent'],
64
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
65
				'rank' => $postArray['rank'],
66
				'access' => $postArray['access'],
67
				'date' => $postArray['date'] ? $postArray['date'] : $now
68
			];
69
			if ($this->_create($createArray))
70
			{
71
				return $this->_success(
72
				[
73
					'route' => $this->_getSuccessRoute($postArray),
74
					'timeout' => 2
75
				]);
76
			}
77
		}
78
79
		/* handle update */
80
81
		if ($action === 'update')
82
		{
83
			$updateArray =
84
			[
85
				'title' => $postArray['title'],
86
				'alias' => $postArray['alias'],
87
				'author' => $myUser,
88
				'description' => $postArray['description'],
89
				'keywords' => $postArray['keywords'],
90
				'robots' => $postArray['robots'],
91
				'language' => $postArray['language'],
92
				'template' => $postArray['template'],
93
				'sibling' => $postArray['sibling'],
94
				'parent' => $postArray['parent'],
95
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
96
				'rank' => $postArray['rank'],
97
				'access' => $postArray['access'],
98
				'date' => $postArray['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
		$specialFilter = new Filter\Special();
129
		$aliasFilter = new Filter\Alias();
130
131
		/* sanitize post */
132
133
		return
134
		[
135
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
136
			'title' => $this->_request->getPost('title'),
137
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
138
			'description' => $this->_request->getPost('description'),
139
			'keywords' => $this->_request->getPost('keywords'),
140
			'robots' => $specialFilter->sanitize($this->_request->getPost('robots')),
141
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
142
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
143
			'sibling' => $specialFilter->sanitize($this->_request->getPost('sibling')),
144
			'parent' => $specialFilter->sanitize($this->_request->getPost('parent')),
145
			'status' => $specialFilter->sanitize($this->_request->getPost('status')),
146
			'rank' => $specialFilter->sanitize($this->_request->getPost('rank')),
147
			'access' => json_encode($this->_request->getPost('access')),
148
			'date' => strtotime($this->_request->getPost('date'))
149
		];
150
	}
151
152
	/**
153
	 * validate the post
154
	 *
155
	 * @since 4.0.0
156
	 *
157
	 * @param array $postArray array of the post
158
	 *
159
	 * @return array
160
	 */
161
162
	protected function _validatePost(array $postArray = []) : array
163
	{
164
		$aliasValidator = new Validator\Alias();
165
		$categoryModel = new Admin\Model\Category();
166
		$articleModel = new Admin\Model\Article();
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 ($articleModel->getByAlias($postArray['alias'])->id || $categoryModel->getByAlias($postArray['alias'])->id !== $categoryModel->getById($postArray['id'])->id)
184
		{
185
			$validateArray[] = $this->_language->get('alias_exists');
186
		}
187
		return $validateArray;
188
	}
189
190
	/**
191
	 * create the category
192
	 *
193
	 * @since 4.0.0
194
	 *
195
	 * @param array $createArray array of the create
196
	 *
197
	 * @return bool
198
	 */
199
200
	protected function _create(array $createArray = []) : bool
201
	{
202
		$categoryModel = new Admin\Model\Category();
203
		return $categoryModel->createByArray($createArray);
204
	}
205
206
	/**
207
	 * update the category
208
	 *
209
	 * @since 4.0.0
210
	 *
211
	 * @param int $categoryId identifier of the category
212
	 * @param array $updateArray array of the update
213
	 *
214
	 * @return bool
215
	 */
216
217
	public function _update(int $categoryId = null, array $updateArray = []) : bool
218
	{
219
		$categoryModel = new Admin\Model\Category();
220
		return $categoryModel->updateByIdAndArray($categoryId, $updateArray);
221
	}
222
223
	/**
224
	 * get success route
225
	 *
226
	 * @since 4.0.0
227
	 *
228
	 * @param array $postArray array of the post
229
	 *
230
	 * @return string
231
	 */
232
233
	protected function _getSuccessRoute(array $postArray = []) : string
234
	{
235
		if ($this->_registry->get('categoriesEdit') && $postArray['id'])
236
		{
237
			return 'admin/view/categories#row-' . $postArray['id'];
238
		}
239
		if ($this->_registry->get('categoriesEdit') && $postArray['alias'])
240
		{
241
			$categoryModel = new Admin\Model\Category();
242
			return 'admin/view/categories#row-' . $categoryModel->getByAlias($postArray['alias'])->id;
243
		}
244
		return 'admin';
245
	}
246
247
	/**
248
	 * get error route
249
	 *
250
	 * @since 4.0.0
251
	 *
252
	 * @param array $postArray array of the post
253
	 *
254
	 * @return string
255
	 */
256
257
	protected function _getErrorRoute(array $postArray = []) : string
258
	{
259
		if ($this->_registry->get('categoriesEdit') && $postArray['id'])
260
		{
261
			return 'admin/edit/categories/' . $postArray['id'];
262
		}
263
		if ($this->_registry->get('categoriesNew'))
264
		{
265
			return 'admin/new/categories';
266
		}
267
		return 'admin';
268
	}
269
}
270