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

includes/Admin/Controller/Extra.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 extra request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class Extra 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 10
	public function process(string $action = null) : string
33
	{
34 10
		$postArray = $this->_normalizePost($this->_sanitizePost());
35 10
		$validateArray = $this->_validatePost($postArray);
36 10
		$myName = $this->_registry->get('myName');
37 10
		$now = $this->_registry->get('now');
38
39
		/* validate post */
40
41 10
		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...
42
		{
43 6
			return $this->_error(
44
			[
45 6
				'route' => $this->_getErrorRoute($postArray),
46 6
				'message' => $validateArray
47
			]);
48
		}
49
50
		/* handle create */
51
52 4
		if ($action === 'create')
53
		{
54
			$createArray =
55
			[
56 1
				'title' => $postArray['title'],
57 1
				'alias' => $postArray['alias'],
58 1
				'author' => $myName,
59 1
				'text' => $postArray['text'],
60 1
				'language' => $postArray['language'],
61 1
				'sibling' => $postArray['sibling'],
62 1
				'category' => $postArray['category'],
63 1
				'article' => $postArray['article'],
64 1
				'headline' => $postArray['headline'],
65 1
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
66 1
				'rank' => $postArray['rank'],
67 1
				'access' => $postArray['access'],
68 1
				'date' => $postArray['date'] ? : $now
69
			];
70 1
			if ($this->_create($createArray))
71
			{
72 1
				return $this->_success(
73
				[
74 1
					'route' => $this->_getSuccessRoute($postArray),
75 1
					'timeout' => 2
76
				]);
77
			}
78
		}
79
80
		/* handle update */
81
82 3
		if ($action === 'update')
83
		{
84
			$updateArray =
85
			[
86 2
				'title' => $postArray['title'],
87 2
				'alias' => $postArray['alias'],
88 2
				'author' => $myName,
89 2
				'text' => $postArray['text'],
90 2
				'language' => $postArray['language'],
91 2
				'sibling' => $postArray['sibling'],
92 2
				'category' => $postArray['category'],
93 2
				'article' => $postArray['article'],
94 2
				'headline' => $postArray['headline'],
95 2
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
96 2
				'rank' => $postArray['rank'],
97 2
				'access' => $postArray['access'],
98 2
				'date' => $postArray['date'] ? : $now
99
			];
100 2
			if ($this->_update($postArray['id'], $updateArray))
101
			{
102 2
				return $this->_success(
103
				[
104 2
					'route' => $this->_getSuccessRoute($postArray),
105 2
					'timeout' => 2
106
				]);
107
			}
108
		}
109
110
		/* handle error */
111
112 1
		return $this->_error(
113
		[
114 1
			'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 10
	protected function _sanitizePost() : array
127
	{
128 10
		$aliasFilter = new Filter\Alias();
129 10
		$htmlFilter = new Filter\Html();
130 10
		$numberFilter = new Filter\Number();
131 10
		$specialFilter = new Filter\Special();
132 10
		$textFilter= new Filter\Text();
133 10
		$toggleFilter = new Filter\Toggle();
134
135
		/* sanitize post */
136
137
		return
138
		[
139 10
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
140 10
			'title' => $textFilter->sanitize($this->_request->getPost('title')),
141 10
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
142 10
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
143 10
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
144 10
			'sibling' => $numberFilter->sanitize($this->_request->getPost('sibling')),
145 10
			'category' => $numberFilter->sanitize($this->_request->getPost('category')),
146 10
			'article' => $numberFilter->sanitize($this->_request->getPost('article')),
147 10
			'headline' => $toggleFilter->sanitize($this->_request->getPost('headline')),
148 10
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
149 10
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
150 10
			'access' => json_encode($this->_request->getPost('access')),
151 10
			'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 10
	protected function _validatePost(array $postArray = []) : array
166
	{
167 10
		$nameValidator = new Validator\Name();
168 10
		$aliasValidator = new Validator\Alias();
169 10
		$extraModel = new Admin\Model\Extra();
170 10
		$validateArray = [];
171
172
		/* validate post */
173
174 10
		if (!$postArray['title'])
175
		{
176 5
			$validateArray[] = $this->_language->get('title_empty');
177
		}
178 5
		else if (!$nameValidator->validate($postArray['title']))
179
		{
180 1
			$validateArray[] = $this->_language->get('title_incorrect');
181
		}
182 10
		if (!$postArray['alias'])
183
		{
184 4
			$validateArray[] = $this->_language->get('alias_empty');
185
		}
186 6
		else if (!$aliasValidator->validate($postArray['alias']))
187
		{
188 1
			$validateArray[] = $this->_language->get('alias_incorrect');
189
		}
190 5
		else if (!$extraModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
191
		{
192 1
			$validateArray[] = $this->_language->get('alias_exists');
193
		}
194 10
		if (!$postArray['text'])
195
		{
196 6
			$validateArray[] = $this->_language->get('extra_empty');
197
		}
198 10
		return $validateArray;
199
	}
200
201
	/**
202
	 * create the extra
203
	 *
204
	 * @since 4.0.0
205
	 *
206
	 * @param array $createArray array of the create
207
	 *
208
	 * @return bool
209
	 */
210
211 1
	protected function _create(array $createArray = []) : bool
212
	{
213 1
		$extraModel = new Admin\Model\Extra();
214 1
		return $extraModel->createByArray($createArray);
215
	}
216
217
	/**
218
	 * update the extra
219
	 *
220
	 * @since 4.0.0
221
	 *
222
	 * @param int $extraId identifier of the extra
223
	 * @param array $updateArray array of the update
224
	 *
225
	 * @return bool
226
	 */
227
228 2
	protected function _update(int $extraId = null, array $updateArray = []) : bool
229
	{
230 2
		$extraModel = new Admin\Model\Extra();
231 2
		return $extraModel->updateByIdAndArray($extraId, $updateArray);
232
	}
233
234
	/**
235
	 * get success route
236
	 *
237
	 * @since 4.0.0
238
	 *
239
	 * @param array $postArray array of the post
240
	 *
241
	 * @return string
242
	 */
243
244 3
	protected function _getSuccessRoute(array $postArray = []) : string
245
	{
246 3
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
247
		{
248 1
			return 'admin/view/extras#row-' . $postArray['id'];
249
		}
250 2
		if ($this->_registry->get('extrasEdit') && $postArray['alias'])
251
		{
252 1
			$extraModel = new Admin\Model\Extra();
253 1
			$extraId = $extraModel->getByAlias($postArray['alias'])->id;
254 1
			if ($extraId)
255
			{
256 1
				return 'admin/view/extras#row-' . $extraId;
257
			}
258
			return 'admin/view/extras';
259
		}
260 1
		return 'admin';
261
	}
262
263
	/**
264
	 * get error route
265
	 *
266
	 * @since 4.0.0
267
	 *
268
	 * @param array $postArray array of the post
269
	 *
270
	 * @return string
271
	 */
272
273 7
	protected function _getErrorRoute(array $postArray = []) : string
274
	{
275 7
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
276
		{
277 2
			return 'admin/edit/extras/' . $postArray['id'];
278
		}
279 5
		if ($this->_registry->get('extrasNew'))
280
		{
281 4
			return 'admin/new/extras';
282
		}
283 1
		return 'admin';
284
	}
285
}
286