/
WidgetsService.php
381 lines (322 loc) · 9.61 KB
/
WidgetsService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
*
* Use the elgg_* versions instead.
*
* @since 1.9.0
* @access private
*/
class Elgg_WidgetsService {
/**
* @see Elgg_WidgetsService::getWidgets()
* @var stdClass
*/
private $widgets;
/**
* @see Elgg_WidgetsService::get()
* @var array
*/
private $widget_cache = array();
/**
* @var array
*/
private $default_widget_info;
/**
* @see Elgg_WidgetsService::registerType
* @var callback
*/
private $set_config_callback;
public function __construct($set_config_callback = 'elgg_set_config') {
$this->set_config_callback = $set_config_callback;
}
/**
* @see elgg_get_widgets
* @access private
* @since 1.9.0
*/
public function get($user_guid, $context) {
$widget_cache_key = "$context-$user_guid";
if (isset($this->widget_cache[$widget_cache_key])) {
return $this->widget_cache[$widget_cache_key];
}
$options = array(
'type' => 'object',
'subtype' => 'widget',
'owner_guid' => $user_guid,
'private_setting_name' => 'context',
'private_setting_value' => $context,
'limit' => 0
);
$widgets = elgg_get_entities_from_private_settings($options);
if (!$widgets) {
return array();
}
$sorted_widgets = array();
foreach ($widgets as $widget) {
if (!isset($sorted_widgets[(int)$widget->column])) {
$sorted_widgets[(int)$widget->column] = array();
}
$sorted_widgets[(int)$widget->column][$widget->order] = $widget;
}
foreach ($sorted_widgets as $col => $widgets) {
ksort($sorted_widgets[$col]);
}
$this->widget_cache[$widget_cache_key] = $sorted_widgets;
return $sorted_widgets;
}
/**
* @see elgg_view_widgets
* @access private
* @since 1.9.0
*/
public function view($user, $context, $column, $show_access = true) {
$widgets = elgg_get_widgets($user->guid, $context);
$column_widgets = $widgets[$column];
$column_html = "<div class=\"elgg-widgets\" id=\"elgg-widget-col-$column\">";
if (count($column_widgets) > 0) {
foreach ($column_widgets as $widget) {
if ($this->validateType($widget->handler)) {
$column_html .= elgg_view_entity($widget, array('show_access' => $show_access));
}
}
}
$column_html .= '</div>';
return $column_html;
}
/**
* @see elgg_create_widget
* @access private
* @since 1.9.0
*/
public function create($owner_guid, $handler, $context, $access_id = null) {
if (empty($owner_guid) || empty($handler) || !$this->validateType($handler)) {
return false;
}
$owner = get_entity($owner_guid);
if (!$owner) {
return false;
}
$widget = new ElggWidget;
$widget->owner_guid = $owner_guid;
$widget->container_guid = $owner_guid; // @todo - will this work for group widgets
if (isset($access_id)) {
$widget->access_id = $access_id;
} else {
$widget->access_id = get_default_access();
}
if (!$widget->save()) {
return false;
}
// private settings cannot be set until ElggWidget saved
$widget->handler = $handler;
$widget->context = $context;
return $widget->getGUID();
}
/**
* @see elgg_can_edit_widget_layout
* @access private
* @since 1.9.0
*/
public function canEditLayout($context, $user_guid = 0) {
$user = get_entity((int)$user_guid);
if (!$user) {
$user = elgg_get_logged_in_user_entity();
}
$return = false;
if (elgg_is_admin_logged_in()) {
$return = true;
}
if (elgg_get_page_owner_guid() == $user->guid) {
$return = true;
}
$params = array(
'user' => $user,
'context' => $context,
'page_owner' => elgg_get_page_owner_entity()
);
return elgg_trigger_plugin_hook('permissions_check', 'widget_layout', $params, $return);
}
/**
* @see elgg_register_widget_type
* @access private
* @since 1.9.0
*/
public function registerType($handler, $name, $description, $context = array('all'), $multiple = false) {
if (!$handler || !$name) {
return false;
}
if (!isset($this->widgets)) {
$this->widgets = new stdClass;
call_user_func($this->set_config_callback, 'widgets', $this->widgets);//backward compatibility
}
if (!isset($this->widgets->handlers)) {
$this->widgets->handlers = array();
}
$handlerobj = new stdClass;
$handlerobj->name = $name;
$handlerobj->description = $description;
if (is_string($context)) {
elgg_deprecated_notice('context parameters for elgg_register_widget_type() should be passed as an array())', 1.9);
$context = explode(",", $context);
} elseif (empty($context)) {
$context = array('all');
}
$handlerobj->context = $context;
$handlerobj->multiple = $multiple;
$this->widgets->handlers[$handler] = $handlerobj;
return true;
}
/**
* @see elgg_unregister_widget_type
* @access private
* @since 1.9.0
*/
public function unregisterType($handler) {
if (!isset($this->widgets)) {
return false;
}
if (!isset($this->widgets->handlers)) {
return false;
}
if (isset($this->widgets->handlers[$handler])) {
unset($this->widgets->handlers[$handler]);
return true;
}
return false;
}
/**
* @see elgg_is_widget_type
* @access private
* @since 1.9.0
*/
public function validateType($handler) {
if (!empty($this->widgets) &&
!empty($this->widgets->handlers) &&
is_array($this->widgets->handlers) &&
array_key_exists($handler, $this->widgets->handlers)) {
return true;
}
return false;
}
/**
* @see elgg_get_widget_types
* @access private
* @since 1.9.0
*/
public function getTypes($context = "", $exact = false) {
if (empty($this->widgets) ||
empty($this->widgets->handlers) ||
!is_array($this->widgets->handlers)) {
// no widgets
return array();
}
if (!$context) {
$context = elgg_get_context();
}
$widgets = array();
foreach ($this->widgets->handlers as $key => $handler) {
if ($exact) {
if (in_array($context, $handler->context)) {
$widgets[$key] = $handler;
}
} else {
if (in_array('all', $handler->context) || in_array($context, $handler->context)) {
$widgets[$key] = $handler;
}
}
}
return $widgets;
}
/**
* @see elgg_default_widgets_init
* @access private
* @since 1.9.0
*/
public function defaultWidgetsInit() {
$default_widgets = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, array());
$this->default_widget_info = $default_widgets;
if ($default_widgets) {
elgg_register_admin_menu_item('configure', 'default_widgets', 'appearance');
// override permissions for creating widget on logged out / just created entities
elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'elgg_default_widgets_permissions_override');
// only register the callback once per event
$events = array();
foreach ($default_widgets as $info) {
$events[$info['event'] . ',' . $info['entity_type']] = $info;
}
foreach ($events as $info) {
elgg_register_event_handler($info['event'], $info['entity_type'], 'elgg_create_default_widgets');
}
}
}
/**
* @see elgg_create_default_widgets
* @access private
* @since 1.9.0
*/
public function createDefault($event, $type, $entity) {
if (!$this->default_widget_info || !$entity) {
return;
}
$type = $entity->getType();
$subtype = $entity->getSubtype();
// event is already guaranteed by the hook registration.
// need to check subtype and type.
foreach ($this->default_widget_info as $info) {
if ($info['entity_type'] == $type) {
if ($info['entity_subtype'] == ELGG_ENTITIES_ANY_VALUE || $info['entity_subtype'] == $subtype) {
// need to be able to access everything
$old_ia = elgg_set_ignore_access(true);
elgg_push_context('create_default_widgets');
// pull in by widget context with widget owners as the site
// not using elgg_get_widgets() because it sorts by columns and we don't care right now.
$options = array(
'type' => 'object',
'subtype' => 'widget',
'owner_guid' => elgg_get_site_entity()->guid,
'private_setting_name' => 'context',
'private_setting_value' => $info['widget_context'],
'limit' => 0
);
$widgets = elgg_get_entities_from_private_settings($options);
/* @var ElggWidget[] $widgets */
foreach ($widgets as $widget) {
// change the container and owner
$new_widget = clone $widget;
$new_widget->container_guid = $entity->guid;
$new_widget->owner_guid = $entity->guid;
// pull in settings
$settings = get_all_private_settings($widget->guid);
foreach ($settings as $name => $value) {
$new_widget->$name = $value;
}
$new_widget->save();
}
elgg_set_ignore_access($old_ia);
elgg_pop_context();
}
}
}
}
/**
* @see elgg_default_widgets_permissions_override
* @access private
* @since 1.9.0
*/
public function defaultWidgetsPermissionsOverride($hook, $type, $return, $params) {
if ($type == 'object' && $params['subtype'] == 'widget') {
return elgg_in_context('create_default_widgets') ? true : null;
}
return null;
}
/**
* @return stdClass An object holding valid widgets and their configurations.
* This object stores the valid context for widgets, and the handlers
* registered, as well as a description of the widget.
* Widgets are added with {@link add_widget_type()}.
*/
public function getWidgets() {
return $this->widgets;
}
}