Elgg  Version 5.1
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menu;
4 
5 use Elgg\Config;
7 
11 class Service {
12 
16  protected $events;
17 
21  protected $config;
22 
26  protected $menus = [];
27 
35  $this->events = $events;
36  $this->config = $config;
37  }
38 
49  public function getMenu(string $name, array $params = []): Menu {
50  return $this->prepareMenu($this->getUnpreparedMenu($name, $params));
51  }
52 
61  public function getUnpreparedMenu(string $name, array $params = []): UnpreparedMenu {
62  $items = $this->prepareMenuItems(elgg_extract('items', $params, []));
63  unset($params['items']);
64 
65  $registered_items = elgg_extract($name, $this->menus);
66  if (is_array($registered_items)) {
67  $items->merge($registered_items);
68  }
69 
70  $params['name'] = $name;
71 
72  $params = $this->events->triggerResults('parameters', "menu:{$name}", $params, $params);
73 
74  if (!isset($params['sort_by'])) {
75  $params['sort_by'] = 'priority';
76  }
77 
78  // trigger specific menu events
79  $entity = elgg_extract('entity', $params);
80  if ($entity instanceof \ElggEntity) {
81  $items = $this->events->triggerResults('register', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $items);
82  }
83 
84  $annotation = elgg_extract('annotation', $params);
85  if ($annotation instanceof \ElggAnnotation) {
86  $items = $this->events->triggerResults('register', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $items);
87  }
88 
89  $relationship = elgg_extract('relationship', $params);
90  if ($relationship instanceof \ElggRelationship) {
91  $items = $this->events->triggerResults('register', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $items);
92  }
93 
94  // trigger generic menu event
95  $items = $this->events->triggerResults('register', "menu:{$name}", $params, $items);
96 
97  return new UnpreparedMenu($params, $items);
98  }
99 
107  public function prepareMenu(UnpreparedMenu $menu): Menu {
108  $name = $menu->getName();
109  $params = $menu->getParams();
110  $sort_by = $menu->getSortBy();
111  $selected_menu_item_name = elgg_extract('selected_item_name', $params, '');
112 
113  $builder = new \ElggMenuBuilder($menu->getItems());
114  $builder->setSelected($selected_menu_item_name);
115 
116  $params['menu'] = $builder->getMenu($sort_by);
117  $params['selected_item'] = $builder->getSelected();
118 
119  // trigger specific menu events
120  $entity = elgg_extract('entity', $params);
121  if ($entity instanceof \ElggEntity) {
122  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $params['menu']);
123  }
124 
125  $annotation = elgg_extract('annotation', $params);
126  if ($annotation instanceof \ElggAnnotation) {
127  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $params['menu']);
128  }
129 
130  $relationship = elgg_extract('relationship', $params);
131  if ($relationship instanceof \ElggRelationship) {
132  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $params['menu']);
133  }
134 
135  // trigger generic menu event
136  $params['menu'] = $this->events->triggerResults('prepare', "menu:$name", $params, $params['menu']);
137 
138  $params['menu'] = $this->prepareVerticalMenu($params['menu'], $params);
139  $params['menu'] = $this->prepareDropdownMenu($params['menu'], $params);
140  $params['menu'] = $this->prepareSelectedParents($params['menu'], $params);
141  $params['menu'] = $this->prepareItemContentsView($params['menu'], $params);
142 
143  return new Menu($params);
144  }
145 
155  if (elgg_extract('prepare_vertical', $params) !== true) {
156  return $menu;
157  }
158 
159  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare) {
160  $child_menu_vars = $menu_item->getChildMenuOptions();
161  if (empty($child_menu_vars['display'])) {
162  $child_menu_vars['display'] = 'toggle';
163  }
164 
165  $menu_item->setChildMenuOptions($child_menu_vars);
166 
167  foreach ($menu_item->getChildren() as $child_menu_item) {
168  $prepare($child_menu_item);
169  }
170  };
171 
172  /* @var $section MenuSection */
173  foreach ($menu as $section) {
174  /* @var $menu_item \ElggMenuItem */
175  foreach ($section as $menu_item) {
176  $prepare($menu_item);
177  }
178  }
179 
180  return $menu;
181  }
182 
192  $selected_item = elgg_extract('selected_item', $params);
193  if (!$selected_item instanceof \ElggMenuItem) {
194  return $menu;
195  }
196 
197  $parent = $selected_item->getParent();
198  while ($parent instanceof \ElggMenuItem) {
199  $parent->setSelected();
200  $parent->addItemClass('elgg-has-selected-child');
201  $parent = $parent->getParent();
202  }
203 
204  return $menu;
205  }
206 
216  if (elgg_extract('prepare_dropdown', $params) !== true) {
217  return $menu;
218  }
219 
220  $items = $menu->getItems('default');
221  if (empty($items)) {
222  return $menu;
223  }
224 
225  $menu_name = elgg_extract('name', $params);
226  $menu->getSection('default')->fill([
228  'name' => 'entity-menu-toggle',
229  'icon' => 'ellipsis-v',
230  'href' => false,
231  'text' => '',
232  'title' => elgg_echo('more'),
233  'child_menu' => [
234  'display' => 'dropdown',
235  'data-position' => json_encode([
236  'at' => 'right bottom',
237  'my' => 'right top',
238  'collision' => 'fit fit',
239  ]),
240  'class' => "elgg-{$menu_name}-dropdown-menu",
241  ],
242  'children' => $items,
243  ]),
244  ]);
245 
246  return $menu;
247  }
248 
259  $item_contents_view = elgg_extract('item_contents_view', $params, 'navigation/menu/elements/item/url');
260 
261  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare, $item_contents_view) {
262  if (!$menu_item->hasItemContentsView()) {
263  $menu_item->setItemContentsView($item_contents_view);
264  }
265 
266  foreach ($menu_item->getChildren() as $child_menu_item) {
267  $prepare($child_menu_item);
268  }
269  };
270 
271  /* @var $section MenuSection */
272  foreach ($menu as $section) {
273  /* @var $menu_item \ElggMenuItem */
274  foreach ($section as $menu_item) {
275  $prepare($menu_item);
276  }
277  }
278 
279  return $menu;
280  }
281 
294  public function combineMenus(array $names = [], array $params = [], string $new_name = ''): UnpreparedMenu {
295  if (!$new_name) {
296  $new_name = implode('__', $names);
297  }
298 
299  $all_items = new MenuItems();
300 
301  foreach ($names as $name) {
302  $items = $this->getUnpreparedMenu($name, $params)->getItems();
303 
304  /* @var $item \ElggMenuItem */
305  foreach ($items as $item) {
306  $section = $item->getSection();
307  if ($section === 'default') {
308  $item->setSection($name);
309  }
310 
311  $item->setData('menu_name', $name);
312 
313  $all_items->add($item);
314  }
315  }
316 
317  $params['name'] = $new_name;
318 
319  return new UnpreparedMenu($params, $all_items);
320  }
321 
329  public function prepareMenuItems(array $items = []): MenuItems {
330  $prepared_items = new MenuItems();
331 
332  foreach ($items as $item) {
333  if (is_array($item)) {
334  $options = $item;
336  }
337 
338  if (!$item instanceof \ElggMenuItem) {
339  continue;
340  }
341 
342  $prepared_items->add($item);
343  }
344 
345  return $prepared_items;
346  }
347 
357  public function registerMenuItem(string $menu_name, \ElggMenuItem $menu_item): void {
358  $this->menus[$menu_name][] = $menu_item;
359  }
360 
370  public function unregisterMenuItem(string $menu_name, string $item_name): ?\ElggMenuItem {
371  if (!isset($this->menus[$menu_name])) {
372  return null;
373  }
374 
375  foreach ($this->menus[$menu_name] as $index => $menu_item) {
376  if ($menu_item->getName() === $item_name) {
377  $item = $this->menus[$menu_name][$index];
378  unset($this->menus[$menu_name][$index]);
379  return $item;
380  }
381  }
382 
383  return null;
384  }
385 
393  public function getAllMenus(): array {
394  return $this->menus;
395  }
396 }
if(!$items) $item
Definition: delete.php:13
prepareDropdownMenu(PreparedMenu $menu, array $params)
Prepares a dropdown menu.
Definition: Service.php:215
getUnpreparedMenu(string $name, array $params=[])
Build an unprepared menu.
Definition: Service.php:61
$params
Saves global plugin settings.
Definition: save.php:13
prepareMenuItems(array $items=[])
Prepare menu items.
Definition: Service.php:329
getSortBy()
Get the designated (or default) sort strategy.
if($id< 1) $annotation
Definition: delete.php:11
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$relationship
Elgg default relationship view.
Definition: default.php:10
Entity Annotation.
Events service.
getItems($section_id)
Get items in a section.
$menu
Admin sidebar – just outputs the page menus.
Definition: sidebar.php:6
getAllMenus()
Returns all registered menu items.
Definition: Service.php:393
static factory(array $options)
Create an ElggMenuItem from an associative array.
getMenu(string $name, array $params=[])
Build a full menu, pulling items from configuration and the "register" menu events.
Definition: Service.php:49
__construct(EventsService $events, Config $config)
Constructor.
Definition: Service.php:34
prepareItemContentsView(PreparedMenu $menu, array $params)
Set a content view for each menu item based on the default for the menu.
Definition: Service.php:258
$options
Elgg admin footer.
Definition: footer.php:6
Elgg Menu Item.
$items
Definition: delete.php:8
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
getParams()
Get the menu parameters.
prepareSelectedParents(PreparedMenu $menu, array $params)
Marks parents of selected items also as selected.
Definition: Service.php:191
Linear set of menu items collected from configuration and the "register" event.
Methods to construct and prepare menus for rendering.
Definition: Service.php:11
getSection($id)
Get menu section.
prepareVerticalMenu(PreparedMenu $menu, array $params)
Prepares a vertical menu by setting the display child menu option to "toggle" if not set...
Definition: Service.php:154
$entity
Definition: reset.php:8
getName()
Get the menu name.
Represents a menu that has been broken down into sections, with menu hierarchy trees setup...
combineMenus(array $names=[], array $params=[], string $new_name= '')
Combine several menus into one.
Definition: Service.php:294
unregisterMenuItem(string $menu_name, string $item_name)
Remove an item from a menu.
Definition: Service.php:370
getItems()
Get the menu items.
$index
Definition: gallery.php:40
registerMenuItem(string $menu_name,\ElggMenuItem $menu_item)
Register a menu item.
Definition: Service.php:357
A collection of menu items.
Definition: MenuItems.php:10
A complete menu, sorted, filtered by the "prepare" event, and split into sections.
Definition: Menu.php:10
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" event.
Definition: Service.php:107