Elgg  Version 1.9
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  $events[$info['event'] . ',' . $info['entity_type']] = $info;
201  }
202  foreach ($events as $info) {
203  elgg_register_event_handler($info['event'], $info['entity_type'], '_elgg_create_default_widgets');
204  }
205  }
206 }
207 
222  $default_widget_info = elgg_get_config('default_widget_info');
223 
224  if (!$default_widget_info || !$entity) {
225  return;
226  }
227 
228  $type = $entity->getType();
229  $subtype = $entity->getSubtype();
230 
231  // event is already guaranteed by the hook registration.
232  // need to check subtype and type.
233  foreach ($default_widget_info as $info) {
234  if ($info['entity_type'] == $type) {
235  if ($info['entity_subtype'] == ELGG_ENTITIES_ANY_VALUE || $info['entity_subtype'] == $subtype) {
236 
237  // need to be able to access everything
238  $old_ia = elgg_set_ignore_access(true);
239  elgg_push_context('create_default_widgets');
240 
241  // pull in by widget context with widget owners as the site
242  // not using elgg_get_widgets() because it sorts by columns and we don't care right now.
243  $options = array(
244  'type' => 'object',
245  'subtype' => 'widget',
246  'owner_guid' => elgg_get_site_entity()->guid,
247  'private_setting_name' => 'context',
248  'private_setting_value' => $info['widget_context'],
249  'limit' => 0
250  );
251 
253  /* @var ElggWidget[] $widgets */
254 
255  foreach ($widgets as $widget) {
256  // change the container and owner
257  $new_widget = clone $widget;
258  $new_widget->container_guid = $entity->guid;
259  $new_widget->owner_guid = $entity->guid;
260 
261  // pull in settings
262  $settings = get_all_private_settings($widget->guid);
263 
264  foreach ($settings as $name => $value) {
265  $new_widget->$name = $value;
266  }
267 
268  $new_widget->save();
269  }
270 
271  elgg_set_ignore_access($old_ia);
273  }
274  }
275  }
276 }
277 
289  if ($type == 'object' && $params['subtype'] == 'widget') {
290  return elgg_in_context('create_default_widgets') ? true : null;
291  }
292 
293  return null;
294 }
295 
296 elgg_register_event_handler('init', 'system', '_elgg_widgets_init');
297 elgg_register_event_handler('ready', 'system', '_elgg_default_widgets_init');
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_get_site_entity($site_guid=0)
Get an ElggSite 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:221
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:29
if($screenshots) $description
Definition: full.php:173
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
$exact
Definition: add_panel.php:12
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
$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:192
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:288
elgg_pop_context()
Removes and returns the top context string from the stack.
Definition: pageowner.php:255
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:273
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2134
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
_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)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
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)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
elgg_push_context($context)
Push a context onto the top of the stack.
Definition: pageowner.php:243
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:10
$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:604