Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

includes/View/Helper/Breadcrumb.php (4 issues)

Labels
Severity

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\Helper;
3
4
use Redaxscript\Db;
5
use Redaxscript\Html;
6
use Redaxscript\Model;
7
use Redaxscript\Module;
8
use Redaxscript\View\ViewAbstract;
9
use function array_key_exists;
10
use function array_keys;
11
use function array_replace_recursive;
12
use function end;
13
use function is_array;
14
15
/**
16
 * helper class to create a breadcrumb navigation
17
 *
18
 * @since 2.1.0
19
 *
20
 * @package Redaxscript
21
 * @category View
22
 * @author Henry Ruhs
23
 * @author Gary Aylward
24
 */
25
26
class Breadcrumb extends ViewAbstract
27
{
28
	/**
29
	 * array of the breadcrumb
30
	 *
31
	 * @var array
32
	 */
33
34
	protected $_breadcrumbArray = [];
35
36
	/**
37
	 * options of the breadcrumb
38
	 *
39
	 * @var array
40
	 */
41
42
	protected $_optionArray =
43
	[
44
		'className' =>
45
		[
46
			'list' => 'rs-list-breadcrumb',
47
			'divider' => 'rs-item-divider'
48
		],
49
		'divider' => null
50
	];
51
52
	/**
53
	 * stringify the breadcrumb
54
	 *
55
	 * @since 3.0.0
56
	 *
57
	 * @return string
58
	 */
59
60 2
	public function __toString() : string
61
	{
62 2
		return $this->render();
63
	}
64
65
	/**
66
	 * init the class
67
	 *
68
	 * @since 2.6.0
69
	 *
70
	 * @param array $optionArray options of the breadcrumb
71
	 *
72
	 * @return self
73
	 */
74
75 17
	public function init(array $optionArray = []) : self
76
	{
77 17
		$settingModel = new Model\Setting();
78 17
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
79 17
		if (!$this->_optionArray['divider'])
80
		{
81 17
			$this->_optionArray['divider'] = $settingModel->get('divider');
82
		}
83 17
		$this->_create();
84 17
		return $this;
85
	}
86
87
	/**
88
	 * get the breadcrumb array
89
	 *
90
	 * @since 2.1.0
91
	 *
92
	 * @return array
93
	 */
94
95 15
	public function getArray() : array
96
	{
97 15
		return $this->_breadcrumbArray;
98
	}
99
100
	/**
101
	 * render the breadcrumb
102
	 *
103
	 * @since 2.1.0
104
	 *
105
	 * @return string
106
	 */
107
108 2
	public function render() : string
109
	{
110 2
		$output = Module\Hook::trigger('breadcrumbStart');
111 2
		$parameterRoute = $this->_registry->get('parameterRoute');
112
113
		/* html element */
114
115 2
		$element = new Html\Element();
116
		$listElement = $element
117 2
			->copy()
118 2
			->init('ul',
119
			[
120 2
				'class' => $this->_optionArray['className']['list']
121
			]);
122 2
		$itemElement = $element->copy()->init('li');
123 2
		$linkElement = $element->copy()->init('a');
124
125
		/* breadcrumb keys */
126
127 2
		$breadcrumbKeys = array_keys($this->_breadcrumbArray);
128 2
		$lastKey = end($breadcrumbKeys);
129
130
		/* process breadcrumb */
131
132 2
		foreach ($this->_breadcrumbArray as $key => $valueArray)
133
		{
134 2
			$title = is_array($valueArray) && array_key_exists('title', $valueArray) ? $valueArray['title'] : null;
135 2
			$route = is_array($valueArray) && array_key_exists('route', $valueArray) ? $valueArray['route'] : null;
136 2
			if ($title)
137
			{
138 2
				$listElement->append(
139
					$itemElement
140 2
						->clear()
141 2
						->append(
142 2
							$route ? $linkElement
143 1
								->attr('href', $parameterRoute . $route)
144 2
								->text($title) : $title
145
						)
146
				);
147
148
				/* append divider */
149
150 2
				if ($key !== $lastKey && $this->_optionArray['divider'])
151
				{
152 1
					$listElement->append(
153
						$itemElement
154 1
							->copy()
155 1
							->addClass($this->_optionArray['className']['divider'])
156 1
							->text($this->_optionArray['divider'])
157
					);
158
				}
159
			}
160
		}
161
162
		/* collect list output */
163
164 2
		$output .= $listElement;
165 2
		$output .= Module\Hook::trigger('breadcrumbEnd');
166 2
		return $output;
167
	}
168
169
	/**
170
	 * create the breadcrumb array
171
	 *
172
	 * @since 2.1.0
173
	 *
174
	 * @param int $key key of the item
175
	 */
176
177 17
	protected function _create(int $key = 0) : void
178
	{
179 17
		$title = $this->_registry->get('useTitle');
180 17
		$firstParameter = $this->_registry->get('firstParameter');
181 17
		$secondParameter = $this->_registry->get('secondParameter');
182 17
		$firstTable = $this->_registry->get('firstTable');
183 17
		$fullRoute = $this->_registry->get('fullRoute');
184 17
		$lastId = $this->_registry->get('lastId');
185
186
		/* handle title */
187
188 17
		if ($title)
189
		{
190 1
			$this->_breadcrumbArray[$key]['title'] = $title;
191
		}
192
193
		/* handle home */
194
195 16
		else if (!$fullRoute)
196
		{
197 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('home');
198
		}
199
200
		/* handle administration */
201
202 15
		else if ($firstParameter === 'admin')
203
		{
204 2
			$this->_createAdmin($key);
205
		}
206
207
		/* handle login */
208
209 13
		else if ($firstParameter === 'login')
210
		{
211 3
			if ($secondParameter === 'recover')
212
			{
213 1
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('recovery');
214
			}
215 2
			else if ($secondParameter === 'reset')
216
			{
217 1
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('reset');
218
			}
219
			else
220
			{
221 3
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('login');
222
			}
223
		}
224
225
		/* handle logout */
226
227 10
		else if ($firstParameter === 'logout')
228
		{
229 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('logout');
230
		}
231
232
		/* handle register */
233
234 9
		else if ($firstParameter === 'register')
235
		{
236 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('registration');
237
		}
238
239
		/* handle module */
240
241 8
		else if ($firstParameter === 'module')
242
		{
243 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('module');
244
		}
245
246
		/* handle search */
247
248 7
		else if ($firstParameter === 'search')
249
		{
250 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('search');
251
		}
252
253
		/* handle error */
254
255 6
		else if (!$lastId)
256
		{
257 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('error');
258
		}
259
260
		/* handle content */
261
262 5
		else if ($firstTable)
263
		{
264 5
			$this->_createContent($key);
265
		}
266 17
	}
267
268
	/**
269
	 * create the breadcrumb array for the administration
270
	 *
271
	 * @since 2.1.0
272
	 *
273
	 * @param int $key key of the item
274
	 */
275
276 2
	protected function _createAdmin(int $key = 0) : void
277
	{
278 2
		$adminParameter = $this->_registry->get('adminParameter');
279 2
		$tableParameter = $this->_registry->get('tableParameter');
280 2
		$lastParameter = $this->_registry->get('lastParameter');
281 2
		$fullRoute = $this->_registry->get('fullRoute');
282
283
		/* join first title */
284
285 2
		$this->_breadcrumbArray[$key]['title'] = $this->_language->get('administration');
286
287
		/* admin parameter */
288
289 2
		if ($adminParameter)
290
		{
291 1
			$this->_breadcrumbArray[$key]['route'] = 'admin';
292
		}
293
294
		/* join admin title */
295
296 2
		if ($adminParameter && $this->_language->get($adminParameter))
0 ignored issues
show
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 278 can also be of type array; however, Redaxscript\Language::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
297
		{
298 1
			$key++;
299 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get($adminParameter);
0 ignored issues
show
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 278 can also be of type array; however, Redaxscript\Language::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
300
301
			/* set the route */
302
303 1
			if ($adminParameter !== $lastParameter)
304
			{
305 1
				$this->_breadcrumbArray[$key]['route'] = $fullRoute;
306
			}
307
308
			/* join table title */
309
310 1
			if ($tableParameter && $this->_language->get($tableParameter))
0 ignored issues
show
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 279 can also be of type array; however, Redaxscript\Language::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
311
			{
312 1
				$key++;
313 1
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get($tableParameter);
0 ignored issues
show
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 279 can also be of type array; however, Redaxscript\Language::get() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
314
			}
315
		}
316 2
	}
317
318
	/**
319
	 * create the breadcrumb array for the content
320
	 *
321
	 * @since 2.1.0
322
	 *
323
	 * @param int $key
324
	 */
325
326 5
	protected function _createContent(int $key = 0) : void
327
	{
328 5
		$firstParameter = $this->_registry->get('firstParameter');
329 5
		$secondParameter = $this->_registry->get('secondParameter');
330 5
		$thirdParameter = $this->_registry->get('thirdParameter');
331 5
		$lastParameter = $this->_registry->get('lastParameter');
332 5
		$firstTable = $this->_registry->get('firstTable');
333 5
		$secondTable = $this->_registry->get('secondTable');
334 5
		$thirdTable = $this->_registry->get('thirdTable');
335
336
		/* join first title */
337
338 5
		$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($firstTable)->where('alias', $firstParameter)->findOne()->title;
339
340
		/* set the route */
341
342 5
		if ($firstParameter !== $lastParameter)
343
		{
344 3
			$this->_breadcrumbArray[$key]['route'] = $firstParameter;
345
		}
346
347
		/* join second title */
348
349 5
		if ($secondTable)
350
		{
351 3
			$key++;
352 3
			$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($secondTable)->where('alias', $secondParameter)->findOne()->title;
353
354
			/* set the route */
355
356 3
			if ($secondParameter !== $lastParameter)
357
			{
358 1
				$this->_breadcrumbArray[$key]['route'] = $firstParameter . '/' . $secondParameter;
359
			}
360
361
			/* join third title */
362
363 3
			if ($thirdTable)
364
			{
365 1
				$key++;
366 1
				$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($thirdTable)->where('alias', $thirdParameter)->findOne()->title;
367
			}
368
		}
369 5
	}
370
}
371