|
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
|
|
|
* @var midcom_helper_nav_backend |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_backend; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var midcom_helper_nav_backend[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $_backends = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var midcom_core_context |
|
40
|
|
|
*/ |
|
41
|
|
|
private $context; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a NAP instance for the currently active context |
|
45
|
|
|
*/ |
|
46
|
449 |
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
449 |
|
$this->context = midcom_core_context::get(); |
|
49
|
449 |
|
$this->_backend = $this->_get_backend(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* This function maintains one NAP Class per context. Usually this is enough, |
|
54
|
|
|
* since you mostly will access it in context 0, the default. The problem is, that |
|
55
|
|
|
* this is not 100% efficient: If you instantiate two different NAP Classes in |
|
56
|
|
|
* different contexts both referring to the same root node, you will get two |
|
57
|
|
|
* different instances. |
|
58
|
|
|
* |
|
59
|
|
|
* @see midcom_helper_nav |
|
60
|
|
|
*/ |
|
61
|
449 |
|
private function _get_backend() : midcom_helper_nav_backend |
|
62
|
|
|
{ |
|
63
|
449 |
|
if (!isset(self::$_backends[$this->context->id])) { |
|
64
|
286 |
|
$root = $this->context->get_key(MIDCOM_CONTEXT_ROOTTOPIC); |
|
65
|
286 |
|
$urltopics = $this->context->get_key(MIDCOM_CONTEXT_URLTOPICS); |
|
66
|
286 |
|
self::$_backends[$this->context->id] = new midcom_helper_nav_backend($root, $urltopics); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
449 |
|
return self::$_backends[$this->context->id]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* The following methods are just interfaces to midcom_helper_nav_backend */ |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve the ID of the currently displayed node. Defined by the topic of |
|
76
|
|
|
* the component that declared able to handle the request. |
|
77
|
|
|
* |
|
78
|
|
|
* @return int The ID of the node in question. |
|
79
|
|
|
* @see midcom_helper_nav_backend::get_current_node() |
|
80
|
|
|
*/ |
|
81
|
240 |
|
public function get_current_node() |
|
82
|
|
|
{ |
|
83
|
240 |
|
return $this->_backend->get_current_node(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve the ID of the currently displayed leaf. This is a leaf that is |
|
88
|
|
|
* displayed by the handling topic. If no leaf is active, this function |
|
89
|
|
|
* returns false. (Remember to make a type sensitive check, e.g. |
|
90
|
|
|
* nav::get_current_leaf() !== false to distinguish '0' and 'false'.) |
|
91
|
|
|
* |
|
92
|
|
|
* @return int The ID of the leaf in question or false on failure. |
|
93
|
|
|
* @see midcom_helper_nav_backend::get_current_leaf() |
|
94
|
|
|
*/ |
|
95
|
257 |
|
public function get_current_leaf() |
|
96
|
|
|
{ |
|
97
|
257 |
|
return $this->_backend->get_current_leaf(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Retrieve the ID of the root node. Note that this ID is dependent from the |
|
102
|
|
|
* ID of the MidCOM Root topic and therefore will change as easily as the |
|
103
|
|
|
* root topic ID might. The MIDCOM_NAV_URL entry of the root node's data will |
|
104
|
|
|
* always be empty. |
|
105
|
|
|
* |
|
106
|
|
|
* @see midcom_helper_nav_backend::get_root_node() |
|
107
|
|
|
*/ |
|
108
|
97 |
|
public function get_root_node() : int |
|
109
|
|
|
{ |
|
110
|
97 |
|
return $this->_backend->get_root_node(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Lists all Sub-nodes of $parent_node. If there are no subnodes you will get |
|
115
|
|
|
* an empty array |
|
116
|
|
|
* |
|
117
|
|
|
* @param boolean $show_noentry Show all objects on-site which have the noentry flag set. |
|
118
|
|
|
* This defaults to false. |
|
119
|
|
|
* @see midcom_helper_nav_backend::list_nodes() |
|
120
|
|
|
*/ |
|
121
|
12 |
|
public function get_nodes(int $parent_node_id, bool $show_noentry = false) : array |
|
122
|
|
|
{ |
|
123
|
12 |
|
return array_map([$this, 'get_node'], $this->_backend->list_nodes($parent_node_id, $show_noentry)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Lists all leaves of $parent_node. If there are no leaves you will get an |
|
128
|
|
|
* empty array. |
|
129
|
|
|
* |
|
130
|
|
|
* @param boolean $show_noentry Show all objects on-site which have the noentry flag set. |
|
131
|
|
|
* This defaults to false. |
|
132
|
|
|
* @see midcom_helper_nav_backend::list_leaves() |
|
133
|
|
|
*/ |
|
134
|
17 |
|
public function get_leaves(int $parent_node_id, bool $show_noentry = false) : array |
|
135
|
|
|
{ |
|
136
|
17 |
|
return array_map([$this, 'get_leaf'], $this->_backend->list_leaves($parent_node_id, $show_noentry)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* This will give you a key-value pair describing the node with the ID |
|
141
|
|
|
* $node_id. The defined keys are described above in Node data interchange |
|
142
|
|
|
* format. You will get false if the node ID is invalid. |
|
143
|
|
|
* |
|
144
|
|
|
* @see midcom_helper_nav_backend::get_node() |
|
145
|
|
|
*/ |
|
146
|
307 |
|
public function get_node($node_id) : ?array |
|
147
|
|
|
{ |
|
148
|
307 |
|
return $this->_backend->get_node($node_id); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* This will give you a key-value pair describing the leaf with the ID |
|
153
|
|
|
* $node_id. The defined keys are described above in leaf data interchange |
|
154
|
|
|
* format. You will get false if the leaf ID is invalid. |
|
155
|
|
|
* |
|
156
|
|
|
* @see midcom_helper_nav_backend::get_leaf() |
|
157
|
|
|
*/ |
|
158
|
40 |
|
public function get_leaf(string $leaf_id) : ?array |
|
159
|
|
|
{ |
|
160
|
40 |
|
return $this->_backend->get_leaf($leaf_id); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the ID of the node to which $leaf_id is associated to, false |
|
165
|
|
|
* on failure. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $leaf_id The Leaf-ID to search an uplink for. |
|
168
|
|
|
* @return int The ID of the Node for which we have a match, or false on failure. |
|
169
|
|
|
* @see midcom_helper_nav_backend::get_leaf_uplink() |
|
170
|
|
|
*/ |
|
171
|
|
|
function get_leaf_uplink($leaf_id) |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
|
|
return $this->_backend->get_leaf_uplink($leaf_id); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns the ID of the node to which $node_id is associated to, false |
|
178
|
|
|
* on failure. The root node's uplink is -1. |
|
179
|
|
|
* |
|
180
|
|
|
* @param int $node_id The Leaf-ID to search an uplink for. |
|
181
|
|
|
* @return int The ID of the Node for which we have a match, -1 for the root node, or false on failure. |
|
182
|
|
|
* @see midcom_helper_nav_backend::get_node_uplink() |
|
183
|
|
|
*/ |
|
184
|
73 |
|
public function get_node_uplink($node_id) |
|
185
|
|
|
{ |
|
186
|
73 |
|
return $this->_backend->get_node_uplink($node_id); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Checks if the given node is within the tree of another node. |
|
191
|
|
|
* |
|
192
|
|
|
* @param int $node_id The node in question. |
|
193
|
|
|
* @param int $root_id The root node to use. |
|
194
|
|
|
*/ |
|
195
|
78 |
|
public function is_node_in_tree($node_id, $root_id) : bool |
|
196
|
|
|
{ |
|
197
|
78 |
|
if ($node_id == $root_id) { |
|
198
|
9 |
|
return true; |
|
199
|
|
|
} |
|
200
|
73 |
|
$uplink = $this->get_node_uplink($node_id); |
|
201
|
|
|
|
|
202
|
73 |
|
if ($uplink == $root_id) { |
|
203
|
18 |
|
return true; |
|
204
|
|
|
} |
|
205
|
55 |
|
if (in_array($uplink, [false, -1])) { |
|
206
|
55 |
|
return false; |
|
207
|
|
|
} |
|
208
|
6 |
|
return $this->is_node_in_tree($uplink, $root_id); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* List all child elements, nodes and leaves alike, of the node with ID |
|
213
|
|
|
* $parent_node_id. For every child element, an array of ID and type (node/leaf) |
|
214
|
|
|
* is given as |
|
215
|
|
|
* |
|
216
|
|
|
* - MIDCOM_NAV_ID => 0, |
|
217
|
|
|
* - MIDCOM_NAV_TYPE => 'node' |
|
218
|
|
|
* |
|
219
|
|
|
* If there are no child elements at all the method will return an empty array, |
|
220
|
|
|
* in case of an error false. NOTE: This method should be quite slow, there's |
|
221
|
|
|
* room for improvement... :-) |
|
222
|
|
|
*/ |
|
223
|
9 |
|
public function list_child_elements(int $parent_node_id) : ?array |
|
224
|
|
|
{ |
|
225
|
9 |
|
if ($parent_node = $this->get_node($parent_node_id)) { |
|
226
|
9 |
|
$nav_object = midcom_helper_nav_itemlist::factory($this, $parent_node); |
|
227
|
9 |
|
return $nav_object->get_sorted_list(); |
|
228
|
|
|
} |
|
229
|
|
|
return null; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Try to resolve a guid into a NAP object. |
|
234
|
|
|
* |
|
235
|
|
|
* The code is optimized trying to avoid a full-scan if possible. To do this it |
|
236
|
|
|
* will treat topic and article guids specially: In both cases the system will |
|
237
|
|
|
* translate it using the topic id into a node id and scan only that part of the |
|
238
|
|
|
* tree non-recursively. |
|
239
|
|
|
* |
|
240
|
|
|
* A full scan of the NAP data is only done if another MidgardObject is used. |
|
241
|
|
|
* |
|
242
|
|
|
* Note: If you want to resolve a GUID you got from a Permalink, use the Permalinks |
|
243
|
|
|
* service within MidCOM, as it covers more objects than the NAP listings. |
|
244
|
|
|
* |
|
245
|
|
|
* @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 ? |
|
246
|
|
|
* @return ?array Either a node or leaf structure, distinguishable by MIDCOM_NAV_TYPE, or null on failure. |
|
247
|
|
|
* @see midcom_services_permalinks |
|
248
|
|
|
*/ |
|
249
|
28 |
|
public function resolve_guid(string $guid, bool $node_is_sufficient = false) : ?array |
|
250
|
|
|
{ |
|
251
|
|
|
// First, check if the GUID is already known by the backend: |
|
252
|
28 |
|
if ($cached_result = $this->_backend->get_loaded_object_by_guid($guid)) { |
|
253
|
1 |
|
debug_add('The GUID was already known by the backend instance, returning the cached copy directly.'); |
|
254
|
1 |
|
return $cached_result; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
// Fetch the object in question for a start, so that we know what to do (tm) |
|
258
|
|
|
// Note, that objects that cannot be resolved will still be processed using a full-scan of |
|
259
|
|
|
// the tree. This is, for example, used by the on-delete cache invalidation. |
|
260
|
|
|
try { |
|
261
|
27 |
|
$object = midcom::get()->dbfactory->get_object_by_guid($guid); |
|
262
|
|
|
} catch (midcom_error $e) { |
|
263
|
|
|
debug_add("Could not load GUID {$guid}, trying to continue anyway. Last error was: " . $e->getMessage(), MIDCOM_LOG_WARN); |
|
264
|
|
|
} |
|
265
|
27 |
|
if (!empty($object)) { |
|
266
|
27 |
|
if ($object instanceof midcom_db_topic) { |
|
267
|
|
|
// Ok. This topic should be within the content tree, |
|
268
|
|
|
// we check this and return the node if everything is ok. |
|
269
|
20 |
|
if (!$this->is_node_in_tree($object->id, $this->get_root_node())) { |
|
270
|
18 |
|
debug_add("The GUID {$guid} leads to an unknown topic not in our tree.", MIDCOM_LOG_WARN); |
|
271
|
18 |
|
return null; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
2 |
|
return $this->get_node($object->id); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
8 |
|
if ($object instanceof midcom_db_article) { |
|
278
|
|
|
// Ok, let's try to find the article using the topic in the tree. |
|
279
|
|
|
if (!$this->is_node_in_tree($object->topic, $this->get_root_node())) { |
|
280
|
|
|
debug_add("The GUID {$guid} leads to an unknown topic not in our tree.", MIDCOM_LOG_WARN); |
|
281
|
|
|
return null; |
|
282
|
|
|
} |
|
283
|
|
|
if ($leaf = $this->_find_leaf_in_topic($object->topic, $guid)) { |
|
284
|
|
|
return $leaf; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
debug_add("The Article GUID {$guid} is somehow hidden from the NAP data in its topic, no results shown.", MIDCOM_LOG_INFO); |
|
288
|
|
|
return null; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
// Ok, unfortunately, this is not an immediate topic. We try to traverse |
|
292
|
|
|
// upwards in the object chain to find a topic. |
|
293
|
8 |
|
if ($topic = $this->find_closest_topic($object)) { |
|
294
|
|
|
debug_add("Found topic #{$topic->id}, searching the leaves"); |
|
295
|
|
|
if ($leaf = $this->_find_leaf_in_topic($topic->id, $guid)) { |
|
296
|
|
|
return $leaf; |
|
297
|
|
|
} |
|
298
|
|
|
if ($node_is_sufficient) { |
|
299
|
|
|
debug_add("Could not find guid in leaves (maybe not listed?), but node is sufficient, returning node"); |
|
300
|
|
|
return $this->get_node($topic->id); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
// this is the rest of the lot, we need to traverse everything, unfortunately. |
|
306
|
|
|
// First, we traverse a list of nodes to be checked on by one, avoiding a recursive |
|
307
|
|
|
// function call. |
|
308
|
8 |
|
$unprocessed_node_ids = [$this->get_root_node()]; |
|
309
|
|
|
|
|
310
|
8 |
|
while (!empty($unprocessed_node_ids)) { |
|
311
|
8 |
|
$node_id = array_shift($unprocessed_node_ids); |
|
312
|
|
|
|
|
313
|
|
|
// Check leaves of this node first. |
|
314
|
8 |
|
if ($leaf = $this->_find_leaf_in_topic($node_id, $guid)) { |
|
315
|
|
|
return $leaf; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
// Ok, append all subnodes to the queue. |
|
319
|
8 |
|
$unprocessed_node_ids = array_merge($unprocessed_node_ids, $this->_backend->list_nodes($node_id, false)); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
8 |
|
debug_add("We were unable to find the GUID {$guid} in the MidCOM tree even with a full scan.", MIDCOM_LOG_INFO); |
|
323
|
8 |
|
return null; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
8 |
|
private function _find_leaf_in_topic(int $topic, string $guid) : ?array |
|
327
|
|
|
{ |
|
328
|
8 |
|
foreach ($this->get_leaves($topic, true) as $leaf) { |
|
329
|
|
|
if ($leaf[MIDCOM_NAV_GUID] == $guid) { |
|
330
|
|
|
return $leaf; |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
8 |
|
return null; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
275 |
|
public function find_closest_topic(midcom_core_dbaobject $object) : ?midcom_db_topic |
|
337
|
|
|
{ |
|
338
|
275 |
|
debug_add('Looking for a topic to use via get_parent()'); |
|
339
|
275 |
|
while ($parent = $object->get_parent()) { |
|
340
|
|
|
// Verify that this topic is within the current sites tree, if it is not, |
|
341
|
|
|
// we ignore it. |
|
342
|
139 |
|
if ( $parent instanceof midcom_db_topic |
|
343
|
139 |
|
&& $this->is_node_in_tree($parent->id, $this->get_root_node())) { |
|
344
|
22 |
|
return $parent; |
|
345
|
|
|
} |
|
346
|
130 |
|
$object = $parent; |
|
347
|
|
|
} |
|
348
|
254 |
|
return null; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/* The more complex interface methods starts here */ |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Construct a breadcrumb line. |
|
355
|
|
|
* |
|
356
|
|
|
* Gives you a line like 'Start > Topic1 > Topic2 > Article' using NAP to |
|
357
|
|
|
* traverse upwards till the root node. $separator is inserted between the |
|
358
|
|
|
* pairs, $class, if non-null, will be used as CSS-class for the A-Tags. |
|
359
|
|
|
* |
|
360
|
|
|
* The parameter skip_levels indicates how much nodes should be skipped at |
|
361
|
|
|
* the beginning of the current path. Default is to show the complete path. A |
|
362
|
|
|
* value of 1 will skip the home link, 2 will skip the home link and the first |
|
363
|
|
|
* subtopic and so on. If a leaf or node is selected, that normally would be |
|
364
|
|
|
* hidden, only its name will be shown. |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $separator The separator to use between the elements. |
|
367
|
|
|
* @param string $class If not-null, it will be assigned to all A tags. |
|
368
|
|
|
* @param int $skip_levels The number of topic levels to skip before starting to work (use this to skip 'Home' links etc.). |
|
369
|
|
|
* @param string $current_class The class that should be assigned to the currently active element. |
|
370
|
|
|
*/ |
|
371
|
10 |
|
public function get_breadcrumb_line(string $separator = ' > ', string $class = null, int $skip_levels = 0, string $current_class = null, array $skip_guids = []) : string |
|
372
|
|
|
{ |
|
373
|
10 |
|
$breadcrumb_data = $this->get_breadcrumb_data(); |
|
374
|
10 |
|
$result = ''; |
|
375
|
|
|
|
|
376
|
|
|
// Detect real starting Node |
|
377
|
10 |
|
if ($skip_levels > 0) { |
|
378
|
3 |
|
if ($skip_levels >= count($breadcrumb_data)) { |
|
379
|
3 |
|
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); |
|
380
|
3 |
|
return $result; |
|
381
|
|
|
} |
|
382
|
|
|
$breadcrumb_data = array_slice($breadcrumb_data, $skip_levels); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
7 |
|
$class = $class === null ? '' : ' class="' . $class . '"'; |
|
386
|
7 |
|
while (current($breadcrumb_data) !== false) { |
|
387
|
7 |
|
$data = current($breadcrumb_data); |
|
388
|
7 |
|
$entry = htmlspecialchars($data[MIDCOM_NAV_NAME]); |
|
389
|
|
|
|
|
390
|
|
|
// Add the next element sensitive to the fact whether we are at the end or not. |
|
391
|
7 |
|
if (next($breadcrumb_data) === false) { |
|
392
|
7 |
|
if ($current_class !== null) { |
|
393
|
7 |
|
$entry = "<span class=\"{$current_class}\">{$entry}</span>"; |
|
394
|
|
|
} |
|
395
|
|
|
} else { |
|
396
|
7 |
|
if ( !empty($data['napobject'][MIDCOM_NAV_GUID]) |
|
397
|
7 |
|
&& in_array($data['napobject'][MIDCOM_NAV_GUID], $skip_guids)) { |
|
398
|
|
|
continue; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
7 |
|
$entry = "<a href=\"{$data[MIDCOM_NAV_URL]}\"{$class}>{$entry}</a>{$separator}"; |
|
402
|
|
|
} |
|
403
|
7 |
|
$result .= $entry; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
7 |
|
return $result; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Construct source data for a breadcrumb line. |
|
411
|
|
|
* |
|
412
|
|
|
* Gives you the data needed to construct a line like |
|
413
|
|
|
* 'Start > Topic1 > Topic2 > Article' using NAP to |
|
414
|
|
|
* traverse upwards till the root node. The components custom breadcrumb |
|
415
|
|
|
* data is inserted at the end of the computed breadcrumb line after any |
|
416
|
|
|
* set NAP leaf. |
|
417
|
|
|
* |
|
418
|
|
|
* See get_breadcrumb_line for a more end-user oriented way of life. |
|
419
|
|
|
* |
|
420
|
|
|
* <b>Return Value</b> |
|
421
|
|
|
* |
|
422
|
|
|
* The breadcrumb data will be returned as a list of associative arrays each |
|
423
|
|
|
* containing these keys: |
|
424
|
|
|
* |
|
425
|
|
|
* - MIDCOM_NAV_URL The fully qualified URL to the node. |
|
426
|
|
|
* - MIDCOM_NAV_NAME The clear-text name of the node. |
|
427
|
|
|
* - MIDCOM_NAV_TYPE One of 'node', 'leaf', 'custom' indicating what type of entry |
|
428
|
|
|
* this is. |
|
429
|
|
|
* - MIDCOM_NAV_ID The Identifier of the structure used to build this entry, this is |
|
430
|
|
|
* either a NAP node/leaf ID or the list key set by the component for custom data. |
|
431
|
|
|
* - 'napobject' This contains the original NAP object retrieved by the function. |
|
432
|
|
|
* Just in case you need more information than is available directly. |
|
433
|
|
|
* |
|
434
|
|
|
* The entry of every level is indexed by its MIDCOM_NAV_ID, where custom keys preserve |
|
435
|
|
|
* their original key (as passed by the component) and prefixing it with 'custom-'. This |
|
436
|
|
|
* allows you to easily check if a given node/leave is within the current breadcrumb-line |
|
437
|
|
|
* by checking with array_key_exists. |
|
438
|
|
|
* |
|
439
|
|
|
* <b>Adding custom data</b> |
|
440
|
|
|
* |
|
441
|
|
|
* Custom elements are added to this array by using the MidCOM custom component context |
|
442
|
|
|
* at this time. You need to add a list with the same structure as above into the |
|
443
|
|
|
* custom component context key <i>midcom.helper.nav.breadcrumb</i>. (This needs |
|
444
|
|
|
* to be an array always, even if you return only one element.) |
|
445
|
|
|
* |
|
446
|
|
|
* Note, that the URL you pass in that list is always prepended with the current anchor |
|
447
|
|
|
* prefix. It is not possible to specify absolute URLs there. No leading slash is required. |
|
448
|
|
|
* |
|
449
|
|
|
* Example: |
|
450
|
|
|
* |
|
451
|
|
|
* <code> |
|
452
|
|
|
* $tmp = [ |
|
453
|
|
|
* [ |
|
454
|
|
|
* MIDCOM_NAV_URL => "list/{$this->_category}/{$this->_mode}/1/", |
|
455
|
|
|
* MIDCOM_NAV_NAME => $this->_category_name, |
|
456
|
|
|
* ], |
|
457
|
|
|
* ]; |
|
458
|
|
|
* midcom_core_context::get()->set_custom_key('midcom.helper.nav.breadcrumb', $tmp); |
|
459
|
|
|
* </code> |
|
460
|
|
|
*/ |
|
461
|
10 |
|
public function get_breadcrumb_data($id = null) : array |
|
462
|
|
|
{ |
|
463
|
10 |
|
$prefix = $this->context->get_key(MIDCOM_CONTEXT_ANCHORPREFIX); |
|
464
|
10 |
|
$result = []; |
|
465
|
|
|
|
|
466
|
10 |
|
if (!$id) { |
|
467
|
10 |
|
$curr_leaf = $this->get_current_leaf(); |
|
468
|
10 |
|
$curr_node = $this->get_current_node(); |
|
469
|
|
|
} else { |
|
470
|
|
|
$curr_leaf = false; |
|
471
|
|
|
$curr_node = -1; |
|
472
|
|
|
|
|
473
|
|
|
if ($leaf = $this->get_leaf($id)) { |
|
474
|
|
|
$curr_leaf = $leaf[MIDCOM_NAV_ID]; |
|
475
|
|
|
$curr_node = $leaf[MIDCOM_NAV_NODEID]; |
|
476
|
|
|
} elseif ($node = $this->get_node($id)) { |
|
477
|
|
|
$curr_node = $node[MIDCOM_NAV_ID]; |
|
478
|
|
|
} |
|
479
|
|
|
} |
|
480
|
10 |
|
foreach ($this->get_node_path($curr_node) as $node_id) { |
|
481
|
10 |
|
$node = $this->get_node($node_id); |
|
482
|
10 |
|
$result[$node[MIDCOM_NAV_ID]] = [ |
|
483
|
10 |
|
MIDCOM_NAV_URL => $node[MIDCOM_NAV_ABSOLUTEURL], |
|
484
|
|
|
MIDCOM_NAV_NAME => $node[MIDCOM_NAV_NAME], |
|
485
|
|
|
MIDCOM_NAV_TYPE => 'node', |
|
486
|
|
|
MIDCOM_NAV_ID => $node_id, |
|
487
|
|
|
'napobject' => $node, |
|
488
|
|
|
]; |
|
489
|
|
|
} |
|
490
|
10 |
|
if ($curr_leaf && $leaf = $this->get_leaf($curr_leaf)) { |
|
491
|
|
|
// Ignore Index Article Leaves |
|
492
|
|
|
if ($leaf[MIDCOM_NAV_URL] != '') { |
|
493
|
|
|
$result[$leaf[MIDCOM_NAV_ID]] = [ |
|
494
|
|
|
MIDCOM_NAV_URL => $leaf[MIDCOM_NAV_ABSOLUTEURL], |
|
495
|
|
|
MIDCOM_NAV_NAME => $leaf[MIDCOM_NAV_NAME], |
|
496
|
|
|
MIDCOM_NAV_TYPE => 'leaf', |
|
497
|
|
|
MIDCOM_NAV_ID => $curr_leaf, |
|
498
|
|
|
'napobject' => $leaf, |
|
499
|
|
|
]; |
|
500
|
|
|
} |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
10 |
|
if (midcom_core_context::get()->has_custom_key('midcom.helper.nav.breadcrumb')) { |
|
504
|
4 |
|
$customdata = midcom_core_context::get()->get_custom_key('midcom.helper.nav.breadcrumb'); |
|
505
|
4 |
|
if (is_array($customdata)) { |
|
506
|
4 |
|
foreach ($customdata as $key => $entry) { |
|
507
|
4 |
|
$id = "custom-{$key}"; |
|
508
|
|
|
|
|
509
|
4 |
|
$url = "{$prefix}{$entry[MIDCOM_NAV_URL]}"; |
|
510
|
4 |
|
if ( str_starts_with($entry[MIDCOM_NAV_URL], '/') |
|
511
|
4 |
|
|| preg_match('|^https?://|', $entry[MIDCOM_NAV_URL])) { |
|
512
|
2 |
|
$url = $entry[MIDCOM_NAV_URL]; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
4 |
|
$result[$id] = [ |
|
516
|
|
|
MIDCOM_NAV_URL => $url, |
|
517
|
|
|
MIDCOM_NAV_NAME => $entry[MIDCOM_NAV_NAME], |
|
518
|
|
|
MIDCOM_NAV_TYPE => 'custom', |
|
519
|
|
|
MIDCOM_NAV_ID => $id, |
|
520
|
|
|
'napobject' => $entry, |
|
521
|
|
|
]; |
|
522
|
|
|
} |
|
523
|
|
|
} |
|
524
|
|
|
} |
|
525
|
10 |
|
return $result; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Retrieve the IDs of the nodes from the URL. First value at key 0 is |
|
530
|
|
|
* the root node ID, possible second value is the first subnode ID etc. |
|
531
|
|
|
* Contains only visible nodes (nodes which can be loaded). |
|
532
|
|
|
*/ |
|
533
|
12 |
|
public function get_node_path($node_id = null) : array |
|
534
|
|
|
{ |
|
535
|
12 |
|
if ($node_id === null) { |
|
536
|
9 |
|
return $this->_backend->get_node_path(); |
|
537
|
|
|
} |
|
538
|
10 |
|
$path = []; |
|
539
|
10 |
|
$node = $this->get_node($node_id); |
|
540
|
10 |
|
while ($node) { |
|
541
|
10 |
|
$path[] = $node[MIDCOM_NAV_ID]; |
|
542
|
10 |
|
if ($node[MIDCOM_NAV_NODEID] === -1) { |
|
543
|
10 |
|
break; |
|
544
|
|
|
} |
|
545
|
7 |
|
$node = $this->get_node($node[MIDCOM_NAV_NODEID]); |
|
546
|
|
|
} |
|
547
|
10 |
|
return array_reverse($path); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* Retrieve the ID of the upper node of the currently displayed node. |
|
552
|
|
|
* |
|
553
|
|
|
* @return mixed The ID of the node in question. |
|
554
|
|
|
*/ |
|
555
|
1 |
|
public function get_current_upper_node() |
|
556
|
|
|
{ |
|
557
|
1 |
|
return $this->_backend->get_current_upper_node(); |
|
558
|
|
|
} |
|
559
|
|
|
} |
|
560
|
|
|
|