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 editting 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  $delete_url = elgg_generate_action_url('entity/delete', [
77  'guid' => $entity->guid,
78  ]);
79 
80  if (empty($delete_url) || !$entity->canDelete()) {
81  return;
82  }
83 
84  /* @var $return MenuItems */
85  $return = $event->getValue();
86 
87  $return[] = \ElggMenuItem::factory([
88  'name' => 'delete',
89  'icon' => 'delete',
90  'text' => elgg_echo('delete'),
91  'title' => elgg_echo('delete:this'),
92  'href' => $delete_url,
93  'confirm' => elgg_echo('deleteconfirm'),
94  'priority' => 950,
95  ]);
96 
97  return $return;
98  }
99 
107  public static function registerComment(\Elgg\Event $event) {
108  $entity = $event->getEntityParam();
109  if (!$entity instanceof \ElggComment || !$entity->canEdit()) {
110  return;
111  }
112 
113  /* @var $return MenuItems */
114  $return = $event->getValue();
115 
116  $return[] = \ElggMenuItem::factory([
117  'name' => 'edit',
118  'icon' => 'edit',
119  'text' => elgg_echo('edit'),
120  'title' => elgg_echo('edit:this'),
121  'href' => false,
122  'priority' => 900,
123  'data-comment-guid' => $entity->guid,
124  ]);
125 
126  return $return;
127  }
128 
136  public static function registerPlugin(\Elgg\Event $event) {
137  $entity = $event->getEntityParam();
138  if (!$entity instanceof \ElggPlugin || !$entity->canEdit()) {
139  return;
140  }
141 
142  /* @var $return MenuItems */
143  $return = $event->getValue();
144 
145  if (elgg_view_exists("plugins/{$entity->getID()}/settings")) {
146  $return[] = \ElggMenuItem::factory([
147  'name' => 'settings',
148  'icon' => 'settings-alt',
149  'text' => elgg_echo('settings'),
150  'href' => elgg_generate_url('admin:plugin_settings', [
151  'plugin_id' => $entity->getID(),
152  ]),
153  'section' => 'admin'
154  ]);
155  }
156 
157  $priority = $entity->getPriority();
158 
159  // top and up link only if not at top
160  if ($priority > 1) {
161  $return[] = \ElggMenuItem::factory([
162  'name' => 'top',
163  'icon' => 'angle-double-up',
164  'text' => elgg_echo('top'),
165  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
166  'plugin_guid' => $entity->guid,
167  'priority' => 'first',
168  ]),
169  'priority' => 11,
170  ]);
171 
172  $return[] = \ElggMenuItem::factory([
173  'name' => 'up',
174  'icon' => 'angle-up',
175  'text' => elgg_echo('up'),
176  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
177  'plugin_guid' => $entity->guid,
178  'priority' => '-1',
179  ]),
180  'priority' => 12,
181  ]);
182  }
183 
184  // down and bottom links only if not at bottom
185  if ($priority < _elgg_services()->plugins->getMaxPriority()) {
186  $return[] = \ElggMenuItem::factory([
187  'name' => 'down',
188  'icon' => 'angle-down',
189  'text' => elgg_echo('down'),
190  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
191  'plugin_guid' => $entity->guid,
192  'priority' => '+1',
193  ]),
194  'priority' => 13,
195  ]);
196 
197  $return[] = \ElggMenuItem::factory([
198  'name' => 'bottom',
199  'icon' => 'angle-double-down',
200  'text' => elgg_echo('bottom'),
201  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
202  'plugin_guid' => $entity->guid,
203  'priority' => 'last',
204  ]),
205  'priority' => 14,
206  ]);
207  }
208 
209  // remove all user and plugin settings
210  $return[] = \ElggMenuItem::factory([
211  'name' => 'remove_settings',
212  'icon' => 'trash-alt',
213  'text' => elgg_echo('plugins:settings:remove:menu:text'),
214  'href' => elgg_generate_action_url('plugins/settings/remove', [
215  'plugin_id' => $entity->getID(),
216  ]),
217  'confirm' => elgg_echo('plugins:settings:remove:menu:confirm'),
218  ]);
219 
220  return $return;
221  }
222 
230  public static function registerUpgrade(\Elgg\Event $event) {
231  $entity = $event->getEntityParam();
232  if (!$entity instanceof \ElggUpgrade || !$entity->canEdit()) {
233  return;
234  }
235 
236  /* @var $return MenuItems */
237  $result = $event->getValue();
238 
239  if (!$entity->isCompleted()) {
241  'name' => 'run_upgrade',
242  'icon' => 'play',
243  'text' => elgg_echo('admin:upgrades:menu:run_single'),
244  'href' => false,
245  'deps' => [
246  'admin/upgrades',
247  ],
248  'data-guid' => $entity->guid,
249  'priority' => 600,
250  ]);
251  } else {
252  $batch = $entity->getBatch();
253  if ($batch instanceof Batch && !$batch->shouldBeSkipped()) {
254  // only show reset if it will have an effect
256  'name' => 'reset',
257  'icon' => 'sync',
258  'text' => elgg_echo('reset'),
259  'href' => elgg_generate_action_url('admin/upgrade/reset', [
260  'guid' => $entity->guid,
261  ]),
262  'priority' => 600,
263  ]);
264  }
265  }
266 
267  return $result;
268  }
269 
277  public static function registerUserHoverAdminSection(\Elgg\Event $event) {
278  $entity = $event->getEntityParam();
279  if (!$entity instanceof \ElggUser || !elgg_is_admin_logged_in()) {
280  return;
281  }
282 
283  if (!(bool) $event->getParam('add_user_hover_admin_section', false)) {
284  return;
285  }
286 
287  $user_hover = elgg()->menus->getUnpreparedMenu('user_hover', [
288  'entity' => $entity,
289  ]);
290 
291  /* @var $result MenuItems */
292  $result = $event->getValue();
293 
294  /* @var $menu_item \ElggMenuItem */
295  foreach ($user_hover->getItems() as $menu_item) {
296  if ($menu_item->getSection() !== 'admin') {
297  continue;
298  }
299 
300  $menu_item->setSection('default');
301  $result[] = $menu_item;
302  }
303 
304  return $result;
305  }
306 }
elgg
Definition: install.js:27
static registerPlugin(\Elgg\Event $event)
Registers menu items for the entity menu of a plugin.
Definition: Entity.php:136
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:52
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:230
static factory(array $options)
Create an ElggMenuItem from an associative array.
$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:277
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:107
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
static registerDelete(\Elgg\Event $event)
Register the delete menu item.
Definition: Entity.php:67
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