Update encode/decode attribute value hook in turba
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 04:00:50 +0000 (22:00 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 04:00:50 +0000 (22:00 -0600)
turba/config/hooks.php.dist
turba/docs/UPGRADING
turba/lib/Object.php

index 7cb0cf6..4902e21 100644 (file)
@@ -28,13 +28,13 @@ class Turba_Hooks
 //         if (!$username || empty($_SESSION['turba']['has_share'])) {
 //             return;
 //         }
-// 
+//
 //         require TURBA_BASE . '/config/sources.php';
 //         $shares = Turba::listShares(true);
 //         if (is_a($shares, 'PEAR_Error')) {
 //             return;
 //         }
-// 
+//
 //         foreach ($shares as $uid => $share) {
 //             $params = @unserialize($share->get('params'));
 //             if (empty($params['source'])) {
@@ -54,33 +54,48 @@ class Turba_Hooks
     /**
      * Called when we store a value.
      *
-     * Passwords should be MD5 encoded, but not displayed.
+     * @param string $attribute      Attribute name.
+     * @param string $new            New value.
+     * @param string $old            Old value.
+     * @param Turba_Object $contact  Contact object.
      *
-     * @todo Make this a generic encode() method.
+     * @return string  The encoded value.
      */
-//     public function encode_password($new_password, $old_password, &$contact)
+//     public function encode_attribute($attribute, $new, $old, $contact)
 //     {
-//         if (is_null($new_password) || $new_password == '' ||
-//             $new_password == '[Not Displayed]') {
-//             return $old_password;
-//         } else {
-//             return md5($new_password);
+//         switch ($attribute) {
+//         case 'password':
+//             /* Passwords should be MD5 encoded, but not displayed. */
+//             return (is_null($new) || ($new == '') || ($new == '[Not Displayed']))
+//                 ? $old
+//                 : hash('md5', $new);
 //         }
+//
+//         return $new;
 //     }
 
     /**
      * Called when we display a value.
      *
-     * Passwords should be MD5 encoded, but not displayed.
+     * @param string $attribute      Attribute name.
+     * @param string $new            New value.
+     * @param string $old            Old value.
+     * @param Turba_Object $contact  Contact object.
      *
-     * @todo Make this a generic decode() method.
+     * @return string  The decoded value.
+     * @throws Turba_Exception  Thrown if there is nothing to decode.
      */
-//     public function decode_password($password, &$contact)
+//     public function decode_attribute($attribute, $value, $contact)
 //     {
-//         if (strstr($_SERVER['PHP_SELF'], 'editobject')) {
-//             return null;
-//         } else {
-//             return '[Not Displayed]';
+//         switch ($attribute) {
+//         case 'password':
+//             /* Passwords should be MD5 encoded, but not displayed. */
+//             return (strstr($_SERVER['PHP_SELF'], 'editobject'))
+//                 ? null
+//                 : '[Not Displayed]';
 //         }
+//
+//         throw new Turba_Exception('No decode handler.');
 //     }
+
 }
index 6d35689..4cb2a75 100644 (file)
@@ -10,6 +10,16 @@ your existing data before running any of the steps described below. You can't
 use the updated data with your old Turba version anymore.
 
 
+Upgrading Turba from 2.x to 3.x
+===============================
+
+The ``_turba_hook_encode_{attribute}`` hook has been moved to the
+'encode_attribute' hook.
+
+The ``_turba_hook_decode_{attribute}`` hook has been moved to the
+'decode_attribute' hook.
+
+
 Upgrading Turba from 2.3.x to 2.3.3
 ===================================
 
index 68c3dc4..c54373b 100644 (file)
@@ -90,27 +90,11 @@ class Turba_Object {
      */
     function getValue($attribute)
     {
-        /* Cache hooks to avoid multiple file_exists() calls. */
-        static $hooks;
-        if (!isset($hooks)) {
-            $hooks = array();
+        if (isset($this->attributes[$attribute])) {
             try {
-                Horde::loadConfiguration('hooks.php', null, 'turba');
-            } catch (Horde_Exception $e) {}
-        }
-        if (!isset($hooks[$attribute])) {
-            $function = '_turba_hook_decode_' . $attribute;
-            if (function_exists($function)) {
-                $hooks[$attribute] = $function;
-            } else {
-                $hooks[$attribute] = false;
-            }
-        }
-
-        if (isset($this->attributes[$attribute]) &&
-            !empty($hooks[$attribute])) {
-            return call_user_func_array($hooks[$attribute],
-                                        array($this->attributes[$attribute], &$this));
+                return Horde::callHook('decode_attribute', array($attribute, $this->attributes[$attribute], $this), 'turba');
+            } catch (Horde_Exception_HookNotSet $e) {}
+            } catch (Turba_Exception $e) {}
         }
 
         if (isset($this->driver->map[$attribute]) &&
@@ -141,25 +125,10 @@ class Turba_Object {
      */
     function setValue($attribute, $value)
     {
-        /* Cache hooks to avoid multiple file_exists() calls. */
-        static $hooks;
-        if (!isset($hooks)) {
-            $hooks = array();
-            try {
-                Horde::loadConfiguration('hooks.php', null, 'turba');
-            } catch (Horde_Exception $e) {}
-        }
-        if (!isset($hooks[$attribute])) {
-            $function = '_turba_hook_encode_' . $attribute;
-            if (function_exists($function)) {
-                $hooks[$attribute] = $function;
-            } else {
-                $hooks[$attribute] = false;
-            }
-        }
-        if ($hooks[$attribute]) {
-            $value = call_user_func_array($hooks[$attribute], array($value, $this->attributes[$attribute], &$this));
-        }
+        try {
+            $value = Horde::callHook('encode_attribute', array($attribute, $value, $this->attributes[$attribute], $this), 'turba');
+        } catch (Horde_Exception_HookNotSet $e) {}
+        } catch (Turba_Exception $e) {}
 
         if (isset($this->driver->map[$attribute]) &&
             is_array($this->driver->map[$attribute]) &&