Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
widgets.php
Go to the documentation of this file.
1 <?php
25  return _elgg_services()->widgets->getWidgets($owner_guid, $context);
26 }
27 
39 function elgg_create_widget($owner_guid, $handler, $context, $access_id = null) {
40  return _elgg_services()->widgets->createWidget($owner_guid, $handler, $context, $access_id);
41 }
42 
55  return _elgg_services()->widgets->canEditLayout($context, $user_guid);
56 }
57 
74 function elgg_register_widget_type($handler, $name = null, $description = null, $context = array('all'), $multiple = false) {
75  if (is_array($handler)) {
77  } else {
78  if (is_string($context)) {
79  elgg_deprecated_notice('context parameters for elgg_register_widget_type() should be passed as an array())', 1.9);
80  $context = explode(",", $context);
81  } elseif (empty($context)) {
82  $context = array('all');
83  }
84 
85  $definition = \Elgg\WidgetDefinition::factory([
86  'id' => $handler,
87  'name' => $name,
88  'description' => $description,
89  'context' => $context,
90  'multiple' => $multiple,
91  ]);
92  }
93 
94  return _elgg_services()->widgets->registerType($definition);
95 }
96 
106  return _elgg_services()->widgets->unregisterType($handler);
107 }
108 
120  return _elgg_services()->widgets->validateType($handler, $context, $container);
121 }
122 
140 function elgg_get_widget_types($context = "", $exact = false) {
141  if (is_array($context)) {
142  $params = $context;
143  } else {
144  $params = [
145  'context' => $context,
146  'exact' => $exact,
147  'container' => null,
148  ];
149  }
150  return _elgg_services()->widgets->getTypes($params);
151 }
152 
163 function _elgg_widgets_set_ajax_title($hook, $type, $results, $params) {
164  if ($params['action'] == 'widgets/save') {
165  // @todo Elgg makes ajax so difficult - no other way to add data to output
166  $widget = get_entity(get_input('guid'));
167  if ($widget && $widget->title) {
168  $results['title'] = $widget->title;
169  return $results;
170  }
171  }
172 }
173 
180 function _elgg_widgets_init() {
181  elgg_register_action('widgets/save');
182  elgg_register_action('widgets/add');
183  elgg_register_action('widgets/move');
184  elgg_register_action('widgets/delete');
185 
186  elgg_register_plugin_hook_handler('output', 'ajax', '_elgg_widgets_set_ajax_title');
187 }
188 
215  global $CONFIG;
216  $default_widgets = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, array());
217 
218  $CONFIG->default_widget_info = $default_widgets;
219 
220  if ($default_widgets) {
221  elgg_register_admin_menu_item('configure', 'default_widgets', 'appearance');
222 
223  // override permissions for creating widget on logged out / just created entities
224  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_default_widgets_permissions_override');
225 
226  // only register the callback once per event
227  $events = array();
228  foreach ($default_widgets as $info) {
229  if (!is_array($info)) {
230  continue;
231  }
232  $event = elgg_extract('event', $info);
233  $entity_type = elgg_extract('entity_type', $info);
234  if (!$event || !$entity_type) {
235  continue;
236  }
237  if (!isset($events[$event][$entity_type])) {
238  elgg_register_event_handler($event, $entity_type, '_elgg_create_default_widgets');
239  $events[$event][$entity_type] = true;
240  }
241  }
242  }
243 }
244 
259  $default_widget_info = elgg_get_config('default_widget_info');
260 
261  if (!$default_widget_info || !$entity) {
262  return;
263  }
264 
265  $type = $entity->getType();
266  $subtype = $entity->getSubtype();
267 
268  // event is already guaranteed by the hook registration.
269  // need to check subtype and type.
270  foreach ($default_widget_info as $info) {
271  if ($info['entity_type'] == $type) {
272  if ($info['entity_subtype'] == ELGG_ENTITIES_ANY_VALUE || $info['entity_subtype'] == $subtype) {
273 
274  // need to be able to access everything
275  $old_ia = elgg_set_ignore_access(true);
276  elgg_push_context('create_default_widgets');
277 
278  // pull in by widget context with widget owners as the site
279  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
280  $options = array(
281  'type' => 'object',
282  'subtype' => 'widget',
283  'owner_guid' => elgg_get_site_entity()->guid,
284  'private_setting_name' => 'context',
285  'private_setting_value' => $info['widget_context'],
286  'limit' => 0
287  );
288 
290  /* @var \ElggWidget[] $widgets */
291 
292  foreach ($widgets as $widget) {
293  // change the container and owner
294  $new_widget = clone $widget;
295  $new_widget->container_guid = $entity->guid;
296  $new_widget->owner_guid = $entity->guid;
297 
298  // pull in settings
299  $settings = get_all_private_settings($widget->guid);
300 
301  foreach ($settings as $name => $value) {
302  $new_widget->$name = $value;
303  }
304 
305  $new_widget->save();
306  }
307 
308  elgg_set_ignore_access($old_ia);
310  }
311  }
312  }
313 }
314 
326  if ($type == 'object' && $params['subtype'] == 'widget') {
327  return elgg_in_context('create_default_widgets') ? true : null;
328  }
329 
330  return null;
331 }
332 
333 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
334  $events->registerHandler('init', 'system', '_elgg_widgets_init');
335  $events->registerHandler('ready', 'system', '_elgg_default_widgets_init');
336 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_get_site_entity($site_guid=0)
Get an entity (default is current site)
Definition: sites.php:18
_elgg_create_default_widgets($event, $type, $entity)
Creates default widgets.
Definition: widgets.php:258
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
_elgg_widgets_init()
Function to initialize widgets functionality.
Definition: widgets.php:180
$default_widgets
Definition: add.php:14
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
$value
Definition: longtext.php:26
$widget
Definition: delete.php:9
$subtype
Definition: delete.php:28
$return
Definition: opendd.php:15
elgg_is_widget_type($handler, $context=null,\ElggEntity $container=null)
Has a widget type with the specified handler been registered.
Definition: widgets.php:119
$exact
Definition: add_panel.php:13
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:715
if($screenshots) $info
Definition: details.php:58
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
elgg_register_admin_menu_item($section, $menu_id, $parent_id=null, $priority=100)
Add an admin area section or child section.
Definition: admin.php:140
if($categories) $description
Definition: full.php:166
elgg_get_widgets($owner_guid, $context)
Get widgets for a particular context.
Definition: widgets.php:24
$container
Definition: delete.php:29
static factory(array $options)
Create an WidgetDefinition from an associative array.
elgg_set_ignore_access($ignore=true)
Set if Elgg's access system should be ignored.
Definition: access.php:43
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_can_edit_widget_layout($context, $user_guid=0)
Can the user edit the widget layout.
Definition: widgets.php:54
global $CONFIG
_elgg_default_widgets_permissions_override($hook, $type, $return, $params)
Overrides permissions checks when creating widgets for logged out users.
Definition: widgets.php:325
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: pageowner.php:225
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2001
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:801
_elgg_default_widgets_init()
Gets a list of events to create default widgets for and register menu items for default widgets with ...
Definition: widgets.php:214
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
if(!$owner) if($owner->guid!=$page_owner->guid) $context
Definition: widgets.php:36
_elgg_widgets_set_ajax_title($hook, $type, $results, $params)
Set the widget title on ajax return from save action.
Definition: widgets.php:163
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1310
$widgets
Definition: widgets.php:46
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:538
elgg_push_context($context)
Push a context onto the top of the stack.
Definition: pageowner.php:215
$owner_guid
Definition: widgets.php:17
elgg_get_widget_types($context="", $exact=false)
Get the widget types for a context.
Definition: widgets.php:140
$entity
Definition: delete.php:7
$handler
Definition: add.php:10
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:85
WidgetDefinition.
$user_guid
Avatar remove action.
Definition: remove.php:6
$settings
elgg_register_widget_type($handler, $name=null, $description=null, $context=array('all'), $multiple=false)
Register a widget type.
Definition: widgets.php:74
get_all_private_settings($entity_guid)
Return an array of all private settings.
elgg_unregister_widget_type($handler)
Remove a widget type.
Definition: widgets.php:105
elgg_create_widget($owner_guid, $handler, $context, $access_id=null)
Create a new widget instance.
Definition: widgets.php:39
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27