Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

modules/Sitemap/Sitemap.php (1 issue)

Check for loose comparison of strings.

Best Practice Bug Major

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'));
87
88
		/* process articles */
89
90
		if (!$categoryArray)
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)
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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