Elgg  Version master
Entity.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
7 
14 class Entity {
15 
23  public static function registerEdit(\Elgg\Event $event) {
24  $entity = $event->getEntityParam();
25  if (!($entity instanceof \ElggEntity) || $entity instanceof \ElggUser) {
26  // users mostly use the hover menu for their actions
27  return;
28  }
29 
30  /* @var $return MenuItems */
31  $return = $event->getValue();
32 
33  if ($return->get('edit')) {
34  // a menu item for editing already exists
35  return;
36  }
37 
38  if (!$entity->canEdit()) {
39  // checking this before generating the url to prevent notices from deprecated routes
40  return;
41  }
42 
43  $edit_url = elgg_generate_entity_url($entity, 'edit');
44  if (empty($edit_url)) {
45  return;
46  }
47 
48  $return[] = \ElggMenuItem::factory([
49  'name' => 'edit',
50  'icon' => 'edit',
51  'text' => elgg_echo('edit'),
52  'title' => elgg_echo('edit:this'),
53  'href' => $edit_url,
54  'priority' => 900,
55  ]);
56 
57  return $return;
58  }
59 
67  public static function registerDelete(\Elgg\Event $event) {
68  $entity = $event->getEntityParam();
69  if (!$entity instanceof \ElggEntity || $entity instanceof \ElggUser || $entity instanceof \ElggPlugin || $entity instanceof \ElggUpgrade) {
70  // users mostly use the hover menu for their actions
71  // plugins can't be removed
72  // upgrades deleting has no point, they'll be rediscovered again
73  return;
74  }
75 
76  if (!$entity->canDelete()) {
77  return;
78  }
79 
80  /* @var $return MenuItems */
81  $return = $event->getValue();
82 
83  $return[] = \ElggMenuItem::factory([
84  'name' => 'delete',
85  'icon' => 'delete',
86  'text' => elgg_echo('delete'),
87  'title' => elgg_echo('delete:this'),
88  'href' => elgg_generate_action_url('entity/delete', [
89  'guid' => $entity->guid,
90  ]),
91  'confirm' => elgg_echo('deleteconfirm'),
92  'priority' => 950,
93  ]);
94 
95  return $return;
96  }
97 
105  public static function registerTrash(\Elgg\Event $event): ?MenuItems {
106  if (!elgg_get_config('trash_enabled')) {
107  return null;
108  }
109 
110  $entity = $event->getEntityParam();
111  if (!$entity instanceof \ElggEntity || $entity instanceof \ElggUser || $entity instanceof \ElggPlugin || $entity instanceof \ElggUpgrade) {
112  // users mostly use the hover menu for their actions
113  // plugins can't be removed
114  // upgrades deleting has no point, they'll be rediscovered again
115  return null;
116  }
117 
118  if ($entity->isDeleted() || !$entity->canDelete() || !$entity->hasCapability('restorable')) {
119  return null;
120  }
121 
122  /* @var $return MenuItems */
123  $return = $event->getValue();
124 
125  // replace the delete menu item with the trash action
126  $return->remove('delete');
127 
128  $return[] = \ElggMenuItem::factory([
129  'name' => 'trash',
130  'icon' => 'trash-alt',
131  'text' => elgg_echo('trash'),
132  'title' => elgg_echo('trash:this'),
133  'href' => elgg_generate_action_url('entity/trash', [
134  'guid' => $entity->guid,
135  ]),
136  'confirm' => elgg_echo('trashconfirm'),
137  'priority' => 950,
138  ]);
139 
140  return $return;
141  }
142 
150  public static function registerComment(\Elgg\Event $event) {
151  $entity = $event->getEntityParam();
152  if (!$entity instanceof \ElggComment || !$entity->canEdit()) {
153  return;
154  }
155 
156  /* @var $return MenuItems */
157  $return = $event->getValue();
158 
159  $return[] = \ElggMenuItem::factory([
160  'name' => 'edit',
161  'icon' => 'edit',
162  'text' => elgg_echo('edit'),
163  'title' => elgg_echo('edit:this'),
164  'href' => false,
165  'priority' => 900,
166  'data-comment-guid' => $entity->guid,
167  ]);
168 
169  return $return;
170  }
171 
179  public static function registerPlugin(\Elgg\Event $event) {
180  $entity = $event->getEntityParam();
181  if (!$entity instanceof \ElggPlugin || !$entity->canEdit()) {
182  return;
183  }
184 
185  /* @var $return MenuItems */
186  $return = $event->getValue();
187 
188  if (elgg_view_exists("plugins/{$entity->getID()}/settings")) {
189  $return[] = \ElggMenuItem::factory([
190  'name' => 'settings',
191  'icon' => 'settings-alt',
192  'text' => elgg_echo('settings'),
193  'href' => elgg_generate_url('admin:plugin_settings', [
194  'plugin_id' => $entity->getID(),
195  ]),
196  'section' => 'admin'
197  ]);
198  }
199 
200  $priority = $entity->getPriority();
201 
202  // top and up link only if not at top
203  if ($priority > 1) {
204  $return[] = \ElggMenuItem::factory([
205  'name' => 'top',
206  'icon' => 'angle-double-up',
207  'text' => elgg_echo('top'),
208  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
209  'plugin_guid' => $entity->guid,
210  'priority' => 'first',
211  ]),
212  'priority' => 11,
213  ]);
214 
215  $return[] = \ElggMenuItem::factory([
216  'name' => 'up',
217  'icon' => 'angle-up',
218  'text' => elgg_echo('up'),
219  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
220  'plugin_guid' => $entity->guid,
221  'priority' => '-1',
222  ]),
223  'priority' => 12,
224  ]);
225  }
226 
227  // down and bottom links only if not at bottom
228  if ($priority < _elgg_services()->plugins->getMaxPriority()) {
229  $return[] = \ElggMenuItem::factory([
230  'name' => 'down',
231  'icon' => 'angle-down',
232  'text' => elgg_echo('down'),
233  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
234  'plugin_guid' => $entity->guid,
235  'priority' => '+1',
236  ]),
237  'priority' => 13,
238  ]);
239 
240  $return[] = \ElggMenuItem::factory([
241  'name' => 'bottom',
242  'icon' => 'angle-double-down',
243  'text' => elgg_echo('bottom'),
244  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
245  'plugin_guid' => $entity->guid,
246  'priority' => 'last',
247  ]),
248  'priority' => 14,
249  ]);
250  }
251 
252  // remove all user and plugin settings
253  $return[] = \ElggMenuItem::factory([
254  'name' => 'remove_settings',
255  'icon' => 'trash-alt',
256  'text' => elgg_echo('plugins:settings:remove:menu:text'),
257  'href' => elgg_generate_action_url('plugins/settings/remove', [
258  'plugin_id' => $entity->getID(),
259  ]),
260  'confirm' => elgg_echo('plugins:settings:remove:menu:confirm'),
261  ]);
262 
263  return $return;
264  }
265 
273  public static function registerUpgrade(\Elgg\Event $event) {
274  $entity = $event->getEntityParam();
275  if (!$entity instanceof \ElggUpgrade || !$entity->canEdit()) {
276  return;
277  }
278 
279  /* @var $return MenuItems */
280  $result = $event->getValue();
281 
282  if (!$entity->isCompleted()) {
284  'name' => 'run_upgrade',
285  'icon' => 'play',
286  'text' => elgg_echo('admin:upgrades:menu:run_single'),
287  'href' => false,
288  'deps' => [
289  'admin/upgrades',
290  ],
291  'data-guid' => $entity->guid,
292  'priority' => 600,
293  ]);
294  } else {
295  $batch = $entity->getBatch();
296  if ($batch instanceof Batch && !$batch->shouldBeSkipped()) {
297  // only show reset if it will have an effect
299  'name' => 'reset',
300  'icon' => 'sync',
301  'text' => elgg_echo('reset'),
302  'href' => elgg_generate_action_url('admin/upgrade/reset', [
303  'guid' => $entity->guid,
304  ]),
305  'priority' => 600,
306  ]);
307  }
308  }
309 
310  return $result;
311  }
312 
320  public static function registerUserHoverAdminSection(\Elgg\Event $event) {
321  $entity = $event->getEntityParam();
322  if (!$entity instanceof \ElggUser || !elgg_is_admin_logged_in()) {
323  return;
324  }
325 
326  if (!(bool) $event->getParam('add_user_hover_admin_section', false)) {
327  return;
328  }
329 
330  $user_hover = elgg()->menus->getUnpreparedMenu('user_hover', [
331  'entity' => $entity,
332  ]);
333 
334  /* @var $result MenuItems */
335  $result = $event->getValue();
336 
337  /* @var $menu_item \ElggMenuItem */
338  foreach ($user_hover->getItems() as $menu_item) {
339  if ($menu_item->getSection() !== 'admin') {
340  continue;
341  }
342 
343  $menu_item->setSection('default');
344  $result[] = $menu_item;
345  }
346 
347  return $result;
348  }
349 }
elgg
Definition: install.js:27
static registerPlugin(\Elgg\Event $event)
Registers menu items for the entity menu of a plugin.
Definition: Entity.php:179
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:52
Plugin class containing helper functions for plugin activation/deactivation, dependency checking capa...
Definition: ElggPlugin.php:17
static registerEdit(\Elgg\Event $event)
Register the edit menu item.
Definition: Entity.php:23
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
static registerUpgrade(\Elgg\Event $event)
Add menu items to the entity menu of ElggUpgrade.
Definition: Entity.php:273
static factory(array $options)
Create an ElggMenuItem from an associative array.
static registerTrash(\Elgg\Event $event)
Register the trash menu item.
Definition: Entity.php:105
$entity
Definition: reset.php:8
static registerUserHoverAdminSection(\Elgg\Event $event)
Add the user hover admin section to the entity menu of an ElggUser, if requested. ...
Definition: Entity.php:320
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
elgg_generate_entity_url(ElggEntity $entity, string $resource= 'view', string $subresource=null, array $parameters=[])
Generate entity URL from a named route.
Long running upgrades should implement this interface.
Definition: Batch.php:10
static registerComment(\Elgg\Event $event)
Registers menu items for the entity menu of a comment.
Definition: Entity.php:150
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
static registerDelete(\Elgg\Event $event)
Register the delete menu item.
Definition: Entity.php:67
A collection of menu items.
Definition: MenuItems.php:10
elgg_view_exists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
Definition: views.php:131
Register menu items to the entity menu.
Definition: Entity.php:14
$priority
Models an event passed to event handlers.
Definition: Event.php:11