Completed
Push — master ( de9e2d...f59dfb )
by Henry
08:37
created

includes/View/Article.php (1 issue)

Check for loose comparison of integers.

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\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Content;
7
use Redaxscript\Html;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Module;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use Redaxscript\Template;
14
use Redaxscript\Validator;
15
use function array_replace_recursive;
16
17
/**
18
 * children class to create the article
19
 *
20
 * @since 4.0.0
21
 *
22
 * @package Redaxscript
23
 * @category View
24
 * @author Henry Ruhs
25
 */
26
27
class Article extends ViewAbstract
28
{
29
	/**
30
	 * instance of the request class
31
	 *
32
	 * @var Request
33
	 */
34
35
	protected $_request;
36
37
	/**
38
	 * instance of the config class
39
	 *
40
	 * @var Config
41
	 */
42
43
	protected $_config;
44
45
	/**
46
	 * options of the article
47
	 *
48
	 * @var array
49
	 */
50
51
	protected $_optionArray =
52
	[
53
		'tag' =>
54
		[
55
			'title' => 'h2',
56
			'box' => 'div'
57
		],
58
		'className' =>
59
		[
60
			'title' => 'rs-title-content',
61
			'box' => 'rs-box-content'
62
		],
63
		'orderColumn' => 'rank',
64
		'partial' =>
65
		[
66
			'error' => 'error.phtml'
67
		]
68
	];
69
70
	/**
71
	 * constructor of the class
72
	 *
73
	 * @since 4.0.0
74
	 *
75
	 * @param Registry $registry instance of the registry class
76
	 * @param Request $request instance of the request class
77
	 * @param Language $language instance of the language class
78
	 * @param Config $config instance of the config class
79
	 */
80
81
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
82
	{
83
		parent::__construct($registry, $language);
84
		$this->_request = $request;
85
		$this->_config = $config;
86
	}
87
88
	/**
89
	 * stringify the article
90
	 *
91
	 * @since 4.0.0
92
	 *
93
	 * @return string
94
	 */
95
96
	public function __toString() : string
97
	{
98
		return $this->render();
99
	}
100
101
	/**
102
	 * init the class
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @param array $optionArray options of the article
107
	 */
108
109
	public function init(array $optionArray = [])
110
	{
111
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
112
	}
113
114
	/**
115
	 * render the view
116
	 *
117
	 * @since 4.0.0
118
	 *
119
	 * @param int $categoryId identifier of the category
120
	 * @param int $articleId identifier of the article
121
	 *
122
	 * @return string
123
	 */
124
125
	public function render(int $categoryId = null, int $articleId = null) : string
126
	{
127
		if ($this->_registry->get('articleReplace'))
128
		{
129
			return Module\Hook::trigger('articleReplace');
130
		}
131
		$output = Module\Hook::trigger('articleStart');
132
		$outputFragment = null;
133
		$accessValidator = new Validator\Access();
134
		$settingModel = new Model\Setting();
135
		$articleModel = new Model\Article();
136
		$articles = null;
137
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
138
		$byline = new Helper\Byline($this->_registry, $this->_language);
139
		$byline->init();
140
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
141
		$adminDock->init();
142
		$language = $this->_registry->get('language');
143
		$loggedIn = $this->_registry->get('loggedIn');
144
		$token = $this->_registry->get('token');
145
		$firstParameter = $this->_registry->get('firstParameter');
146
		$lastSubParameter = $this->_registry->get('lastSubParameter');
147
		$lastTable = $this->_registry->get('lastTable');
148
		$parameterRoute = $this->_registry->get('parameterRoute');
149
		$myGroups = $this->_registry->get('myGroups');
150
151
		/* html element */
152
153
		$element = new Html\Element();
154
		$titleElement = $element
155
			->copy()
156
			->init($this->_optionArray['tag']['title'],
157
			[
158
				'class' => $this->_optionArray['className']['title']
159
			]);
160
		$linkElement = $element->copy()->init('a');
161
		$boxElement = $element
162
			->copy()
163
			->init($this->_optionArray['tag']['box'],
164
			[
165
				'class' => $this->_optionArray['className']['box']
166
			]);
167
168
		/* query articles */
169
170
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
171
		{
172
			if ($settingModel->get('pagination'))
173
			{
174
				$articles = $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
175
			}
176
			else
177
			{
178
				$articles = $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
179
			}
180
		}
181
		else if ($articleId)
182
		{
183
			$articles = $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
184
		}
185
186
		/* process articles */
187
188
		foreach ($articles as $value)
189
		{
190
			if ($accessValidator->validate($value->access, $myGroups))
191
			{
192
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
193
				if ($value->headline)
194
				{
195
					$outputFragment .= $titleElement
196
						->attr('id', 'article-' . $value->alias)
197
						->html($lastTable === 'categories' ? $linkElement
198
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
199
							->text($value->title) : $value->title
200
						);
201
				}
202
				$contentParser->process($value->text);
203
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
204
205
				/* admin dock */
206
207
				if ($loggedIn === $token && $firstParameter !== 'logout')
208
				{
209
					$outputFragment .= $adminDock->render('articles', $value->id);
210
				}
211
			}
212
		}
213
214
		/* collect output */
215
216
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
217
		$output .= Module\Hook::trigger('articleEnd');
218
		return $output;
219
	}
220
}
221