Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

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

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

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
8
/**
9
 * children class to process the admin extra request
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Controller
15
 * @author Henry Ruhs
16
 */
17
18
class Extra 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
				'text' => $postArray['text'],
58
				'language' => $postArray['language'],
59
				'sibling' => $postArray['sibling'],
60
				'category' => $postArray['category'],
61
				'article' => $postArray['article'],
62
				'headline' => $postArray['headline'],
63
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
64
				'rank' => $postArray['rank'],
65
				'access' => $postArray['access'],
66
				'date' => $postArray['date'] ? $postArray['date'] : $now
67
			];
68
			if ($this->_create($createArray))
69
			{
70
				return $this->_success(
71
				[
72
					'route' => $this->_getSuccessRoute($postArray),
73
					'timeout' => 2
74
				]);
75
			}
76
		}
77
78
		/* handle update */
79
80
		if ($action === 'update')
81
		{
82
			$updateArray =
83
			[
84
				'title' => $postArray['title'],
85
				'alias' => $postArray['alias'],
86
				'author' => $myUser,
87
				'text' => $postArray['text'],
88
				'language' => $postArray['language'],
89
				'sibling' => $postArray['sibling'],
90
				'category' => $postArray['category'],
91
				'article' => $postArray['article'],
92
				'headline' => $postArray['headline'],
93
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
94
				'rank' => $postArray['rank'],
95
				'access' => $postArray['access'],
96
				'date' => $postArray['date'] ? $postArray['date'] : $now
97
			];
98
			if ($this->_update($postArray['id'], $updateArray))
99
			{
100
				return $this->_success(
101
				[
102
					'route' => $this->_getSuccessRoute($postArray),
103
					'timeout' => 2
104
				]);
105
			}
106
		}
107
108
		/* handle error */
109
110
		return $this->_error(
111
		[
112
			'route' => $this->_getErrorRoute($postArray)
113
		]);
114
	}
115
116
	/**
117
	 * sanitize the post
118
	 *
119
	 * @since 4.0.0
120
	 *
121
	 * @return array
122
	 */
123
124
	protected function _sanitizePost() : array
125
	{
126
		$numberFilter = new Filter\Number();
127
		$specialFilter = new Filter\Special();
128
		$aliasFilter = new Filter\Alias();
129
		$htmlFilter = new Filter\Html();
130
131
		/* sanitize post */
132
133
		return
134
		[
135
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
136
			'title' => $this->_request->getPost('title'),
137
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
138
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
139
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
140
			'sibling' => $this->_request->getPost('sibling'),
141
			'category' => $this->_request->getPost('category'),
142
			'article' => $this->_request->getPost('article'),
143
			'headline' => $numberFilter->sanitize($this->_request->getPost('headline')),
144
			'status' => $numberFilter->sanitize($this->_request->getPost('status')),
145
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
146
			'access' => json_encode($this->_request->getPost('access')),
147
			'date' => strtotime($this->_request->getPost('date'))
148
		];
149
	}
150
151
	/**
152
	 * validate the post
153
	 *
154
	 * @since 4.0.0
155
	 *
156
	 * @param array $postArray array of the post
157
	 *
158
	 * @return array
159
	 */
160
161
	protected function _validatePost(array $postArray = []) : array
162
	{
163
		$aliasValidator = new Validator\Alias();
164
		$extraModel = new Admin\Model\Extra();
165
		$validateArray = [];
166
167
		/* validate post */
168
169
		if (!$postArray['title'])
170
		{
171
			$validateArray[] = $this->_language->get('title_empty');
172
		}
173
		if (!$postArray['alias'])
174
		{
175
			$validateArray[] = $this->_language->get('alias_empty');
176
		}
177
		else if ($aliasValidator->validate($postArray['alias'], 'general') || $aliasValidator->validate($postArray['alias'], 'system'))
178
		{
179
			$validateArray[] = $this->_language->get('alias_incorrect');
180
		}
181
		else if (!$extraModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
182
		{
183
			$validateArray[] = $this->_language->get('alias_exists');
184
		}
185
		if (!$postArray['text'])
186
		{
187
			$validateArray[] = $this->_language->get('extra_empty');
188
		}
189
		return $validateArray;
190
	}
191
192
	/**
193
	 * create the extra
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
		$extraModel = new Admin\Model\Extra();
205
		return $extraModel->createByArray($createArray);
206
	}
207
208
	/**
209
	 * update the extra
210
	 *
211
	 * @since 4.0.0
212
	 *
213
	 * @param int $extraId identifier of the extra
214
	 * @param array $updateArray array of the update
215
	 *
216
	 * @return bool
217
	 */
218
219
	public function _update(int $extraId = null, array $updateArray = []) : bool
220
	{
221
		$extraModel = new Admin\Model\Extra();
222
		return $extraModel->updateByIdAndArray($extraId, $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('extrasEdit') && $postArray['id'])
238
		{
239
			return 'admin/view/extras#row-' . $postArray['id'];
240
		}
241
		if ($this->_registry->get('extrasEdit') && $postArray['alias'])
242
		{
243
			$extraModel = new Admin\Model\Extra();
244
			return 'admin/view/extras#row-' . $extraModel->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('extrasEdit') && $postArray['id'])
262
		{
263
			return 'admin/edit/extras/' . $postArray['id'];
264
		}
265
		if ($this->_registry->get('extrasNew'))
266
		{
267
			return 'admin/new/extras';
268
		}
269
		return 'admin';
270
	}
271
}
272