Convert turba addressbooks pref to json format
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 15:17:03 +0000 (09:17 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 16:35:04 +0000 (10:35 -0600)
turba/config/prefs.php.dist
turba/lib/Application.php
turba/lib/Forms/DeleteAddressBook.php
turba/lib/LoginTasks/SystemTask/UpgradeFromTurba2.php [new file with mode: 0644]
turba/lib/LoginTasks/SystemTask/UpgradePrefs.php
turba/lib/Turba.php
turba/lib/tests/BrowsePageTest.php
turba/lib/tests/ViewBrowseTest.php

index 6e01b49..3f736a4 100644 (file)
@@ -40,10 +40,11 @@ $_prefs['addressbookselect'] = array(
 
 // Address books to be displayed in the address book selection widget
 // and in the Browse menu item.  The address book name is stored using
-// the source key from sources.php (e.g. "localsql").  Separate
-// entries with "\n" , e. g. 'value' => "localsql\nlocalldap" (the
-// double quotes are REQUIRED).  If 'value' is empty (''), all address
-// books that the user has permissions to will be listed.
+// the source key from sources.php (e.g. "localsql").
+// You can provide default values this way:
+//   'value' => json_encode('source_one', 'source_two')
+// If 'value' is empty (''), all address books that the user has permissions
+// to will be listed.
 $_prefs['addressbooks'] = array(
     'value' => ''
 );
index 0eee7fc..aba9073 100644 (file)
@@ -324,7 +324,7 @@ class Turba_Application extends Horde_Registry_Application
         case 'addressbookselect':
             $data = Horde_Core_Prefs_Ui_Widgets::sourceUpdate($ui);
             if (isset($data['sources'])) {
-                $ui->setValue('addressbooks', Horde_Serialize::unserialize($data['sources'], Horde_Serialize::JSON));
+                $ui->setValue('addressbooks', $data['sources']);
                 return true;
             }
             break;
index 6683be4..e334ff4 100644 (file)
@@ -73,10 +73,10 @@ class Turba_DeleteAddressBookForm extends Horde_Form {
             unset($_SESSION['turba']['source']);
         }
 
-        $abooks = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
+        $abooks = json_decode($GLOBALS['prefs']->getValue('addressbooks'));
         if (($pos = array_search($this->_addressbook->getName(), $abooks)) !== false) {
             unset($abooks[$pos]);
-            $GLOBALS['prefs']->setValue('addressbooks', implode("\n", $abooks));
+            $GLOBALS['prefs']->setValue('addressbooks', json_encode($abooks));
         }
 
         return true;
diff --git a/turba/lib/LoginTasks/SystemTask/UpgradeFromTurba2.php b/turba/lib/LoginTasks/SystemTask/UpgradeFromTurba2.php
new file mode 100644 (file)
index 0000000..b0ffae6
--- /dev/null
@@ -0,0 +1,48 @@
+<?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 Turba
+ */
+class Turba_LoginTasks_SystemTask_UpgradeFromTurba2 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;
+
+        $abooks = $prefs->getValue('addressbooks');
+        if (!is_array(json_decode($abooks))) {
+            $abooks = @explode("\n", $abooks);
+            if (empty($abooks)) {
+                $abooks = array();
+            }
+
+            return $prefs->setValue('addressbooks', json_encode($abooks));
+        }
+    }
+
+}
index 50190ad..508f883 100644 (file)
@@ -67,14 +67,19 @@ class Turba_LoginTasks_SystemTask_UpgradePrefs extends Horde_LoginTasks_SystemTa
     {
         global $prefs;
 
-        $abooks = explode("\n", $prefs->getValue('addressbooks'));
+        $abooks = $prefs->getValue('addressbooks');
+        if (is_array(json_decode($abooks))) {
+            return;
+        }
+
+        $abooks = explode("\n", $abooks);
         if (is_array($abooks) && !empty($abooks[0])) {
             $new_prefs = array();
             foreach ($abooks as $abook) {
                 $new_prefs[] = $this->_updateShareName($abook);
             }
 
-            return $prefs->setValue('addressbooks', implode("\n", $new_prefs));
+            return $prefs->setValue('addressbooks', json_encode($new_prefs));
         }
 
         return true;
index da991ac..1acabc5 100644 (file)
@@ -112,14 +112,16 @@ class Turba {
      */
     function getAddressBookOrder()
     {
-        $i = 0;
-        $lines = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
-        $temp = $lines;
+        $lines = json_decode($GLOBALS['prefs']->getValue('addressbooks'));
         $addressbooks = array();
-        foreach ($lines as $line) {
-            $line = trim($line);
-            if ($line && isset($GLOBALS['cfgSources'][$line])) {
-                $addressbooks[$line] = $i++;
+
+        if (!empty($lines)) {
+            $i = 0;
+            foreach ($lines as $line) {
+                $line = trim($line);
+                if ($line && isset($GLOBALS['cfgSources'][$line])) {
+                    $addressbooks[$line] = $i++;
+                }
             }
         }
         return $addressbooks;
@@ -132,11 +134,13 @@ class Turba {
      */
     function getDefaultAddressBook()
     {
-        $lines = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
-        foreach ($lines as $line) {
-            $line = trim($line);
-            if ($line && isset($GLOBALS['cfgSources'][$line])) {
-                return $line;
+        $lines = json_decode($GLOBALS['prefs']->getValue('addressbooks'));
+        if (!empty($lines)) {
+            foreach ($lines as $line) {
+                $line = trim($line);
+                if ($line && isset($GLOBALS['cfgSources'][$line])) {
+                    return $line;
+                }
             }
         }
 
@@ -577,12 +581,9 @@ class Turba {
         /* Add the new addressbook to the user's list of visible address
          * books. */
         $prefs = $GLOBALS['prefs']->getValue('addressbooks');
-        if ($prefs) {
-            $prefs = explode("\n", $prefs);
-            if (array_search($share_id, $prefs) === false) {
-                $prefs[] = $share_id;
-                $GLOBALS['prefs']->setValue('addressbooks', implode("\n", $prefs));
-            }
+        if (array_search($share_id, $prefs) === false) {
+            $prefs[] = $share_id;
+            $GLOBALS['prefs']->setValue('addressbooks', json_encode($prefs));
         }
 
         return $share;
index fa3ae37..7a32e53 100644 (file)
@@ -52,7 +52,7 @@ class Turba_BrowsePageTest extends Turba_TestBase {
         $GLOBALS['copymoveSources'] = array();
         $GLOBALS['cfgSources'] = $cfgSources;
 
-        $this->setPref('addressbooks', '_test_sql');
+        $this->setPref('addressbooks', json_encode(array('_test_sql')));
     }
 
     function getPage()
index 596017d..4460872 100644 (file)
@@ -53,7 +53,7 @@ class Turba_ViewBrowseTest extends Turba_TestBase {
         $GLOBALS['copymoveSources'] = array();
         $GLOBALS['cfgSources'] = $cfgSources;
 
-        $this->setPref('addressbooks', '_test_sql');
+        $this->setPref('addressbooks', json_encode(array('_test_sql')));
     }
 
     function getPage()