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

includes/View/Article.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\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 = []) : void
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 (!$firstParameter && $settingModel->get('homepage'))
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $settingModel->get('homepage') 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...
171
		{
172
			$articles = $articleModel->getByIdAndLanguageAndOrder($settingModel->get('homepage'), $language, $this->_optionArray['orderColumn']);
173
		}
174
		else if ($categoryId)
175
		{
176
			if ($settingModel->get('pagination'))
177
			{
178
				$articles = $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
179
			}
180
			else
181
			{
182
				$articles = $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
183
			}
184
		}
185
		else if ($articleId)
186
		{
187
			$articles = $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
188
		}
189
190
		/* process articles */
191
192
		foreach ($articles as $value)
193
		{
194
			if ($accessValidator->validate($value->access, $myGroups))
195
			{
196
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
197
				if ($value->headline)
198
				{
199
					$outputFragment .= $titleElement
200
						->attr('id', 'article-' . $value->alias)
201
						->html($lastTable === 'categories' ? $linkElement
202
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
203
							->text($value->title) : $value->title
204
						);
205
				}
206
				$contentParser->process($value->text);
207
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
208
209
				/* admin dock */
210
211
				if ($loggedIn === $token && $firstParameter !== 'logout')
212
				{
213
					$outputFragment .= $adminDock->render('articles', $value->id);
214
				}
215
			}
216
		}
217
218
		/* collect output */
219
220
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
221
		$output .= Module\Hook::trigger('articleEnd');
222
		return $output;
223
	}
224
}
225