]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
saving widget position after moving
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 19 Nov 2010 03:28:41 +0000 (03:28 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 19 Nov 2010 03:28:41 +0000 (03:28 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@7343 36083f99-b078-4883-b0ff-0f9b5a30f544

actions/widgets/add.php
actions/widgets/move.php [new file with mode: 0644]
engine/lib/widgets.php
js/lib/ui.widgets.js
languages/en.php
views/default/layouts/widgets.php
views/default/widgets/wrapper.php

index 78f73e26768abb6b1a09a05017a3039ae5f6ebc3..33a818e549a91592c5c802337a1c0db5793d6586 100644 (file)
@@ -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 (file)
index 0000000..2747c14
--- /dev/null
@@ -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
index 724e098a7d7b9af0e53f6a6f4d0e403e6a328f11..f75f57141013345d269486096899149cfb0965d9 100644 (file)
  */
 
 /**
- * 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;
 }
 
 /**
@@ -145,156 +247,6 @@ function get_widgets($user_guid, $context, $column) {
        return false;
 }
 
-/**
- * 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
  *
@@ -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
index 507a1a1a86535c5a747b2a4b35e694e1cb036d6e..32de71871053b19310b1f9e74ab24947c6c68c4f 100644 (file)
@@ -6,7 +6,18 @@ elgg.ui.widgets.init = function() {
                connectWith:          '.widget_column',\r
                handle:               'div.drag_handle',\r
                forcePlaceholderSize: true,\r
-               placeholder:          'widget_placeholder'\r
+               placeholder:          'widget_placeholder',\r
+               stop:                 function(event, ui) {\r
+                       elgg.action('widgets/move', {\r
+                               data: {\r
+                                       // widget_<guid>\r
+                                       guid: ui.item.attr('id').substring(7),\r
+                                       // widget_col_<column>\r
+                                       column: ui.item.parent().attr('id').substring(11),\r
+                                       position: ui.item.index()\r
+                               }\r
+                       });\r
+               }\r
        });\r
 \r
        $('#widget_add_button a').bind('click', function(event) {\r
@@ -33,7 +44,7 @@ elgg.ui.widgets.init = function() {
 \r
 // insert a widget into the layout\r
 elgg.ui.widgets.insert = function(html) {\r
-       $('.widget_col_1').prepend(html);\r
+       $('#widget_col_1').prepend(html);\r
 }\r
 \r
 elgg.ui.widgets.equalHeight = function(selector) {\r
index 682c43333ed12b67ecf20a4d908623d81ae676a0..a7185b0c5c2c803464e1834ba686e0de89586265 100644 (file)
@@ -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.',
 
 /**
index 74cb6da8d15cfb8fc1d1ef30895db04dead28658..055b0453eff85013a8e4a828a4bf1e2729dbeea4 100644 (file)
@@ -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);
index 51f4725b9b6bd4b75ae8af689e8c105159d37829..fd849bdeee617341185dd02c08e3a2fddcab5a6a 100644 (file)
@@ -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>