From 95c79a67493521d2d9761fde60ccfbf2af48ce87 Mon Sep 17 00:00:00 2001 From: cash Date: Sat, 20 Nov 2010 14:33:17 +0000 Subject: moved save widget settings function to ElggWidget git-svn-id: http://code.elgg.org/elgg/trunk@7383 36083f99-b078-4883-b0ff-0f9b5a30f544 --- actions/widgets/add.php | 5 +++-- actions/widgets/save.php | 11 ++++------- engine/classes/ElggWidget.php | 45 +++++++++++++++++++++++++++++++++++++++++++ engine/lib/widgets.php | 43 ++--------------------------------------- 4 files changed, 54 insertions(+), 50 deletions(-) diff --git a/actions/widgets/add.php b/actions/widgets/add.php index 6c2859c21..cdb26ef07 100644 --- a/actions/widgets/add.php +++ b/actions/widgets/add.php @@ -14,10 +14,11 @@ $column = get_input('column', 1); if (!empty($user_guid)) { $user = get_entity($user_guid); if ($user && $user->canEdit()) { - $guid = elgg_create_widget($user->getGUID(), $handler); + $guid = elgg_create_widget($user->getGUID(), $handler, $context); if ($guid) { $widget = get_entity($guid); - $widget->setContext($context); + + // position the widget $widget->move($column, 0); // send widget html for insertion diff --git a/actions/widgets/save.php b/actions/widgets/save.php index 13ea90e85..8854d7810 100644 --- a/actions/widgets/save.php +++ b/actions/widgets/save.php @@ -9,15 +9,12 @@ $guid = get_input('guid'); $params = get_input('params'); -$result = elgg_save_widget_settings($guid, $params); - -if (!$result) { - register_error(elgg_echo('widgets:save:failure')); -} else { - // send back the widget contents - $widget = get_entity($guid); +$widget = get_entity($guid); +if ($widget && $widget->saveSettings($params)) { $view = "widgets/$widget->handler/view"; echo elgg_view($view, array('entity' => $widget)); +} else { + register_error(elgg_echo('widgets:save:failure')); } forward(REFERER); \ No newline at end of file diff --git a/engine/classes/ElggWidget.php b/engine/classes/ElggWidget.php index e1c975777..1622aa5b3 100644 --- a/engine/classes/ElggWidget.php +++ b/engine/classes/ElggWidget.php @@ -144,4 +144,49 @@ class ElggWidget extends ElggObject { } $this->column = $column; } + + /** + * Saves the widget's settings + * + * Plugins can override the save mechanism using the plugin hook: + * 'widget_settings', . The widget and + * the parameters are passed. The plugin hook handler should return + * true to indicate that it has successfully saved the settings. + * + * @warning The values in the parameter array cannot be arrays + * + * @param array $params An array of name => value parameters + * + * @return bool + * @since 1.8.0 + */ + public function saveSettings($params) { + if (!$this->canEdit()) { + return false; + } + + // plugin hook handlers should return true to indicate the settings have + // been saved so that default code does not run + $hook_params = array( + 'widget' => $this, + 'params' => $params + ); + if (elgg_trigger_plugin_hook('widget_settings', $this->handler, $hook_params, false) == true) { + return true; + } + + if (is_array($params) && count($params) > 0) { + foreach ($params as $name => $value) { + if (is_array($value)) { + // private settings cannot handle arrays + return false; + } else { + $this->$name = $value; + } + } + $this->save(); + } + + return true; + } } \ No newline at end of file diff --git a/engine/lib/widgets.php b/engine/lib/widgets.php index f5b473fd9..4a879e1ee 100644 --- a/engine/lib/widgets.php +++ b/engine/lib/widgets.php @@ -58,7 +58,7 @@ function elgg_get_widgets($user_guid, $context) { * @return int|false Widget GUID or false on failure * @since 1.8 */ -function elgg_create_widget($owner_guid, $handler, $access_id = null) { +function elgg_create_widget($owner_guid, $handler, $context, $access_id = null) { if (empty($owner_guid) || empty($handler) || !elgg_is_widget_type($handler)) { return false; } @@ -83,50 +83,11 @@ function elgg_create_widget($owner_guid, $handler, $access_id = null) { // private settings cannot be set until ElggWidget saved $widget->handler = $handler; + $widget->context = $context; return $widget->getGUID(); } -/** - * Saves a widget's settings - * - * Plugins can override this save function by defining a function of the name - * "elgg_save_{$widget->handler}_widget_settings" that takes the widget object - * and the parameter array as arguments - * - * @param int $guid The GUID of the widget - * @param array $params An array of name => value parameters - * - * @return bool - * @since 1.8.0 - */ -function elgg_save_widget_settings($guid, $params) { - $widget = get_entity($guid); - if (!$widget || !$widget->canEdit()) { - return false; - } - - // check if a plugin is overriding the save function - $function = "elgg_save_{$widget->handler}_widget_settings"; - if (is_callable($function)) { - return $function($widget, $params); - } - - if (is_array($params) && count($params) > 0) { - foreach ($params as $name => $value) { - if (is_array($value)) { - // private settings cannot handle arrays - return false; - } else { - $widget->$name = $value; - } - } - $widget->save(); - } - - return true; -} - /** * Can the user edit the widget layout * -- cgit v1.2.3