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