Passed
Push — master ( 0f7b61...be3b3f )
by Jeroen
04:30
created

navigation.php ➔ _elgg_page_menu_setup()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 10
nop 1
dl 0
loc 26
ccs 0
cts 21
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 93 and the first side effect is on line 763.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Elgg navigation library
4
 * Functions for managing menus and other navigational elements
5
 *
6
 * Breadcrumbs
7
 * Elgg uses a breadcrumb stack. The page handlers (controllers in MVC terms)
8
 * push the breadcrumb links onto the stack. @see elgg_push_breadcrumb()
9
 *
10
 *
11
 * Pagination
12
 * Automatically handled by Elgg when using elgg_list_entities* functions.
13
 * @see elgg_list_entities()
14
 *
15
 *
16
 * Tabs
17
 * @see navigation/tabs view
18
 *
19
 *
20
 * Menus
21
 * Elgg uses a single interface to manage its menus. Menu items are added with
22
 * {@link elgg_register_menu_item()}. This is generally used for menus that
23
 * appear only once per page. For dynamic menus (such as the hover
24
 * menu for user's avatar), a plugin hook is emitted when the menu is being
25
 * created. The hook is 'register', 'menu:<menu_name>'. For more details on this,
26
 * @see elgg_view_menu().
27
 *
28
 * Menus supported by the Elgg core
29
 * Standard menus:
30
 *     site   Site navigation shown on every page.
31
 *     page   Page menu usually shown in a sidebar. Uses Elgg's context.
32
 *     topbar Topbar menu shown on every page. The default has two sections.
33
 *     footer Like the topbar but in the footer.
34
 *     extras Links about content on the page. The RSS link is added to this.
35
 *
36
 * Dynamic menus (also called just-in-time menus):
37
 *     user_hover  Avatar hover menu. The user entity is passed as a parameter.
38
 *     entity      The set of links shown in the summary of an entity.
39
 *     river       Links shown on river items.
40
 *     owner_block Links shown for a user or group in their owner block.
41
 *     filter      The tab filter for content (all, mine, friends)
42
 *     title       The buttons shown next to a content title.
43
 *     longtext    The links shown above the input/longtext view.
44
 *     login       Menu of links at bottom of login box
45
 *
46
 * @package    Elgg.Core
47
 * @subpackage Navigation
48
 */
49
50
/**
51
 * Register an item for an Elgg menu
52
 *
53
 * @warning Generally you should not use this in response to the plugin hook:
54
 * 'register', 'menu:<menu_name>'. If you do, you may end up with many incorrect
55
 * links on a dynamic menu.
56
 *
57
 * @warning A menu item's name must be unique per menu. If more than one menu
58
 * item with the same name are registered, the last menu item takes priority.
59
 *
60
 * @see elgg_view_menu() for the plugin hooks available for modifying a menu as
61
 * it is being rendered.
62
 *
63
 * @see ElggMenuItem::factory() is used to turn an array value of $menu_item into an
64
 * ElggMenuItem object.
65
 *
66
 * @param string $menu_name The name of the menu: site, page, userhover,
67
 *                          userprofile, groupprofile, or any custom menu
68
 * @param mixed  $menu_item A \ElggMenuItem object or an array of options in format:
69
 *                          name        => STR  Menu item identifier (required)
70
 *                          text        => STR  Menu item display text as HTML (required)
71
 *                          href        => STR  Menu item URL (required) (false for non-links.
72
 *                                              @warning If you disable the href the <a> tag will
73
 *                                              not appear, so the link_class will not apply. If you
74
 *                                              put <a> tags in manually through the 'text' option
75
 *                                              the default CSS selector .elgg-menu-$menu > li > a
76
 *                                              may affect formatting. Wrap in a <span> if it does.)
77
 *                          contexts    => ARR  Page context strings
78
 *                          section     => STR  Menu section identifier
79
 *                          title       => STR  Menu item tooltip
80
 *                          selected    => BOOL Is this menu item currently selected
81
 *                          parent_name => STR  Identifier of the parent menu item
82
 *                          link_class  => STR  A class or classes for the <a> tag
83
 *                          item_class  => STR  A class or classes for the <li> tag
84
 *                          deps     => STR  One or more AMD modules to require
85
 *
86
 *                          Additional options that the view output/url takes can be
87
 *							passed in the array. Custom options can be added by using
88
 *							the 'data' key with the	value being an associative array.
89
 *
90
 * @return bool False if the item could not be added
91
 * @since 1.8.0
92
 */
93
function elgg_register_menu_item($menu_name, $menu_item) {
94 1
	global $CONFIG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
95
96 1
	if (is_array($menu_item)) {
97 1
		$options = $menu_item;
98 1
		$menu_item = \ElggMenuItem::factory($options);
99 1
		if (!$menu_item) {
100 1
			$menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
101 1
			elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
102 1
			return false;
103
		}
104 1
	}
105
106 1
	if (!$menu_item instanceof ElggMenuItem) {
107 1
		elgg_log('Second argument of elgg_register_menu_item() must be an instance of ElggMenuItem or an array of menu item factory options', 'ERROR');
108 1
		return false;
109
	}
110
111 1
	if (!isset($CONFIG->menus[$menu_name])) {
112 1
		$CONFIG->menus[$menu_name] = array();
113 1
	}
114 1
	$CONFIG->menus[$menu_name][] = $menu_item;
115 1
	return true;
116
}
117
118
/**
119
 * Remove an item from a menu
120
 *
121
 * @param string $menu_name The name of the menu
122
 * @param string $item_name The unique identifier for this menu item
123
 *
124
 * @return \ElggMenuItem|null
125
 * @since 1.8.0
126
 */
127
function elgg_unregister_menu_item($menu_name, $item_name) {
128
	global $CONFIG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
129
130
	if (empty($CONFIG->menus[$menu_name])) {
131
		return null;
132
	}
133
134
	foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
135
		/* @var \ElggMenuItem $menu_object */
136
		if ($menu_object instanceof ElggMenuItem && $menu_object->getName() == $item_name) {
137
			$item = $CONFIG->menus[$menu_name][$index];
138
			unset($CONFIG->menus[$menu_name][$index]);
139
			return $item;
140
		}
141
	}
142
143
	return null;
144
}
145
146
/**
147
 * Check if a menu item has been registered
148
 *
149
 * @param string $menu_name The name of the menu
150
 * @param string $item_name The unique identifier for this menu item
151
 * 
152
 * @return bool
153
 * @since 1.8.0
154
 */
155
function elgg_is_menu_item_registered($menu_name, $item_name) {
156
	global $CONFIG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
157
158
	if (!isset($CONFIG->menus[$menu_name])) {
159
		return false;
160
	}
161
162
	foreach ($CONFIG->menus[$menu_name] as $menu_object) {
163
		/* @var \ElggMenuItem $menu_object */
164
		if ($menu_object->getName() == $item_name) {
165
			return true;
166
		}
167
	}
168
169
	return false;
170
}
171
172
/**
173
 * Get a menu item registered for a menu
174
 *
175
 * @param string $menu_name The name of the menu
176
 * @param string $item_name The unique identifier for this menu item
177
 *
178
 * @return ElggMenuItem|null
179
 * @since 1.9.0
180
 */
181
function elgg_get_menu_item($menu_name, $item_name) {
182
	global $CONFIG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
183
184
	if (!isset($CONFIG->menus[$menu_name])) {
185
		return null;
186
	}
187
188
	foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
189
		/* @var \ElggMenuItem $menu_object */
190
		if ($menu_object->getName() == $item_name) {
191
			return $CONFIG->menus[$menu_name][$index];
192
		}
193
	}
194
195
	return null;
196
}
197
198
/**
199
 * Convenience function for registering a button to the title menu
200
 *
201
 * The URL must be $handler/$name/$guid where $guid is the guid of the page owner.
202
 * The label of the button is "$handler:$name" so that must be defined in a
203
 * language file.
204
 *
205
 * This is used primarily to support adding an add content button
206
 *
207
 * @param string $handler        The handler to use or null to autodetect from context
208
 * @param string $name           Name of the button (defaults to 'add')
209
 * @param string $entity_type    Optional entity type to be added (used to verify canWriteToContainer permission)
210
 * @param string $entity_subtype Optional entity subtype to be added (used to verify canWriteToContainer permission)
211
 * @return void
212
 * @since 1.8.0
213
 */
214
function elgg_register_title_button($handler = null, $name = 'add', $entity_type = 'all', $entity_subtype = 'all') {
215
	
216
	if (!$handler) {
217
		$handler = elgg_get_context();
218
	}
219
220
	$owner = elgg_get_page_owner_entity();
221
	if (!$owner) {
222
		// noone owns the page so this is probably an all site list page
223
		$owner = elgg_get_logged_in_user_entity();
224
	}
225
	if (!$owner || !$owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
226
		return;
227
	}
228
229
	elgg_register_menu_item('title', array(
230
		'name' => $name,
231
		'href' => "$handler/$name/$owner->guid",
232
		'text' => elgg_echo("$handler:$name"),
233
		'link_class' => 'elgg-button elgg-button-action',
234
	));
235
}
236
237
/**
238
 * Adds a breadcrumb to the breadcrumbs stack.
239
 *
240
 * See elgg_get_breadcrumbs() and the navigation/breadcrumbs view.
241
 *
242
 * @param string $title The title to display. During rendering this is HTML encoded.
243
 * @param string $link  Optional. The link for the title. During rendering links are
244
 *                      normalized via elgg_normalize_url().
245
 *
246
 * @return void
247
 * @since 1.8.0
248
 * @see elgg_get_breadcrumbs
249
 */
250
function elgg_push_breadcrumb($title, $link = null) {
251 3
	$breadcrumbs = (array)elgg_get_config('breadcrumbs');
252 3
	$breadcrumbs[] = array('title' => $title, 'link' => $link);
253 3
	elgg_set_config('breadcrumbs', $breadcrumbs);
254 3
}
255
256
/**
257
 * Removes last breadcrumb entry.
258
 *
259
 * @return array popped breadcrumb array or empty array
260
 * @since 1.8.0
261
 */
262
function elgg_pop_breadcrumb() {
263 1
	$breadcrumbs = (array)elgg_get_config('breadcrumbs');
264
265 1
	if (empty($breadcrumbs)) {
266
		return array();
267
	}
268
269 1
	$popped = array_pop($breadcrumbs);
270 1
	elgg_set_config('breadcrumbs', $breadcrumbs);
271
272 1
	return $popped;
273
}
274
275
/**
276
 * Returns all breadcrumbs as an array
277
 * <code>
278
 * [
279
 *    [
280
 *       'title' => 'Breadcrumb title',
281
 *       'link' => '/path/to/page',
282
 *    ]
283
 * ]
284
 * </code>
285
 *
286
 * Breadcrumbs are filtered through the plugin hook [prepare, breadcrumbs] before
287
 * being returned.
288
 *
289
 * @param array $breadcrumbs An array of breadcrumbs
290
 *                           If set, will override breadcrumbs in the stack
291
 * @return array
292
 * @since 1.8.0
293
 * @see elgg_prepare_breadcrumbs
294
 */
295
function elgg_get_breadcrumbs(array $breadcrumbs = null) {
296 3
	if (!isset($breadcrumbs)) {
297
		// if no crumbs set, still allow hook to populate it
298 3
		$breadcrumbs = (array)elgg_get_config('breadcrumbs');
299 3
	}
300
301 3
	if (!is_array($breadcrumbs)) {
302
		_elgg_services()->logger->error(__FUNCTION__ . ' expects breadcrumbs as an array');
303
		$breadcrumbs = [];
304
	}
305
	
306
	$params = array(
307 3
		'breadcrumbs' => $breadcrumbs,
308 3
	);
309
310 3
	$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
311 3
	$params['segments'] = _elgg_services()->request->getUrlSegments();
312 3
	array_shift($params['segments']);
313
314 3
	$breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
315 3
	if (!is_array($breadcrumbs)) {
316
		_elgg_services()->logger->error('"prepare, breadcrumbs" hook must return an array of breadcrumbs');
317
		return [];
318
	}
319
320 3
	return $breadcrumbs;
321
}
322
323
/**
324
 * Prepare breadcrumbs before display. This turns titles into 100-character excerpts, and also
325
 * removes the last crumb if it's not a link.
326
 *
327
 * @param string $hook        "prepare"
328
 * @param string $type        "breadcrumbs"
329
 * @param array  $breadcrumbs Breadcrumbs to be altered
330
 * @param array  $params      Hook parameters
331
 *
332
 * @return array
333
 * @since 1.11
334
 */
335
function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
336
	// remove last crumb if not a link
337
	$last_crumb = end($breadcrumbs);
338
	if (empty($last_crumb['link'])) {
339
		array_pop($breadcrumbs);
340
	}
341
342
	// apply excerpt to titles
343
	foreach (array_keys($breadcrumbs) as $i) {
344
		$breadcrumbs[$i]['title'] = elgg_get_excerpt($breadcrumbs[$i]['title'], 100);
345
	}
346
	return $breadcrumbs;
347
}
348
349
/**
350
 * Returns default filter tabs (All, Mine, Friends) for the user
351
 * 
352
 * @param string   $context  Context to be used to prefix tab URLs
353
 * @param string   $selected Name of the selected tab
354
 * @param ElggUser $user     User who owns the layout (defaults to logged in user)
355
 * @param array    $vars     Additional vars
356
 * @return ElggMenuItem[]
357
 * @since 2.3
358
 */
359
function elgg_get_filter_tabs($context = null, $selected = null, ElggUser $user = null, array $vars = []) {
360
361
	if (!isset($selected)) {
362
		$selected = 'all';
363
	}
364
365
	if (!$user) {
366
		$user = elgg_get_logged_in_user_entity();
367
	}
368
369
	$items = [];
370
	if ($user) {
371
		$items[] = ElggMenuItem::factory([
372
			'name' => 'all',
373
			'text' => elgg_echo('all'),
374
			'href' => (isset($vars['all_link'])) ? $vars['all_link'] : "$context/all",
375
			'selected' => ($selected == 'all'),
376
			'priority' => 200,
377
		]);
378
		$items[] = ElggMenuItem::factory([
379
			'name' => 'mine',
380
			'text' => elgg_echo('mine'),
381
			'href' => (isset($vars['mine_link'])) ? $vars['mine_link'] : "$context/owner/{$user->username}",
382
			'selected' => ($selected == 'mine'),
383
			'priority' => 300,
384
		]);
385
	}
386
387
	$params = [
388
		'selected' => $selected,
389
		'user' => $user,
390
		'vars' => $vars,
391
	];
392
	$items = _elgg_services()->hooks->trigger('filter_tabs', $context, $params, $items);
393
394
	return $items;
395
}
396
397
/**
398
 * Set up the site menu
399
 *
400
 * Handles default, featured, and custom menu items
401
 *
402
 * @access private
403
 */
404
function _elgg_site_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
405
406
	$featured_menu_names = elgg_get_config('site_featured_menu_names');
407
	$custom_menu_items = elgg_get_config('site_custom_menu_items');
408
	if ($featured_menu_names || $custom_menu_items) {
409
		// we have featured or custom menu items
410
411
		$registered = $return['default'];
412
		/* @var \ElggMenuItem[] $registered */
413
414
		// set up featured menu items
415
		$featured = array();
416
		foreach ($featured_menu_names as $name) {
417
			foreach ($registered as $index => $item) {
418
				if ($item->getName() == $name) {
419
					$featured[] = $item;
420
					unset($registered[$index]);
421
				}
422
			}
423
		}
424
425
		// add custom menu items
426
		$n = 1;
427
		foreach ($custom_menu_items as $title => $url) {
428
			$item = new \ElggMenuItem("custom$n", $title, $url);
429
			$featured[] = $item;
430
			$n++;
431
		}
432
433
		$return['default'] = $featured;
434
		if (count($registered) > 0) {
435
			$return['more'] = $registered;
436
		}
437
	} else {
438
		// no featured menu items set
439
		$max_display_items = 5;
440
441
		// the first n are shown, rest added to more list
442
		// if only one item on more menu, stick it with the rest
443
		$num_menu_items = count($return['default']);
444
		if ($num_menu_items > ($max_display_items + 1)) {
445
			$return['more'] = array_splice($return['default'], $max_display_items);
446
		}
447
	}
448
	
449
	// check if we have anything selected
450
	$selected = false;
451
	foreach ($return as $section) {
452
		/* @var \ElggMenuItem[] $section */
453
454
		foreach ($section as $item) {
455
			if ($item->getSelected()) {
456
				$selected = true;
457
				break 2;
458
			}
459
		}
460
	}
461
	
462
	if (!$selected) {
463
		// nothing selected, match name to context or match url
464
		$current_url = current_page_url();
465
		foreach ($return as $section_name => $section) {
466
			foreach ($section as $key => $item) {
467
				// only highlight internal links
468
				if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
469
					if ($item->getName() == elgg_get_context()) {
470
						$return[$section_name][$key]->setSelected(true);
471
						break 2;
472
					}
473
					if ($item->getHref() == $current_url) {
474
						$return[$section_name][$key]->setSelected(true);
475
						break 2;
476
					}
477
				}
478
			}
479
		}
480
	}
481
482
	return $return;
483
}
484
485
/**
486
 * Prepare page menu
487
 * Sets the display child menu option to "toggle" if not set
488
 * Recursively marks parents of the selected item as selected (expanded)
489
 *
490
 * @param \Elgg\Hook $hook
491
 * @access private
492
 */
493
function _elgg_page_menu_setup(\Elgg\Hook $hook) {
494
	$menu = $hook->getValue();
495
496
	foreach ($menu as $section => $menu_items) {
497
		foreach ($menu_items as $menu_item) {
498
			if ($menu_item instanceof ElggMenuItem) {
499
				$child_menu_vars = $menu_item->getChildMenuOptions();
500
				if (empty($child_menu_vars['display'])) {
501
					$child_menu_vars['display'] = 'toggle';
502
				}
503
				$menu_item->setChildMenuOptions($child_menu_vars);
504
			}
505
		}
506
	}
507
508
	$selected_item = $hook->getParam('selected_item');
509
	if ($selected_item instanceof \ElggMenuItem) {
510
		$parent = $selected_item->getParent();
511
		while ($parent instanceof \ElggMenuItem) {
512
			$parent->setSelected();
513
			$parent = $parent->getParent();
514
		}
515
	}
516
517
	return $menu;
518
}
519
520
/**
521
 * Add the comment and like links to river actions menu
522
 * @access private
523
 */
524
function _elgg_river_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
525
	if (elgg_is_logged_in()) {
526
		$item = $params['item'];
527
		/* @var \ElggRiverItem $item */
528
		$object = $item->getObjectEntity();
529
		// add comment link but annotations cannot be commented on
530
		if ($item->annotation_id == 0) {
531
			if ($object->canComment()) {
532
				$options = array(
533
					'name' => 'comment',
534
					'href' => "#comments-add-{$object->guid}-{$item->id}",
535
					'text' => elgg_view_icon('speech-bubble'),
536
					'title' => elgg_echo('comment:this'),
537
					'rel' => 'toggle',
538
					'priority' => 50,
539
				);
540
				$return[] = \ElggMenuItem::factory($options);
541
			}
542
		}
543
		
544
		if ($item->canDelete()) {
545
			$options = array(
546
				'name' => 'delete',
547
				'href' => elgg_add_action_tokens_to_url("action/river/delete?id={$item->id}"),
548
				'text' => elgg_view_icon('delete'),
549
				'title' => elgg_echo('river:delete'),
550
				'confirm' => elgg_echo('deleteconfirm'),
551
				'priority' => 200,
552
			);
553
			$return[] = \ElggMenuItem::factory($options);
554
		}
555
	}
556
557
	return $return;
558
}
559
560
/**
561
 * Entity menu is list of links and info on any entity
562
 * @access private
563
 */
564
function _elgg_entity_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
565
	if (elgg_in_context('widgets')) {
566
		return $return;
567
	}
568
	
569
	$entity = $params['entity'];
570
	/* @var \ElggEntity $entity */
571
	$handler = elgg_extract('handler', $params, false);
572
	
573
	if ($entity->canEdit() && $handler) {
574
		// edit link
575
		$options = array(
576
			'name' => 'edit',
577
			'text' => elgg_echo('edit'),
578
			'title' => elgg_echo('edit:this'),
579
			'href' => "$handler/edit/{$entity->getGUID()}",
580
			'priority' => 200,
581
		);
582
		$return[] = \ElggMenuItem::factory($options);
583
	}
584
585
	if ($entity->canDelete() && $handler) {
586
		// delete link
587
		if (elgg_action_exists("$handler/delete")) {
588
			$action = "action/$handler/delete";
589
		} else {
590
			$action = "action/entity/delete";
591
		}
592
		$options = array(
593
			'name' => 'delete',
594
			'text' => elgg_view_icon('delete'),
595
			'title' => elgg_echo('delete:this'),
596
			'href' => "$action?guid={$entity->getGUID()}",
597
			'confirm' => elgg_echo('deleteconfirm'),
598
			'priority' => 300,
599
		);
600
		$return[] = \ElggMenuItem::factory($options);
601
	}
602
603
	return $return;
604
}
605
606
/**
607
 * Widget menu is a set of widget controls
608
 * @access private
609
 */
610
function _elgg_widget_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
611
612
	$widget = $params['entity'];
613
	/* @var \ElggWidget $widget */
614
	$show_edit = elgg_extract('show_edit', $params, true);
615
616
	$collapse = array(
617
		'name' => 'collapse',
618
		'text' => ' ',
619
		'href' => "#elgg-widget-content-$widget->guid",
620
		'link_class' => 'elgg-widget-collapse-button',
621
		'rel' => 'toggle',
622
		'priority' => 1,
623
	);
624
	$return[] = \ElggMenuItem::factory($collapse);
625
626
	if ($widget->canEdit()) {
627
		$delete = array(
628
			'name' => 'delete',
629
			'text' => elgg_view_icon('delete-alt'),
630
			'title' => elgg_echo('widget:delete', array($widget->getTitle())),
631
			'href' => "action/widgets/delete?widget_guid=$widget->guid",
632
			'is_action' => true,
633
			'link_class' => 'elgg-widget-delete-button',
634
			'id' => "elgg-widget-delete-button-$widget->guid",
635
			'data-elgg-widget-type' => $widget->handler,
636
			'priority' => 900,
637
		);
638
		$return[] = \ElggMenuItem::factory($delete);
639
640
		if ($show_edit) {
641
			$edit = array(
642
				'name' => 'settings',
643
				'text' => elgg_view_icon('settings-alt'),
644
				'title' => elgg_echo('widget:edit'),
645
				'href' => "#widget-edit-$widget->guid",
646
				'link_class' => "elgg-widget-edit-button",
647
				'rel' => 'toggle',
648
				'priority' => 800,
649
			);
650
			$return[] = \ElggMenuItem::factory($edit);
651
		}
652
	}
653
654
	return $return;
655
}
656
657
/**
658
 * Add the register and forgot password links to login menu
659
 * @access private
660
 */
661
function _elgg_login_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
662
663
	if (elgg_get_config('allow_registration')) {
664
		$return[] = \ElggMenuItem::factory(array(
665
			'name' => 'register',
666
			'href' => elgg_get_registration_url(),
667
			'text' => elgg_echo('register'),
668
			'link_class' => 'registration_link',
669
		));
670
	}
671
672
	$return[] = \ElggMenuItem::factory(array(
673
		'name' => 'forgotpassword',
674
		'href' => 'forgotpassword',
675
		'text' => elgg_echo('user:password:lost'),
676
		'link_class' => 'forgot_link',
677
	));
678
679
	return $return;
680
}
681
682
/**
683
 * Add the RSS link to the extras menu
684
 * @access private
685
 */
686
function _elgg_extras_menu_setup($hook, $type, $return, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
687
688
	if (!elgg_is_logged_in()) {
689
		return;
690
	}
691
	
692
	if (!_elgg_has_rss_link()) {
693
		return;
694
	}
695
696
	$url = current_page_url();
697
	$return[] = ElggMenuItem::factory([
698
		'name' => 'rss',
699
		'text' => elgg_echo('feed:rss'),
700
		'icon' => 'rss',
701
		'href' => elgg_http_add_url_query_elements($url, [
702
			'view' => 'rss',
703
		]),
704
		'title' => elgg_echo('feed:rss'),
705
	]);
706
707
	return $return;
708
}
709
710
/**
711
 * Navigation initialization
712
 * @access private
713
 */
714
function _elgg_nav_init() {
715
	elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
716
717
	elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup');
718
	elgg_register_plugin_hook_handler('prepare', 'menu:page', '_elgg_page_menu_setup', 999);
719
720
	elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
721
	elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
722
	elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
723
	elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
724
	elgg_register_plugin_hook_handler('register', 'menu:extras', '_elgg_extras_menu_setup');
725
726
	elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
727
728
	elgg_register_menu_item('footer', \ElggMenuItem::factory(array(
729
		'name' => 'powered',
730
		'text' => elgg_echo("elgg:powered"),
731
		'href' => 'http://elgg.org',
732
		'title' => 'Elgg ' . elgg_get_version(true),
733
		'section' => 'meta',
734
	)));
735
736
	elgg_register_ajax_view('navigation/menu/user_hover/contents');
737
738
	// Using a view extension to ensure that themes that have replaced the item view
739
	// still load the required AMD modules
740
	elgg_extend_view('navigation/menu/elements/item', 'navigation/menu/elements/item_deps');
741
}
742
743
/**
744
 * Extend public pages
745
 *
746
 * @param string   $hook_name    "public_pages"
747
 * @param string   $entity_type  "walled_garden"
748
 * @param string[] $return_value Paths accessible outside the "walled garden"
749
 * @param mixed    $params       unused
750
 *
751
 * @return string[]
752
 * @access private
753
 * @since 1.11.0
754
 */
755
function _elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $entity_type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
756
	if (is_array($return_value)) {
757
		$return_value[] = 'navigation/menu/user_hover/contents';
758
	}
759
760
	return $return_value;
761
}
762
763
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
0 ignored issues
show
Unused Code introduced by
The parameter $hooks is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
764
	$events->registerHandler('init', 'system', '_elgg_nav_init');
765
};
766