diff options
-rw-r--r-- | actions/widgets/add.php | 4 | ||||
-rw-r--r-- | actions/widgets/move.php | 22 | ||||
-rw-r--r-- | engine/lib/widgets.php | 344 | ||||
-rw-r--r-- | js/lib/ui.widgets.js | 15 | ||||
-rw-r--r-- | languages/en.php | 1 | ||||
-rw-r--r-- | views/default/layouts/widgets.php | 2 | ||||
-rw-r--r-- | views/default/widgets/wrapper.php | 4 |
7 files changed, 202 insertions, 190 deletions
diff --git a/actions/widgets/add.php b/actions/widgets/add.php index 78f73e267..33a818e54 100644 --- a/actions/widgets/add.php +++ b/actions/widgets/add.php @@ -15,10 +15,10 @@ $guid = false; if (!empty($user_guid)) { $user = get_entity($user_guid); if ($user && $user->canEdit()) { - $guid = elgg_add_widget($user->getGUID(), $handler); + $guid = elgg_create_widget($user->getGUID(), $handler); if ($guid) { $widget = get_entity($guid); - elgg_prepend_widget($widget, $context, $column); + elgg_move_widget($widget, $context, $column, 0); // send widget html for insertion echo elgg_view_entity($widget); diff --git a/actions/widgets/move.php b/actions/widgets/move.php new file mode 100644 index 000000000..2747c149f --- /dev/null +++ b/actions/widgets/move.php @@ -0,0 +1,22 @@ +<?php +/** + * Elgg widget move action + * + * @package Elgg.Core + * @subpackage Widgets.Management + */ + +$guid = get_input('guid'); +$column = get_input('column', 1); +$position = get_input('position'); + +$user = get_loggedin_user(); + +$widget = get_entity($guid); +if ($widget && $user->canEdit()) { + elgg_move_widget($widget, $widget->context, $column, $position); + forward(REFERER); +} + +register_error(elgg_echo('widgets:move:failure')); +forward(REFERER);
\ No newline at end of file diff --git a/engine/lib/widgets.php b/engine/lib/widgets.php index 724e098a7..f75f57141 100644 --- a/engine/lib/widgets.php +++ b/engine/lib/widgets.php @@ -8,44 +8,146 @@ */ /** - * Register a particular context for use with widgets. + * Get widgets for a particular context * - * @param string $context The context we wish to enable context for + * The widgets are ordered for display and grouped in columns. + * $widgets = elgg_get_widgets(get_loggedin_userid(), 'dashboard'); + * $first_column_widgets = $widgets[1]; * - * @return void + * @param int $user_guid The owner user GUID + * @param string $context The context (profile, dashboard, etc) + * @return array|false An 2D array of ElggWidget objects or false + * @since 1.8.0 */ -function use_widgets($context) { - global $CONFIG; +function elgg_get_widgets($user_guid, $context) { + $options = array( + 'type' => 'object', + 'subtype' => 'widget', + 'owner_guid' => $user_guid, + 'private_setting_name' => 'context', + 'private_setting_value' => $context + ); + $widgets = elgg_get_entities_from_private_settings($options); + if (!$widgets) { + return false; + } - if (!isset($CONFIG->widgets)) { - $CONFIG->widgets = new stdClass; + $sorted_widgets = array(); + foreach ($widgets as $widget) { + if (!isset($sorted_widgets[$widget->column])) { + $sorted_widgets[$widget->column] = array(); + } + $sorted_widgets[$widget->column][$widget->order] = $widget; } - if (!isset($CONFIG->widgets->contexts)) { - $CONFIG->widgets->contexts = array(); + foreach ($sorted_widgets as $col => $widgets) { + ksort($sorted_widgets[$col]); } - if (!empty($context)) { - $CONFIG->widgets->contexts[] = $context; + return $sorted_widgets; +} + +/** + * 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 + * @return int|false Widget GUID or false on failure + * @since 1.8 + */ +function elgg_create_widget($owner_guid, $handler, $access_id = null) { + if (empty($owner_guid) || empty($handler) || !widget_type_exists($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; + + return $widget->getGUID(); } /** - * Determines whether or not the current context is using widgets + * Move a widget to a location in a widget layout * - * @return bool Depending on widget status + * @param ElggWidget $widget The widget to move + * @param int $column The widget column + * @param int $rank Zero-based rank from the top of the column + * @return none + * @since 1.8.0 */ -function using_widgets() { - global $CONFIG; +function elgg_move_widget($widget, $context, $column, $rank) { + $options = array( + 'type' => 'object', + 'subtype' => 'widget', + 'private_setting_name_value_pairs' => array( + array('name' => 'context', 'value' => $context), + array('name' => 'column', 'value' => $column) + ) + ); + $widgets = elgg_get_entities_from_private_settings($options); + if (!$widgets) { + $widget->column = $column; + $widget->order = 0; + return; + } - $context = elgg_get_context(); - if (isset($CONFIG->widgets->contexts) && is_array($CONFIG->widgets->contexts)) { - if (in_array($context, $CONFIG->widgets->contexts)) { - return true; + usort($widgets, create_function('$a,$b','return (int)$a->order > (int)$b->order;')); + + if ($rank == 0) { + // top of the column + $widget->order = $widgets[0]->order - 10; + } elseif ($rank == count($widgets)) { + // bottom of the column + $widget->order = end($widgets)->order + 10; + } else { + // reorder widgets that are below + $widget->order = $widgets[$rank]->order; + for ($index = $rank; $index < count($widgets); $index++) { + if ($widgets[$index]->guid != $widget->guid) { + $widgets[$index]-> order += 10; + } } } + $widget->column = $column; + $widget->context = $context; +} - return false; +/** + * Can the user edit the widgets + * + * @param int $user_guid The GUID of the user or 0 for logged in user + * @return bool + */ +function elgg_can_edit_widgets($user_guid = 0) { + $return = false; + if (isadminloggedin()) { + $return = true; + } + if (elgg_get_page_owner_guid() == get_loggedin_userid()) { + $return = true; + } + + // @todo add plugin hook + return $return; } /** @@ -146,156 +248,6 @@ function get_widgets($user_guid, $context, $column) { } /** - * Get widgets for a particular context in order of display - * - * @param int $user_guid The owner user GUID - * @param string $context The context (profile, dashboard, etc) - * @return array|false An array of widget ElggObjects, or false - * @since 1.8.0 - */ -function elgg_get_widgets($user_guid, $context) { - $options = array( - 'type' => 'object', - 'subtype' => 'widget', - 'owner_guid' => $user_guid, - 'private_setting_name' => 'context', - 'private_setting_value' => $context - ); - $widgets = elgg_get_entities_from_private_settings($options); - if (!$widgets) { - return false; - } - - $sorted_widgets = array(); - foreach ($widgets as $widget) { - if (!isset($sorted_widgets[$widget->column])) { - $sorted_widgets[$widget->column] = array(); - } - $sorted_widgets[$widget->column][$widget->order] = $widget; - } - - foreach ($sorted_widgets as $col => $widgets) { - krsort($sorted_widgets[$col]); - } - - return $sorted_widgets; -} - -/** - * Add 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 - * @return int|false Widget GUID or false on failure - * @since 1.8 - */ -function elgg_add_widget($owner_guid, $handler, $access_id = null) { - if (empty($owner_guid) || empty($handler) || !widget_type_exists($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; - - return $widget->getGUID(); -} - -/** - * Prepend a widget to a column - * - * @param ElggWidget $widget The widget object - * @param string $context The widget context - * @param int $column The column index (default is 1) - * @return none - * @since 1.8.0 - */ -function elgg_prepend_widget($widget, $context, $column = 1) { - $options = array( - 'type' => 'object', - 'subtype' => 'widget', - 'private_setting_name_value_pairs' => array( - array('name' => 'context', 'value' => $context), - array('name' => 'column', 'value' => $column) - ) - ); - $widgets = elgg_get_entities_from_private_settings($options); - $max_order = -10; - foreach ($widgets as $column_widget) { - if ($column_widget->order > $max_order) { - $max_order = $column_widget->order; - } - } - - $widget->order = $max_order + 10; - $widget->context = $context; - $widget->column = $column; -} - -/** - * Move a widget to a new location in a layout - * - * @param ElggWidget $widget The widget to move - * @param int $column The widget column - * @param int $rank Zero-based rank from the bottom of the column - * @return none - * @since 1.8.0 - */ -function elgg_move_widget($widget, $column, $rank) { - $options = array( - 'type' => 'object', - 'subtype' => 'widget', - 'private_setting_name_value_pairs' => array( - array('name' => 'context', 'value' => $widget->context), - array('name' => 'column', 'value' => $column) - ) - ); - $widgets = elgg_get_entities_from_private_settings($options); - if (!$widgets) { - $widget->column = $column; - $widget->order = 0; - return; - } - - usort($widgets, create_function('$a,$b','return $a->order > $b->order;')); - - if ($rank == 0) { - // bottom of the column - $widget->order = $widgets[0]->order - 10; - } elseif ($rank == count($widgets)) { - // top of the column - $widget->order = end($widgets)->order + 10; - } else { - // reorder widgets that are above - $widget->order = $widgets[$rank]->order; - for ($index = $rank; $index < count($widgets); $index++) { - if ($widgets[$index]->guid != $widget->guid) { - $widgets[$index]-> order += 10; - } - } - } - $widget->column = $column; -} - -/** * Add a new widget instance * * @param int $entity_guid GUID of entity that owns this widget @@ -617,22 +569,48 @@ function reorder_widgets_from_panel($panelstring1, $panelstring2, $panelstring3, } /** - * Can the user edit the widgets + * Register a particular context for use with widgets. * - * @param int $user_guid The GUID of the user or 0 for logged in user - * @return bool + * @param string $context The context we wish to enable context for + * + * @return void + * @deprecated 1.8 */ -function elgg_can_edit_widgets($user_guid = 0) { - $return = false; - if (isadminloggedin()) { - $return = true; +function use_widgets($context) { + elgg_deprecated_notice("use_widgets is deprecated", 1.8); + global $CONFIG; + + if (!isset($CONFIG->widgets)) { + $CONFIG->widgets = new stdClass; } - if (elgg_get_page_owner_guid() == get_loggedin_userid()) { - $return = true; + + if (!isset($CONFIG->widgets->contexts)) { + $CONFIG->widgets->contexts = array(); } - // @todo add plugin hook - return $return; + if (!empty($context)) { + $CONFIG->widgets->contexts[] = $context; + } +} + +/** + * Determines whether or not the current context is using widgets + * + * @return bool Depending on widget status + * @deprecated 1.8 + */ +function using_widgets() { + elgg_deprecated_notice("using_widgets is deprecated", 1.8); + global $CONFIG; + + $context = elgg_get_context(); + if (isset($CONFIG->widgets->contexts) && is_array($CONFIG->widgets->contexts)) { + if (in_array($context, $CONFIG->widgets->contexts)) { + return true; + } + } + + return false; } /** @@ -662,6 +640,7 @@ function widgets_init() { register_action('widgets/reorder'); register_action('widgets/save'); register_action('widgets/add'); + register_action('widgets/move'); // Now run this stuff, but only once run_function_once("widget_run_once"); @@ -669,6 +648,3 @@ function widgets_init() { // Register event elgg_register_event_handler('init', 'system', 'widgets_init'); - -// Use widgets on the dashboard -use_widgets('dashboard');
\ No newline at end of file diff --git a/js/lib/ui.widgets.js b/js/lib/ui.widgets.js index 507a1a1a8..32de71871 100644 --- a/js/lib/ui.widgets.js +++ b/js/lib/ui.widgets.js @@ -6,7 +6,18 @@ elgg.ui.widgets.init = function() { connectWith: '.widget_column',
handle: 'div.drag_handle',
forcePlaceholderSize: true,
- placeholder: 'widget_placeholder'
+ placeholder: 'widget_placeholder',
+ stop: function(event, ui) {
+ elgg.action('widgets/move', {
+ data: {
+ // widget_<guid>
+ guid: ui.item.attr('id').substring(7),
+ // widget_col_<column>
+ column: ui.item.parent().attr('id').substring(11),
+ position: ui.item.index()
+ }
+ });
+ }
});
$('#widget_add_button a').bind('click', function(event) {
@@ -33,7 +44,7 @@ elgg.ui.widgets.init = function() { // insert a widget into the layout
elgg.ui.widgets.insert = function(html) {
- $('.widget_col_1').prepend(html);
+ $('#widget_col_1').prepend(html);
}
elgg.ui.widgets.equalHeight = function(selector) {
diff --git a/languages/en.php b/languages/en.php index 682c43333..a7185b0c5 100644 --- a/languages/en.php +++ b/languages/en.php @@ -247,6 +247,7 @@ $english = array( 'widgets:save:failure' => "We could not save your widget. Please try again.", 'widgets:add:success' => "The widget was successfully added.", 'widgets:add:failure' => "We could not add your widget.", + 'widgets:move:failure' => "We could not store the new widget position.", 'widgets:handlernotfound' => 'This widget is either broken or has been disabled by the site administrator.', /** diff --git a/views/default/layouts/widgets.php b/views/default/layouts/widgets.php index 74cb6da8d..055b0453e 100644 --- a/views/default/layouts/widgets.php +++ b/views/default/layouts/widgets.php @@ -34,7 +34,7 @@ $widget_class = "widget_{$num_columns}_columns"; for ($column_index = 1; $column_index <= $num_columns; $column_index++) { $column_widgets = $widgets[$column_index]; - echo "<div class=\"widget_column $widget_class widget_col_$column_index\">"; + echo "<div class=\"widget_column $widget_class\" id=\"widget_col_$column_index\">"; if (is_array($column_widgets) && sizeof($column_widgets) > 0) { foreach ($column_widgets as $widget) { echo elgg_view_entity($widget); diff --git a/views/default/widgets/wrapper.php b/views/default/widgets/wrapper.php index 51f4725b9..fd849bdee 100644 --- a/views/default/widgets/wrapper.php +++ b/views/default/widgets/wrapper.php @@ -8,6 +8,8 @@ $widgettypes = get_widget_types(); +$widget = $vars['entity']; + if ($vars['entity'] instanceof ElggObject && $vars['entity']->getSubtype() == 'widget') { $handler = $vars['entity']->handler; $title = $widgettypes[$vars['entity']->handler]->name; @@ -23,7 +25,7 @@ $display_view = "widgets/$handler/view"; $edit_view = "widgets/$handler/edit"; ?> -<div class="widget draggable"> +<div class="widget draggable" id="widget_<?php echo $widget->guid; ?>"> <div class="widget_title drag_handle"> <h3>Widget Title</h3> </div> |