Improvements to addressbook pref handling
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 13:04:06 +0000 (07:04 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 13:04:06 +0000 (07:04 -0600)
25 files changed:
framework/Core/lib/Horde/Core/Prefs/Ui/Widgets.php
framework/Core/lib/Horde/Core/Prefs/Utils.php [deleted file]
framework/Core/package.xml
horde/js/addressbooksprefs.js
horde/js/sourceselect.js
horde/templates/prefs/addressbooks.html
imp/config/prefs.php.dist
imp/contacts.php
imp/lib/Ajax/Imple/ContactAutoCompleter.php
imp/lib/Compose.php
imp/lib/Crypt/Pgp.php
imp/lib/Crypt/Smime.php
imp/lib/IMP.php
imp/lib/LoginTasks/SystemTask/UpgradeFromImp4.php
imp/lib/Mime/Viewer/Html.php
imp/lib/Prefs/Ui.php
kronolith/config/prefs.php.dist
kronolith/contacts.php
kronolith/lib/Ajax/Imple/ContactAutoCompleter.php
kronolith/lib/Application.php
kronolith/lib/LoginTasks/SystemTask/UpgradeFromKronolith2.php [new file with mode: 0644]
whups/config/prefs.php.dist
whups/lib/Ajax/Imple/ContactAutoCompleter.php
whups/lib/Application.php
whups/lib/LoginTasks/SystemTask/UpgradeFromWhups1 [new file with mode: 0644]

index 99f1325..6b1800b 100644 (file)
@@ -29,6 +29,12 @@ class Horde_Core_Prefs_Ui_Widgets
     /**
      * Create code needed for source selection.
      *
+     * Returns data in a form variable named 'sources'. If only one source
+     * was originally given, this variable will contain the list of selected
+     * values (JSON encoded). If multiple sources were given, this variable
+     * will contain a list of arrays; each subarray contains the source name
+     * and the list of selected values (JSON encoded).
+     *
      * @param Horde_Core_Prefs_Ui $ui  The UI object.
      * @param array $data              Data items:
      * <pre>
@@ -55,14 +61,14 @@ class Horde_Core_Prefs_Ui_Widgets
         foreach ($data['sources'] as $key => $val) {
             $selected = $unselected = array();
 
-            foreach ($data['selected'] as $key2 => $val2) {
+            foreach ($val['selected'] as $key2 => $val2) {
                 $selected[] = array(
                     'l' => $val2,
                     'v' => $key2
                 );
             }
 
-            foreach ($data['unselected'] as $key2 => $val2) {
+            foreach ($val['unselected'] as $key2 => $val2) {
                 $unselected[] = array(
                     'l' => $val2,
                     'v' => $key2
@@ -116,6 +122,11 @@ class Horde_Core_Prefs_Ui_Widgets
     /**
      * Create code needed for addressbook selection.
      *
+     * Returns data in form variables named sources and search_fields.
+     * Sources contains the list of selected addressbooks (JSON encoded).
+     * search_fields contains a hash containing sources as keys and an array
+     * of search fields as the value.
+     *
      * @param Horde_Core_Prefs_Ui $ui  The UI object.
      *
      * @return string  HTML UI code.
@@ -179,49 +190,36 @@ class Horde_Core_Prefs_Ui_Widgets
 
             $js = array();
             foreach (array_keys($readable) as $source) {
-                $tmp = array($source);
+                $tmp = $tmpsel = array();
 
                 try {
                     foreach ($registry->call('contacts/fields', array($source)) as $field) {
                         if ($field['search']) {
-                            $tmp[] = array($field['name'], $field['label'], isset($search['fields'][$source]) && in_array($field['name'], $search['fields'][$source]));
+                            $tmp[] = array(
+                                'name' => $field['name'],
+                                'label' => $field['label']
+                            );
+                            if (isset($search['fields'][$source]) &&
+                                in_array($field['name'], $search['fields'][$source])) {
+                                $tmpsel[] = $field['name'];
+                            }
                         }
                     }
                 } catch (Horde_Exception $e) {}
 
-                $js[] = $tmp;
+                $js[$source] = array(
+                    'entries' => $tmp,
+                    'selected' => $tmpsel
+                );
             }
 
             Horde::addInlineScript(array(
-                'HordeAddressbooksPrefs.fields = ' . Horde_Serialize::serialize($js, Horde_Serialize::JSON, Horde_Nls::getCharset())
+                'HordeAddressbooksPrefs.fields = ' . Horde_Serialize::serialize($js, Horde_Serialize::JSON, Horde_Nls::getCharset()),
+                'HordeAddressbooksPrefs.nonetext = ' . Horde_Serialize::serialize(_("No address book selected."), Horde_Serialize::JSON, Horde_Nls::getCharset())
             ));
         }
 
         return $out . $t->fetch(HORDE_TEMPLATES . '/prefs/addressbooks.html');
     }
 
-    /**
-     * Update prefs for addressbook selection.
-     *
-     * @param Horde_Core_Prefs_Ui $ui  The UI object.
-     *
-     * @return boolean  True if preferences were updated.
-     */
-    static public function addressbooksUpdate($ui)
-    {
-        $updated = false;
-
-        if (isset($ui->vars->sources)) {
-            $GLOBALS['prefs']->setValue('search_sources', implode("\t", Horde_Serialize::unserialize($ui->vars->sources, Horde_Serialize::JSON)));
-            $updated = true;
-        }
-
-        if (isset($ui->vars->search_fields_string)) {
-            $GLOBALS['prefs']->setValue('search_fields', $ui->vars->search_fields_string);
-            $updated = true;
-        }
-
-        return $updated;
-    }
-
 }
diff --git a/framework/Core/lib/Horde/Core/Prefs/Utils.php b/framework/Core/lib/Horde/Core/Prefs/Utils.php
deleted file mode 100644 (file)
index cd26036..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-/**
- * Collection of prefs UI utilities for use with common preferences.
- *
- * Copyright 2010 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
- *
- * @author   Michael Slusarz <slusarz@horde.org>
- * @category Horde
- * @package  Core
- */
-class Horde_Core_Prefs_Utils
-{
-    /**
-     * Determines parameters needed to do an address search
-     *
-     * @return array  An array with two keys: 'sources' and 'fields'.
-     */
-    static public function getAddressbookSearchParams()
-    {
-        $src = explode("\t", $GLOBALS['prefs']->getValue('search_sources'));
-        if ((count($src) == 1) && empty($src[0])) {
-            $src = array();
-        }
-
-        $fields = array();
-        if (($val = $GLOBALS['prefs']->getValue('search_fields'))) {
-            $field_arr = explode("\n", $val);
-            foreach ($field_arr as $field) {
-                $field = trim($field);
-                if (!empty($field)) {
-                    $tmp = explode("\t", $field);
-                    if (count($tmp) > 1) {
-                        $source = array_splice($tmp, 0, 1);
-                        $fields[$source[0]] = $tmp;
-                    }
-                }
-            }
-        }
-
-        return array(
-            'fields' => $fields,
-            'sources' => $src
-        );
-    }
-
-}
index 2b5aa1c..38d8e4f 100644 (file)
@@ -99,7 +99,6 @@ Application Framework.
         <file name="Widgets.php" role="php" />
        </dir> <!-- /lib/Horde/Core/Prefs/Ui -->
        <file name="Ui.php" role="php" />
-       <file name="Utils.php" role="php" />
       </dir> <!-- /lib/Horde/Core/Prefs -->
      </dir> <!-- /lib/Horde/Core -->
      <file name="ErrorHandler.php" role="php" />
@@ -236,7 +235,6 @@ Application Framework.
    <install name="lib/Horde/Core/Notification/Status.php" as="Horde/Core/Notification/Status.php" />
    <install name="lib/Horde/Core/Prefs/Ui/Widgets.php" as="Horde/Core/Prefs/Ui/Widgets.php" />
    <install name="lib/Horde/Core/Prefs/Ui.php" as="Horde/Core/Prefs/Ui.php" />
-   <install name="lib/Horde/Core/Prefs/Utils.php" as="Horde/Core/Prefs/Utils.php" />
    <install name="lib/Horde/ErrorHandler.php" as="Horde/ErrorHandler.php" />
    <install name="lib/Horde/Exception/HookNotSet.php" as="Horde/Exception/HookNotSet.php" />
    <install name="lib/Horde/Help.php" as="Horde/Help.php" />
index 675032a..4daf8c8 100644 (file)
@@ -7,80 +7,59 @@
 
 var HordeAddressbooksPrefs = {
 
-    // Variables set by other code: fields
+    // Variables set by other code: fields, nonetext
 
     updateSearchFields: function()
     {
-        var sv = this._getSelectedValue(false),
-            sf = $('search_fields');
+        var tmp,
+            sv = $F('selected_sources'),
+            sf = $('search_fields_select');
 
-        sf.update('');
-        this.fields.each(function(f) {
-            if (f[0] == sv) {
-                f.slice(1).each(function(o) {
-                    var tmp = new Option(o[1], o[0]);
-                    if (o[2]) {
-                        tmp.selected = true;
-                    }
-                    sf.insert(tmp);
-                });
-            }
-        });
+        sf.childElements().invoke('remove');
 
-        this.changeSearchFields();
-    },
-
-    _getSelectedValue: function(index)
-    {
-        var ss = $('selected_sources');
-        if (ss) {
-            if (index) {
-                return ss.selectedIndex;
-            }
-            if (ss.selectedIndex >= 0) {
-                return ss.options[ss.selectedIndex].value;
-            }
-            return '';
+        if (sv.size() == 1) {
+            tmp = this.fields.get(sv.first());
+            tmp.entries.each(function(o) {
+                var opt = new Option(o.label, o.name);
+                if (tmp.selected.include(o.name)) {
+                    opt.selected = true;
+                }
+                sf.insert(opt);
+            });
         } else {
-            return index ? 0 : this.fields[0][0];
+            tmp = new Option(this.nonetext, '');
+            tmp.disabled = true;
+            sf.insert(tmp);
         }
     },
 
     changeSearchFields: function()
     {
-        var data = [],
-            i = 0,
-            sf = $('search_fields'),
-            sv = this._getSelectedValue(true);
+        var tmp,
+            out = $H(),
+            sv = $F('selected_sources');
 
-        $A(sf.options).each(function(o) {
-            this.fields[sv][i][2] = o.selected;
-            ++i;
-        }.bind(this));
+        if (sv.size() == 1) {
+            tmp = this.fields.get(sv.first());
+            tmp.selected = $F('search_fields_select');
+            this.fields.set(sv.first(), tmp);
 
-        this.fields.each(function(f) {
-            var tmp = [ f[0] ];
-            f.slice(1).each(function(o) {
-                if (o[2]) {
-                    tmp.push(o[0]);
-                }
+            this.fields.each(function(f) {
+                out.set(f.key, f.value.selected);
             });
-            data.push(tmp.join("\t"));
-        });
-        $('search_fields_string').setValue(data.join("\n"));
+
+            $('search_fields').setValue(out.toJSON());
+        }
     },
 
     onDomLoad: function()
     {
-        this.updateSearchFields();
+        this.fields = $H(this.fields);
 
-        if ($('search_fields')) {
-            $('search_fields').observe('change', this.changeSearchFields.bind(this));
-        }
+        this.updateSearchFields();
 
-        if ($('selected_sources')) {
-            $('selected_sources').observe('change', this.updateSearchFields.bind(this));
-        }
+        $('search_fields_select').observe('change', this.changeSearchFields.bind(this));
+        $('selected_sources').observe('change', this.updateSearchFields.bind(this));
     }
 
 };
index b34b958..d9897c1 100644 (file)
@@ -34,6 +34,7 @@ var HordeSourceSelectPrefs = {
         $(from).childElements().each(function(c) {
             if (c.selected) {
                 c.remove();
+                c.selected = false;
                 $(to).insert(c);
             }
         });
index 72602a6..6dfa384 100644 (file)
  <gettext>To select multiple fields, hold down the Control (PC) or Command (Mac) while clicking.</gettext>
 </div>
 
-<input type="hidden" name="search_fields_string" id="search_fields_string" />
+<input type="hidden" name="search_fields" id="search_fields" />
 <table>
  <tr>
   <td>
-   <label for="search_fields" class="hidden"><gettext>Fields to search</gettext></label>
-   <select id="search_fields" name="search_fields" multiple="multiple" size="5"></select>
+   <label for="search_fields_select" class="hidden"><gettext>Fields to search</gettext></label>
+   <select id="search_fields_select" name="search_fields_select" multiple="multiple" size="5"></select>
   </td>
  </tr>
 </table>
index 6291b41..8bbadc2 100644 (file)
@@ -1409,23 +1409,28 @@ $_prefs['sourceselect'] = array(
     'type' => 'special'
 );
 
-// address book(s) to use when expanding addresses
-// You can provide default values this way (note the \t and the double quotes):
-// 'value' => "source_one\tsource_two"
-// refer to turba/config/sources.php for possible source values
+// Address book(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source values
+//
+// You can provide default values this way:
+//   'value' => json_encode('source_one', 'source_two')
 $_prefs['search_sources'] = array(
-    'value' => ""
+    'value' => ''
 );
 
-// field(s) to use when expanding addresses
-// This depends on the search_sources preference if you want to provide
-// default values:
-// 'value' => "source_one\tfield_one\tfield_two\nsource_two\tfield_three"
+// Field(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source and field values
+//
+// If you want to provide a default value, this field depends on the
+// search_sources preference. For example:
+//   'value' => json_encode(array(
+//       'source_one' => array('field_one', 'field_two'),
+//       'source_two' => array('field_three')
+//   ))
 // will search the fields 'field_one' and 'field_two' in source_one and
 // 'field_three' in source_two.
-// refer to turba/config/sources.php for possible source and field values
 $_prefs['search_fields'] = array(
-    'value' => ""
+    'value' => ''
 );
 
 // address book to use for adding addresses
index ebeef82..b9c63f1 100644 (file)
@@ -40,7 +40,7 @@ if (!isset($vars->formname)) {
     $vars->formname = 'compose';
 }
 
-$search_params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+$search_params = IMP::getAddressbookSearchParams();
 $apiargs = array(
     'addresses' => array($vars->search),
     'addressbooks' => array($vars->source),
index cad00ec..b43131c 100644 (file)
@@ -40,7 +40,7 @@ class IMP_Ajax_Imple_ContactAutoCompleter extends Horde_Ajax_Imple_AutoCompleter
 
         if ($ac_browser && !isset($_SESSION['imp']['cache']['ac_ajax'])) {
             $success = $use_ajax = true;
-            $sparams = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+            $sparams = IMP::getAddressbookSearchParams();
             foreach ($sparams['fields'] as $val) {
                 array_map('strtolower', $val);
                 sort($val);
index c7851a0..49b4760 100644 (file)
@@ -2838,7 +2838,7 @@ class IMP_Compose
      */
     static public function getAddressList($search = '')
     {
-        $sparams = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $sparams = IMP::getAddressbookSearchParams();
         try {
             $res = $GLOBALS['registry']->call('contacts/search', array($search, $sparams['sources'], $sparams['fields'], false));
         } catch (Horde_Exception $e) {
index 4db65f3..f526660 100644 (file)
@@ -226,7 +226,7 @@ class IMP_Crypt_Pgp extends Horde_Crypt_Pgp
      */
     public function listPublicKeys()
     {
-        $params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $params = IMP::getAddressbookSearchParams();
         if (empty($params['sources'])) {
             return array();
         }
@@ -243,7 +243,7 @@ class IMP_Crypt_Pgp extends Horde_Crypt_Pgp
      */
     public function deletePublicKey($email)
     {
-        $params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $params = IMP::getAddressbookSearchParams();
         return $GLOBALS['registry']->call('contacts/deleteField', array($email, self::PUBKEY_FIELD, $params['sources']));
     }
 
index ae8e79a..76e1445 100644 (file)
@@ -162,7 +162,7 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
         } catch (Horde_Exception_HookNotSet $e) {
         }
 
-        $params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $params = IMP::getAddressbookSearchParams();
 
         try {
             $key = $GLOBALS['registry']->call('contacts/getField', array($address, self::PUBKEY_FIELD, $params['sources'], false, true));
@@ -191,7 +191,7 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
      */
     public function listPublicKeys()
     {
-        $params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $params = IMP::getAddressbookSearchParams();
         if (empty($params['sources'])) {
             return array();
         }
index 8f4aa09..9d73174 100644 (file)
@@ -1137,4 +1137,27 @@ class IMP
         return 'RedBox.overlay = false; RedBox.showHtml(\'' . addcslashes($t_html, "'/") . '\');';
     }
 
+    /**
+     * Determines parameters needed to do an address search
+     *
+     * @return array  An array with two keys: 'fields' and 'sources'.
+     */
+    static public function getAddressbookSearchParams()
+    {
+        $src = json_decode($GLOBALS['prefs']->getValue('search_sources'));
+        if (empty($src)) {
+            $src = array();
+        }
+
+        $fields = json_decode($GLOBALS['prefs']->getValue('search_fields'), true);
+        if (empty($fields)) {
+            $fields = array();
+        }
+
+        return array(
+            'fields' => $fields,
+            'sources' => $src
+        );
+    }
+
 }
index 5d99b1c..80ec99d 100644 (file)
@@ -24,27 +24,59 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
      */
     public function execute()
     {
+        $this->_upgradeAbookPrefs();
         $this->_upgradeForwardPrefs();
         $this->_upgradeSortPrefs();
         $this->_upgradeVirtualFolders();
     }
 
     /**
+     * Upgrade to the new addressbook preferences.
+     */
+    protected function _upgradeAbookPrefs()
+    {
+        global $prefs;
+
+        $src = $prefs->getValue('search_sources');
+        if (!is_array(json_decode($src))) {
+            $prefs->setValue('search_sources', json_encode(explode("\t", $src)));
+        }
+
+        $val = $prefs->getValue('search_fields');
+        if (!is_array(json_decode($val, true))) {
+            $fields = array();
+            foreach (explode("\n", $val) as $field) {
+                $field = trim($field);
+                if (!empty($field)) {
+                    $tmp = explode("\t", $field);
+                    if (count($tmp) > 1) {
+                        $source = array_splice($tmp, 0, 1);
+                        $fields[$source[0]] = $tmp;
+                    }
+                }
+            }
+            $prefs->setValue('search_fields', $fields);
+        }
+    }
+
+    /**
      * Upgrade to the new forward preferences.
      */
     protected function _upgradeForwardPrefs()
     {
-        switch ($GLOBALS['prefs']->getValue('forward_default')) {
+        global $prefs;
+
+        switch ($prefs->getValue('forward_default')) {
         case 'forward_attachments':
-            $GLOBALS['prefs']->setValue('forward_default', 'both');
+            $prefs->setValue('forward_default', 'both');
             break;
 
         case 'forward_all':
-            $GLOBALS['prefs']->setValue('forward_default', 'attach');
+            $prefs->setValue('forward_default', 'attach');
             break;
 
         case 'forward_body':
-            $GLOBALS['prefs']->setValue('forward_default', 'body');
+            $prefs->setValue('forward_default', 'body');
             break;
 
         case 'attach':
@@ -54,8 +86,8 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
             break;
 
         default:
-            $GLOBALS['prefs']->setValue('forward_default', 'attach');
-            $GLOBALS['prefs']->setDefault('forward_default', true);
+            $prefs->setValue('forward_default', 'attach');
+            $prefs->setDefault('forward_default', true);
             break;
         }
     }
@@ -65,8 +97,10 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
      */
     protected function _upgradeSortPrefs()
     {
+        global $prefs;
+
         $update = false;
-        $sortpref = @unserialize($GLOBALS['prefs']->getValue('sortpref'));
+        $sortpref = @unserialize($prefs->getValue('sortpref'));
         foreach ($sortpref as $key => $val) {
             $sb = $this->_newSortbyValue($val['b']);
             if (!is_null($sb)) {
@@ -76,12 +110,12 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
         }
 
         if ($update) {
-            $GLOBALS['prefs']->setValue('sortpref', serialize($sortpref));
+            $prefs->setValue('sortpref', serialize($sortpref));
         }
 
-        $sb = $this->_newSortbyValue($GLOBALS['prefs']->getValue('sortby'));
+        $sb = $this->_newSortbyValue($prefs->getValue('sortby'));
         if (!is_null($sb)) {
-            $GLOBALS['prefs']->setValue('sortby', $sb);
+            $prefs->setValue('sortby', $sb);
         }
     }
 
index a44bcc1..051ec7a 100644 (file)
@@ -310,7 +310,7 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
             return false;
         }
 
-        $params = Horde_Core_Prefs_Utils::getAddressbookSearchParams();
+        $params = IMP::getAddressbookSearchParams();
         $headers = $this->_params['contents']->getHeaderOb();
 
         /* Try to get back a result from the search. */
index 6b43e95..ef67968 100644 (file)
@@ -382,10 +382,7 @@ class IMP_Prefs_Ui
             return $prefs->setValue('nav_audio', $ui->vars->nav_audio);
 
         case 'sourceselect':
-            if (isset($ui->vars->sources)) {
-                unset($_SESSION['imp']['cache']['ac_ajax']);
-            }
-            return Horde_Core_Prefs_Ui_Widgets::addressbooksUpdate($ui);
+            return $this->_updateSource($ui);
 
         case 'spamselect':
             return $this->_updateSpecialFolders('spam_folder', $vars->spam, $vars->spam_new, $ui);
@@ -1321,6 +1318,35 @@ class IMP_Prefs_Ui
         return $t->fetch(IMP_TEMPLATES . '/prefs/sound.html');
     }
 
+    /* Addressbook selection. */
+
+    /**
+     * Update address book related preferences.
+     *
+     * @param Horde_Core_Prefs_Ui $ui  The UI object.
+     *
+     * @return boolean  True if preferences were updated.
+     */
+    protected function _updateSource($ui)
+    {
+        global $prefs;
+
+        $updated = false;
+
+        if (isset($ui->vars->sources)) {
+            $prefs->setValue('search_sources', $ui->vars->sources);
+            unset($_SESSION['imp']['cache']['ac_ajax']);
+            $updated = true;
+        }
+
+        if (isset($ui->vars->search_fields)) {
+            $prefs->setValue('search_fields', $ui->vars->search_fields);
+            $updated = true;
+        }
+
+        return $updated;
+    }
+
     /* Spam selection. */
 
     /**
index 8600474..587e74c 100644 (file)
@@ -314,23 +314,28 @@ $_prefs['display_contact'] = array(
 // address book selection widget
 $_prefs['sourceselect'] = array('type' => 'special');
 
-// address book(s) to use when expanding addresses
-// You can provide default values this way (note the \t and the double quotes):
-// 'value' => "source_one\tsource_two"
-// refer to turba/config/sources.php for possible source values
+// Address book(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source values
+//
+// You can provide default values this way:
+//   'value' => json_encode('source_one', 'source_two')
 $_prefs['search_sources'] = array(
-    'value' => ""
+    'value' => ''
 );
 
-// field(s) to use when expanding addresses
-// This depends on the search_sources preference if you want to provide
-// default values:
-// 'value' => "source_one\tfield_one\tfield_two\nsource_two\tfield_three"
+// Field(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source and field values
+//
+// If you want to provide a default value, this field depends on the
+// search_sources preference. For example:
+//   'value' => json_encode(array(
+//       'source_one' => array('field_one', 'field_two'),
+//       'source_two' => array('field_three')
+//   ))
 // will search the fields 'field_one' and 'field_two' in source_one and
 // 'field_three' in source_two.
-// refer to turba/config/sources.php for possible source and field values
 $_prefs['search_fields'] = array(
-    'value' => ""
+    'value' => ''
 );
 
 $_prefs['fb_url'] = array(
index 2a64ed1..c2e9013 100644 (file)
@@ -33,15 +33,9 @@ $apiargs['addresses'] = array($search);
 $apiargs['addressbooks'] = array($source);
 $apiargs['fields'] = array();
 
-if ($search_fields_pref = $prefs->getValue('search_fields')) {
-    foreach (explode("\n", $search_fields_pref) as $s) {
-        $s = trim($s);
-        $s = explode("\t", $s);
-        if (!empty($s[0]) && ($s[0] == $source)) {
-            $apiargs['fields'][array_shift($s)] = $s;
-            break;
-        }
-    }
+$search_fields_pref = json_decode($prefs->getValue('search_fields'), true);
+if (!empty($search_fields_pref) && isset($search_fields_pref[$source])) {
+    $apiargs['fields'][$source] = $search_fields_pref[$source];
 }
 
 if ($search || $prefs->getValue('display_contact')) {
index bce7459..38666c3 100644 (file)
@@ -75,24 +75,14 @@ class Kronolith_Ajax_Imple_ContactAutoCompleter extends Horde_Ajax_Imple_AutoCom
 
         $search = reset(array_filter(array_map('trim', Horde_Mime_Address::explode($addrString, ',;'))));
 
-        $src = explode("\t", $GLOBALS['prefs']->getValue('search_sources'));
-        if ((count($src) == 1) && empty($src[0])) {
+        $src = json_decode($GLOBALS['prefs']->getValue('search_sources'));
+        if (!is_array($src)) {
             $src = array();
         }
 
-        $fields = array();
-        if (($val = $GLOBALS['prefs']->getValue('search_fields'))) {
-            $field_arr = explode("\n", $val);
-            foreach ($field_arr as $field) {
-                $field = trim($field);
-                if (!empty($field)) {
-                    $tmp = explode("\t", $field);
-                    if (count($tmp) > 1) {
-                        $source = array_splice($tmp, 0, 1);
-                        $fields[$source[0]] = $tmp;
-                    }
-                }
-            }
+        $fields = json_decode($GLOBALS['prefs']->getValue('search_fields'), true);
+        if (!is_array($fields)) {
+            $fields = array();
         }
 
         try {
index b48eb62..f82f4ae 100644 (file)
@@ -227,7 +227,7 @@ class Kronolith_Application extends Horde_Registry_Application
             return $this->_prefsRemoteCalManagement($ui);
 
         case 'sourceselect':
-            return Horde_Core_Prefs_Ui_Widgets::addressbooksUpdate($ui);
+            return $this->_prefsSourceselect($ui);
         }
 
         return false;
@@ -359,6 +359,32 @@ class Kronolith_Application extends Horde_Registry_Application
     }
 
     /**
+     * Update address book related preferences.
+     *
+     * @param Horde_Core_Prefs_Ui $ui  The UI object.
+     *
+     * @return boolean  True if preferences were updated.
+     */
+    protected function _prefsSourceselect($ui)
+    {
+        global $prefs;
+
+        $updated = false;
+
+        if (isset($ui->vars->sources)) {
+            $prefs->setValue('search_sources', $ui->vars->sources);
+            $updated = true;
+        }
+
+        if (isset($ui->vars->search_fields)) {
+            $prefs->setValue('search_fields', $ui->vars->search_fields);
+            $updated = true;
+        }
+
+        return $updated;
+    }
+
+    /**
      * Removes user data.
      *
      * @param string $user  Name of user to remove data for.
diff --git a/kronolith/lib/LoginTasks/SystemTask/UpgradeFromKronolith2.php b/kronolith/lib/LoginTasks/SystemTask/UpgradeFromKronolith2.php
new file mode 100644 (file)
index 0000000..0849769
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Login system task for automated upgrade tasks.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Kronolith
+ */
+class Kronolith_LoginTasks_SystemTask_UpgradeFromKronolith2 extends Horde_LoginTasks_SystemTask
+{
+    /**
+     * The interval at which to run the task.
+     *
+     * @var integer
+     */
+    public $interval = Horde_LoginTasks::ONCE;
+
+    /**
+     * Perform all functions for this task.
+     */
+    public function execute()
+    {
+        $this->_upgradeAbookPrefs();
+    }
+
+    /**
+     * Upgrade to the new addressbook preferences.
+     */
+    protected function _upgradeAbookPrefs()
+    {
+        global $prefs;
+
+        $src = $prefs->getValue('search_sources');
+        if (!is_array(json_decode($src))) {
+            $prefs->setValue('search_sources', json_encode(explode("\t", $src)));
+        }
+
+        $val = $prefs->getValue('search_fields');
+        if (!is_array(json_decode($val, true))) {
+            $fields = array();
+            foreach (explode("\n", $val) as $field) {
+                $field = trim($field);
+                if (!empty($field)) {
+                    $tmp = explode("\t", $field);
+                    if (count($tmp) > 1) {
+                        $source = array_splice($tmp, 0, 1);
+                        $fields[$source[0]] = $tmp;
+                    }
+                }
+            }
+            $prefs->setValue('search_fields', $fields);
+        }
+    }
+
+}
index 32cae07..dcc34a2 100644 (file)
@@ -143,21 +143,27 @@ $_prefs['comment_sort_dir'] = array(
 // address book selection widget
 $_prefs['sourceselect'] = array('type' => 'special');
 
-// address book(s) to use when expanding addresses
-// You can provide default values this way (note the \t and the double quotes):
-// 'value' => "source_one\tsource_two"
-// refer to turba/config/sources.php for possible source values
+// Address book(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source values
+//
+// You can provide default values this way:
+//   'value' => json_encode('source_one', 'source_two')
+
 $_prefs['search_sources'] = array(
-    'value' => ""
+    'value' => ''
 );
 
-// field(s) to use when expanding addresses
-// This depends on the search_sources preference if you want to provide
-// default values:
-// 'value' => "source_one\tfield_one\tfield_two\nsource_two\tfield_three"
+// Field(s) to use when expanding addresses
+// Refer to turba/config/sources.php for possible source and field values
+//
+// If you want to provide a default value, this field depends on the
+// search_sources preference. For example:
+//   'value' => json_encode(array(
+//       'source_one' => array('field_one', 'field_two'),
+//       'source_two' => array('field_three')
+//   ))
 // will search the fields 'field_one' and 'field_two' in source_one and
 // 'field_three' in source_two.
-// refer to turba/config/sources.php for possible source and field values
 $_prefs['search_fields'] = array(
-    'value' => ""
+    'value' => ''
 );
index 5fe3ae2..990e8d2 100644 (file)
@@ -134,27 +134,21 @@ class Whups_Ajax_Imple_ContactAutoCompleter extends Horde_Ajax_Imple_AutoComplet
      */
     static public function getAddressSearchParams()
     {
-        $src = explode("\t", $GLOBALS['prefs']->getValue('search_sources'));
-        if ((count($src) == 1) && empty($src[0])) {
+        $src = json_decode($GLOBALS['prefs']->getValue('search_sources'));
+        if (!is_array($src)) {
             $src = array();
         }
 
-        $fields = array();
-        if (($val = $GLOBALS['prefs']->getValue('search_fields'))) {
-            $field_arr = explode("\n", $val);
-            foreach ($field_arr as $field) {
-                $field = trim($field);
-                if (!empty($field)) {
-                    $tmp = explode("\t", $field);
-                    if (count($tmp) > 1) {
-                        $source = array_splice($tmp, 0, 1);
-                        $fields[$source[0]] = $tmp;
-                    }
-                }
-            }
+
+        $fields = json_decode($GLOBALS['prefs']->getValue('search_fields'), true);
+        if (!is_array($fields)) {
+            $fields = array();
         }
 
-        return array('sources' => $src, 'fields' => $fields);
+        return array(
+            'fields' => $fields,
+            'sources' => $src
+        );
     }
 
 }
index aa18dbb..6e79634 100644 (file)
@@ -172,7 +172,15 @@ class Whups_Application extends Horde_Registry_Application
 
         switch ($item) {
         case 'sourceselect':
-            $updated = Horde_Core_Prefs_Ui_Widgets::addressbooksUpdate($ui);
+            if (isset($ui->vars->sources)) {
+                $prefs->setValue('search_sources', $ui->vars->sources);
+                $updated = true;
+            }
+
+            if (isset($ui->vars->search_fields)) {
+                $prefs->setValue('search_fields', $ui->vars->search_fields);
+                $updated = true;
+            }
             break;
         }
 
diff --git a/whups/lib/LoginTasks/SystemTask/UpgradeFromWhups1 b/whups/lib/LoginTasks/SystemTask/UpgradeFromWhups1
new file mode 100644 (file)
index 0000000..d0601c0
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Login system task for automated upgrade tasks.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (BSD). If you
+ * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Whups
+ */
+class Whups_LoginTasks_SystemTask_UpgradeFromWhups1 extends Horde_LoginTasks_SystemTask
+{
+    /**
+     * The interval at which to run the task.
+     *
+     * @var integer
+     */
+    public $interval = Horde_LoginTasks::ONCE;
+
+    /**
+     * Perform all functions for this task.
+     */
+    public function execute()
+    {
+        $this->_upgradeAbookPrefs();
+    }
+
+    /**
+     * Upgrade to the new addressbook preferences.
+     */
+    protected function _upgradeAbookPrefs()
+    {
+        global $prefs;
+
+        $src = $prefs->getValue('search_sources');
+        if (!is_array(json_decode($src))) {
+            $prefs->setValue('search_sources', json_encode(explode("\t", $src)));
+        }
+
+        $val = $prefs->getValue('search_fields');
+        if (!is_array(json_decode($val, true))) {
+            $fields = array();
+            foreach (explode("\n", $val) as $field) {
+                $field = trim($field);
+                if (!empty($field)) {
+                    $tmp = explode("\t", $field);
+                    if (count($tmp) > 1) {
+                        $source = array_splice($tmp, 0, 1);
+                        $fields[$source[0]] = $tmp;
+                    }
+                }
+            }
+            $prefs->setValue('search_fields', $fields);
+        }
+    }
+
+}