aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/widgets.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib/widgets.php')
-rw-r--r--engine/lib/widgets.php165
1 files changed, 154 insertions, 11 deletions
diff --git a/engine/lib/widgets.php b/engine/lib/widgets.php
index 8310e83cc..699462a1b 100644
--- a/engine/lib/widgets.php
+++ b/engine/lib/widgets.php
@@ -16,7 +16,7 @@
*
* @param int $user_guid The owner user GUID
* @param string $context The context (profile, dashboard, etc)
- *
+ *
* @return array An 2D array of ElggWidget objects
* @since 1.8.0
*/
@@ -26,7 +26,8 @@ function elgg_get_widgets($user_guid, $context) {
'subtype' => 'widget',
'owner_guid' => $user_guid,
'private_setting_name' => 'context',
- 'private_setting_value' => $context
+ 'private_setting_value' => $context,
+ 'limit' => 0
);
$widgets = elgg_get_entities_from_private_settings($options);
if (!$widgets) {
@@ -51,10 +52,11 @@ function elgg_get_widgets($user_guid, $context) {
/**
* Create a new widget instance
*
- * @param int $entity_guid GUID of entity that owns this widget
- * @param string $handler The handler for this widget
- * @param int $access_id If not specified, it is set to the default access level
- *
+ * @param int $owner_guid GUID of entity that owns this widget
+ * @param string $handler The handler for this widget
+ * @param string $context The context for this widget
+ * @param int $access_id If not specified, it is set to the default access level
+ *
* @return int|false Widget GUID or false on failure
* @since 1.8.0
*/
@@ -134,7 +136,7 @@ function elgg_can_edit_widget_layout($context, $user_guid = 0) {
* widget is allowed (default: 'all')
* @param bool $multiple Whether or not multiple instances of this widget
* are allowed in a single layout (default: false)
- *
+ *
* @return bool
* @since 1.8.0
*/
@@ -168,7 +170,7 @@ function elgg_register_widget_type($handler, $name, $description, $context = "al
* Remove a widget type
*
* @param string $handler The identifier for the widget
- *
+ *
* @return void
* @since 1.8.0
*/
@@ -192,7 +194,7 @@ function elgg_unregister_widget_type($handler) {
* Has a widget type with the specified handler been registered
*
* @param string $handler The widget handler identifying string
- *
+ *
* @return bool Whether or not that widget type exists
* @since 1.8.0
*/
@@ -217,7 +219,7 @@ function elgg_is_widget_type($handler) {
*
* @param string $context The widget context or empty string for current context
* @param bool $exact Only return widgets registered for this context (false)
- *
+ *
* @return array
* @since 1.8.0
*/
@@ -255,6 +257,7 @@ function elgg_get_widget_types($context = "", $exact = false) {
* Regsiter entity of object, widget as ElggWidget objects
*
* @return void
+ * @access private
*/
function elgg_widget_run_once() {
add_subtype("object", "widget", "ElggWidget");
@@ -264,14 +267,154 @@ function elgg_widget_run_once() {
* Function to initialize widgets functionality
*
* @return void
+ * @access private
*/
function elgg_widgets_init() {
elgg_register_action('widgets/save');
elgg_register_action('widgets/add');
elgg_register_action('widgets/move');
elgg_register_action('widgets/delete');
+ elgg_register_action('widgets/upgrade', '', 'admin');
run_function_once("elgg_widget_run_once");
}
-elgg_register_event_handler('init', 'system', 'elgg_widgets_init'); \ No newline at end of file
+/**
+ * Gets a list of events to create default widgets for and
+ * register menu items for default widgets with the admin section.
+ *
+ * A plugin that wants to register a new context for default widgets should
+ * register for the plugin hook 'get_list', 'default_widgets'. The handler
+ * can register the new type of default widgets by adding an associate array to
+ * the return value array like this:
+ * array(
+ * 'name' => elgg_echo('profile'),
+ * 'widget_context' => 'profile',
+ * 'widget_columns' => 3,
+ *
+ * 'event' => 'create',
+ * 'entity_type' => 'user',
+ * 'entity_subtype' => ELGG_ENTITIES_ANY_VALUE,
+ * );
+ *
+ * The first set of keys define information about the new type of default
+ * widgets and the second set determine what event triggers the creation of the
+ * new widgets.
+ *
+ * @return void
+ * @access private
+ */
+function elgg_default_widgets_init() {
+ global $CONFIG;
+ $default_widgets = elgg_trigger_plugin_hook('get_list', 'default_widgets', null, array());
+
+ $CONFIG->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');
+ }
+ }
+}
+
+/**
+ * Creates default widgets
+ *
+ * This plugin hook handler is registered for events based on what kinds of
+ * default widgets have been registered. See elgg_default_widgets_init() for
+ * information on registering new default widget contexts.
+ *
+ * @param string $event The event
+ * @param string $type The type of object
+ * @param ElggEntity $entity The entity being created
+ * @return void
+ * @access private
+ */
+function elgg_create_default_widgets($event, $type, $entity) {
+ $default_widget_info = elgg_get_config('default_widget_info');
+
+ if (!$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 ($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();
+ }
+ }
+ }
+}
+
+/**
+ * Overrides permissions checks when creating widgets for logged out users.
+ *
+ * @param string $hook The permissions hook.
+ * @param string $type The type of entity being created.
+ * @param string $return Value
+ * @param mixed $params Params
+ * @return true|null
+ * @access private
+ */
+function elgg_default_widgets_permissions_override($hook, $type, $return, $params) {
+ if ($type == 'object' && $params['subtype'] == 'widget') {
+ return elgg_in_context('create_default_widgets') ? true : null;
+ }
+
+ return null;
+}
+
+elgg_register_event_handler('init', 'system', 'elgg_widgets_init');
+// register default widget hooks from plugins
+elgg_register_event_handler('ready', 'system', 'elgg_default_widgets_init');