Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
Service.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Menu;
3 
5 use Elgg\Config;
7 
11 class Service {
12 
16  private $hooks;
17 
21  private $config;
22 
31  public function __construct(PluginHooksService $hooks, Config $config) {
32  $this->hooks = $hooks;
33  $this->config = $config;
34  }
35 
46  public function getMenu($name, array $params = []) {
47  return $this->prepareMenu($this->getUnpreparedMenu($name, $params));
48  }
49 
58  public function getUnpreparedMenu($name, array $params = []) {
59  $menus = $this->config->getVolatile('menus');
60  $items = [];
61  if ($menus && isset($menus[$name])) {
62  $items = elgg_extract($name, $menus, []);
63  }
64 
65  $params['name'] = $name;
66 
67  $params = $this->hooks->trigger('parameters', "menu:$name", $params, $params);
68 
69  if (!isset($params['sort_by'])) {
70  $params['sort_by'] = 'priority';
71  }
72 
73  $items = $this->hooks->trigger('register', "menu:$name", $params, $items);
74 
75  return new UnpreparedMenu($params, $items);
76  }
77 
85  public function prepareMenu(UnpreparedMenu $menu) {
86  $name = $menu->getName();
87  $params = $menu->getParams();
88  $sort_by = $menu->getSortBy();
89 
90  $builder = new ElggMenuBuilder($menu->getItems());
91  $params['menu'] = $builder->getMenu($sort_by);
92  $params['selected_item'] = $builder->getSelected();
93 
94  $params['menu'] = $this->hooks->trigger('prepare', "menu:$name", $params, $params['menu']);
95 
96  return new Menu($params);
97  }
98 
111  function combineMenus(array $names = [], array $params = [], $new_name = '') {
112  if (!$new_name) {
113  $new_name = implode('__' , $names);
114  }
115 
116  $all_items = [];
117  foreach ($names as $name) {
118  $items = $this->getMenu($name, $params)->getItems();
119 
120  foreach ($items as $item) {
121  $section = $item->getSection();
122  if ($section == 'default') {
123  $item->setSection($name);
124  }
125  $item->setData('menu_name', $name);
126  $all_items[] = $item;
127  }
128  }
129 
130  $params['name'] = $new_name;
131 
132  return new UnpreparedMenu($params, $all_items);
133  }
134 }
getSortBy()
Get the designated (or default) sort strategy.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
__construct(PluginHooksService $hooks, Config $config)
Constructor.
Definition: Service.php:31
getMenu($name, array $params=[])
Build a full menu, pulling items from configuration and the "register" menu hooks.
Definition: Service.php:46
Access to configuration values.
Definition: Config.php:11
getUnpreparedMenu($name, array $params=[])
Build an unprepared menu.
Definition: Service.php:58
$params
Definition: login.php:72
getParams()
Get the menu parameters.
getSelected()
Get the selected menu item.
Linear set of menu items collected from configuration and the "register" hook.
Methods to construct and prepare menus for rendering.
Definition: Service.php:11
getName()
Get the menu name.
$item
Definition: item.php:12
combineMenus(array $names=[], array $params=[], $new_name= '')
Combine several menus into one.
Definition: Service.php:111
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
$items
Definition: list.php:11
getItems()
Get the menu items.
$menu
Definition: default.php:19
A complete menu, sorted, filtered by the "prepare" hook, and split into sections. ...
Definition: Menu.php:11
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" hook.
Definition: Service.php:85