Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
navigation.php
Go to the documentation of this file.
1 <?php
93 function elgg_register_menu_item($menu_name, $menu_item) {
94  global $CONFIG;
95 
96  if (is_array($menu_item)) {
97  $options = $menu_item;
98  $menu_item = \ElggMenuItem::factory($options);
99  if (!$menu_item) {
100  $menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
101  elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
102  return false;
103  }
104  }
105 
106  if (!$menu_item instanceof ElggMenuItem) {
107  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  return false;
109  }
110 
111  if (!isset($CONFIG->menus[$menu_name])) {
112  $CONFIG->menus[$menu_name] = array();
113  }
114  $CONFIG->menus[$menu_name][] = $menu_item;
115  return true;
116 }
117 
127 function elgg_unregister_menu_item($menu_name, $item_name) {
128  global $CONFIG;
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 
155 function elgg_is_menu_item_registered($menu_name, $item_name) {
156  global $CONFIG;
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 
181 function elgg_get_menu_item($menu_name, $item_name) {
182  global $CONFIG;
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 
214 function elgg_register_title_button($handler = null, $name = 'add', $entity_type = 'all', $entity_subtype = 'all') {
215 
216  if (!$handler) {
218  }
219 
221  if (!$owner) {
222  // noone owns the page so this is probably an all site list page
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 
250 function elgg_push_breadcrumb($title, $link = null) {
251  global $CONFIG;
252  if (!isset($CONFIG->breadcrumbs)) {
253  $CONFIG->breadcrumbs = array();
254  }
255 
256  $CONFIG->breadcrumbs[] = array('title' => $title, 'link' => $link);
257 }
258 
266  global $CONFIG;
267 
268  if (empty($CONFIG->breadcrumbs) || !is_array($CONFIG->breadcrumbs)) {
269  return array();
270  }
271  return array_pop($CONFIG->breadcrumbs);
272 }
273 
285  global $CONFIG;
286 
287  // if no crumbs set, still allow hook to populate it
288  if (isset($CONFIG->breadcrumbs) && is_array($CONFIG->breadcrumbs)) {
289  $breadcrumbs = $CONFIG->breadcrumbs;
290  } else {
291  $breadcrumbs = array();
292  }
293 
294  $params = array(
295  'breadcrumbs' => $breadcrumbs,
296  );
297  $breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
298  if (!is_array($breadcrumbs)) {
299  return array();
300  }
301 
302  return $breadcrumbs;
303 }
304 
317 function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
318  // remove last crumb if not a link
319  $last_crumb = end($breadcrumbs);
320  if (empty($last_crumb['link'])) {
321  array_pop($breadcrumbs);
322  }
323 
324  // apply excerpt to titles
325  foreach (array_keys($breadcrumbs) as $i) {
326  $breadcrumbs[$i]['title'] = elgg_get_excerpt($breadcrumbs[$i]['title'], 100);
327  }
328  return $breadcrumbs;
329 }
330 
339 
340  $featured_menu_names = elgg_get_config('site_featured_menu_names');
341  $custom_menu_items = elgg_get_config('site_custom_menu_items');
343  // we have featured or custom menu items
344 
345  $registered = $return['default'];
346  /* @var \ElggMenuItem[] $registered */
347 
348  // set up featured menu items
349  $featured = array();
350  foreach ($featured_menu_names as $name) {
351  foreach ($registered as $index => $item) {
352  if ($item->getName() == $name) {
353  $featured[] = $item;
354  unset($registered[$index]);
355  }
356  }
357  }
358 
359  // add custom menu items
360  $n = 1;
361  foreach ($custom_menu_items as $title => $url) {
362  $item = new \ElggMenuItem("custom$n", $title, $url);
363  $featured[] = $item;
364  $n++;
365  }
366 
367  $return['default'] = $featured;
368  if (count($registered) > 0) {
369  $return['more'] = $registered;
370  }
371  } else {
372  // no featured menu items set
373  $max_display_items = 5;
374 
375  // the first n are shown, rest added to more list
376  // if only one item on more menu, stick it with the rest
377  $num_menu_items = count($return['default']);
378  if ($num_menu_items > ($max_display_items + 1)) {
379  $return['more'] = array_splice($return['default'], $max_display_items);
380  }
381  }
382 
383  // check if we have anything selected
384  $selected = false;
385  foreach ($return as $section) {
386  /* @var \ElggMenuItem[] $section */
387 
388  foreach ($section as $item) {
389  if ($item->getSelected()) {
390  $selected = true;
391  break 2;
392  }
393  }
394  }
395 
396  if (!$selected) {
397  // nothing selected, match name to context or match url
398  $current_url = current_page_url();
399  foreach ($return as $section_name => $section) {
400  foreach ($section as $key => $item) {
401  // only highlight internal links
402  if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
403  if ($item->getName() == elgg_get_context()) {
404  $return[$section_name][$key]->setSelected(true);
405  break 2;
406  }
407  if ($item->getHref() == $current_url) {
408  $return[$section_name][$key]->setSelected(true);
409  break 2;
410  }
411  }
412  }
413  }
414  }
415 
416  return $return;
417 }
418 
424  if (elgg_is_logged_in()) {
425  $item = $params['item'];
426  /* @var \ElggRiverItem $item */
427  $object = $item->getObjectEntity();
428  // add comment link but annotations cannot be commented on
429  if ($item->annotation_id == 0) {
430  if ($object->canComment()) {
431  $options = array(
432  'name' => 'comment',
433  'href' => "#comments-add-{$object->guid}-{$item->id}",
434  'text' => elgg_view_icon('speech-bubble'),
435  'title' => elgg_echo('comment:this'),
436  'rel' => 'toggle',
437  'priority' => 50,
438  );
440  }
441  }
442 
443  if (elgg_is_admin_logged_in()) {
444  $options = array(
445  'name' => 'delete',
446  'href' => elgg_add_action_tokens_to_url("action/river/delete?id=$item->id"),
447  'text' => elgg_view_icon('delete'),
448  'title' => elgg_echo('river:delete'),
449  'confirm' => elgg_echo('deleteconfirm'),
450  'priority' => 200,
451  );
453  }
454  }
455 
456  return $return;
457 }
458 
464  if (elgg_in_context('widgets')) {
465  return $return;
466  }
467 
468  $entity = $params['entity'];
469  /* @var \ElggEntity $entity */
470  $handler = elgg_extract('handler', $params, false);
471 
472  // access
473  if (elgg_is_logged_in()) {
474  $access = elgg_view('output/access', array('entity' => $entity));
475  $options = array(
476  'name' => 'access',
477  'text' => $access,
478  'href' => false,
479  'priority' => 100,
480  );
482  }
483 
484  if ($entity->canEdit() && $handler) {
485  // edit link
486  $options = array(
487  'name' => 'edit',
488  'text' => elgg_echo('edit'),
489  'title' => elgg_echo('edit:this'),
490  'href' => "$handler/edit/{$entity->getGUID()}",
491  'priority' => 200,
492  );
494  }
495 
496  if ($entity->canDelete() && $handler) {
497  // delete link
498  if (elgg_action_exists("$handler/delete")) {
499  $action = "action/$handler/delete";
500  } else {
501  $action = "action/entity/delete";
502  }
503  $options = array(
504  'name' => 'delete',
505  'text' => elgg_view_icon('delete'),
506  'title' => elgg_echo('delete:this'),
507  'href' => "$action?guid={$entity->getGUID()}",
508  'confirm' => elgg_echo('deleteconfirm'),
509  'priority' => 300,
510  );
512  }
513 
514  return $return;
515 }
516 
522 
523  $widget = $params['entity'];
524  /* @var \ElggWidget $widget */
525  $show_edit = elgg_extract('show_edit', $params, true);
526 
527  $collapse = array(
528  'name' => 'collapse',
529  'text' => ' ',
530  'href' => "#elgg-widget-content-$widget->guid",
531  'link_class' => 'elgg-widget-collapse-button',
532  'rel' => 'toggle',
533  'priority' => 1,
534  );
535  $return[] = \ElggMenuItem::factory($collapse);
536 
537  if ($widget->canEdit()) {
538  $delete = array(
539  'name' => 'delete',
540  'text' => elgg_view_icon('delete-alt'),
541  'title' => elgg_echo('widget:delete', array($widget->getTitle())),
542  'href' => "action/widgets/delete?widget_guid=$widget->guid",
543  'is_action' => true,
544  'link_class' => 'elgg-widget-delete-button',
545  'id' => "elgg-widget-delete-button-$widget->guid",
546  'data-elgg-widget-type' => $widget->handler,
547  'priority' => 900,
548  );
549  $return[] = \ElggMenuItem::factory($delete);
550 
551  if ($show_edit) {
552  $edit = array(
553  'name' => 'settings',
554  'text' => elgg_view_icon('settings-alt'),
555  'title' => elgg_echo('widget:edit'),
556  'href' => "#widget-edit-$widget->guid",
557  'link_class' => "elgg-widget-edit-button",
558  'rel' => 'toggle',
559  'priority' => 800,
560  );
561  $return[] = \ElggMenuItem::factory($edit);
562  }
563  }
564 
565  return $return;
566 }
567 
573 
574  if (elgg_get_config('allow_registration')) {
576  'name' => 'register',
577  'href' => 'register',
578  'text' => elgg_echo('register'),
579  'link_class' => 'registration_link',
580  ));
581  }
582 
584  'name' => 'forgotpassword',
585  'href' => 'forgotpassword',
586  'text' => elgg_echo('user:password:lost'),
587  'link_class' => 'forgot_link',
588  ));
589 
590  return $return;
591 }
592 
593 
598 function _elgg_nav_init() {
599  elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
600 
601  elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup');
602  elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
603  elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
604  elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
605  elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
606 
607  elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
608 
610  'name' => 'powered',
611  'text' => elgg_echo("elgg:powered"),
612  'href' => 'http://elgg.org',
613  'title' => 'Elgg ' . elgg_get_version(true),
614  'section' => 'meta',
615  )));
616 
617  elgg_register_ajax_view('navigation/menu/user_hover/contents');
618 
619  // Using a view extension to ensure that themes that have replaced the item view
620  // still load the required AMD modules
621  elgg_extend_view('navigation/menu/elements/item', 'navigation/menu/elements/item_deps');
622 }
623 
636 function _elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params) {
637  if (is_array($return_value)) {
638  $return_value[] = 'navigation/menu/user_hover/contents';
639  }
640 
641  return $return_value;
642 }
643 
644 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
645  $events->registerHandler('init', 'system', '_elgg_nav_init');
646 };
$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:1447
$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:125
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1123
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:60
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:216
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:715
$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
$item
Definition: item.php:12
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:801
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:342
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:220
elgg_extend_view($view, $view_extension, $priority=501)
Extends a view with another view.
Definition: views.php:386
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:1310
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1000
elgg_get_version($human_readable=false)
Get the current Elgg version information.
Definition: elgglib.php:1043
getName()
Get the identifier of the menu item.
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:72
$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
$handler
Definition: add.php:10
$access
Definition: save.php:15
if(!$display_name) $type
Definition: delete.php:27