Elgg  Version 4.3
Entity.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
6 
13 class Entity {
14 
22  public static function registerEdit(\Elgg\Hook $hook) {
23  $entity = $hook->getEntityParam();
24  if (!($entity instanceof \ElggEntity) || $entity instanceof \ElggUser) {
25  // users mostly use the hover menu for their actions
26  return;
27  }
28 
29  $edit_url = elgg_generate_entity_url($entity, 'edit');
30 
31  if (empty($edit_url) || !$entity->canEdit()) {
32  return;
33  }
34 
35  /* @var $return MenuItems */
36  $return = $hook->getValue();
37 
38  $return[] = \ElggMenuItem::factory([
39  'name' => 'edit',
40  'icon' => 'edit',
41  'text' => elgg_echo('edit'),
42  'title' => elgg_echo('edit:this'),
43  'href' => $edit_url,
44  'priority' => 900,
45  ]);
46 
47  return $return;
48  }
49 
57  public static function registerDelete(\Elgg\Hook $hook) {
58  $entity = $hook->getEntityParam();
59  if (!$entity instanceof \ElggEntity || $entity instanceof \ElggUser || $entity instanceof \ElggPlugin || $entity instanceof \ElggUpgrade) {
60  // users mostly use the hover menu for their actions
61  // plugins can't be removed
62  // upgrades deleting has no point, they'll be rediscovered again
63  return;
64  }
65 
66  $delete_url = elgg_generate_action_url('entity/delete', [
67  'guid' => $entity->guid,
68  ]);
69 
70  if (empty($delete_url) || !$entity->canDelete()) {
71  return;
72  }
73 
74  /* @var $return MenuItems */
75  $return = $hook->getValue();
76 
77  $return[] = \ElggMenuItem::factory([
78  'name' => 'delete',
79  'icon' => 'delete',
80  'text' => elgg_echo('delete'),
81  'title' => elgg_echo('delete:this'),
82  'href' => $delete_url,
83  'confirm' => elgg_echo('deleteconfirm'),
84  'priority' => 950,
85  ]);
86 
87  return $return;
88  }
89 
97  public static function registerPlugin(\Elgg\Hook $hook) {
98  $entity = $hook->getEntityParam();
99  if (!$entity instanceof \ElggPlugin || !$entity->canEdit()) {
100  return;
101  }
102 
103  /* @var $return MenuItems */
104  $return = $hook->getValue();
105 
106  if (elgg_view_exists("plugins/{$entity->getID()}/settings")) {
107  $return[] = \ElggMenuItem::factory([
108  'name' => 'settings',
109  'icon' => 'settings-alt',
110  'text' => elgg_echo('settings'),
111  'href' => elgg_generate_url('admin:plugin_settings', [
112  'plugin_id' => $entity->getID(),
113  ]),
114  'section' => 'admin'
115  ]);
116  }
117 
118  $priority = $entity->getPriority();
119 
120  // top and up link only if not at top
121  if ($priority > 1) {
122  $return[] = \ElggMenuItem::factory([
123  'name' => 'top',
124  'icon' => 'angle-double-up',
125  'text' => elgg_echo('top'),
126  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
127  'plugin_guid' => $entity->guid,
128  'priority' => 'first',
129  ]),
130  'priority' => 11,
131  ]);
132 
133  $return[] = \ElggMenuItem::factory([
134  'name' => 'up',
135  'icon' => 'angle-up',
136  'text' => elgg_echo('up'),
137  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
138  'plugin_guid' => $entity->guid,
139  'priority' => '-1',
140  ]),
141  'priority' => 12,
142  ]);
143  }
144 
145  // down and bottom links only if not at bottom
146  if ($priority < _elgg_services()->plugins->getMaxPriority()) {
147  $return[] = \ElggMenuItem::factory([
148  'name' => 'down',
149  'icon' => 'angle-down',
150  'text' => elgg_echo('down'),
151  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
152  'plugin_guid' => $entity->guid,
153  'priority' => '+1',
154  ]),
155  'priority' => 13,
156  ]);
157 
158  $return[] = \ElggMenuItem::factory([
159  'name' => 'bottom',
160  'icon' => 'angle-double-down',
161  'text' => elgg_echo('bottom'),
162  'href' => elgg_generate_action_url('admin/plugins/set_priority', [
163  'plugin_guid' => $entity->guid,
164  'priority' => 'last',
165  ]),
166  'priority' => 14,
167  ]);
168  }
169 
170  // remove all user and plugin settings
171  $return[] = \ElggMenuItem::factory([
172  'name' => 'remove_settings',
173  'icon' => 'trash-alt',
174  'text' => elgg_echo('plugins:settings:remove:menu:text'),
175  'href' => elgg_generate_action_url('plugins/settings/remove', [
176  'plugin_id' => $entity->getID(),
177  ]),
178  'confirm' => elgg_echo('plugins:settings:remove:menu:confirm'),
179  ]);
180 
181  return $return;
182  }
183 
191  public static function registerUpgrade(\Elgg\Hook $hook) {
192  $entity = $hook->getEntityParam();
193  if (!$entity instanceof \ElggUpgrade || !$entity->canEdit()) {
194  return;
195  }
196 
197  /* @var $return MenuItems */
198  $result = $hook->getValue();
199 
200  if (!$entity->isCompleted()) {
202  'name' => 'run_upgrade',
203  'icon' => 'play',
204  'text' => elgg_echo('admin:upgrades:menu:run_single'),
205  'href' => false,
206  'deps' => [
207  'core/js/upgrader',
208  ],
209  'data-guid' => $entity->guid,
210  'priority' => 600,
211  ]);
212  } elseif ($batch = $entity->getBatch()) {
213  if (!$batch->shouldBeSkipped()) {
214  // only show reset if it will have an effect
216  'name' => 'reset',
217  'icon' => 'sync',
218  'text' => elgg_echo('reset'),
219  'href' => elgg_generate_action_url('admin/upgrade/reset', [
220  'guid' => $entity->guid,
221  ]),
222  'priority' => 600,
223  ]);
224  }
225  }
226 
227  return $result;
228  }
229 
237  public static function registerUserHoverAdminSection(\Elgg\Hook $hook) {
238  $entity = $hook->getEntityParam();
239  if (!$entity instanceof \ElggUser || !elgg_is_admin_logged_in()) {
240  return;
241  }
242 
243  if (!(bool) $hook->getParam('add_user_hover_admin_section', false)) {
244  return;
245  }
246 
247  $user_hover = elgg()->menus->getUnpreparedMenu('user_hover', [
248  'entity' => $entity,
249  ]);
250 
251  /* @var $result MenuItems */
252  $result = $hook->getValue();
253 
254  /* @var $menu_item \ElggMenuItem */
255  foreach ($user_hover->getItems() as $menu_item) {
256  if ($menu_item->getSection() !== 'admin') {
257  continue;
258  }
259 
260  $menu_item->setSection('default');
261  $result[] = $menu_item;
262  }
263 
264  return $result;
265  }
266 }
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:152
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:52
elgg_generate_action_url($action, array $query=[], $add_csrf_tokens=true)
Generate an action URL.
static registerUserHoverAdminSection(\Elgg\Hook $hook)
Add the user hover admin section to the entity menu of an ElggUser, if requested. ...
Definition: Entity.php:237
static registerDelete(\Elgg\Hook $hook)
Register the delete menu item.
Definition: Entity.php:57
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
static factory($options)
Create an ElggMenuItem from an associative array.
Models an event passed to hook handlers.
Definition: Hook.php:11
$entity
Definition: reset.php:8
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
static registerPlugin(\Elgg\Hook $hook)
Registers menu items for the entity menu of a plugin.
Definition: Entity.php:97
static registerUpgrade(\Elgg\Hook $hook)
Add menu items to the entity menu of ElggUpgrade.
Definition: Entity.php:191
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
static registerEdit(\Elgg\Hook $hook)
Register the edit menu item.
Definition: Entity.php:22
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
Register menu items to the entity menu.
Definition: Entity.php:13
elgg_generate_entity_url(ElggEntity $entity, $resource= 'view', $subresource=null, array $parameters=[])
Generate entity URL from a named route.
var elgg
Definition: elgglib.js:4
$priority