Completed
Push — master ( 9027b3...432a29 )
by Henry
12:53 queued 02:53
created

includes/Admin/View/CategoryForm.php (16 issues)

Labels
Severity

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\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Dater;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use Redaxscript\Validator;
9
use function count;
10
use function json_decode;
11
12
/**
13
 * children class to create the category form
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category View
19
 * @author Henry Ruhs
20
 */
21
22
class CategoryForm extends ViewAbstract
23
{
24
	/**
25
	 * render the view
26
	 *
27
	 * @since 3.0.0
28
	 *
29
	 * @param int $categoryId identifier of the category
30
	 *
31
	 * @return string
32
	 */
33
34 4
	public function render(int $categoryId = null) : string
35
	{
36 4
		$output = Module\Hook::trigger('adminCategoryFormStart');
37 4
		$helperOption = new Helper\Option($this->_language);
38 4
		$categoryModel = new Admin\Model\Category();
39 4
		$category = $categoryModel->getById($categoryId);
40 4
		$nameValidator = new Validator\Name();
41 4
		$aliasValidator = new Validator\Alias();
42 4
		$dater = new Dater();
43 4
		$dater->init($category->date);
44
45
		/* html element */
46
47 4
		$titleElement = new Html\Element();
48
		$titleElement
49 4
			->init('h2',
50
			[
51 4
				'class' => 'rs-admin-title-content',
52
			])
53 4
			->text($category->id ? $category->title : $this->_language->get('category_new'));
54 4
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
55 4
		$formElement->init(
56
		[
57
			'form' =>
58
			[
59 4
				'class' => 'rs-admin-js-validate rs-admin-js-alias rs-admin-fn-tab rs-admin-component-tab rs-admin-form-default rs-admin-form-category'
60
			],
61
			'button' =>
62
			[
63
				'create' =>
64
				[
65
					'name' => self::class
66
				],
67
				'save' =>
68
				[
69
					'name' => self::class
70
				]
71
			],
72
			'link' =>
73
			[
74
				'cancel' =>
75
				[
76 4
					'href' => $this->_registry->get('categoriesEdit') && $this->_registry->get('categoriesDelete') ? $this->_registry->get('parameterRoute') . 'admin/view/categories' : $this->_registry->get('parameterRoute') . 'admin'
77
				],
78
				'delete' =>
79
				[
80 4
					'href' => $category->id ? $this->_registry->get('parameterRoute') . 'admin/delete/categories/' . $category->id . '/' . $this->_registry->get('token') : null
81
				]
82
			]
83
		]);
84
85
		/* create the form */
86
87
		$formElement
88
89
			/* category */
90
91 4
			->radio(
92
			[
93 4
				'id' => self::class . '\Category',
94
				'class' => 'rs-admin-fn-status-tab',
95
				'name' => self::class . '\Tab',
96
				'checked' => 'checked'
97
			])
98 4
			->label($this->_language->get('category'),
0 ignored issues
show
It seems like $this->_language->get('category') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
			[
100 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
101
				'for' => self::class . '\Category'
102
			])
103 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
104 4
			->label($this->_language->get('title'),
0 ignored issues
show
It seems like $this->_language->get('title') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
105
			[
106 4
				'for' => 'title'
107
			])
108 4
			->text(
109
			[
110 4
				'autofocus' => 'autofocus',
111 4
				'class' => 'rs-admin-js-alias-input rs-admin-field-default rs-admin-field-text',
112 4
				'id' => 'title',
113 4
				'name' => 'title',
114 4
				'pattern' => $nameValidator->getPattern(),
115 4
				'required' => 'required',
116 4
				'value' => $category->title
117
			])
118 4
			->append('</li><li>')
119 4
			->label($this->_language->get('alias'),
0 ignored issues
show
It seems like $this->_language->get('alias') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
120
			[
121 4
				'for' => 'alias'
122
			])
123 4
			->text(
124
			[
125 4
				'class' => 'rs-admin-js-alias-output rs-admin-field-default rs-admin-field-text',
126 4
				'id' => 'alias',
127 4
				'name' => 'alias',
128 4
				'pattern' => $aliasValidator->getPattern(),
129 4
				'required' => 'required',
130 4
				'value' => $category->alias
131
			])
132 4
			->append('</li><li>')
133 4
			->label($this->_language->get('description'),
0 ignored issues
show
It seems like $this->_language->get('description') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
134
			[
135 4
				'for' => 'description'
136
			])
137 4
			->textarea(
138
			[
139 4
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
140 4
				'id' => 'description',
141 4
				'name' => 'description',
142 4
				'rows' => 1,
143 4
				'value' => $category->description
144
			])
145 4
			->append('</li><li>')
146 4
			->label($this->_language->get('keywords'),
0 ignored issues
show
It seems like $this->_language->get('keywords') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
			[
148 4
				'for' => 'keywords'
149
			])
150 4
			->textarea(
151
			[
152 4
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
153 4
				'id' => 'keywords',
154 4
				'name' => 'keywords',
155 4
				'rows' => 1,
156 4
				'value' => $category->keywords
157
			])
158 4
			->append('</li><li>')
159 4
			->label($this->_language->get('robots'),
0 ignored issues
show
It seems like $this->_language->get('robots') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
160
			[
161 4
				'for' => 'robots'
162
			])
163 4
			->select($helperOption->getRobotArray(),
164
			[
165 4
				$category->robots
166
			],
167
			[
168 4
				'id' => 'robots',
169
				'name' => 'robots'
170
			])
171 4
			->append('</li></ul>')
172
173
			/* general */
174
175 4
			->radio(
176
			[
177 4
				'id' => self::class . '\General',
178
				'class' => 'rs-admin-fn-status-tab',
179
				'name' => self::class . '\Tab'
180
			])
181 4
			->label($this->_language->get('general'),
0 ignored issues
show
It seems like $this->_language->get('general') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
182
			[
183 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
184
				'for' => self::class . '\General'
185
			])
186 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
187 4
			->label($this->_language->get('language'),
0 ignored issues
show
It seems like $this->_language->get('language') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
188
			[
189 4
				'for' => 'language'
190
			])
191 4
			->select($helperOption->getLanguageArray(),
192
			[
193 4
				$category->language
194
			],
195
			[
196 4
				'id' => 'language',
197
				'name' => 'language'
198
			])
199 4
			->append('</li><li>')
200 4
			->label($this->_language->get('template'),
0 ignored issues
show
It seems like $this->_language->get('template') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
201
			[
202 4
				'for' => 'template'
203
			])
204 4
			->select($helperOption->getTemplateArray(),
205
			[
206 4
				$category->template
207
			],
208
			[
209 4
				'id' => 'template',
210
				'name' => 'template'
211
			])
212 4
			->append('</li><li>')
213 4
			->label($this->_language->get('category_sibling'),
0 ignored issues
show
It seems like $this->_language->get('category_sibling') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
214
			[
215 4
				'for' => 'sibling'
216
			])
217 4
			->select($helperOption->getSiblingForCategoryArray($category->id),
218
			[
219 4
				$category->sibling
220
			],
221
			[
222 4
				'id' => 'sibling',
223
				'name' => 'sibling'
224
			])
225 4
			->append('</li><li>')
226 4
			->label($this->_language->get('category_parent'),
0 ignored issues
show
It seems like $this->_language->get('category_parent') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
227
			[
228 4
				'for' => 'parent'
229
			])
230 4
			->select($helperOption->getParentForCategoryArray($category->id),
231
			[
232 4
				$category->parent
233
			],
234
			[
235 4
				'id' => 'parent',
236
				'name' => 'parent'
237
			])
238 4
			->append('</li></ul>')
239
240
			/* customize */
241
242 4
			->radio(
243
			[
244 4
				'id' => self::class . '\Customize',
245
				'class' => 'rs-admin-fn-status-tab',
246
				'name' => self::class . '\Tab'
247
			])
248 4
			->label($this->_language->get('customize'),
0 ignored issues
show
It seems like $this->_language->get('customize') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
249
			[
250 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
251
				'for' => self::class . '\Customize'
252
			])
253 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
254 4
			->label($this->_language->get('status'),
0 ignored issues
show
It seems like $this->_language->get('status') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
255
			[
256 4
				'for' => 'status'
257
			])
258 4
			->checkbox(!$category->id || $category->status ?
259
			[
260 4
				'id' => 'status',
261
				'class' => 'rs-admin-fn-status-switch',
262
				'name' => 'status',
263
				'checked' => 'checked'
264
			] :
265
			[
266 4
				'id' => 'status',
267
				'class' => 'rs-admin-fn-status-switch',
268
				'name' => 'status'
269
			])
270 4
			->label(null,
271
			[
272 4
				'class' => 'rs-admin-label-switch',
273 4
				'for' => 'status',
274 4
				'data-on' => $this->_language->get('publish'),
275 4
				'data-off' => $this->_language->get('unpublish')
276
			])
277 4
			->append('</li><li>')
278 4
			->label($this->_language->get('rank'),
0 ignored issues
show
It seems like $this->_language->get('rank') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
279
			[
280 4
				'for' => 'rank'
281
			])
282 4
			->number(
283
			[
284 4
				'id' => 'rank',
285 4
				'name' => 'rank',
286 4
				'value' => $category->id ? $category->rank : $categoryModel->query()->max('rank') + 1
287
			])
288 4
			->append('</li>');
289 4
		if ($this->_registry->get('groupsEdit'))
290
		{
291
			$formElement
292 4
				->append('<li>')
293 4
				->label($this->_language->get('access'),
0 ignored issues
show
It seems like $this->_language->get('access') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
294
				[
295 4
					'for' => 'access'
296
				])
297 4
				->select($helperOption->getGroupArray(),
298 4
				(array)json_decode($category->access),
299
				[
300 4
					'id' => 'access',
301 4
					'name' => 'access[]',
302 4
					'multiple' => 'multiple',
303 4
					'size' => count($helperOption->getGroupArray())
304
				])
305 4
				->append('</li>');
306
		}
307
		$formElement
308 4
			->append('<li>')
309 4
			->label($this->_language->get('date'),
0 ignored issues
show
It seems like $this->_language->get('date') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
310
			[
311 4
				'for' => 'date'
312
			])
313 4
			->datetime(
314
			[
315 4
				'id' => 'date',
316 4
				'name' => 'date',
317 4
				'value' => $dater->formatField()
318
			])
319 4
			->append('</li></ul>')
320 4
			->hidden(
321
			[
322 4
				'name' => 'id',
323 4
				'value' => $category->id
324
			])
325 4
			->token()
326 4
			->append('<div class="rs-admin-wrapper-button">')
327 4
			->cancel();
328 4
		if ($category->id)
329
		{
330 2
			if ($this->_registry->get('categoriesDelete'))
331
			{
332 1
				$formElement->delete();
333
			}
334 2
			if ($this->_registry->get('categoriesEdit'))
335
			{
336 2
				$formElement->save();
337
			}
338
		}
339 2
		else if ($this->_registry->get('categoriesNew'))
340
		{
341 1
			$formElement->create();
342
		}
343 4
		$formElement->append('</div>');
344
345
		/* collect output */
346
347 4
		$output .= $titleElement . $formElement;
348 4
		$output .= Module\Hook::trigger('adminCategoryFormEnd');
349 4
		return $output;
350
	}
351
}
352