Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

includes/View/Helper/Breadcrumb.php (3 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 2
	 * @since 3.0.0
56
	 *
57 2
	 * @return string
58
	 */
59
60
	public function __toString() : string
61
	{
62
		return $this->render();
63
	}
64
65
	/**
66
	 * init the class
67
	 *
68 16
	 * @since 2.6.0
69
	 *
70 16
	 * @param array $optionArray options of the breadcrumb
71 16
	 */
72 16
73
	public function init(array $optionArray = [])
74 16
	{
75
		$settingModel = new Model\Setting();
76 16
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
77 16
		if (!$this->_optionArray['divider'])
78
		{
79
			$this->_optionArray['divider'] = $settingModel->get('divider');
80
		}
81
		$this->_create();
82
	}
83
84
	/**
85
	 * get the breadcrumb array
86
	 *
87 14
	 * @since 2.1.0
88
	 *
89 14
	 * @return array
90
	 */
91
92
	public function getArray() : array
93
	{
94
		return $this->_breadcrumbArray;
95
	}
96
97
	/**
98
	 * render the breadcrumb
99
	 *
100 2
	 * @since 2.1.0
101
	 *
102 2
	 * @return string
103 2
	 */
104
105
	public function render() : string
106
	{
107 2
		$output = Module\Hook::trigger('breadcrumbStart');
108
		$parameterRoute = $this->_registry->get('parameterRoute');
109 2
110 2
		/* html element */
111
112 2
		$element = new Html\Element();
113
		$listElement = $element
114 2
			->copy()
115 2
			->init('ul',
116
			[
117
				'class' => $this->_optionArray['className']['list']
118
			]);
119 2
		$itemElement = $element->copy()->init('li');
120 2
		$linkElement = $element->copy()->init('a');
121
122
		/* breadcrumb keys */
123
124 2
		$breadcrumbKeys = array_keys($this->_breadcrumbArray);
125
		$lastKey = end($breadcrumbKeys);
126 2
127 2
		/* process breadcrumb */
128 2
129
		foreach ($this->_breadcrumbArray as $key => $valueArray)
130 2
		{
131
			$title = is_array($valueArray) && array_key_exists('title', $valueArray) ? $valueArray['title'] : null;
132
			$route = is_array($valueArray) && array_key_exists('route', $valueArray) ? $valueArray['route'] : null;
133
			if ($title)
134 2
			{
135
				$itemElement->clear();
136 1
137
				/* append link */
138 1
139 1
				if ($route)
140
				{
141
					$itemElement->append(
142
						$linkElement
143
							->attr('href', $parameterRoute . $route)
144
							->text($title)
145
					);
146
				}
147 2
148
				/* else append text */
149 2
150
				else
151
				{
152
					$itemElement->text($title);
153 2
				}
154
				$listElement->append($itemElement);
155 1
156
				/* append divider */
157 1
158 1
				if ($key !== $lastKey && $this->_optionArray['divider'])
159 2
				{
160
					$listElement->append(
161
						$itemElement
162
							->copy()
163
							->addClass($this->_optionArray['className']['divider'])
164
							->text($this->_optionArray['divider'])
165
					);
166
				}
167 2
			}
168 2
		}
169 2
170
		/* collect list output */
171
172
		$output .= $listElement;
173
		$output .= Module\Hook::trigger('breadcrumbEnd');
174
		return $output;
175
	}
176
177
	/**
178
	 * create the breadcrumb array
179
	 *
180 16
	 * @since 2.1.0
181
	 *
182 16
	 * @param int $key key of the item
183 16
	 */
184 16
185 16
	protected function _create(int $key = 0)
186 16
	{
187 16
		$title = $this->_registry->get('useTitle');
188
		$firstParameter = $this->_registry->get('firstParameter');
189
		$secondParameter = $this->_registry->get('secondParameter');
190
		$firstTable = $this->_registry->get('firstTable');
191 16
		$fullRoute = $this->_registry->get('fullRoute');
192
		$lastId = $this->_registry->get('lastId');
193 1
194
		/* handle title */
195
196
		if ($title)
197
		{
198 15
			$this->_breadcrumbArray[$key]['title'] = $title;
199
		}
200 1
201
		/* handle home */
202
203
		else if (!$fullRoute)
204
		{
205 14
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('home');
206
		}
207 2
208
		/* handle administration */
209
210
		else if ($firstParameter === 'admin')
211
		{
212 12
			$this->_createAdmin($key);
213
		}
214 3
215
		/* handle login */
216 1
217
		else if ($firstParameter === 'login')
218 2
		{
219
			if ($secondParameter === 'recover')
220 1
			{
221
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('recovery');
222
			}
223
			else if ($secondParameter === 'reset')
224 3
			{
225
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('reset');
226
			}
227
			else
228
			{
229
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('login');
230 9
			}
231
		}
232
233
		/* handle logout */
234
235
		else if ($firstParameter === 'logout')
236
		{
237 9
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('logout');
238
		}
239 1
240
		/* handle register */
241
242
		else if ($firstParameter === 'register')
243
		{
244 8
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('registration');
245
		}
246 1
247
		/* handle module */
248
249
		else if ($firstParameter === 'module')
250
		{
251 7
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('module');
252
		}
253 1
254
		/* handle search */
255
256
		else if ($firstParameter === 'search')
257
		{
258 6
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('search');
259
		}
260 1
261
		/* handle error */
262
263
		else if (!$lastId)
264
		{
265 5
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('error');
266
		}
267 5
268
		/* handle content */
269 16
270
		else if ($firstTable)
271
		{
272
			$this->_createContent($key);
273
		}
274
	}
275
276
	/**
277
	 * create the breadcrumb array for the administration
278
	 *
279 2
	 * @since 2.1.0
280
	 *
281 2
	 * @param int $key key of the item
282 2
	 */
283 2
284 2
	protected function _createAdmin(int $key = 0)
285
	{
286
		$adminParameter = $this->_registry->get('adminParameter');
287
		$tableParameter = $this->_registry->get('tableParameter');
288 2
		$lastParameter = $this->_registry->get('lastParameter');
289
		$fullRoute = $this->_registry->get('fullRoute');
290
291
		/* join first title */
292 2
293
		$this->_breadcrumbArray[$key]['title'] = $this->_language->get('administration');
294 1
295
		/* admin parameter */
296
297
		if ($adminParameter)
298
		{
299 2
			$this->_breadcrumbArray[$key]['route'] = 'admin';
300
		}
301 1
302 1
		/* join admin title */
303
304
		if ($adminParameter && $this->_language->get($adminParameter))
305
		{
306 1
			$key++;
307
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get($adminParameter);
308 1
309
			/* set the route */
310
311
			if ($adminParameter !== $lastParameter)
312
			{
313 1
				$this->_breadcrumbArray[$key]['route'] = $fullRoute;
314
			}
315 1
316 1
			/* join table title */
317
318
			if ($tableParameter && $this->_language->get($tableParameter))
319 2
			{
320
				$key++;
321
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get($tableParameter);
322
			}
323
		}
324
	}
325
326
	/**
327
	 * create the breadcrumb array for the content
328
	 *
329 5
	 * @since 2.1.0
330
	 *
331 5
	 * @param int $key
332 5
	 */
333 5
334 5
	protected function _createContent(int $key = 0)
335 5
	{
336 5
		$firstParameter = $this->_registry->get('firstParameter');
337 5
		$secondParameter = $this->_registry->get('secondParameter');
338
		$thirdParameter = $this->_registry->get('thirdParameter');
339
		$lastParameter = $this->_registry->get('lastParameter');
340
		$firstTable = $this->_registry->get('firstTable');
341 5
		$secondTable = $this->_registry->get('secondTable');
342
		$thirdTable = $this->_registry->get('thirdTable');
343
344
		/* join first title */
345 5
346
		$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($firstTable)->where('alias', $firstParameter)->findOne()->title;
0 ignored issues
show
It seems like $firstTable defined by $this->_registry->get('firstTable') on line 340 can also be of type array; however, Redaxscript\Db::forTablePrefix() 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...
347 3
348
		/* set the route */
349
350
		if ($firstParameter !== $lastParameter)
351
		{
352 5
			$this->_breadcrumbArray[$key]['route'] = $firstParameter;
353
		}
354 3
355 3
		/* join second title */
356
357
		if ($secondTable)
358
		{
359 3
			$key++;
360
			$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($secondTable)->where('alias', $secondParameter)->findOne()->title;
0 ignored issues
show
It seems like $secondTable defined by $this->_registry->get('secondTable') on line 341 can also be of type array; however, Redaxscript\Db::forTablePrefix() 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...
361 1
362
			/* set the route */
363
364
			if ($secondParameter !== $lastParameter)
365
			{
366 3
				$this->_breadcrumbArray[$key]['route'] = $firstParameter . '/' . $secondParameter;
367
			}
368 1
369 1
			/* join third title */
370
371
			if ($thirdTable)
372 5
			{
373
				$key++;
374
				$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($thirdTable)->where('alias', $thirdParameter)->findOne()->title;
0 ignored issues
show
It seems like $thirdTable defined by $this->_registry->get('thirdTable') on line 342 can also be of type array; however, Redaxscript\Db::forTablePrefix() 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...
375
			}
376
		}
377
	}
378
}
379