Completed
Push — master ( 3d4ee7...df55da )
by Andreas
14:46
created

midcom_helper_nav::get_node_uplink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Main Navigation interface class.
11
 *
12
 * Basically, this class proxies all requests to a midcom_helper_nav_backend
13
 * class. See the interface definition of it for further details.
14
 *
15
 * Additionally this class implements a couple of helper functions to make
16
 * common NAP tasks easier.
17
 *
18
 * <b>Important note:</b> Whenever you add new code to this class, or extend it through
19
 * inheritance, never call the proxy-functions of the backend directly, this is strictly
20
 * forbidden.
21
 *
22
 * @todo End-User documentation of node and leaf data, as the one in the backend is incomplete too.
23
 * @package midcom.helper
24
 * @see midcom_helper_nav_backend
25
 */
26
class midcom_helper_nav
27
{
28
    /**
29
     * The backend instance in use.
30
     *
31
     * @var midcom_helper_nav_backend
32
     */
33
    private $_backend;
34
35
    /**
36
     * The cache of instantiated NAP backends
37
     *
38
     * @var array
39
     */
40
    private static $_backends = [];
41
42
    /**
43
     * The context ID we're associated with.
44
     *
45
     * @var midcom_core_context
46
     */
47
    private $context;
48
49
    /**
50
     * Create a NAP instance for the currently active context
51
     */
52 437
    public function __construct()
53
    {
54 437
        $this->context = midcom_core_context::get();
55 437
        $this->_backend = $this->_get_backend();
56 437
    }
57
58
    /**
59
     * This function maintains one NAP Class per context. Usually this is enough,
60
     * since you mostly will access it in context 0, the default. The problem is, that
61
     * this is not 100% efficient: If you instantiate two different NAP Classes in
62
     * different contexts both referring to the same root node, you will get two
63
     * different instances.
64
     *
65
     * @return midcom_helper_nav_backend The backend instance in the cache.
66
     * @see midcom_helper_nav
67
     */
68 437
    private function _get_backend()
69
    {
70 437
        if (!isset(self::$_backends[$this->context->id])) {
71 295
            $root = $this->context->get_key(MIDCOM_CONTEXT_ROOTTOPIC);
72 295
            $urltopics = $this->context->get_key(MIDCOM_CONTEXT_URLTOPICS);
73 295
            self::$_backends[$this->context->id] = new midcom_helper_nav_backend($root, $urltopics);
0 ignored issues
show
Bug introduced by
It seems like $urltopics can also be of type false; however, parameter $urltopics of midcom_helper_nav_backend::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            self::$_backends[$this->context->id] = new midcom_helper_nav_backend($root, /** @scrutinizer ignore-type */ $urltopics);
Loading history...
Bug introduced by
It seems like $root can also be of type false; however, parameter $root of midcom_helper_nav_backend::__construct() does only seem to accept midcom_db_topic, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            self::$_backends[$this->context->id] = new midcom_helper_nav_backend(/** @scrutinizer ignore-type */ $root, $urltopics);
Loading history...
74
        }
75
76 437
        return self::$_backends[$this->context->id];
77
    }
78
79
    /* The following methods are just interfaces to midcom_helper_nav_backend */
80
81
    /**
82
     * Retrieve the ID of the currently displayed node. Defined by the topic of
83
     * the component that declared able to handle the request.
84
     *
85
     * @return int    The ID of the node in question.
86
     * @see midcom_helper_nav_backend::get_current_node()
87
     */
88 249
    public function get_current_node()
89
    {
90 249
        return $this->_backend->get_current_node();
91
    }
92
93
    /**
94
     * Retrieve the ID of the currently displayed leaf. This is a leaf that is
95
     * displayed by the handling topic. If no leaf is active, this function
96
     * returns false. (Remember to make a type sensitive check, e.g.
97
     * nav::get_current_leaf() !== false to distinguish '0' and 'false'.)
98
     *
99
     * @return int    The ID of the leaf in question or false on failure.
100
     * @see midcom_helper_nav_backend::get_current_leaf()
101
     */
102 275
    public function get_current_leaf()
103
    {
104 275
        return $this->_backend->get_current_leaf();
105
    }
106
107
    /**
108
     * Retrieve the ID of the root node. Note that this ID is dependent from the
109
     * ID of the MidCOM Root topic and therefore will change as easily as the
110
     * root topic ID might. The MIDCOM_NAV_URL entry of the root node's data will
111
     * always be empty.
112
     *
113
     * @return int    The ID of the root node.
114
     * @see midcom_helper_nav_backend::get_root_node()
115
     */
116 107
    public function get_root_node()
117
    {
118 107
        return $this->_backend->get_root_node();
119
    }
120
121
    /**
122
     * Lists all Sub-nodes of $parent_node. If there are no subnodes you will get
123
     * an empty array, if there was an error (for instance an unknown parent node
124
     * ID) you will get false.
125
     *
126
     * @param int $parent_node    The id of the node of which the subnodes are searched.
127
     * @param boolean $show_noentry Show all objects on-site which have the noentry flag set.
128
     *     This defaults to false.
129
     * @return Array            An Array of Node IDs or false on failure.
130
     * @see midcom_helper_nav_backend::list_nodes()
131
     */
132 21
    public function list_nodes($parent_node, $show_noentry = false)
133
    {
134 21
        return $this->_backend->list_nodes($parent_node, $show_noentry);
135
    }
136
137
    /**
138
     * Lists all leaves of $parent_node. If there are no leaves you will get an
139
     * empty array, if there was an error (for instance an unknown parent node ID)
140
     * you will get false.
141
     *
142
     * @param int $parent_node    The ID of the node of which the leaves are searched.
143
     * @param boolean $show_noentry Show all objects on-site which have the noentry flag set.
144
     *     This defaults to false.
145
     * @return Array             A list of leaves found, or false on failure.
146
     * @see midcom_helper_nav_backend::list_leaves()
147
     */
148 19
    public function list_leaves($parent_node, $show_noentry = false)
149
    {
150 19
        return $this->_backend->list_leaves($parent_node, $show_noentry);
151
    }
152
153
    /**
154
     * This will give you a key-value pair describing the node with the ID
155
     * $node_id. The defined keys are described above in Node data interchange
156
     * format. You will get false if the node ID is invalid.
157
     *
158
     * @param int $node_id    The node ID to be retrieved.
159
     * @return Array        The node data as outlined in the class introduction, false on failure
160
     * @see midcom_helper_nav_backend::get_node()
161
     */
162 322
    public function get_node($node_id)
163
    {
164 322
        return $this->_backend->get_node($node_id);
165
    }
166
167
    /**
168
     * This will give you a key-value pair describing the leaf with the ID
169
     * $node_id. The defined keys are described above in leaf data interchange
170
     * format. You will get false if the leaf ID is invalid.
171
     *
172
     * @param string $leaf_id    The leaf-id to be retrieved.
173
     * @return Array        The leaf-data as outlined in the class introduction, false on failure
174
     * @see midcom_helper_nav_backend::get_leaf()
175
     */
176 45
    public function get_leaf($leaf_id)
177
    {
178 45
        return $this->_backend->get_leaf($leaf_id);
179
    }
180
181
    /**
182
     * Returns the ID of the node to which $leaf_id is associated to, false
183
     * on failure.
184
     *
185
     * @param string $leaf_id    The Leaf-ID to search an uplink for.
186
     * @return int             The ID of the Node for which we have a match, or false on failure.
187
     * @see midcom_helper_nav_backend::get_leaf_uplink()
188
     */
189
    function get_leaf_uplink($leaf_id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
190
    {
191
        return $this->_backend->get_leaf_uplink($leaf_id);
192
    }
193
194
    /**
195
     * Returns the ID of the node to which $node_id is associated to, false
196
     * on failure. The root node's uplink is -1.
197
     *
198
     * @param int $node_id    The Leaf-ID to search an uplink for.
199
     * @return int             The ID of the Node for which we have a match, -1 for the root node, or false on failure.
200
     * @see midcom_helper_nav_backend::get_node_uplink()
201
     */
202 85
    public function get_node_uplink($node_id)
203
    {
204 85
        return $this->_backend->get_node_uplink($node_id);
205
    }
206
207
    /**
208
     * Checks if the given node is within the tree of another node.
209
     *
210
     * @param int    $node_id    The node in question.
211
     * @param int    $root_id    The root node to use.
212
     * @return boolean                True, if the node is a subnode of the root node, false otherwise.
213
     */
214 85
    public function is_node_in_tree($node_id, $root_id)
215
    {
216 85
        $uplink = $this->get_node_uplink($node_id);
217 85
        if ($uplink == $root_id) {
218 34
            return true;
219
        }
220 56
        if (   $uplink == false
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $uplink of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
221 56
            || $uplink == -1) {
222 56
            return false;
223
        }
224 11
        return $this->is_node_in_tree($uplink, $root_id);
225
    }
226
227
    /**
228
     * List all child elements, nodes and leaves alike, of the node with ID
229
     * $parent_node_id. For every child element, an array of ID and type (node/leaf)
230
     * is given as
231
     *
232
     * - MIDCOM_NAV_ID => 0,
233
     * - MIDCOM_NAV_TYPE => 'node'
234
     *
235
     * If there are no child elements at all the method will return an empty array,
236
     * in case of an error false.  NOTE: This method should be quite slow, there's
237
     * room for improvement... :-)
238
     *
239
     * @param int $parent_node_id    The ID of the parent node.
240
     * @return Array                A list of found elements, or false on failure.
241
     */
242 11
    public function list_child_elements($parent_node_id)
243
    {
244 11
        $parent_node = $this->get_node($parent_node_id);
245 11
        if (!$parent_node) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parent_node of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
246
            return false;
247
        }
248
249 11
        $guid = $parent_node[MIDCOM_NAV_GUID];
250 11
        $navorder = (int) midcom_db_parameter::get_by_objectguid($guid, 'midcom.helper.nav', 'navorder');
251 11
        if ($navorder == MIDCOM_NAVORDER_ARTICLESFIRST) {
252
            $navorder = 'articlesfirst';
253 11
        } elseif ($navorder == MIDCOM_NAVORDER_SCORE) {
254
            $navorder = 'score';
255
        } else {
256 11
            $navorder = 'topicsfirst';
257
        }
258
259 11
        $nav_object = midcom_helper_nav_itemlist::factory($navorder, $this, $parent_node_id);
260 11
        return $nav_object->get_sorted_list();
261
    }
262
263
    /**
264
     * Try to resolve a guid into a NAP object.
265
     *
266
     * The code is optimized trying to avoid a full-scan if possible. To do this it
267
     * will treat topic and article guids specially: In both cases the system will
268
     * translate it using the topic id into a node id and scan only that part of the
269
     * tree non-recursively.
270
     *
271
     * A full scan of the NAP data is only done if another MidgardObject is used.
272
     *
273
     * Note: If you want to resolve a GUID you got from a Permalink, use the Permalinks
274
     * service within MidCOM, as it covers more objects than the NAP listings.
275
     *
276
     * @param string $guid The GUID of the object to be looked up.
277
     * @param boolean $node_is_sufficient if we could return a good guess of correct parent node but said node does not list the $guid in leaves return the node or try to do a full (and very expensive) NAP scan ?
278
     * @return mixed Either a node or leaf structure, distinguishable by MIDCOM_NAV_TYPE, or false on failure.
279
     * @see midcom_services_permalinks
280
     */
281 28
    public function resolve_guid($guid, $node_is_sufficient = false)
282
    {
283
        // First, check if the GUID is already known by the backend:
284 28
        if ($cached_result = $this->_backend->get_loaded_object_by_guid($guid)) {
285 4
            debug_add('The GUID was already known by the backend instance, returning the cached copy directly.');
286 4
            return $cached_result;
287
        }
288
289
        // Fetch the object in question for a start, so that we know what to do (tm)
290
        // Note, that objects that cannot be resolved will still be processed using a full-scan of
291
        // the tree. This is, for example, used by the on-delete cache invalidation.
292
        try {
293 27
            $object = midcom::get()->dbfactory->get_object_by_guid($guid);
294
        } catch (midcom_error $e) {
295
            debug_add("Could not load GUID {$guid}, trying to continue anyway. Last error was: " . $e->getMessage(), MIDCOM_LOG_WARN);
296
        }
297 27
        if (!empty($object)) {
298 27
            if ($object instanceof midcom_db_topic) {
299
                // Ok. This topic should be within the content tree,
300
                // we check this and return the node if everything is ok.
301 20
                if (!$this->is_node_in_tree($object->id, $this->get_root_node())) {
302 18
                    debug_add("The GUID {$guid} leads to an unknown topic not in our tree.", MIDCOM_LOG_WARN);
303 18
                    return false;
304
                }
305
306 2
                return $this->get_node($object->id);
307
            }
308
309 8
            if ($object instanceof midcom_db_article) {
310
                // Ok, let's try to find the article using the topic in the tree.
311
                if (!$this->is_node_in_tree($object->topic, $this->get_root_node())) {
312
                    debug_add("The GUID {$guid} leads to an unknown topic not in our tree.", MIDCOM_LOG_WARN);
313
                    return false;
314
                }
315
                if ($leaf = $this->_find_leaf_in_topic($object->topic, $guid)) {
316
                    return $leaf;
317
                }
318
319
                debug_add("The Article GUID {$guid} is somehow hidden from the NAP data in its topic, no results shown.", MIDCOM_LOG_INFO);
320
                return false;
321
            }
322
323
            // Ok, unfortunately, this is not an immediate topic. We try to traverse
324
            // upwards in the object chain to find a topic.
325 8
            if ($topic = $this->find_closest_topic($object)) {
326
                debug_add("Found topic #{$topic->id}, searching the leaves");
327
                if ($leaf = $this->_find_leaf_in_topic($topic->id, $guid)) {
328
                    return $leaf;
329
                }
330
                if ($node_is_sufficient) {
331
                    debug_add("Could not find guid in leaves (maybe not listed?), but node is sufficient, returning node");
332
                    return $this->get_node($topic->id);
333
                }
334
            }
335
        }
336
337
        // this is the rest of the lot, we need to traverse everything, unfortunately.
338
        // First, we traverse a list of nodes to be checked on by one, avoiding a recursive
339
        // function call.
340 8
        $unprocessed_node_ids = [$this->get_root_node()];
341
342 8
        while (!empty($unprocessed_node_ids)) {
343 8
            $node_id = array_shift($unprocessed_node_ids);
344
345
            // Check leaves of this node first.
346 8
            if ($leaf = $this->_find_leaf_in_topic($node_id, $guid)) {
347
                return $leaf;
348
            }
349
350
            // Ok, append all subnodes to the queue.
351 8
            $unprocessed_node_ids = array_merge($unprocessed_node_ids, $this->list_nodes($node_id));
352
        }
353
354 8
        debug_add("We were unable to find the GUID {$guid} in the MidCOM tree even with a full scan.", MIDCOM_LOG_INFO);
355 8
        return false;
356
    }
357
358 8
    private function _find_leaf_in_topic($topic, $guid)
359
    {
360 8
        foreach ($this->list_leaves($topic, true) as $leafid) {
361
            $leaf = $this->get_leaf($leafid);
362
            if ($leaf[MIDCOM_NAV_GUID] == $guid) {
363
                return $leaf;
364
            }
365
        }
366 8
        return false;
367
    }
368
369 268
    public function find_closest_topic($object)
370
    {
371 268
        if (!is_object($object)) {
372
            return null;
373
        }
374 268
        debug_add('Looking for a topic to use via get_parent()');
375 268
        while ($parent = $object->get_parent()) {
376 142
            if (is_a($parent, midcom_db_topic::class)) {
377
                // Verify that this topic is within the current sites tree, if it is not,
378
                // we ignore it.
379 65
                if ($this->is_node_in_tree($parent->id, $this->get_root_node())) {
380 32
                    return $parent;
381
                }
382
            }
383 127
            $object = $parent;
384
        }
385 243
        return null;
386
    }
387
388
    /* The more complex interface methods starts here */
389
390
    /**
391
     * Construct a breadcrumb line.
392
     *
393
     * Gives you a line like 'Start > Topic1 > Topic2 > Article' using NAP to
394
     * traverse upwards till the root node. $separator is inserted between the
395
     * pairs, $class, if non-null, will be used as CSS-class for the A-Tags.
396
     *
397
     * The parameter skip_levels indicates how much nodes should be skipped at
398
     * the beginning of the current path. Default is to show the complete path. A
399
     * value of 1 will skip the home link, 2 will skip the home link and the first
400
     * subtopic and so on. If a leaf or node is selected, that normally would be
401
     * hidden, only its name will be shown.
402
     *
403
     * @param string    $separator        The separator to use between the elements.
404
     * @param string    $class            If not-null, it will be assigned to all A tags.
405
     * @param int       $skip_levels      The number of topic levels to skip before starting to work (use this to skip 'Home' links etc.).
406
     * @param string    $current_class    The class that should be assigned to the currently active element.
407
     * @param array     $skip_guids       Array of guids that are skipped.
408
     * @return string    The computed breadcrumb line.
409
     */
410 9
    public function get_breadcrumb_line($separator = ' &gt; ', $class = null, $skip_levels = 0, $current_class = null, $skip_guids = [])
411
    {
412 9
        $breadcrumb_data = $this->get_breadcrumb_data();
413 9
        $result = '';
414
415
        // We traverse this list using the iterator of the array, since this allows
416
        // us direct treatment of the final element.
417 9
        reset($breadcrumb_data);
418
419
        // Detect real starting Node
420 9
        if ($skip_levels > 0) {
421
            if ($skip_levels >= count($breadcrumb_data)) {
422
                debug_add('We were asked to skip all breadcrumb elements that were present (or even more). Returning an empty breadcrumb line therefore.', MIDCOM_LOG_INFO);
423
                return '';
424
            }
425
            $breadcrumb_data = array_slice($breadcrumb_data, $skip_levels);
426
        }
427
428 9
        $class = $class === null ? '' : ' class="' . $class . '"';
429 9
        while (current($breadcrumb_data) !== false) {
430 9
            $data = current($breadcrumb_data);
431 9
            $entry = htmlspecialchars($data[MIDCOM_NAV_NAME]);
432
433
            // Add the next element sensitive to the fact whether we are at the end or not.
434 9
            if (next($breadcrumb_data) === false) {
435 9
                if ($current_class !== null) {
436 9
                    $entry = "<span class=\"{$current_class}\">{$entry}</span>";
437
                }
438
            } else {
439 9
                if (   !empty($data['napobject'][MIDCOM_NAV_GUID])
440 9
                    && in_array($data['napobject'][MIDCOM_NAV_GUID], $skip_guids)) {
441
                    continue;
442
                }
443
444 9
                $entry = "<a href=\"{$data[MIDCOM_NAV_URL]}\"{$class}>{$entry}</a>{$separator}";
445
            }
446 9
            $result .= $entry;
447
        }
448
449 9
        return $result;
450
    }
451
452
    /**
453
     * Construct source data for a breadcrumb line.
454
     *
455
     * Gives you the data needed to construct a line like
456
     * 'Start > Topic1 > Topic2 > Article' using NAP to
457
     * traverse upwards till the root node. The components custom breadcrumb
458
     * data is inserted at the end of the computed breadcrumb line after any
459
     * set NAP leaf.
460
     *
461
     * See get_breadcrumb_line for a more end-user oriented way of life.
462
     *
463
     * <b>Return Value</b>
464
     *
465
     * The breadcrumb data will be returned as a list of associative arrays each
466
     * containing these keys:
467
     *
468
     * - MIDCOM_NAV_URL The fully qualified URL to the node.
469
     * - MIDCOM_NAV_NAME The clear-text name of the node.
470
     * - MIDCOM_NAV_TYPE One of 'node', 'leaf', 'custom' indicating what type of entry
471
     *   this is.
472
     * - MIDCOM_NAV_ID The Identifier of the structure used to build this entry, this is
473
     *   either a NAP node/leaf ID or the list key set by the component for custom data.
474
     * - 'napobject' This contains the original NAP object retrieved by the function.
475
     *   Just in case you need more information than is available directly.
476
     *
477
     * The entry of every level is indexed by its MIDCOM_NAV_ID, where custom keys preserve
478
     * their original key (as passed by the component) and prefixing it with 'custom-'. This
479
     * allows you to easily check if a given node/leave is within the current breadcrumb-line
480
     * by checking with array_key_exists.
481
     *
482
     * <b>Adding custom data</b>
483
     *
484
     * Custom elements are added to this array by using the MidCOM custom component context
485
     * at this time. You need to add a list with the same structure as above into the
486
     * custom component context key <i>midcom.helper.nav.breadcrumb</i>. (This needs
487
     * to be an array always, even if you return only one element.)
488
     *
489
     * Note, that the URL you pass in that list is always prepended with the current anchor
490
     * prefix. It is not possible to specify absolute URLs there. No leading slash is required.
491
     *
492
     * Example:
493
     *
494
     * <code>
495
     * $tmp = [
496
     *     [
497
     *         MIDCOM_NAV_URL => "list/{$this->_category}/{$this->_mode}/1/",
498
     *         MIDCOM_NAV_NAME => $this->_category_name,
499
     *     ],
500
     * ];
501
     * midcom_core_context::get()->set_custom_key('midcom.helper.nav.breadcrumb', $tmp);
502
     * </code>
503
     *
504
     * @return array The computed breadcrumb data as outlined above.
505
     */
506 9
    public function get_breadcrumb_data($id = null)
507
    {
508 9
        $prefix = $this->context->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
509 9
        $result = [];
510
511 9
        if (!$id) {
512 9
            $curr_leaf = $this->get_current_leaf();
513 9
            $curr_node = $this->get_current_node();
514
        } else {
515
            $curr_leaf = $this->get_leaf($id);
516
            $curr_node = -1;
517
518
            if (!$curr_leaf) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $curr_leaf of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
519
                if ($node = $this->get_node($id)) {
520
                    $curr_node = $node[MIDCOM_NAV_ID];
521
                }
522
            } else {
523
                $curr_node = $this->get_node($curr_leaf[MIDCOM_NAV_NODEID]);
524
            }
525
        }
526 9
        foreach ($this->get_node_path($curr_node) as $node_id) {
527 9
            $node = $this->get_node($node_id);
528 9
            $result[$node[MIDCOM_NAV_ID]] = [
529 9
                MIDCOM_NAV_URL => $node[MIDCOM_NAV_ABSOLUTEURL],
530
                MIDCOM_NAV_NAME => $node[MIDCOM_NAV_NAME],
531 9
                MIDCOM_NAV_TYPE => 'node',
532 9
                MIDCOM_NAV_ID => $node_id,
533 9
                'napobject' => $node,
534
            ];
535
        }
536 9
        if ($curr_leaf !== false) {
0 ignored issues
show
introduced by
The condition $curr_leaf !== false is always true.
Loading history...
537
            $leaf = $this->get_leaf($curr_leaf);
0 ignored issues
show
Bug introduced by
It seems like $curr_leaf can also be of type array; however, parameter $leaf_id of midcom_helper_nav::get_leaf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

537
            $leaf = $this->get_leaf(/** @scrutinizer ignore-type */ $curr_leaf);
Loading history...
538
539
            // Ignore Index Article Leaves
540
            if ($leaf[MIDCOM_NAV_URL] != '') {
541
                $result[$leaf[MIDCOM_NAV_ID]] = [
542
                    MIDCOM_NAV_URL => $leaf[MIDCOM_NAV_ABSOLUTEURL],
543
                    MIDCOM_NAV_NAME => $leaf[MIDCOM_NAV_NAME],
544
                    MIDCOM_NAV_TYPE => 'leaf',
545
                    MIDCOM_NAV_ID => $curr_leaf,
546
                    'napobject' => $leaf,
547
                ];
548
            }
549
        }
550
551 9
        if (midcom_core_context::get()->has_custom_key('midcom.helper.nav.breadcrumb')) {
552 6
            $customdata = midcom_core_context::get()->get_custom_key('midcom.helper.nav.breadcrumb');
0 ignored issues
show
Bug introduced by
'midcom.helper.nav.breadcrumb' of type string is incompatible with the type integer expected by parameter $key of midcom_core_context::get_custom_key(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

552
            $customdata = midcom_core_context::get()->get_custom_key(/** @scrutinizer ignore-type */ 'midcom.helper.nav.breadcrumb');
Loading history...
553 6
            if (is_array($customdata)) {
554 6
                foreach ($customdata as $key => $entry) {
555 6
                    $id = "custom-{$key}";
556
557 6
                    $url = "{$prefix}{$entry[MIDCOM_NAV_URL]}";
558 6
                    if (   substr($entry[MIDCOM_NAV_URL], 0, 1) == '/'
559 6
                        || preg_match('|^https?://|', $entry[MIDCOM_NAV_URL])) {
560 6
                        $url = $entry[MIDCOM_NAV_URL];
561
                    }
562
563 6
                    $result[$id] = [
564 6
                        MIDCOM_NAV_URL => $url,
565
                        MIDCOM_NAV_NAME => $entry[MIDCOM_NAV_NAME],
566 6
                        MIDCOM_NAV_TYPE => 'custom',
567 6
                        MIDCOM_NAV_ID => $id,
568 6
                        'napobject' => $entry,
569
                    ];
570
                }
571
            }
572
        }
573 9
        return $result;
574
    }
575
576
    /**
577
     * Retrieve the IDs of the nodes from the URL. First value at key 0 is
578
     * the root node ID, possible second value is the first subnode ID etc.
579
     * Contains only visible nodes (nodes which can be loaded).
580
     *
581
     * @return Array    The node path array.
582
     */
583 11
    public function get_node_path($node_id = null)
584
    {
585 11
        if ($node_id === null) {
586 11
            return $this->_backend->get_node_path();
587
        }
588 9
        $path = [];
589 9
        $node = $this->get_node($node_id);
590 9
        while ($node) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
591 9
            $path[] = $node[MIDCOM_NAV_ID];
592 9
            if ($node[MIDCOM_NAV_NODEID] === -1) {
593 9
                break;
594
            }
595 5
            $node = $this->get_node($node[MIDCOM_NAV_NODEID]);
596
        }
597 9
        return array_reverse($path);
598
    }
599
600
    /**
601
     * Retrieve the ID of the upper node of the currently displayed node.
602
     *
603
     * @return mixed    The ID of the node in question.
604
     */
605 1
    public function get_current_upper_node()
606
    {
607 1
        return $this->_backend->get_current_upper_node();
608
    }
609
}
610