Elgg  Version 1.11
widgets.php
Go to the documentation of this file.
1 <?php
24  return _elgg_services()->widgets->getWidgets($owner_guid, $context);
25 }
26 
38 function elgg_create_widget($owner_guid, $handler, $context, $access_id = null) {
39  return _elgg_services()->widgets->createWidget($owner_guid, $handler, $context, $access_id);
40 }
41 
54  return _elgg_services()->widgets->canEditLayout($context, $user_guid);
55 }
56 
73 function elgg_register_widget_type($handler, $name, $description, $context = array('all'), $multiple = false) {
74  if (is_string($context)) {
75  elgg_deprecated_notice('context parameters for elgg_register_widget_type() should be passed as an array())', 1.9);
76  $context = explode(",", $context);
77  } elseif (empty($context)) {
78  $context = array('all');
79  }
80 
81  return _elgg_services()->widgets->registerType($handler, $name, $description, $context, $multiple);
82 }
83 
93  return _elgg_services()->widgets->unregisterType($handler);
94 }
95 
105  return _elgg_services()->widgets->validateType($handler);
106 }
107 
119 function elgg_get_widget_types($context = "", $exact = false) {
120  return _elgg_services()->widgets->getTypes($context, $exact);
121 }
122 
133 function _elgg_widgets_set_ajax_title($hook, $type, $results, $params) {
134  if ($params['action'] == 'widgets/save') {
135  // @todo Elgg makes ajax so difficult - no other way to add data to output
136  $widget = get_entity(get_input('guid'));
137  if ($widget && $widget->title) {
138  $results['title'] = $widget->title;
139  return $results;
140  }
141  }
142 }
143 
150 function _elgg_widgets_init() {
151  elgg_register_action('widgets/save');
152  elgg_register_action('widgets/add');
153  elgg_register_action('widgets/move');
154  elgg_register_action('widgets/delete');
155  elgg_register_action('widgets/upgrade', '', 'admin');
156 
157  elgg_register_plugin_hook_handler('output', 'ajax', '_elgg_widgets_set_ajax_title');
158 }
159 
186  global $CONFIG;
187  $default_widgets = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, array());
188 
189  $CONFIG->default_widget_info = $default_widgets;
190 
191  if ($default_widgets) {
192  elgg_register_admin_menu_item('configure', 'default_widgets', 'appearance');
193 
194  // override permissions for creating widget on logged out / just created entities
195  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_default_widgets_permissions_override');
196 
197  // only register the callback once per event
198  $events = array();
199  foreach ($default_widgets as $info) {
200  if (!is_array($info)) {
201  continue;
202  }
203  $event = elgg_extract('event', $info);
204  $entity_type = elgg_extract('entity_type', $info);
205  if (!$event || !$entity_type) {
206  continue;
207  }
208  if (!isset($events[$event][$entity_type])) {
209  elgg_register_event_handler($event, $entity_type, '_elgg_create_default_widgets');
210  $events[$event][$entity_type] = true;
211  }
212  }
213  }
214 }
215 
230  $default_widget_info = elgg_get_config('default_widget_info');
231 
232  if (!$default_widget_info || !$entity) {
233  return;
234  }
235 
236  $type = $entity->getType();
237  $subtype = $entity->getSubtype();
238 
239  // event is already guaranteed by the hook registration.
240  // need to check subtype and type.
241  foreach ($default_widget_info as $info) {
242  if ($info['entity_type'] == $type) {
243  if ($info['entity_subtype'] == ELGG_ENTITIES_ANY_VALUE || $info['entity_subtype'] == $subtype) {
244 
245  // need to be able to access everything
246  $old_ia = elgg_set_ignore_access(true);
247  elgg_push_context('create_default_widgets');
248 
249  // pull in by widget context with widget owners as the site
250  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
251  $options = array(
252  'type' => 'object',
253  'subtype' => 'widget',
254  'owner_guid' => elgg_get_site_entity()->guid,
255  'private_setting_name' => 'context',
256  'private_setting_value' => $info['widget_context'],
257  'limit' => 0
258  );
259 
261  /* @var \ElggWidget[] $widgets */
262 
263  foreach ($widgets as $widget) {
264  // change the container and owner
265  $new_widget = clone $widget;
266  $new_widget->container_guid = $entity->guid;
267  $new_widget->owner_guid = $entity->guid;
268 
269  // pull in settings
270  $settings = get_all_private_settings($widget->guid);
271 
272  foreach ($settings as $name => $value) {
273  $new_widget->$name = $value;
274  }
275 
276  $new_widget->save();
277  }
278 
279  elgg_set_ignore_access($old_ia);
281  }
282  }
283  }
284 }
285 
297  if ($type == 'object' && $params['subtype'] == 'widget') {
298  return elgg_in_context('create_default_widgets') ? true : null;
299  }
300 
301  return null;
302 }
303 
304 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
305  $events->registerHandler('init', 'system', '_elgg_widgets_init');
306  $events->registerHandler('ready', 'system', '_elgg_default_widgets_init');
307 };
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_is_widget_type($handler)
Has a widget type with the specified handler been registered.
Definition: widgets.php:104
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
_elgg_create_default_widgets($event, $type, $entity)
Creates default widgets.
Definition: widgets.php:229
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
_elgg_widgets_init()
Function to initialize widgets functionality.
Definition: widgets.php:150
$default_widgets
Definition: add.php:14
elgg_register_widget_type($handler, $name, $description, $context=array('all'), $multiple=false)
Register a widget type.
Definition: widgets.php:73
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
$value
Definition: longtext.php:26
if($screenshots) $description
Definition: full.php:173
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1246
$exact
Definition: add_panel.php:12
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
$params
Definition: login.php:72
$options
Definition: index.php:14
$owner_guid
elgg_register_admin_menu_item($section, $menu_id, $parent_id=null, $priority=100)
Add an admin area section or child section.
Definition: admin.php:142
elgg_get_widgets($owner_guid, $context)
Get widgets for a particular context.
Definition: widgets.php:23
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
elgg_can_edit_widget_layout($context, $user_guid=0)
Can the user edit the widget layout.
Definition: widgets.php:53
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
_elgg_default_widgets_permissions_override($hook, $type, $return, $params)
Overrides permissions checks when creating widgets for logged out users.
Definition: widgets.php:296
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: pageowner.php:234
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:250
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:1967
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:775
_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:185
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
_elgg_widgets_set_ajax_title($hook, $type, $results, $params)
Set the widget title on ajax return from save action.
Definition: widgets.php:133
$widgets
Definition: widgets.php:24
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:519
elgg_push_context($context)
Push a context onto the top of the stack.
Definition: pageowner.php:224
elgg_get_widget_types($context="", $exact=false)
Get the widget types for a context.
Definition: widgets.php:119
$context
Definition: widgets.php:21
$handler
Definition: add.php:10
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:85
$user_guid
Avatar remove action.
Definition: remove.php:6
$entity
Definition: delete.php:10
$subtype
Definition: river.php:12
$settings
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:92
elgg_create_widget($owner_guid, $handler, $context, $access_id=null)
Create a new widget instance.
Definition: widgets.php:38
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382