aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-11-18 23:01:39 +0000
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-11-18 23:01:39 +0000
commitebcbf0cb3b4c4053565adc089acfb8bc96605701 (patch)
treed2fcaba04d6da52016dae5389b92f4cc54853790
parenta24e700999b38185bea3e80dc8461aed271406cb (diff)
downloadelgg-ebcbf0cb3b4c4053565adc089acfb8bc96605701.tar.gz
elgg-ebcbf0cb3b4c4053565adc089acfb8bc96605701.tar.bz2
Added functions to support moving and adding widgets
git-svn-id: http://code.elgg.org/elgg/trunk@7340 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--actions/widgets/add.php26
-rw-r--r--engine/lib/widgets.php126
-rw-r--r--js/lib/ui.widgets.js20
3 files changed, 149 insertions, 23 deletions
diff --git a/actions/widgets/add.php b/actions/widgets/add.php
index c57e3596a..78f73e267 100644
--- a/actions/widgets/add.php
+++ b/actions/widgets/add.php
@@ -13,21 +13,21 @@ $column = get_input('column', 1);
$guid = false;
if (!empty($user_guid)) {
- if ($user = get_entity($user_guid)) {
- if ($user->canEdit()) {
- $guid = add_widget($user->getGUID(), $handler, $context, 0, $column);
- }
- }
-}
+ $user = get_entity($user_guid);
+ if ($user && $user->canEdit()) {
+ $guid = elgg_add_widget($user->getGUID(), $handler);
+ if ($guid) {
+ $widget = get_entity($guid);
+ elgg_prepend_widget($widget, $context, $column);
-if ($guid) {
- system_message(elgg_echo('widgets:add:success'));
+ // send widget html for insertion
+ echo elgg_view_entity($widget);
- // send widget html for insertion
- $widget = get_entity($guid);
- echo elgg_view_entity($widget);
-} else {
- register_error(elgg_echo('widgets:add:failure'));
+ system_message(elgg_echo('widgets:add:success'));
+ forward(REFERER);
+ }
+ }
}
+register_error(elgg_echo('widgets:add:failure'));
forward(REFERER); \ No newline at end of file
diff --git a/engine/lib/widgets.php b/engine/lib/widgets.php
index 5d6a7fcc1..724e098a7 100644
--- a/engine/lib/widgets.php
+++ b/engine/lib/widgets.php
@@ -150,8 +150,8 @@ function get_widgets($user_guid, $context, $column) {
*
* @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(
@@ -175,18 +175,124 @@ function elgg_get_widgets($user_guid, $context) {
}
foreach ($sorted_widgets as $col => $widgets) {
- ksort($sorted_widgets[$col]);
+ krsort($sorted_widgets[$col]);
}
return $sorted_widgets;
}
/**
- * @deprecated 1.8
+ * 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 display_widget(ElggObject $widget) {
- elgg_deprecated_notice("display_widget() was been deprecated. Use elgg_view_entity().", 1.8);
- return elgg_view_entity($widget);
+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;
}
/**
@@ -530,6 +636,14 @@ function elgg_can_edit_widgets($user_guid = 0) {
}
/**
+ * @deprecated 1.8
+ */
+function display_widget(ElggObject $widget) {
+ elgg_deprecated_notice("display_widget() was been deprecated. Use elgg_view_entity().", 1.8);
+ return elgg_view_entity($widget);
+}
+
+/**
* Regsiter entity of object, widget as ElggWidget objects
*
* @return void
diff --git a/js/lib/ui.widgets.js b/js/lib/ui.widgets.js
index b1e7b2a99..507a1a1a8 100644
--- a/js/lib/ui.widgets.js
+++ b/js/lib/ui.widgets.js
@@ -1,7 +1,7 @@
elgg.provide('elgg.ui.widgets');
elgg.ui.widgets.init = function() {
- $(".widget_column" ).sortable({
+ $(".widget_column").sortable({
items: 'div.widget',
connectWith: '.widget_column',
handle: 'div.drag_handle',
@@ -27,11 +27,23 @@ elgg.ui.widgets.init = function() {
});
event.preventDefault();
});
+
+ elgg.ui.widgets.equalHeight(".widget_column");
};
// insert a widget into the layout
elgg.ui.widgets.insert = function(html) {
- $('.widget_first_col').prepend(html);
+ $('.widget_col_1').prepend(html);
+}
+
+elgg.ui.widgets.equalHeight = function(selector) {
+ var maxHeight = 0;
+ $(selector).each(function() {
+ if ($(this).height() > maxHeight) {
+ maxHeight = $(this).height();
+ }
+ })
+ $(selector).css('min-height', maxHeight);
}
//List active widgets for each page column
@@ -85,7 +97,7 @@ elgg.ui.widgets.toggleContent = function(e) {
// set cookie for widget panel open-state
thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
- elgg.session.cookie(thisWidgetName, 'expanded', {expires: 365 });
+ elgg.session.cookie(thisWidgetName, 'expanded', {expires: 365});
} else {
targetContent.slideUp(400);
@@ -96,7 +108,7 @@ elgg.ui.widgets.toggleContent = function(e) {
// set cookie for widget panel closed-state
thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
- elgg.session.cookie(thisWidgetName, 'collapsed', { expires: 365 });
+ elgg.session.cookie(thisWidgetName, 'collapsed', {expires: 365});
}
return false;
};