]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
moved save widget settings function to ElggWidget
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 20 Nov 2010 14:33:17 +0000 (14:33 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 20 Nov 2010 14:33:17 +0000 (14:33 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@7383 36083f99-b078-4883-b0ff-0f9b5a30f544

actions/widgets/add.php
actions/widgets/save.php
engine/classes/ElggWidget.php
engine/lib/widgets.php

index 6c2859c219d781438c78e8959e87265004d2c372..cdb26ef072a3708a3f49781d83a19f45480af747 100644 (file)
@@ -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
index 13ea90e85df5e230e7e2330c22d273f2390729c8..8854d781083da791cd37a27ee290d7dd211e0ac7 100644 (file)
@@ -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
index e1c97577735005c19fb4ee8ac840ceefddd7cf16..1622aa5b34b068102383cdc25f3b37594470f693 100644 (file)
@@ -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', <widget handler identifier>. 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
index f5b473fd99720dc8c9ffee4e7efe7fc61e2cc87f..4a879e1ee1ef7b7fa5eb19b89c1649512bd4bb73 100644 (file)
@@ -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
  *