Simplify preference hooks.
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 15 Oct 2010 20:59:39 +0000 (14:59 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 20 Oct 2010 16:07:14 +0000 (10:07 -0600)
Instead of having separate hooks for each preference, define single hook
for each of the preference events (init, change) and pass these hooks
the preference name.

framework/Prefs/lib/Horde/Prefs.php
horde/config/hooks.php.dist
horde/docs/UPGRADING
imp/config/hooks.php.dist
imp/lib/Prefs/Identity.php

index ad8379e..ef450d8 100644 (file)
@@ -280,7 +280,7 @@ class Horde_Prefs implements ArrayAccess
 
             /* If this preference has a change hook, call it now. */
             try {
-                Horde::callHook('prefs_change_hook_' . $pref, array(), $scope);
+                Horde::callHook('prefs_change', array($pref), $scope);
             } catch (Horde_Exception_HookNotSet $e) {}
         }
 
@@ -736,7 +736,7 @@ class Horde_Prefs implements ArrayAccess
                 $this->_scopes[$pref_scope][$name]['m'] & self::PREFS_DEFAULT) {
 
                 try {
-                    $val = Horde::callHook('prefs_hook_' . $name, array($this->getUser()), $scope);
+                    $val = Horde::callHook('prefs_init', array($name, $this->getUser()), $scope);
                 } catch (Horde_Exception_HookNotSet $e) {
                     continue;
                 }
index 44f32de..17f1c67 100644 (file)
  *
  * On change
  * ---------
- * A hook named prefs_change_hook_<prefname> will be called after that
- * preference is altered. It does not receive any parameters and does not
- * expect a return value.
- *
- * See the prefs_change_hook_theme declaration below for an example.
+ * A hook named prefs_change will be called after a preference is altered.
+ * It is passed the preference name that has changed and does not expect a
+ * return value.
  *
  * Setting value
  * -------------
  * If
  *   'hook' => true
- * exists for a preference (config/prefs.php), a hook named
- * prefs_hook_<prefname> will be run on login. It receives the username as
- * the only parameter and the return value from the function will be used as
- * the preference value.
+ * exists for a preference (config/prefs.php), the prefs_init hook will be run
+ * on login to allow alteration of the default value. This hook receives the
+ * preference name and the username as parameters and uses the return value
+ * from the hook as the new preference value.
  *
  * This hook is ONLY executed on login and preferences are cached during a
  * users' session.
@@ -48,8 +46,6 @@
  *    be called. However, if the preference is locked, the result of the hook
  *    will never be saved.
  *
- * See the prefs_hook_theme declaration below for an example.
- *
  * Authentication/Login Hooks
  * ==========================
  * There are three special hooks called during the initial authentication
 
 class Horde_Hooks
 {
-//    // Example theme hook function. This shows how you can access things
-//    // like the currently logged in user, global variables, server config,
-//    // etc. It isn't, however, something you probably want to actually use
-//    // in production, by virtue of demonstrating all those. :)
-//    public function prefs_hook_theme($username = null)
+//    // PREFERENCES INIT: See above for documentation.
+//    public function prefs_init($pref, $username = null)
 //    {
-//        if ($GLOBALS['registry']->getAuth() != 'chuck') {
-//            return 'mozilla';
-//        }
+//        switch ($pref) {
+//        case 'from_addr':
+//            // Example from_addr init. THIS FUNCTION ASSUMES THAT YOU ARE
+//            // USING A LDAP SERVER and that your /etc/ldap.conf is correctly
+//            // set to a valid host.
+//            //
+//            // You are responsible for bringing in to the local scope any
+//            // information you need. You can "global" anything else you
+//            // need.
+//            //
+//            // Returns an address: either just the user@ side or a full
+//            // address.
+//
+//            // Example #1
+//            if (is_null($name)) {
+//                $name = $GLOBALS['registry']->getAuth();
+//            }
 //
-//        switch ($GLOBALS['registry']->getApp()) {
-//        case 'imp':
-//            return 'brown';
+//            if (!empty($name)) {
+//                $base_context = 'o=myorg';
+//                $scope = 'sub';
 //
-//        case 'turba':
-//            return 'orange';
+//                // You will probably need to replace cd= with uid=; this
+//                // syntax is for Netware 5.1 nldap.
+//                $cmd = '/usr/bin/ldapsearch -b ' . $base_context . ' -s ' . $scope . ' cn=' .
+//                       escapeShellCmd($GLOBALS['registry']->getAuth()) .
+//                       ' | /usr/bin/grep mail | /usr/bin/awk \'{print $2}\'';
+//                $mails = `$cmd`;
+//                $mail_array = explode("\n", $mails);
 //
-//        case 'kronolith':
-//            return 'green';
+//                // Send back the first email found, not the whole list.
+//                $mail = $mail_array['0'];
+//
+//                // If no email address is found, then the login name will
+//                // be used.
+//                return (empty($mail) ? '' : $mail);
+//            }
 //
-//        default:
 //            return '';
-//        }
-//    }
-
-
-//    // Example preferences change hook.
-//    public function prefs_change_hook_theme()
-//    {
-//        $GLOBALS['notification']->push('You changed your theme to ' . $GLOBALS['prefs']->getValue('theme') . '.');
-//    }
-
-
-//    // Example from_addr hook function. THIS FUNCTION ASSUMES THAT YOU ARE
-//    // USING A LDAP SERVER and that your /etc/ldap.conf is correctly set to
-//    // a valid host.
-//    //
-//    // You get passed NOTHING; you are responsible for bringing in to scope
-//    // any information you need. You can "global" anything else you need.
-//    // Return an address - either just the user@ side or a full address -
-//    // and it will be used.
-//    public function prefs_hook_from_addr($name = null)
-//    {
-//        // Example #1
-//        if (is_null($name)) {
-//            $name = $GLOBALS['registry']->getAuth();
-//        }
 //
-//        if (!empty($name)) {
-//            $base_context = 'o=myorg';
-//            $scope = 'sub';
 //
-//            // You will probably need to replace cd= with uid=; this syntax
+//            // Example #2
+//            $ldapServer = '172.31.0.236';
+//            $ldapPort = '389';
+//            $searchBase = 'o=myorg';
+//
+//            $ds = @ldap_connect($ldapServer, $ldapPort);
+//
+//            if (is_null($name)) {
+//                $name = $GLOBALS['registry']->getAuth();
+//                if ($name === false) {
+//                    return '';
+//                }
+//            }
+//
+//            // You will probably need to replace cn= with uid=; this syntax
 //            // is for Netware 5.1 nldap.
-//            $cmd  = '/usr/bin/ldapsearch -b ' . $base_context . ' -s ' . $scope . ' cn=' .
-//                escapeShellCmd($GLOBALS['registry']->getAuth()) .
-//                ' | /usr/bin/grep mail | /usr/bin/awk \'{print $2}\'';
-//            $mails = `$cmd`;
-//            $mail_array = explode("\n", $mails);
-//
-//            // Send back the first email found, not the whole list.
-//            $mail = $mail_array['0'];
-//
-//            // If no email address is found, then the login name will
-//            // be used.
-//            return (empty($mail) ? '' : $mail);
-//        }
+//            $searchResult = @ldap_search($ds, $searchBase, 'cn=' . $name);
+//            $information = @ldap_get_entries($ds, $searchResult);
+//            if (($information === false) || ($information['count'] == 0)) {
+//               $user = '';
+//            } else {
+//                $user = ($information[0]['mail'][0] != '')
+//                    ? $information[0]['mail'][0]
+//                    : $information[0]['cn'][0];
+//            }
 //
-//        return '';
+//            ldap_close($ds);
 //
+//            return empty($user) ? $name : $user;
 //
-//        // Example #2
-//        $ldapServer = '172.31.0.236';
-//        $ldapPort = '389';
-//        $searchBase = 'o=myorg';
 //
-//        $ds = @ldap_connect($ldapServer, $ldapPort);
+//        case 'fullname':
+//            // Examples on how to set the fullname.
 //
-//        if (is_null($name)) {
-//            $name = $GLOBALS['registry']->getAuth();
-//            if ($name === false) {
-//                return '';
+//            // Example #1: Set the fullname from the GECOS information in
+//            // the passwd file.
+//            if (is_null($user)) {
+//                $user = $GLOBALS['registry']->getAuth('bare');
+//                if ($user === false) {
+//                    return '';
+//                }
 //            }
-//        }
 //
-//        // You will probably need to replace cn= with uid=; this syntax
-//        // is for Netware 5.1 nldap.
-//        $searchResult = @ldap_search($ds, $searchBase, 'cn=' . $name);
-//        $information = @ldap_get_entries($ds, $searchResult);
-//        if (($information === false) || ($information['count'] == 0)) {
-//             $user = '';
-//        } else {
-//            $user = ($information[0]['mail'][0] != '')
-//                ? $information[0]['mail'][0]
-//                : $information[0]['cn'][0];
-//        }
+//            $array = posix_getpwnam($user);
+//            $gecos_array = explode(',', $array['gecos']);
+//            return empty($gecos_array) ? $user : $gecos_array[0];
 //
-//        ldap_close($ds);
 //
-//        return empty($user) ? $name : $user;
-//     }
-
-
-//    // Examples on how to set the fullname.
-//    public function prefs_hook_fullname($user = null)
-//    {
-//        // Example #1: Set the fullname from the GECOS information in the
-//        // passwd file.
-//        if (is_null($user)) {
-//            $user = $GLOBALS['registry']->getAuth('bare');
-//            if ($user === false) {
-//                return '';
+//            // Example #2: Set the fullname from LDAP information. In this
+//            // example we look if a Spanish name exists and return this or
+//            // the standard 'cn' entry if not.
+//            $ldapServer = 'ldap.example.com';
+//            $ldapPort = '389';
+//            $searchBase = 'ou=people,o=example.com';
+//
+//            $ds = @ldap_connect($ldapServer, $ldapPort);
+//
+//            if (is_null($user)) {
+//                $user = $GLOBALS['registry']->getAuth();
+//                if ($user === false) {
+//                    return '';
+//                }
 //            }
-//        }
 //
-//        $array = posix_getpwnam($user);
-//        $gecos_array = explode(',', $array['gecos']);
-//        return empty($gecos_array) ? $user : $gecos_array[0];
+//            $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $user);
+//            $information = @ldap_get_entries($ds, $searchResult);
+//            if (($information === false) || ($information['count'] == 0)) {
+//                $name = '';
+//            } else {
+//                $name = ($information[0]['cn;lang-es'][0] != '')
+//                    ? $information[0]['cn;lang-es'][0]
+//                    : $information[0]['cn'][0];
+//            }
 //
+//            ldap_close($ds);
 //
-//        // Example #2: Set the fullname from LDAP information. In this
-//        // example we look if a Spanish name exists and return this or the
-//        // standard 'cn' entry if not.
-//        $$dapServer = 'ldap.example.com';
-//        $ldapPort = '389';
-//        $searchBase = 'ou=people,o=example.com';
+//            return empty($name) ? $user : $name;
 //
-//        $ds = @ldap_connect($ldapServer, $ldapPort);
 //
-//        if (is_null($user)) {
-//            $user = $GLOBALS['registry']->getAuth();
-//            if ($user === false) {
-//                return '';
+//        case 'theme':
+//            // Example theme init. This shows how you can access things
+//            // like the currently logged in user, global variables, server
+//            // config, etc. It isn't, however, something you probably want
+//            // to actually use in production, by virtue of demonstrating all
+//            // those. :)
+//            if ($GLOBALS['registry']->getAuth() != 'foo') {
+//                return 'mozilla';
 //            }
-//        }
 //
-//        $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $user);
-//        $information = @ldap_get_entries($ds, $searchResult);
-//        if (($information === false) || ($information['count'] == 0)) {
-//            $name = '';
-//        } else {
-//            $name = ($information[0]['cn;lang-es'][0] != '')
-//                ? $information[0]['cn;lang-es'][0]
-//                : $information[0]['cn'][0];
-//        }
+//            switch ($GLOBALS['registry']->getApp()) {
+//            case 'imp':
+//                return 'brown';
 //
-//        ldap_close($ds);
+//            case 'kronolith':
+//                return 'green';
 //
-//        return empty($name) ? $user : $name;
+//            case 'turba':
+//                return 'orange';
+//
+//            default:
+//                return '';
+//            }
+//            break;
+//        }
+//    }
+
+
+//    // PREFERENCES CHANGE: See above for documentation.
+//    public function prefs_change($pref)
+//    {
+//        switch ($pref) {
+//        case 'theme':
+//            $GLOBALS['notification']->push('You changed your theme to ' . $GLOBALS['prefs']->getValue('theme') . '.');
+//            break;
+//        }
 //    }
 
 
index 5329ded..508e2cd 100644 (file)
@@ -38,6 +38,7 @@ The following hooks have changed:
     * postauthenticate (parameters; return value)
     * username_tobackend, username_frombackend (combined and renamed to
       authusername).
+    * prefs init/change
 
 The format for the config/mime_drivers.php configuration files has changed.
 
index 9cd91a0..f32d855 100644 (file)
@@ -46,68 +46,41 @@ class IMP_Hooks
 
 
     /**
-     * PREFERENCE HOOK: Dynamically set the signature preference.
+     * PREFERENCE INIT: Set preference values on login.
      *
      * See horde/config/hooks.php.dist for more information.
      *
      * @param string $username  The username.
      *
-     * @return string  The signature text to use.
+     * @return string  The prefrence value.
      */
-//    public function prefs_hook_signature($username = null)
-//    {
-//        // Example #1: Set the signature from the system taglines file; the
-//        // string "%TAG%" (if present in a user's signature) will be replaced
-//        // by the content of the file "/usr/share/tagline" (generated by the
-//        // "TaRT" utility).
-//        $sig = $GLOBALS['prefs']->getValue('signature');
-//        if (preg_match('/%TAG%/', $sig)) {
-//            $tag = `cat /usr/share/tagline`;
-//            $sig = preg_replace('|%TAG%|', $tag, $sig);
-//        }
-//        return $sig;
-//    }
-
-
-    /**
-     * PREFERENCE HOOK: Dynamically set the add_source preference.
-     *
-     * See horde/config/hooks.php.dist for more information.
-     *
-     * @param string $username  The username.
-     *
-     * @return string  The add_source value.
-     */
-//    public function prefs_hook_add_source($username = null)
+//    public function prefs_init($pref, $username = null)
 //    {
 //        if (!$username) {
 //            return;
 //        }
 //
-//        // Example #1: Useful hook when using a Turba source with shares
-//        // enabled (i.e. the example localsql configuration).
-//        return $GLOBALS['registry']->call('contacts/getDefaultShare');
-//    }
-
-
-    /**
-     * PREFERENCE HOOK: Dynamically set the search_sources preference.
-     *
-     * See horde/config/hooks.php.dist for more information.
-     *
-     * @param string $username  The username.
-     *
-     * @return string  The search_sources value.
-     */
-//    public function prefs_hook_search_sources($username = null)
-//    {
-//        if (!$username) {
-//            return;
-//        }
+//        switch ($pref) {
+//        case 'add_source':
+//            // Dynamically set the add_source preference.
+//
+//            // Example #1: Useful hook when using a Turba source with shares
+//            // enabled (i.e. the example localsql configuration).
+//            return $GLOBALS['registry']->call('contacts/getDefaultShare');
+//
 //
-//        if ($GLOBALS['registry']->hasMethod('contacts/sources')) {
-//            $sources = $GLOBALS['registry']->call('contacts/sources');
-//            return json_encode(array_keys($sources));
+//        case 'search_sources':
+//            // Dynamically set the search_sources preference.
+//
+//            // Example #1: Use the list of sources defined in the contacts
+//            // application (e.g. Turba) to populate the search_sources
+//            // value.
+//            if ($GLOBALS['registry']->hasMethod('contacts/sources')) {
+//                $sources = $GLOBALS['registry']->call('contacts/sources');
+//                return json_encode(array_keys($sources));
+//            }
+//
+//            return $GLOBALS['prefs']->getValue($pref);
 //        }
 //    }
 
@@ -267,6 +240,27 @@ class IMP_Hooks
 
 
     /**
+     * Dynamically set the signature preference.
+     *
+     * @param string $sig  The signature.
+     *
+     * @return string  The signature text to use.
+     */
+//    public function signature($sig)
+//    {
+//        // Example #1: Set the signature from the system taglines file; the
+//        // string "%TAG%" (if present in a user's signature) will be replaced
+//        // by the content of the file "/usr/share/tagline" (generated by the
+//        // "TaRT" utility).
+//        if (preg_match('/%TAG%/', $sig)) {
+//            $tag = `cat /usr/share/tagline`;
+//            $sig = preg_replace('|%TAG%|', $tag, $sig);
+//        }
+//        return $sig;
+//    }
+
+
+    /**
      * Add additional message flags in the message listing screen for a
      * mailbox.
      *
index 1d47281..212bb68 100644 (file)
@@ -485,7 +485,7 @@ class Imp_Prefs_Identity extends Horde_Core_Prefs_Identity
         }
 
         try {
-            $val = Horde::callHook('prefs_hook_signature', array($val), 'imp');
+            $val = Horde::callHook('signature', array($val), 'imp');
         } catch (Horde_Exception_HookNotSet $e) {}
 
         $this->_cached['signatures'][$key] = $val;