Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

includes/Navigation/Article.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 article navigation
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Navigation
16
 * @author Henry Ruhs
17
 */
18
19
class Article 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-articles',
32
			'active' => 'rs-item-active'
33
		],
34
		'orderColumn' => 'rank'
35
	];
36
37
	/**
38
	 * render the view
39
	 *
40
	 * @since 3.3.0
41
	 *
42
	 * @return string
43
	 */
44
45 3
	public function render() : string
46
	{
47 3
		$output = Module\Hook::trigger('navigationArticleStart');
48 3
		$outputItem = null;
49 3
		$articleModel = new Model\Article();
50 3
		$accessValidator = new Validator\Access();
51
52
		/* html element */
53
54 3
		$element = new Html\Element();
55
		$listElement = $element
56 3
			->copy()
57 3
			->init('ul',
58
			[
59 3
				'class' => $this->_optionArray['className']['list']
60
			]);
61 3
		$itemElement = $element->copy()->init('li');
62 3
		$linkElement = $element->copy()->init('a');
63
64
		/* query articles */
65
66
		$articles = $articleModel
67 3
			->query()
68 3
			->whereLanguageIs($this->_registry->get('language'))
69 3
			->where('status', 1)
70 3
			->orderBySetting($this->_optionArray['orderColumn'])
71 3
			->limit($this->_optionArray['limit'])
72 3
			->findMany();
73
74
		/* collect item output */
75
76 3
		foreach ($articles as $value)
77
		{
78 3
			if ($accessValidator->validate($value->access, $this->_registry->get('myGroups')))
79
			{
80
				$outputItem .= $itemElement
81 3
					->copy()
82 3
					->addClass($this->_registry->get('articleId') === (int)$value->id ? $this->_optionArray['className']['active'] : null)
83 3
					->html($linkElement
84 3
						->copy()
85 3
						->attr(
86
						[
87 3
							'href' => $this->_registry->get('parameterRoute') . $articleModel->getRouteById($value->id)
88
						])
89 3
						->text($value->title)
90
					);
91
			}
92
		}
93
94
		/* collect output */
95
96 3
		if ($outputItem)
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputItem 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...
97
		{
98 3
			$output .= $listElement->html($outputItem);
99
		}
100 3
		$output .= Module\Hook::trigger('navigationArticleEnd');
101 3
		return $output;
102
	}
103
}
104