Elgg  Version 2.3
navigation.php
Go to the documentation of this file.
1 <?php
97 function elgg_register_menu_item($menu_name, $menu_item) {
99 
100  if (is_array($menu_item)) {
101  $options = $menu_item;
102  $menu_item = \ElggMenuItem::factory($options);
103  if (!$menu_item) {
104  $menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
105  elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
106  return false;
107  }
108  }
109 
110  if (!$menu_item instanceof ElggMenuItem) {
111  elgg_log('Second argument of elgg_register_menu_item() must be an instance of '
112  . 'ElggMenuItem or an array of menu item factory options', 'ERROR');
113  return false;
114  }
115 
116  if (!isset($CONFIG->menus[$menu_name])) {
117  $CONFIG->menus[$menu_name] = array();
118  }
119  $CONFIG->menus[$menu_name][] = $menu_item;
120  return true;
121 }
122 
132 function elgg_unregister_menu_item($menu_name, $item_name) {
133  global $CONFIG;
134 
135  if (empty($CONFIG->menus[$menu_name])) {
136  return null;
137  }
138 
139  foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
140  /* @var \ElggMenuItem $menu_object */
141  if ($menu_object instanceof ElggMenuItem && $menu_object->getName() == $item_name) {
142  $item = $CONFIG->menus[$menu_name][$index];
143  unset($CONFIG->menus[$menu_name][$index]);
144  return $item;
145  }
146  }
147 
148  return null;
149 }
150 
160 function elgg_is_menu_item_registered($menu_name, $item_name) {
161  global $CONFIG;
162 
163  if (!isset($CONFIG->menus[$menu_name])) {
164  return false;
165  }
166 
167  foreach ($CONFIG->menus[$menu_name] as $menu_object) {
168  /* @var \ElggMenuItem $menu_object */
169  if ($menu_object->getName() == $item_name) {
170  return true;
171  }
172  }
173 
174  return false;
175 }
176 
186 function elgg_get_menu_item($menu_name, $item_name) {
187  global $CONFIG;
188 
189  if (!isset($CONFIG->menus[$menu_name])) {
190  return null;
191  }
192 
193  foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
194  /* @var \ElggMenuItem $menu_object */
195  if ($menu_object->getName() == $item_name) {
196  return $CONFIG->menus[$menu_name][$index];
197  }
198  }
199 
200  return null;
201 }
202 
219 function elgg_register_title_button($handler = null, $name = 'add', $entity_type = 'all', $entity_subtype = 'all') {
220 
221  if (!$handler) {
223  }
224 
226  if (!$owner) {
227  // noone owns the page so this is probably an all site list page
229  }
230  if (!$owner || !$owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
231  return;
232  }
233 
234  elgg_register_menu_item('title', array(
235  'name' => $name,
236  'href' => "$handler/$name/$owner->guid",
237  'text' => elgg_echo("$handler:$name"),
238  'link_class' => 'elgg-button elgg-button-action',
239  ));
240 }
241 
255 function elgg_push_breadcrumb($title, $link = null) {
256  global $CONFIG;
257  if (!isset($CONFIG->breadcrumbs)) {
258  $CONFIG->breadcrumbs = array();
259  }
260 
261  $CONFIG->breadcrumbs[] = array('title' => $title, 'link' => $link);
262 }
263 
271  global $CONFIG;
272 
273  if (empty($CONFIG->breadcrumbs) || !is_array($CONFIG->breadcrumbs)) {
274  return array();
275  }
276  return array_pop($CONFIG->breadcrumbs);
277 }
278 
290  global $CONFIG;
291 
292  // if no crumbs set, still allow hook to populate it
293  if (isset($CONFIG->breadcrumbs) && is_array($CONFIG->breadcrumbs)) {
294  $breadcrumbs = $CONFIG->breadcrumbs;
295  } else {
296  $breadcrumbs = array();
297  }
298 
299  $params = array(
300  'breadcrumbs' => $breadcrumbs,
301  );
302  $breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
303  if (!is_array($breadcrumbs)) {
304  return array();
305  }
306 
307  return $breadcrumbs;
308 }
309 
322 function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
323  // remove last crumb if not a link
324  $last_crumb = end($breadcrumbs);
325  if (empty($last_crumb['link'])) {
326  array_pop($breadcrumbs);
327  }
328 
329  // apply excerpt to titles
330  foreach (array_keys($breadcrumbs) as $i) {
331  $breadcrumbs[$i]['title'] = elgg_get_excerpt($breadcrumbs[$i]['title'], 100);
332  }
333  return $breadcrumbs;
334 }
335 
344 
345  $featured_menu_names = elgg_get_config('site_featured_menu_names');
346  $custom_menu_items = elgg_get_config('site_custom_menu_items');
348  // we have featured or custom menu items
349 
350  $registered = $return['default'];
351  /* @var \ElggMenuItem[] $registered */
352 
353  // set up featured menu items
354  $featured = array();
355  foreach ($featured_menu_names as $name) {
356  foreach ($registered as $index => $item) {
357  if ($item->getName() == $name) {
358  $featured[] = $item;
359  unset($registered[$index]);
360  }
361  }
362  }
363 
364  // add custom menu items
365  $n = 1;
366  foreach ($custom_menu_items as $title => $url) {
367  $item = new \ElggMenuItem("custom$n", $title, $url);
368  $featured[] = $item;
369  $n++;
370  }
371 
372  $return['default'] = $featured;
373  if (count($registered) > 0) {
374  $return['more'] = $registered;
375  }
376  } else {
377  // no featured menu items set
378  $max_display_items = 5;
379 
380  // the first n are shown, rest added to more list
381  // if only one item on more menu, stick it with the rest
382  $num_menu_items = count($return['default']);
383  if ($num_menu_items > ($max_display_items + 1)) {
384  $return['more'] = array_splice($return['default'], $max_display_items);
385  }
386  }
387 
388  // check if we have anything selected
389  $selected = false;
390  foreach ($return as $section) {
391  /* @var \ElggMenuItem[] $section */
392 
393  foreach ($section as $item) {
394  if ($item->getSelected()) {
395  $selected = true;
396  break 2;
397  }
398  }
399  }
400 
401  if (!$selected) {
402  // nothing selected, match name to context or match url
403  $current_url = current_page_url();
404  foreach ($return as $section_name => $section) {
405  foreach ($section as $key => $item) {
406  // only highlight internal links
407  if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
408  if ($item->getName() == elgg_get_context()) {
409  $return[$section_name][$key]->setSelected(true);
410  break 2;
411  }
412  if ($item->getHref() == $current_url) {
413  $return[$section_name][$key]->setSelected(true);
414  break 2;
415  }
416  }
417  }
418  }
419  }
420 
421  return $return;
422 }
423 
429  if (elgg_is_logged_in()) {
430  $item = $params['item'];
431  /* @var \ElggRiverItem $item */
432  $object = $item->getObjectEntity();
433  // add comment link but annotations cannot be commented on
434  if ($item->annotation_id == 0) {
435  if ($object->canComment()) {
436  $options = array(
437  'name' => 'comment',
438  'href' => "#comments-add-{$object->guid}-{$item->id}",
439  'text' => elgg_view_icon('speech-bubble'),
440  'title' => elgg_echo('comment:this'),
441  'rel' => 'toggle',
442  'priority' => 50,
443  );
445  }
446  }
447 
448  if ($item->canDelete()) {
449  $options = array(
450  'name' => 'delete',
451  'href' => elgg_add_action_tokens_to_url("action/river/delete?id={$item->id}"),
452  'text' => elgg_view_icon('delete'),
453  'title' => elgg_echo('river:delete'),
454  'confirm' => elgg_echo('deleteconfirm'),
455  'priority' => 200,
456  );
458  }
459  }
460 
461  return $return;
462 }
463 
469  if (elgg_in_context('widgets')) {
470  return $return;
471  }
472 
473  $entity = $params['entity'];
474  /* @var \ElggEntity $entity */
475  $handler = elgg_extract('handler', $params, false);
476 
477  // access
478  if (elgg_is_logged_in()) {
479  $access = elgg_view('output/access', array('entity' => $entity));
480  $options = array(
481  'name' => 'access',
482  'text' => $access,
483  'href' => false,
484  'priority' => 100,
485  );
487  }
488 
489  if ($entity->canEdit() && $handler) {
490  // edit link
491  $options = array(
492  'name' => 'edit',
493  'text' => elgg_echo('edit'),
494  'title' => elgg_echo('edit:this'),
495  'href' => "$handler/edit/{$entity->getGUID()}",
496  'priority' => 200,
497  );
499  }
500 
501  if ($entity->canDelete() && $handler) {
502  // delete link
503  if (elgg_action_exists("$handler/delete")) {
504  $action = "action/$handler/delete";
505  } else {
506  $action = "action/entity/delete";
507  }
508  $options = array(
509  'name' => 'delete',
510  'text' => elgg_view_icon('delete'),
511  'title' => elgg_echo('delete:this'),
512  'href' => "$action?guid={$entity->getGUID()}",
513  'confirm' => elgg_echo('deleteconfirm'),
514  'priority' => 300,
515  );
517  }
518 
519  return $return;
520 }
521 
527 
528  $widget = $params['entity'];
529  /* @var \ElggWidget $widget */
530  $show_edit = elgg_extract('show_edit', $params, true);
531 
532  $collapse = array(
533  'name' => 'collapse',
534  'text' => ' ',
535  'href' => "#elgg-widget-content-$widget->guid",
536  'link_class' => 'elgg-widget-collapse-button',
537  'rel' => 'toggle',
538  'priority' => 1,
539  );
540  $return[] = \ElggMenuItem::factory($collapse);
541 
542  if ($widget->canEdit()) {
543  $delete = array(
544  'name' => 'delete',
545  'text' => elgg_view_icon('delete-alt'),
546  'title' => elgg_echo('widget:delete', array($widget->getTitle())),
547  'href' => "action/widgets/delete?widget_guid=$widget->guid",
548  'is_action' => true,
549  'link_class' => 'elgg-widget-delete-button',
550  'id' => "elgg-widget-delete-button-$widget->guid",
551  'data-elgg-widget-type' => $widget->handler,
552  'priority' => 900,
553  );
554  $return[] = \ElggMenuItem::factory($delete);
555 
556  if ($show_edit) {
557  $edit = array(
558  'name' => 'settings',
559  'text' => elgg_view_icon('settings-alt'),
560  'title' => elgg_echo('widget:edit'),
561  'href' => "#widget-edit-$widget->guid",
562  'link_class' => "elgg-widget-edit-button",
563  'rel' => 'toggle',
564  'priority' => 800,
565  );
566  $return[] = \ElggMenuItem::factory($edit);
567  }
568  }
569 
570  return $return;
571 }
572 
578 
579  if (elgg_get_config('allow_registration')) {
581  'name' => 'register',
582  'href' => elgg_get_registration_url(),
583  'text' => elgg_echo('register'),
584  'link_class' => 'registration_link',
585  ));
586  }
587 
589  'name' => 'forgotpassword',
590  'href' => 'forgotpassword',
591  'text' => elgg_echo('user:password:lost'),
592  'link_class' => 'forgot_link',
593  ));
594 
595  return $return;
596 }
597 
598 
603 function _elgg_nav_init() {
604  elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
605 
606  elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup');
607  elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
608  elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
609  elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
610  elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
611 
612  elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
613 
615  'name' => 'powered',
616  'text' => elgg_echo("elgg:powered"),
617  'href' => 'http://elgg.org',
618  'title' => 'Elgg ' . elgg_get_version(true),
619  'section' => 'meta',
620  )));
621 
622  elgg_register_ajax_view('navigation/menu/user_hover/contents');
623 
624  // Using a view extension to ensure that themes that have replaced the item view
625  // still load the required AMD modules
626  elgg_extend_view('navigation/menu/elements/item', 'navigation/menu/elements/item_deps');
627 }
628 
641 function _elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params) {
642  if (is_array($return_value)) {
643  $return_value[] = 'navigation/menu/user_hover/contents';
644  }
645 
646  return $return_value;
647 }
648 
649 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
650  $events->registerHandler('init', 'system', '_elgg_nav_init');
651 };
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$custom_menu_items
Definition: save.php:21
elgg_view_icon($name, $vars=array())
View one of the icons.
Definition: views.php:1582
$n
Profile fields.
Definition: list.php:9
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:51
$action
Definition: full.php:133
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1151
elgg_get_registration_url(array $query=[], $fragment= '')
Returns site&#39;s registration URL Triggers a &#39;registration_url&#39;, &#39;site&#39; plugin hook that can be used by...
Definition: users.php:411
if(!$items) $item
Definition: delete.php:17
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_action_exists($action)
Check if an action is registered and its script exists.
Definition: actions.php:227
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
$link
Definition: container.php:14
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
$url
Definition: exceptions.php:24
$title
Definition: save.php:22
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
$featured_menu_names
Definition: save.php:15
static factory($options)
Create an ElggMenuItem from an associative array.
$owner
Definition: crop.php:8
$key
Definition: summary.php:34
global $CONFIG
$num_menu_items
Definition: save.php:20
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:826
elgg_get_context()
Get the current context.
Definition: pageowner.php:204
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:336
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:56
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:221
elgg_extend_view($view, $view_extension, $priority=501)
Extends a view with another view.
Definition: views.php:380
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1028
elgg_get_version($human_readable=false)
Get the current Elgg version information.
Definition: elgglib.php:1071
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:62
$entity
Definition: delete.php:7
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$index
Definition: gallery.php:49
$handler
Definition: add.php:10
$access
Definition: save.php:15
if(!$display_name) $type
Definition: delete.php:27