Completed
Push — master ( d0c173...e44a53 )
by Henry
09:36
created

includes/View/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\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
	 * @return self
109
	 */
110
111
	public function init(array $optionArray = []) : self
112
	{
113
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
114
		return $this;
115
	}
116
117
	/**
118
	 * render the view
119
	 *
120
	 * @since 4.0.0
121
	 *
122
	 * @param int $categoryId identifier of the category
123
	 * @param int $articleId identifier of the article
124
	 *
125
	 * @return string
126
	 */
127
128
	public function render(int $categoryId = null, int $articleId = null) : string
129
	{
130
		if ($this->_registry->get('articleReplace'))
131
		{
132
			return Module\Hook::trigger('articleReplace');
133
		}
134
		$output = Module\Hook::trigger('articleStart');
135
		$outputFragment = null;
136
		$accessValidator = new Validator\Access();
137
		$articleModel = new Model\Article();
138
		$articles = null;
0 ignored issues
show
$articles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
140
		$byline = new Helper\Byline($this->_registry, $this->_language);
141
		$byline->init();
142
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
143
		$adminDock->init();
144
		$loggedIn = $this->_registry->get('loggedIn');
145
		$token = $this->_registry->get('token');
146
		$firstParameter = $this->_registry->get('firstParameter');
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
		$articles = $this->queryArticles($categoryId, $articleId);
168
169
		/* process articles */
170
171
		foreach ($articles as $value)
172
		{
173
			if ($accessValidator->validate($value->access, $myGroups))
174
			{
175
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
176
				if ($value->headline)
177
				{
178
					$outputFragment .= $titleElement
179
						->attr('id', 'article-' . $value->alias)
180
						->html($lastTable === 'categories' ? $linkElement
181
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
182
							->text($value->title) : $value->title
183
						);
184
				}
185
				$contentParser->process($value->text);
186
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
187
188
				/* admin dock */
189
190
				if ($loggedIn === $token && $firstParameter !== 'logout')
191
				{
192
					$outputFragment .= $adminDock->render('articles', $value->id);
193
				}
194
			}
195
		}
196
197
		/* collect output */
198
199
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
200
		$output .= Module\Hook::trigger('articleEnd');
201
		return $output;
202
	}
203
204
	/**
205
	 * query the articles
206
	 *
207
	 * @since 4.0.0
208
	 *
209
	 * @param int $categoryId identifier of the category
210
	 * @param int $articleId identifier of the article
211
	 *
212
	 * @return object|null
213
	 */
214
215
	public function queryArticles(int $categoryId = null, int $articleId = null) : ?object
216
	{
217
		$articleModel = new Model\Article();
218
		$settingModel = new Model\Setting();
219
		$lastSubParameter = $this->_registry->get('lastSubParameter');
220
		$language = $this->_registry->get('language');
221
222
		/* query articles */
223
224
		if ($categoryId)
225
		{
226
			$limitStep = $settingModel->get('pagination') ? $lastSubParameter - 1 : null;
227
			return $articleModel->getSiblingByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $limitStep);
228
		}
229
		if ($articleId)
230
		{
231
			return $articleModel->getSiblingByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
232
		}
233
		return $articleModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
234
	}
235
}
236