Elgg  Version master
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 $menus = [];
17 
24  public function __construct(protected EventsService $events, protected Config $config) {
25  }
26 
37  public function getMenu(string $name, array $params = []): Menu {
39  }
40 
49  public function getUnpreparedMenu(string $name, array $params = []): UnpreparedMenu {
50  $items = $this->prepareMenuItems(elgg_extract('items', $params, []));
51  unset($params['items']);
52 
53  $registered_items = elgg_extract($name, $this->menus);
54  if (is_array($registered_items)) {
55  $items->merge($registered_items);
56  }
57 
58  $params['name'] = $name;
59 
60  $params = $this->events->triggerResults('parameters', "menu:{$name}", $params, $params);
61 
62  if (!isset($params['sort_by'])) {
63  $params['sort_by'] = 'priority';
64  }
65 
66  // trigger specific menu events
67  $entity = elgg_extract('entity', $params);
68  if ($entity instanceof \ElggEntity) {
69  $items = $this->events->triggerResults('register', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $items);
70  }
71 
72  $annotation = elgg_extract('annotation', $params);
73  if ($annotation instanceof \ElggAnnotation) {
74  $items = $this->events->triggerResults('register', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $items);
75  }
76 
77  $relationship = elgg_extract('relationship', $params);
78  if ($relationship instanceof \ElggRelationship) {
79  $items = $this->events->triggerResults('register', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $items);
80  }
81 
82  // trigger generic menu event
83  $items = $this->events->triggerResults('register', "menu:{$name}", $params, $items);
84 
85  return new UnpreparedMenu($params, $items);
86  }
87 
95  public function prepareMenu(UnpreparedMenu $menu): Menu {
96  $name = $menu->getName();
97  $params = $menu->getParams();
98  $sort_by = $menu->getSortBy();
99  $selected_menu_item_name = elgg_extract('selected_item_name', $params, '');
100 
101  $builder = new \ElggMenuBuilder($menu->getItems());
102  $builder->setSelected($selected_menu_item_name);
103 
104  $params['menu'] = $builder->getMenu($sort_by);
105  $params['selected_item'] = $builder->getSelected();
106 
107  // trigger specific menu events
108  $entity = elgg_extract('entity', $params);
109  if ($entity instanceof \ElggEntity) {
110  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $params['menu']);
111  }
112 
113  $annotation = elgg_extract('annotation', $params);
114  if ($annotation instanceof \ElggAnnotation) {
115  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $params['menu']);
116  }
117 
118  $relationship = elgg_extract('relationship', $params);
119  if ($relationship instanceof \ElggRelationship) {
120  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $params['menu']);
121  }
122 
123  // trigger generic menu event
124  $params['menu'] = $this->events->triggerResults('prepare', "menu:$name", $params, $params['menu']);
125 
126  $params['menu'] = $this->prepareVerticalMenu($params['menu'], $params);
127  $params['menu'] = $this->prepareDropdownMenu($params['menu'], $params);
128  $params['menu'] = $this->prepareSelectedParents($params['menu'], $params);
129  $params['menu'] = $this->prepareItemContentsView($params['menu'], $params);
130 
131  return new Menu($params);
132  }
133 
143  if (elgg_extract('prepare_vertical', $params) !== true) {
144  return $menu;
145  }
146 
147  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare) {
148  $child_menu_vars = $menu_item->getChildMenuOptions();
149  if (empty($child_menu_vars['display'])) {
150  $child_menu_vars['display'] = 'toggle';
151  }
152 
153  $menu_item->setChildMenuOptions($child_menu_vars);
154 
155  foreach ($menu_item->getChildren() as $child_menu_item) {
156  $prepare($child_menu_item);
157  }
158  };
159 
160  /* @var $section MenuSection */
161  foreach ($menu as $section) {
162  /* @var $menu_item \ElggMenuItem */
163  foreach ($section as $menu_item) {
164  $prepare($menu_item);
165  }
166  }
167 
168  return $menu;
169  }
170 
180  $selected_item = elgg_extract('selected_item', $params);
181  if (!$selected_item instanceof \ElggMenuItem) {
182  return $menu;
183  }
184 
185  $parent = $selected_item->getParent();
186  while ($parent instanceof \ElggMenuItem) {
187  $parent->setSelected();
188  $parent->addItemClass('elgg-has-selected-child');
189  $parent = $parent->getParent();
190  }
191 
192  return $menu;
193  }
194 
204  if (elgg_extract('prepare_dropdown', $params) !== true) {
205  return $menu;
206  }
207 
208  $items = $menu->getItems('default');
209  if (empty($items)) {
210  return $menu;
211  }
212 
213  $menu_name = elgg_extract('name', $params);
214  $menu->getSection('default')->fill([
216  'name' => 'entity-menu-toggle',
217  'icon' => 'ellipsis-v',
218  'href' => false,
219  'text' => '',
220  'title' => elgg_echo('more'),
221  'child_menu' => [
222  'display' => 'dropdown',
223  'data-position' => json_encode([
224  'at' => 'right bottom',
225  'my' => 'right top',
226  'collision' => 'fit fit',
227  ]),
228  'class' => "elgg-{$menu_name}-dropdown-menu",
229  ],
230  'children' => $items,
231  ]),
232  ]);
233 
234  return $menu;
235  }
236 
247  $item_contents_view = elgg_extract('item_contents_view', $params, 'navigation/menu/elements/item/url');
248 
249  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare, $item_contents_view) {
250  if (!$menu_item->hasItemContentsView()) {
251  $menu_item->setItemContentsView($item_contents_view);
252  }
253 
254  foreach ($menu_item->getChildren() as $child_menu_item) {
255  $prepare($child_menu_item);
256  }
257  };
258 
259  /* @var $section MenuSection */
260  foreach ($menu as $section) {
261  /* @var $menu_item \ElggMenuItem */
262  foreach ($section as $menu_item) {
263  $prepare($menu_item);
264  }
265  }
266 
267  return $menu;
268  }
269 
282  public function combineMenus(array $names = [], array $params = [], string $new_name = ''): UnpreparedMenu {
283  if (!$new_name) {
284  $new_name = implode('__', $names);
285  }
286 
287  $all_items = new MenuItems();
288 
289  foreach ($names as $name) {
290  $items = $this->getUnpreparedMenu($name, $params)->getItems();
291 
292  /* @var $item \ElggMenuItem */
293  foreach ($items as $item) {
294  $section = $item->getSection();
295  if ($section === 'default') {
296  $item->setSection($name);
297  }
298 
299  $item->setData('menu_name', $name);
300 
301  $all_items->add($item);
302  }
303  }
304 
305  $params['name'] = $new_name;
306 
307  return new UnpreparedMenu($params, $all_items);
308  }
309 
317  public function prepareMenuItems(array $items = []): MenuItems {
318  $prepared_items = new MenuItems();
319 
320  foreach ($items as $item) {
321  if (is_array($item)) {
322  $options = $item;
324  }
325 
326  if (!$item instanceof \ElggMenuItem) {
327  continue;
328  }
329 
330  $prepared_items->add($item);
331  }
332 
333  return $prepared_items;
334  }
335 
345  public function registerMenuItem(string $menu_name, \ElggMenuItem $menu_item): void {
346  $this->menus[$menu_name][] = $menu_item;
347  }
348 
358  public function unregisterMenuItem(string $menu_name, string $item_name): ?\ElggMenuItem {
359  if (!isset($this->menus[$menu_name])) {
360  return null;
361  }
362 
363  foreach ($this->menus[$menu_name] as $index => $menu_item) {
364  if ($menu_item->getName() === $item_name) {
365  $item = $this->menus[$menu_name][$index];
366  unset($this->menus[$menu_name][$index]);
367  return $item;
368  }
369  }
370 
371  return null;
372  }
373 
381  public function getAllMenus(): array {
382  return $this->menus;
383  }
384 }
$entity
Definition: reset.php:8
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if($id< 1) $annotation
Definition: delete.php:11
$params
Saves global plugin settings.
Definition: save.php:13
if(! $items) $item
Definition: delete.php:13
$items
Definition: delete.php:8
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
Entity Annotation.
Elgg Menu Item.
static factory(array $options)
Create an ElggMenuItem from an associative array.
Events service.
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
Represents a menu that has been broken down into sections, with menu hierarchy trees setup.
Methods to construct and prepare menus for rendering.
Definition: Service.php:11
prepareDropdownMenu(PreparedMenu $menu, array $params)
Prepares a dropdown menu.
Definition: Service.php:203
prepareVerticalMenu(PreparedMenu $menu, array $params)
Prepares a vertical menu by setting the display child menu option to "toggle" if not set.
Definition: Service.php:142
getMenu(string $name, array $params=[])
Build a full menu, pulling items from configuration and the "register" menu events.
Definition: Service.php:37
__construct(protected EventsService $events, protected Config $config)
Constructor.
Definition: Service.php:24
getUnpreparedMenu(string $name, array $params=[])
Build an unprepared menu.
Definition: Service.php:49
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" event.
Definition: Service.php:95
prepareSelectedParents(PreparedMenu $menu, array $params)
Marks parents of selected items also as selected.
Definition: Service.php:179
registerMenuItem(string $menu_name, \ElggMenuItem $menu_item)
Register a menu item.
Definition: Service.php:345
getAllMenus()
Returns all registered menu items.
Definition: Service.php:381
unregisterMenuItem(string $menu_name, string $item_name)
Remove an item from a menu.
Definition: Service.php:358
prepareItemContentsView(PreparedMenu $menu, array $params)
Set a content view for each menu item based on the default for the menu.
Definition: Service.php:246
combineMenus(array $names=[], array $params=[], string $new_name='')
Combine several menus into one.
Definition: Service.php:282
prepareMenuItems(array $items=[])
Prepare menu items.
Definition: Service.php:317
Linear set of menu items collected from configuration and the "register" event.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$index
Definition: gallery.php:40
$relationship
Elgg default relationship view.
Definition: default.php:10
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:256
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$section
Definition: section.php:30
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$menu
Form body for setting up site menu.
Definition: save.php:7