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

Extra::_getErrorRoute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
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
		$specialFilter = new Filter\Special();
127
		$aliasFilter = new Filter\Alias();
128
		$htmlFilter = new Filter\Html();
129
130
		/* sanitize post */
131
132
		return
133
		[
134
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
135
			'title' => $this->_request->getPost('title'),
136
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
137
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
138
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
139
			'sibling' => $specialFilter->sanitize($this->_request->getPost('sibling')),
140
			'category' => $specialFilter->sanitize($this->_request->getPost('category')),
141
			'article' => $specialFilter->sanitize($this->_request->getPost('article')),
142
			'headline' => $specialFilter->sanitize($this->_request->getPost('headline')),
143
			'status' => $specialFilter->sanitize($this->_request->getPost('status')),
144
			'rank' => $specialFilter->sanitize($this->_request->getPost('rank')),
145
			'access' => json_encode($this->_request->getPost('access')),
146
			'date' => strtotime($this->_request->getPost('date'))
147
		];
148
	}
149
150
	/**
151
	 * validate the post
152
	 *
153
	 * @since 4.0.0
154
	 *
155
	 * @param array $postArray array of the post
156
	 *
157
	 * @return array
158
	 */
159
160
	protected function _validatePost(array $postArray = []) : array
161
	{
162
		$aliasValidator = new Validator\Alias();
163
		$extraModel = new Admin\Model\Extra();
164
		$validateArray = [];
165
166
		/* validate post */
167
168
		if (!$postArray['title'])
169
		{
170
			$validateArray[] = $this->_language->get('title_empty');
171
		}
172
		if (!$postArray['alias'])
173
		{
174
			$validateArray[] = $this->_language->get('alias_empty');
175
		}
176
		else if ($aliasValidator->validate($postArray['alias'], 'general') || $aliasValidator->validate($postArray['alias'], 'system'))
177
		{
178
			$validateArray[] = $this->_language->get('alias_incorrect');
179
		}
180
		else if ($extraModel->getByAlias($postArray['alias'])->id !== $extraModel->getById($postArray['id'])->id)
181
		{
182
			$validateArray[] = $this->_language->get('alias_exists');
183
		}
184
		if (!$postArray['text'])
185
		{
186
			$validateArray[] = $this->_language->get('text_empty');
187
		}
188
		return $validateArray;
189
	}
190
191
	/**
192
	 * create the extra
193
	 *
194
	 * @since 4.0.0
195
	 *
196
	 * @param array $createArray array of the create
197
	 *
198
	 * @return bool
199
	 */
200
201
	protected function _create(array $createArray = []) : bool
202
	{
203
		$extraModel = new Admin\Model\Extra();
204
		return $extraModel->createByArray($createArray);
205
	}
206
207
	/**
208
	 * update the extra
209
	 *
210
	 * @since 4.0.0
211
	 *
212
	 * @param int $extraId identifier of the extra
213
	 * @param array $updateArray array of the update
214
	 *
215
	 * @return bool
216
	 */
217
218
	public function _update(int $extraId = null, array $updateArray = []) : bool
219
	{
220
		$extraModel = new Admin\Model\Extra();
221
		return $extraModel->updateByIdAndArray($extraId, $updateArray);
222
	}
223
224
	/**
225
	 * get success route
226
	 *
227
	 * @since 4.0.0
228
	 *
229
	 * @param array $postArray array of the post
230
	 *
231
	 * @return string
232
	 */
233
234
	protected function _getSuccessRoute(array $postArray = []) : string
235
	{
236
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
237
		{
238
			return 'admin/view/extras#row-' . $postArray['id'];
239
		}
240
		if ($this->_registry->get('extrasEdit') && $postArray['alias'])
241
		{
242
			$extraModel = new Admin\Model\Extra();
243
			return 'admin/view/extras#row-' . $extraModel->getByAlias($postArray['alias'])->id;
244
		}
245
		return 'admin';
246
	}
247
248
	/**
249
	 * get error route
250
	 *
251
	 * @since 4.0.0
252
	 *
253
	 * @param array $postArray array of the post
254
	 *
255
	 * @return string
256
	 */
257
258
	protected function _getErrorRoute(array $postArray = []) : string
259
	{
260
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
261
		{
262
			return 'admin/edit/extras/' . $postArray['id'];
263
		}
264
		if ($this->_registry->get('extrasNew'))
265
		{
266
			return 'admin/new/extras';
267
		}
268
		return 'admin';
269
	}
270
}
271