From 2b3abe74f295dcedc13498a2c86131c493977d4b Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 7 Apr 2010 09:17:03 -0600 Subject: [PATCH] Convert turba addressbooks pref to json format --- turba/config/prefs.php.dist | 9 ++-- turba/lib/Application.php | 2 +- turba/lib/Forms/DeleteAddressBook.php | 4 +- .../LoginTasks/SystemTask/UpgradeFromTurba2.php | 48 ++++++++++++++++++++++ turba/lib/LoginTasks/SystemTask/UpgradePrefs.php | 9 +++- turba/lib/Turba.php | 37 +++++++++-------- turba/lib/tests/BrowsePageTest.php | 2 +- turba/lib/tests/ViewBrowseTest.php | 2 +- 8 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 turba/lib/LoginTasks/SystemTask/UpgradeFromTurba2.php diff --git a/turba/config/prefs.php.dist b/turba/config/prefs.php.dist index 6e01b49b9..3f736a4be 100644 --- a/turba/config/prefs.php.dist +++ b/turba/config/prefs.php.dist @@ -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' => '' ); diff --git a/turba/lib/Application.php b/turba/lib/Application.php index 0eee7fc94..aba907359 100644 --- a/turba/lib/Application.php +++ b/turba/lib/Application.php @@ -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; diff --git a/turba/lib/Forms/DeleteAddressBook.php b/turba/lib/Forms/DeleteAddressBook.php index 6683be4de..e334ff4eb 100644 --- a/turba/lib/Forms/DeleteAddressBook.php +++ b/turba/lib/Forms/DeleteAddressBook.php @@ -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 index 000000000..b0ffae6da --- /dev/null +++ b/turba/lib/LoginTasks/SystemTask/UpgradeFromTurba2.php @@ -0,0 +1,48 @@ + + * @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)); + } + } + +} diff --git a/turba/lib/LoginTasks/SystemTask/UpgradePrefs.php b/turba/lib/LoginTasks/SystemTask/UpgradePrefs.php index 50190ad6c..508f8831a 100644 --- a/turba/lib/LoginTasks/SystemTask/UpgradePrefs.php +++ b/turba/lib/LoginTasks/SystemTask/UpgradePrefs.php @@ -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; diff --git a/turba/lib/Turba.php b/turba/lib/Turba.php index da991ac5c..1acabc57b 100644 --- a/turba/lib/Turba.php +++ b/turba/lib/Turba.php @@ -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; diff --git a/turba/lib/tests/BrowsePageTest.php b/turba/lib/tests/BrowsePageTest.php index fa3ae377a..7a32e5346 100644 --- a/turba/lib/tests/BrowsePageTest.php +++ b/turba/lib/tests/BrowsePageTest.php @@ -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() diff --git a/turba/lib/tests/ViewBrowseTest.php b/turba/lib/tests/ViewBrowseTest.php index 596017dcd..44608720f 100644 --- a/turba/lib/tests/ViewBrowseTest.php +++ b/turba/lib/tests/ViewBrowseTest.php @@ -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() -- 2.11.0