Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
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
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
		$articleModel = new Model\Article();
135
		$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...
136
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
137
		$byline = new Helper\Byline($this->_registry, $this->_language);
138
		$byline->init();
139
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
140
		$adminDock->init();
141
		$loggedIn = $this->_registry->get('loggedIn');
142
		$token = $this->_registry->get('token');
143
		$firstParameter = $this->_registry->get('firstParameter');
144
		$lastTable = $this->_registry->get('lastTable');
145
		$parameterRoute = $this->_registry->get('parameterRoute');
146
		$myGroups = $this->_registry->get('myGroups');
147
148
		/* html element */
149
150
		$element = new Html\Element();
151
		$titleElement = $element
152
			->copy()
153
			->init($this->_optionArray['tag']['title'],
154
			[
155
				'class' => $this->_optionArray['className']['title']
156
			]);
157
		$linkElement = $element->copy()->init('a');
158
		$boxElement = $element
159
			->copy()
160
			->init($this->_optionArray['tag']['box'],
161
			[
162
				'class' => $this->_optionArray['className']['box']
163
			]);
164
		$articles = $this->queryArticles($categoryId, $articleId);
165
166
		/* process articles */
167
168
		foreach ($articles as $value)
169
		{
170
			if ($accessValidator->validate($value->access, $myGroups))
171
			{
172
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
173
				if ($value->headline)
174
				{
175
					$outputFragment .= $titleElement
176
						->attr('id', 'article-' . $value->alias)
177
						->html($lastTable === 'categories' ? $linkElement
178
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
179
							->text($value->title) : $value->title
180
						);
181
				}
182
				$contentParser->process($value->text);
183
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
184
185
				/* admin dock */
186
187
				if ($loggedIn === $token && $firstParameter !== 'logout')
188
				{
189
					$outputFragment .= $adminDock->render('articles', $value->id);
190
				}
191
			}
192
		}
193
194
		/* collect output */
195
196
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
197
		$output .= Module\Hook::trigger('articleEnd');
198
		return $output;
199
	}
200
201
	/**
202
	 * query the articles
203
	 *
204
	 * @since 4.0.0
205
	 *
206
	 * @param int $categoryId identifier of the category
207
	 * @param int $articleId identifier of the article
208
	 *
209
	 * @return object|null
210
	 */
211
212
	public function queryArticles(int $categoryId = null, int $articleId = null) : ?object
213
	{
214
		$articleModel = new Model\Article();
215
		$settingModel = new Model\Setting();
216
		$lastSubParameter = $this->_registry->get('lastSubParameter');
217
		$language = $this->_registry->get('language');
218
219
		/* query articles */
220
221
		if ($categoryId)
222
		{
223
			if ($settingModel->get('pagination'))
224
			{
225
				return $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
226
			}
227
			return $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
228
		}
229
		if ($articleId)
230
		{
231
			return $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
232
		}
233
		return $articleModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
234
	}
235
}
236