Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

modules/Sitemap/Sitemap.php (2 issues)

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\Modules\Sitemap;
3
4
use Redaxscript\Db;
5
use Redaxscript\Html;
6
use Redaxscript\Model;
7
use Redaxscript\Module;
8
9
/**
10
 * generate a sitemap
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Modules
16
 * @author Henry Ruhs
17
 */
18
19
class Sitemap extends Module\Module
20
{
21
	/**
22
	 * array of the module
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_moduleArray =
28
	[
29
		'name' => 'Sitemap',
30
		'alias' => 'Sitemap',
31
		'author' => 'Redaxmedia',
32
		'description' => 'Generate a sitemap tree',
33
		'version' => '4.0.0'
34
	];
35
36
	/**
37
	 * array of the option
38
	 *
39
	 * @var array
40
	 */
41
42
	protected $_optionArray =
43
	[
44
		'className' =>
45
		[
46
			'title' => 'rs-title-content-sub rs-title-sitemap',
47
			'list' => 'rs-list-default rs-list-sitemap'
48
		]
49
	];
50
51
	/**
52
	 * render
53
	 *
54
	 * @since 3.3.0
55
	 *
56
	 * @return string
57
	 */
58
59
	public function render() : string
60
	{
61
		$output = null;
62
		$outputItem = null;
63
		$error = null;
64
		$articleModel = new Model\Article();
65
66
		/* html element */
67
68
		$element = new Html\Element();
69
		$titleElement = $element
70
			->copy()
71
			->init('h3',
72
			[
73
				'class' => $this->_optionArray['className']['title']
74
			]);
75
		$listElement = $element
76
			->copy()
77
			->init('ul',
78
			[
79
				'class' => $this->_optionArray['className']['list']
80
			]);
81
		$itemElement = $element->copy()->init('li');
82
		$linkElement = $element->copy()->init('a');
83
84
		/* query articles */
85
86
		$categoryArray = $this->_getCategoryArrayByLanguage($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\Modules\Site...tegoryArrayByLanguage() 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...
87
88
		/* process articles */
89
90
		if (!$categoryArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryArray 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...
91
		{
92
			$error = $this->_language->get('article_no') . $this->_language->get('point');
93
		}
94
		else
95
		{
96
			foreach ($categoryArray as $key => $articles)
97
			{
98
				/* collect item output */
99
100
				foreach ($articles as $value)
101
				{
102
					$outputItem .= $itemElement
103
						->copy()
104
						->html($linkElement
105
							->copy()
106
							->attr('href', $this->_registry->get('parameterRoute') . $articleModel->getRouteById($value->id))
107
							->text($value->title)
108
						);
109
				}
110
111
				/* collect output */
112
113
				$categoryName = $key < 1 ? $this->_language->get('uncategorized') : Db::forTablePrefix('categories')->whereIdIs($key)->findOne()->title;
114
				$output .= $titleElement->text($categoryName);
115
				$output .= $listElement->html($outputItem);
116
				$outputItem = null;
117
			}
118
		}
119
120
		/* handle error */
121
122
		if ($error)
123
		{
124
			$output = $listElement->html(
125
				$itemElement->html($error)
126
			);
127
		}
128
		return $output;
129
	}
130
131
	/**
132
	 * get category array by language
133
	 *
134
	 * @since 3.3.0
135
	 *
136
	 * @param string $language
137
	 *
138
	 * @return array
139
	 */
140
141
	protected function _getCategoryArrayByLanguage(string $language = null) : array
142
	{
143
		$categoryArray = [];
144
		$articles = Db::forTablePrefix('articles')
145
			->whereLanguageIs($language)
146
			->whereNull('access')
147
			->where('status', 1)
148
			->orderByDesc('category')
149
			->findMany();
150
151
		/* process article */
152
153
		foreach ($articles as $value)
154
		{
155
			$categoryKey = $value->category ? : 0;
156
			$categoryArray[$categoryKey][] = $value;
157
		}
158
		return $categoryArray;
159
	}
160
}
161