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