Completed
Push — master ( 1da492...320203 )
by Henry
07:00
created

includes/Navigation/Category.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\Navigation;
3
4
use Redaxscript\Html;
5
use Redaxscript\Model;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
9
/**
10
 * children class to create the category navigation
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Navigation
16
 * @author Henry Ruhs
17
 */
18
19
class Category extends NavigationAbstract
20
{
21
	/**
22
	 * options of the navigation
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_optionArray =
28
	[
29
		'className' =>
30
		[
31
			'list' => 'rs-list-categories',
32
			'children' => 'rs-list-children',
33
			'active' => 'rs-item-active'
34
		],
35
		'orderColumn' => 'rank'
36
	];
37
38
	/**
39
	 * render the view
40
	 *
41
	 * @since 3.3.0
42
	 *
43
	 * @return string
44
	 */
45
46
	public function render() : string
47
	{
48
		$output = Module\Hook::trigger('navigationCategoryStart');
49
		$categoryModel = new Model\Category();
50
51
		/* query categories */
52
53
		$categories = $categoryModel
54
			->query()
55
			->whereLanguageIs($this->_registry->get('language'))
0 ignored issues
show
It seems like $this->_registry->get('language') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Db::whereLanguageIs() 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...
56
			->whereNull('parent')
57
			->where('status', 1)
58
			->orderBySetting($this->_optionArray['orderColumn'])
59
			->limit($this->_optionArray['limit'])
60
			->findMany();
61
62
		/* collect output */
63
64
		$output .= $this->_renderList($categories);
65
		$output .= Module\Hook::trigger('navigationCategoryEnd');
66
		return $output;
67
	}
68
69
	/**
70
	 * render the list
71
	 *
72
	 * @since 3.3.0
73
	 *
74
	 * @param object $categories
75
	 * @param int $level
76
	 *
77
	 * @return string|null
78
	 */
79
80
	protected function _renderList(object $categories = null, int $level = 0) : ?string
81
	{
82
		$output = null;
83
		$outputItem = null;
84
		$categoryModel = new Model\Category();
85
		$accessValidator = new Validator\Access();
86
87
		/* html element */
88
89
		$element = new Html\Element();
90
		$listElement = $element
91
			->copy()
92
			->init('ul',
93
			[
94
				'class' => $level === 0 ? $this->_optionArray['className']['list'] : $this->_optionArray['className']['children']
95
			]);
96
		$itemElement = $element->copy()->init('li');
97
		$linkElement = $element->copy()->init('a');
98
		$textElement = $element->copy()->init('span');
99
100
		/* collect item output */
101
102
		foreach ($categories as $value)
103
		{
104
			if ($accessValidator->validate($value->access, $this->_registry->get('myGroups')))
105
			{
106
				$outputItem .= $itemElement
107
					->copy()
108
					->addClass($this->_registry->get('firstParameter') === $value->alias ? $this->_optionArray['className']['active'] : null)
109
					->html($linkElement
110
						->copy()
111
						->attr(
112
						[
113
							'href' => $this->_registry->get('parameterRoute') . $categoryModel->getRouteById($value->id)
114
						])
115
						->text($value->title)
116
					)
117
					->append(
118
						$this->_renderList($categoryModel
119
							->query()
120
							->whereLanguageIs($this->_registry->get('language'))
121
							->where(
122
							[
123
								'parent' => $value->id,
124
								'status' => 1
125
							])
126
							->orderBySetting($this->_optionArray['orderColumn'])
127
							->limit($this->_optionArray['limit'])
128
							->findMany(), ++$level
129
						)
130
					);
131
			}
132
		}
133
134
		/* collect output */
135
136
		if ($outputItem)
137
		{
138
			$output .= $listElement->html($outputItem);
139
		}
140
		else if ($level === 0)
141
		{
142
			$output .= $listElement->html($itemElement->html($textElement->text($this->_language->get('category_no'))));
143
		}
144
		return $output;
145
	}
146
}
147