1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Menus; |
4
|
|
|
|
5
|
|
|
use Elgg\Menu\MenuItems; |
6
|
|
|
use Elgg\Upgrade\Batch; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Register menu items to the entity menu |
10
|
|
|
* |
11
|
|
|
* @since 4.0 |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class Entity { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Register the edit menu item |
18
|
|
|
* |
19
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity' |
20
|
|
|
* |
21
|
|
|
* @return void|MenuItems |
22
|
|
|
*/ |
23
|
20 |
|
public static function registerEdit(\Elgg\Event $event) { |
24
|
20 |
|
$entity = $event->getEntityParam(); |
25
|
20 |
|
if (!($entity instanceof \ElggEntity) || $entity instanceof \ElggUser) { |
26
|
|
|
// users mostly use the hover menu for their actions |
27
|
8 |
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/* @var $return MenuItems */ |
31
|
13 |
|
$return = $event->getValue(); |
32
|
|
|
|
33
|
13 |
|
if ($return->get('edit')) { |
34
|
|
|
// a menu item for editting already exists |
35
|
2 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
11 |
|
$edit_url = elgg_generate_entity_url($entity, 'edit'); |
39
|
11 |
|
if (empty($edit_url) || !$entity->canEdit()) { |
40
|
11 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$return[] = \ElggMenuItem::factory([ |
44
|
|
|
'name' => 'edit', |
45
|
|
|
'icon' => 'edit', |
46
|
|
|
'text' => elgg_echo('edit'), |
47
|
|
|
'title' => elgg_echo('edit:this'), |
48
|
|
|
'href' => $edit_url, |
49
|
|
|
'priority' => 900, |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
return $return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register the delete menu item |
57
|
|
|
* |
58
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity' |
59
|
|
|
* |
60
|
|
|
* @return void|MenuItems |
61
|
|
|
*/ |
62
|
20 |
|
public static function registerDelete(\Elgg\Event $event) { |
63
|
20 |
|
$entity = $event->getEntityParam(); |
64
|
20 |
|
if (!$entity instanceof \ElggEntity || $entity instanceof \ElggUser || $entity instanceof \ElggPlugin || $entity instanceof \ElggUpgrade) { |
65
|
|
|
// users mostly use the hover menu for their actions |
66
|
|
|
// plugins can't be removed |
67
|
|
|
// upgrades deleting has no point, they'll be rediscovered again |
68
|
8 |
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
13 |
|
$delete_url = elgg_generate_action_url('entity/delete', [ |
72
|
13 |
|
'guid' => $entity->guid, |
73
|
13 |
|
]); |
74
|
|
|
|
75
|
13 |
|
if (empty($delete_url) || !$entity->canDelete()) { |
76
|
11 |
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* @var $return MenuItems */ |
80
|
2 |
|
$return = $event->getValue(); |
81
|
|
|
|
82
|
2 |
|
$return[] = \ElggMenuItem::factory([ |
83
|
2 |
|
'name' => 'delete', |
84
|
2 |
|
'icon' => 'delete', |
85
|
2 |
|
'text' => elgg_echo('delete'), |
86
|
2 |
|
'title' => elgg_echo('delete:this'), |
87
|
2 |
|
'href' => $delete_url, |
88
|
2 |
|
'confirm' => elgg_echo('deleteconfirm'), |
89
|
2 |
|
'priority' => 950, |
90
|
2 |
|
]); |
91
|
|
|
|
92
|
2 |
|
return $return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Registers menu items for the entity menu of a comment |
97
|
|
|
* |
98
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity:object:comment' |
99
|
|
|
* |
100
|
|
|
* @return void|MenuItems |
101
|
|
|
*/ |
102
|
3 |
|
public static function registerComment(\Elgg\Event $event) { |
103
|
3 |
|
$entity = $event->getEntityParam(); |
104
|
3 |
|
if (!$entity instanceof \ElggComment || !$entity->canEdit()) { |
105
|
1 |
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/* @var $return MenuItems */ |
109
|
2 |
|
$return = $event->getValue(); |
110
|
|
|
|
111
|
2 |
|
$return[] = \ElggMenuItem::factory([ |
112
|
2 |
|
'name' => 'edit', |
113
|
2 |
|
'icon' => 'edit', |
114
|
2 |
|
'text' => elgg_echo('edit'), |
115
|
2 |
|
'title' => elgg_echo('edit:this'), |
116
|
2 |
|
'href' => false, |
117
|
2 |
|
'priority' => 900, |
118
|
2 |
|
'data-comment-guid' => $entity->guid, |
119
|
2 |
|
]); |
120
|
|
|
|
121
|
2 |
|
return $return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Registers menu items for the entity menu of a plugin |
126
|
|
|
* |
127
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity:object:plugin' |
128
|
|
|
* |
129
|
|
|
* @return void|MenuItems |
130
|
|
|
*/ |
131
|
|
|
public static function registerPlugin(\Elgg\Event $event) { |
132
|
|
|
$entity = $event->getEntityParam(); |
133
|
|
|
if (!$entity instanceof \ElggPlugin || !$entity->canEdit()) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/* @var $return MenuItems */ |
138
|
|
|
$return = $event->getValue(); |
139
|
|
|
|
140
|
|
|
if (elgg_view_exists("plugins/{$entity->getID()}/settings")) { |
141
|
|
|
$return[] = \ElggMenuItem::factory([ |
142
|
|
|
'name' => 'settings', |
143
|
|
|
'icon' => 'settings-alt', |
144
|
|
|
'text' => elgg_echo('settings'), |
145
|
|
|
'href' => elgg_generate_url('admin:plugin_settings', [ |
146
|
|
|
'plugin_id' => $entity->getID(), |
147
|
|
|
]), |
148
|
|
|
'section' => 'admin' |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$priority = $entity->getPriority(); |
153
|
|
|
|
154
|
|
|
// top and up link only if not at top |
155
|
|
|
if ($priority > 1) { |
156
|
|
|
$return[] = \ElggMenuItem::factory([ |
157
|
|
|
'name' => 'top', |
158
|
|
|
'icon' => 'angle-double-up', |
159
|
|
|
'text' => elgg_echo('top'), |
160
|
|
|
'href' => elgg_generate_action_url('admin/plugins/set_priority', [ |
161
|
|
|
'plugin_guid' => $entity->guid, |
162
|
|
|
'priority' => 'first', |
163
|
|
|
]), |
164
|
|
|
'priority' => 11, |
165
|
|
|
]); |
166
|
|
|
|
167
|
|
|
$return[] = \ElggMenuItem::factory([ |
168
|
|
|
'name' => 'up', |
169
|
|
|
'icon' => 'angle-up', |
170
|
|
|
'text' => elgg_echo('up'), |
171
|
|
|
'href' => elgg_generate_action_url('admin/plugins/set_priority', [ |
172
|
|
|
'plugin_guid' => $entity->guid, |
173
|
|
|
'priority' => '-1', |
174
|
|
|
]), |
175
|
|
|
'priority' => 12, |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// down and bottom links only if not at bottom |
180
|
|
|
if ($priority < _elgg_services()->plugins->getMaxPriority()) { |
181
|
|
|
$return[] = \ElggMenuItem::factory([ |
182
|
|
|
'name' => 'down', |
183
|
|
|
'icon' => 'angle-down', |
184
|
|
|
'text' => elgg_echo('down'), |
185
|
|
|
'href' => elgg_generate_action_url('admin/plugins/set_priority', [ |
186
|
|
|
'plugin_guid' => $entity->guid, |
187
|
|
|
'priority' => '+1', |
188
|
|
|
]), |
189
|
|
|
'priority' => 13, |
190
|
|
|
]); |
191
|
|
|
|
192
|
|
|
$return[] = \ElggMenuItem::factory([ |
193
|
|
|
'name' => 'bottom', |
194
|
|
|
'icon' => 'angle-double-down', |
195
|
|
|
'text' => elgg_echo('bottom'), |
196
|
|
|
'href' => elgg_generate_action_url('admin/plugins/set_priority', [ |
197
|
|
|
'plugin_guid' => $entity->guid, |
198
|
|
|
'priority' => 'last', |
199
|
|
|
]), |
200
|
|
|
'priority' => 14, |
201
|
|
|
]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// remove all user and plugin settings |
205
|
|
|
$return[] = \ElggMenuItem::factory([ |
206
|
|
|
'name' => 'remove_settings', |
207
|
|
|
'icon' => 'trash-alt', |
208
|
|
|
'text' => elgg_echo('plugins:settings:remove:menu:text'), |
209
|
|
|
'href' => elgg_generate_action_url('plugins/settings/remove', [ |
210
|
|
|
'plugin_id' => $entity->getID(), |
211
|
|
|
]), |
212
|
|
|
'confirm' => elgg_echo('plugins:settings:remove:menu:confirm'), |
213
|
|
|
]); |
214
|
|
|
|
215
|
|
|
return $return; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Add menu items to the entity menu of ElggUpgrade |
220
|
|
|
* |
221
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity:object:elgg_upgrade' |
222
|
|
|
* |
223
|
|
|
* @return void|MenuItems |
224
|
|
|
*/ |
225
|
|
|
public static function registerUpgrade(\Elgg\Event $event) { |
226
|
|
|
$entity = $event->getEntityParam(); |
227
|
|
|
if (!$entity instanceof \ElggUpgrade || !$entity->canEdit()) { |
228
|
|
|
return; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/* @var $return MenuItems */ |
232
|
|
|
$result = $event->getValue(); |
233
|
|
|
|
234
|
|
|
if (!$entity->isCompleted()) { |
235
|
|
|
$result[] = \ElggMenuItem::factory([ |
236
|
|
|
'name' => 'run_upgrade', |
237
|
|
|
'icon' => 'play', |
238
|
|
|
'text' => elgg_echo('admin:upgrades:menu:run_single'), |
239
|
|
|
'href' => false, |
240
|
|
|
'deps' => [ |
241
|
|
|
'core/js/upgrader', |
242
|
|
|
], |
243
|
|
|
'data-guid' => $entity->guid, |
244
|
|
|
'priority' => 600, |
245
|
|
|
]); |
246
|
|
|
} else { |
247
|
|
|
$batch = $entity->getBatch(); |
248
|
|
|
if ($batch instanceof Batch && !$batch->shouldBeSkipped()) { |
249
|
|
|
// only show reset if it will have an effect |
250
|
|
|
$result[] = \ElggMenuItem::factory([ |
251
|
|
|
'name' => 'reset', |
252
|
|
|
'icon' => 'sync', |
253
|
|
|
'text' => elgg_echo('reset'), |
254
|
|
|
'href' => elgg_generate_action_url('admin/upgrade/reset', [ |
255
|
|
|
'guid' => $entity->guid, |
256
|
|
|
]), |
257
|
|
|
'priority' => 600, |
258
|
|
|
]); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $result; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Add the user hover admin section to the entity menu of an ElggUser, if requested |
267
|
|
|
* |
268
|
|
|
* @param \Elgg\Event $event 'register', 'menu:entity' |
269
|
|
|
* |
270
|
|
|
* @return void|MenuItems |
271
|
|
|
*/ |
272
|
20 |
|
public static function registerUserHoverAdminSection(\Elgg\Event $event) { |
273
|
20 |
|
$entity = $event->getEntityParam(); |
274
|
20 |
|
if (!$entity instanceof \ElggUser || !elgg_is_admin_logged_in()) { |
275
|
20 |
|
return; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if (!(bool) $event->getParam('add_user_hover_admin_section', false)) { |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$user_hover = elgg()->menus->getUnpreparedMenu('user_hover', [ |
283
|
|
|
'entity' => $entity, |
284
|
|
|
]); |
285
|
|
|
|
286
|
|
|
/* @var $result MenuItems */ |
287
|
|
|
$result = $event->getValue(); |
288
|
|
|
|
289
|
|
|
/* @var $menu_item \ElggMenuItem */ |
290
|
|
|
foreach ($user_hover->getItems() as $menu_item) { |
291
|
|
|
if ($menu_item->getSection() !== 'admin') { |
292
|
|
|
continue; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$menu_item->setSection('default'); |
296
|
|
|
$result[] = $menu_item; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $result; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|