Completed
Push — master ( d0c173...e44a53 )
by Henry
09:36
created

includes/Admin/View/Helper/Panel.php (14 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\Helper;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
use function array_key_exists;
9
use function array_replace_recursive;
10
11
/**
12
 * helper class to create the admin panel
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class Panel extends Admin\View\ViewAbstract
22
{
23
	/**
24
	 * options of the panel
25
	 *
26
	 * @var array
27
	 */
28
29
	protected $_optionArray =
30
	[
31
		'className' =>
32
		[
33
			'list' =>
34
			[
35
				'panel' => 'rs-admin-fn-dropdown rs-admin-list-panel',
36
				'content' => 'rs-admin-fn-content-panel rs-admin-list-panel-children',
37
				'access' => 'rs-admin-fn-content-panel rs-admin-list-panel-children',
38
				'system' => 'rs-admin-fn-content-panel rs-admin-list-panel-children',
39
				'notification' => 'rs-admin-fn-content-panel rs-admin-list-panel-children rs-admin-list-notification'
40
			],
41
			'item' =>
42
			[
43
				'content' => 'rs-admin-item-panel',
44
				'access' => 'rs-admin-item-panel',
45
				'system' => 'rs-admin-item-panel',
46
				'profile' => 'rs-admin-item-panel',
47
				'notification' => 'rs-admin-item-panel',
48
				'logout' => 'rs-admin-item-panel rs-admin-item-panel-logout'
49
			],
50
			'link' =>
51
			[
52
				'view' => 'rs-admin-link-panel rs-admin-link-panel-view',
53
				'new' => 'rs-admin-link-panel rs-admin-link-panel-new',
54
				'system' => 'rs-admin-link-panel rs-admin-link-panel-system',
55
				'profile' => 'rs-admin-link-panel rs-admin-link-panel-profile',
56
				'logout' => 'rs-admin-link-panel rs-admin-link-panel-logout'
57
			],
58
			'label' =>
59
			[
60
				'content' => 'rs-admin-fn-toggle-panel rs-admin-label-panel rs-admin-label-panel-content',
61
				'access' => 'rs-admin-fn-toggle-panel rs-admin-label-panel rs-admin-label-panel-access',
62
				'system' => 'rs-admin-fn-toggle-panel rs-admin-label-panel rs-admin-label-panel-system',
63
				'notification' => 'rs-admin-fn-toggle-panel rs-admin-label-panel rs-admin-label-panel-notification'
64
			],
65
			'note' =>
66
			[
67
				'success' => 'rs-admin-is-success',
68
				'warning' => 'rs-admin-is-warning',
69
				'error' => 'rs-admin-is-error',
70
				'info' => 'rs-admin-is-info'
71
			],
72
			'text' => 'rs-admin-text-panel-group',
73
			'input' => 'rs-admin-fn-status-panel',
74
			'sup' => 'rs-admin-sup-panel-notification'
75
		]
76
	];
77
78
	/**
79
	 * init the class
80
	 *
81
	 * @since 4.0.0
82
	 *
83
	 * @param array $optionArray options of the panel
84
	 *
85
	 * @return self
86
	 */
87
88 8
	public function init(array $optionArray = []) : self
89
	{
90 8
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
91 8
		return $this;
92
	}
93
94
	/**
95
	 * render the view
96
	 *
97
	 * @since 4.0.0
98
	 *
99
	 * @return string
100
	 */
101
102 8
	public function render() : string
103
	{
104 8
		$output = Module\Hook::trigger('adminPanelStart');
105 8
		$outputItem = null;
106
107
		/* html element */
108
109 8
		$listElement = new Html\Element();
110 8
		$listElement->init('ul',
111
		[
112 8
			'class' => $this->_optionArray['className']['list']['panel']
113
		]);
114
115
		/* collect item output */
116
117 8
		if ($this->_hasPermission('contents'))
118
		{
119 2
			$outputItem .= $this->_renderContent();
120
		}
121 8
		if ($this->_hasPermission('access'))
122
		{
123 2
			$outputItem .= $this->_renderAccess();
124
		}
125 8
		if ($this->_hasPermission('system'))
126
		{
127 1
			$outputItem .= $this->_renderSystem();
128
		}
129 8
		if ($this->_hasPermission('profile'))
130
		{
131 1
			$outputItem .= $this->_renderProfile();
132
		}
133 8
		if ($this->_hasPermission('notification'))
134
		{
135 1
			$outputItem .= $this->_renderNotification();
136
		}
137 8
		$outputItem .= $this->_renderLogout();
138
139
		/* collect output */
140
141 8
		$output .= $listElement->append($outputItem);
142 8
		$output .= Module\Hook::trigger('adminPanelEnd');
143 8
		return $output;
144
	}
145
146
	/**
147
	 * has the permission
148
	 *
149
	 * @since 4.0.0
150
	 *
151
	 * @param string $type
152
	 *
153
	 * @return bool
154
	 */
155
156 8
	protected function _hasPermission(string $type = null) : bool
157
	{
158 8
		$permissionArray = [];
159 8
		$accessValidator = new Validator\Access();
160 8
		if ($this->_registry->get('categoriesEdit'))
161
		{
162 2
			$permissionArray['categories'] = $permissionArray['contents'] = true;
163
		}
164 8
		if ($this->_registry->get('articlesEdit'))
165
		{
166
			$permissionArray['articles'] = $permissionArray['contents'] = true;
167
		}
168 8
		if ($this->_registry->get('extrasEdit'))
169
		{
170
			$permissionArray['extras'] = $permissionArray['contents'] = true;
171
		}
172 8
		if ($this->_registry->get('commentsEdit'))
173
		{
174
			$permissionArray['comments'] = $permissionArray['contents'] = true;
175
		}
176 8
		if ($this->_registry->get('usersEdit'))
177
		{
178 2
			$permissionArray['users'] = $permissionArray['access'] = true;
179
		}
180 8
		if ($this->_registry->get('groupsEdit'))
181
		{
182
			$permissionArray['groups'] = $permissionArray['access'] = true;
183
		}
184 8
		if ($this->_registry->get('modulesEdit'))
185
		{
186 1
			$permissionArray['modules'] = $permissionArray['system'] = true;
187
		}
188 8
		if ($this->_registry->get('settingsEdit'))
189
		{
190 1
			$permissionArray['settings'] = $permissionArray['system'] = true;
191
		}
192 8
		if ($this->_registry->get('myId'))
193
		{
194 1
			$permissionArray['profile'] = true;
195
		}
196 8
		if ($accessValidator->validate(1, $this->_registry->get('myGroups')))
0 ignored issues
show
It seems like $this->_registry->get('myGroups') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Validator\Access::validate() 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...
197
		{
198 1
			$permissionArray['notification'] = true;
199
		}
200 8
		return array_key_exists($type, $permissionArray);
201
	}
202
203
	/**
204
	 * render the content
205
	 *
206
	 * @since 4.0.0
207
	 *
208
	 * @return string|null
209
	 */
210
211 2
	protected function _renderContent() : ?string
212
	{
213 2
		$output = null;
214 2
		$parameterRoute = $this->_registry->get('parameterRoute');
215
		$contentArray =
216
		[
217 2
			'categories',
218
			'articles',
219
			'extras',
220
			'comments'
221
		];
222
223
		/* html element */
224
225 2
		$element = new Html\Element();
226
		$listElement = $element
227 2
			->copy()
228 2
			->init('ul',
229
			[
230 2
				'class' => $this->_optionArray['className']['list']['content']
231
			]);
232 2
		$itemElement = $element->copy()->init('li');
233 2
		$linkElement = $element->copy()->init('a');
234
		$textElement = $element
235 2
			->copy()
236 2
			->init('span',
237
			[
238 2
				'class' => $this->_optionArray['className']['text']
239
			]);
240
		$labelElement = $element
241 2
			->copy()
242 2
			->init('label',
243
			[
244 2
				'class' => $this->_optionArray['className']['label']['content'],
245
				'for' => self::class . '\Content'
246
			])
247 2
			->text($this->_language->get('contents'));
0 ignored issues
show
It seems like $this->_language->get('contents') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
248
		$inputElement = $element
249 2
			->copy()
250 2
			->init('input',
251
			[
252 2
				'id' => self::class . '\Content',
253 2
				'class' => $this->_optionArray['className']['input'],
254 2
				'type' => 'radio',
255
				'name' => self::class . '\Panel'
256
			]);
257
258
		/* process contents */
259
260 2
		foreach ($contentArray as $type)
261
		{
262 2
			$tableNew = $this->_registry->get($type . 'New');
263 2
			if ($this->_hasPermission($type))
264
			{
265 2
				$listElement->append(
266
					$itemElement
267 2
						->copy()
268 2
						->html(
269
							$textElement
270 2
								->copy()
271 2
								->append(
272
									$linkElement
273 2
										->copy()
274 2
										->addClass($this->_optionArray['className']['link']['view'])
275 2
										->attr('href', $parameterRoute . 'admin/view/' . $type)
276 2
										->text($this->_language->get($type))
0 ignored issues
show
It seems like $this->_language->get($type) targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
277
								)
278 2
								->append($tableNew ? $linkElement
0 ignored issues
show
It seems like $tableNew ? $linkElement...age->get('new')) : null can also be of type object<Redaxscript\Html\Element>; however, Redaxscript\Html\HtmlAbstract::append() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
279 1
									->copy()
280 1
									->addClass($this->_optionArray['className']['link']['new'])
281 1
									->attr('href', $parameterRoute . 'admin/new/' . $type)
282 2
									->text($this->_language->get('new')) : null
0 ignored issues
show
It seems like $this->_language->get('new') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
283
								)
284
						)
285
				);
286
			}
287
		}
288
289
		/* collect output */
290
291
		$output .= $itemElement
292 2
			->copy()
293 2
			->addClass($this->_optionArray['className']['item']['content'])
294 2
			->html($inputElement . $labelElement . $listElement);
295 2
		return $output;
296
	}
297
298
	/**
299
	 * render the access
300
	 *
301
	 * @since 4.0.0
302
	 *
303
	 * @return string|null
304
	 */
305
306 2
	protected function _renderAccess() : ?string
307
	{
308 2
		$output = null;
309 2
		$parameterRoute = $this->_registry->get('parameterRoute');
310
		$accessArray =
311
		[
312 2
			'users',
313
			'groups'
314
		];
315
316
		/* html element */
317
318 2
		$element = new Html\Element();
319
		$listElement = $element
320 2
			->copy()
321 2
			->init('ul',
322
			[
323 2
				'class' => $this->_optionArray['className']['list']['access']
324
			]);
325 2
		$itemElement = $element->copy()->init('li');
326 2
		$linkElement = $element->copy()->init('a');
327
		$textElement = $element
328 2
			->copy()
329 2
			->init('span',
330
			[
331 2
				'class' => $this->_optionArray['className']['text']
332
			]);
333
		$labelElement = $element
334 2
			->copy()
335 2
			->init('label',
336
			[
337 2
				'class' => $this->_optionArray['className']['label']['access'],
338
				'for' => self::class . '\Access'
339
			])
340 2
			->text($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\Element::text() does only seem to accept string|integer|null, 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...
341
		$inputElement = $element
342 2
			->copy()
343 2
			->init('input',
344
			[
345 2
				'id' => self::class . '\Access',
346 2
				'class' => $this->_optionArray['className']['input'],
347 2
				'type' => 'radio',
348
				'name' => self::class . '\Panel'
349
			]);
350
351
		/* process access */
352
353 2
		foreach ($accessArray as $type)
354
		{
355 2
			$tableNew = $this->_registry->get($type . 'New');
356 2
			if ($this->_hasPermission($type))
357
			{
358 2
				$listElement->append(
359
					$itemElement
360 2
						->copy()
361 2
						->html(
362
							$textElement
363 2
								->copy()
364 2
								->append(
365
									$linkElement
366 2
										->copy()
367 2
										->addClass($this->_optionArray['className']['link']['view'])
368 2
										->attr('href', $parameterRoute . 'admin/view/' . $type)
369 2
										->text($this->_language->get($type))
0 ignored issues
show
It seems like $this->_language->get($type) targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
370
								)
371 2
								->append($tableNew ? $linkElement
0 ignored issues
show
It seems like $tableNew ? $linkElement...age->get('new')) : null can also be of type object<Redaxscript\Html\Element>; however, Redaxscript\Html\HtmlAbstract::append() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
372 1
									->copy()
373 1
									->addClass($this->_optionArray['className']['link']['new'])
374 1
									->attr('href', $parameterRoute . 'admin/new/' . $type)
375 2
									->text($this->_language->get('new')) : null
0 ignored issues
show
It seems like $this->_language->get('new') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
376
								)
377
						)
378
				);
379
			}
380
		}
381
382
		/* collect output */
383
384
		$output .= $itemElement
385 2
			->copy()
386 2
			->addClass($this->_optionArray['className']['item']['access'])
387 2
			->html($inputElement . $labelElement . $listElement);
388 2
		return $output;
389
	}
390
391
	/**
392
	 * render the system
393
	 *
394
	 * @since 4.0.0
395
	 *
396
	 * @return string|null
397
	 */
398
399 1
	protected function _renderSystem() : ?string
400
	{
401 1
		$output = null;
402 1
		$parameterRoute = $this->_registry->get('parameterRoute');
403
		$systemArray =
404
		[
405 1
			'modules',
406
			'settings'
407
		];
408
409
		/* html element */
410
411 1
		$element = new Html\Element();
412
		$listElement = $element
413 1
			->copy()
414 1
			->init('ul',
415
			[
416 1
				'class' => $this->_optionArray['className']['list']['system']
417
			]);
418 1
		$itemElement = $element->copy()->init('li');
419
		$linkElement = $element
420 1
			->copy()
421 1
			->init('a',
422
			[
423 1
				'class' => $this->_optionArray['className']['link']['system'],
424
			]);
425
		$labelElement = $element
426 1
			->copy()
427 1
			->init('label',
428
			[
429 1
				'class' => $this->_optionArray['className']['label']['system'],
430
				'for' => self::class . '\System'
431
			])
432 1
			->text($this->_language->get('system'));
0 ignored issues
show
It seems like $this->_language->get('system') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
433
		$inputElement = $element
434 1
			->copy()
435 1
			->init('input',
436
			[
437 1
				'id' => self::class . '\System',
438 1
				'class' => $this->_optionArray['className']['input'],
439 1
				'type' => 'radio',
440
				'name' => self::class . '\Panel'
441
			]);
442
443
		/* process system */
444
445 1
		foreach ($systemArray as $type)
446
		{
447 1
			if ($this->_hasPermission($type))
448
			{
449 1
				$listElement->append(
450
					$itemElement
451 1
						->copy()
452 1
						->html(
453
							$linkElement
454 1
								->copy()
455 1
								->attr('href', $type === 'settings' ? $parameterRoute . 'admin/edit/settings' : $parameterRoute . 'admin/view/' . $type)
456 1
								->text($this->_language->get($type))
0 ignored issues
show
It seems like $this->_language->get($type) targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
457
						)
458
				);
459
			}
460
		}
461
462
		/* collect output */
463
464
		$output .= $itemElement
465 1
			->copy()
466 1
			->addClass($this->_optionArray['className']['item']['system'])
467 1
			->html($inputElement . $labelElement . $listElement);
468 1
		return $output;
469
	}
470
471
	/**
472
	 * render the profile
473
	 *
474
	 * @since 4.0.0
475
	 *
476
	 * @return string
477
	 */
478
479 1
	protected function _renderProfile() : string
480
	{
481 1
		$parameterRoute = $this->_registry->get('parameterRoute');
482 1
		$myId = $this->_registry->get('myId');
483
484
		/* html element */
485
486 1
		$element = new Html\Element();
487
		$itemElement = $element
488 1
			->copy()
489 1
			->init('li',
490
			[
491 1
				'class' => $this->_optionArray['className']['item']['profile']
492
			]);
493
		$linkElement = $element
494 1
			->copy()
495 1
			->init('a',
496
			[
497 1
				'href' => $parameterRoute . 'admin/edit/users/' . $myId,
498 1
				'class' => $this->_optionArray['className']['link']['profile']
499
			])
500 1
			->text($this->_language->get('profile'));
0 ignored issues
show
It seems like $this->_language->get('profile') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
501
502
		/* collect item output */
503
504 1
		$output = $itemElement->html($linkElement);
505 1
		return $output;
506
	}
507
508
	/**
509
	 * render the notification
510
	 *
511
	 * @since 4.0.0
512
	 *
513
	 * @return string|null
514
	 */
515
516 1
	protected function _renderNotification() : ?string
517
	{
518 1
		$output = null;
519 1
		$adminNotification = new Notification($this->_language);
520 1
		$adminNotification->init(
521
		[
522
			'className' =>
523
			[
524 1
				'list' => $this->_optionArray['className']['list']['notification']
525
			]
526
		]);
527 1
		$metaArray = $adminNotification->getMetaArray();
528
529
		/* html element */
530
531 1
		$element = new Html\Element();
532
		$itemElement = $element
533 1
			->copy()
534 1
			->init('li',
535
			[
536 1
				'class' => $this->_optionArray['className']['item']['notification']
537
			]);
538
		$labelElement = $element
539 1
			->copy()
540 1
			->init('label',
541
			[
542 1
				'class' => $this->_optionArray['className']['label']['notification'],
543
				'for' => self::class . '\Notification'
544
			])
545 1
			->text($this->_language->get('notification'));
0 ignored issues
show
It seems like $this->_language->get('notification') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
546
		$inputElement = $element
547 1
			->copy()
548 1
			->init('input',
549
			[
550 1
				'id' => self::class . '\Notification',
551 1
				'class' => $this->_optionArray['className']['input'],
552 1
				'type' => 'radio',
553
				'name' => self::class . '\Panel',
554 1
				'checked' => 'checked'
555
			]);
556
		$supElement = $element
557 1
			->copy()
558 1
			->init('sup',
559
			[
560 1
				'class' => $this->_optionArray['className']['sup']
561
			])
562 1
			->text($metaArray['total']);
563
564
		/* process meta */
565
566 1
		foreach ($metaArray as $key => $value)
567
		{
568 1
			$supElement->addClass($this->_optionArray['className']['note'][$key]);
569
		}
570 1
		$labelElement->append($supElement);
571
572
		/* collect item output */
573
574 1
		if ($metaArray['total'])
575
		{
576 1
			$output = $itemElement->html($inputElement . $labelElement . $adminNotification->render());
577
		}
578 1
		return $output;
579
	}
580
581
	/**
582
	 * render the logout
583
	 *
584
	 * @since 4.0.0
585
	 *
586
	 * @return string
587
	 */
588
589 8
	protected function _renderLogout() : string
590
	{
591 8
		$parameterRoute = $this->_registry->get('parameterRoute');
592
593
		/* html element */
594
595 8
		$element = new Html\Element();
596
		$itemElement = $element
597 8
			->copy()
598 8
			->init('li',
599
			[
600 8
				'class' => $this->_optionArray['className']['item']['logout']
601
			]);
602
		$linkElement = $element
603 8
			->copy()
604 8
			->init('a',
605
			[
606 8
				'href' => $parameterRoute . 'logout',
607 8
				'class' => $this->_optionArray['className']['link']['logout']
608
			])
609 8
			->text($this->_language->get('logout'));
0 ignored issues
show
It seems like $this->_language->get('logout') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
610
611
		/* collect item output */
612
613 8
		$output = $itemElement->html($linkElement);
614 8
		return $output;
615
	}
616
}
617