From 65dff8f812cc6b0c4fdd470905700143910289b2 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 23 Nov 2012 03:11:19 -0500
Subject: [PATCH] Fix hidden flag and default_addressbook settings handling

---
 program/steps/addressbook/import.inc |    2 +-
 program/steps/addressbook/edit.inc   |    4 ++--
 program/steps/mail/compose.inc       |    2 +-
 program/include/rcmail.php           |   34 +++++++++++++++++++++++++---------
 program/steps/addressbook/func.inc   |   11 -----------
 program/steps/settings/func.inc      |    2 +-
 6 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 99a68e8..72c2fd9 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -207,14 +207,22 @@
       }
     }
 
+    // when user requested default writeable addressbook
+    // we need to check if default is writeable, if not we
+    // will return first writeable book (if any exist)
+    if ($contacts && $default && $contacts->readonly && $writeable) {
+      $contacts = null;
+    }
+
     // Get first addressbook from the list if configured default doesn't exist
     // This can happen when user deleted the addressbook (e.g. Kolab folder)
     if (!$contacts && (!$id || $default)) {
-      $source = reset($this->get_address_sources($writeable));
+      $source = reset($this->get_address_sources($writeable, !$default));
       if (!empty($source)) {
         $contacts = $this->get_address_book($source['id']);
-        if ($contacts)
+        if ($contacts) {
           $id = $source['id'];
+        }
       }
     }
 
@@ -226,16 +234,17 @@
         true, true);
     }
 
+    // add to the 'books' array for shutdown function
+    $this->address_books[$id] = $contacts;
+
     if ($writeable && $contacts->readonly) {
       return null;
     }
 
     // set configured sort order
-    if ($sort_col = $this->config->get('addressbook_sort_col'))
+    if ($sort_col = $this->config->get('addressbook_sort_col')) {
         $contacts->set_sort_order($sort_col);
-
-    // add to the 'books' array for shutdown function
-    $this->address_books[$id] = $contacts;
+    }
 
     return $contacts;
   }
@@ -245,10 +254,11 @@
    * Return address books list
    *
    * @param boolean True if the address book needs to be writeable
+   * @param boolean True if the address book needs to be not hidden
    *
    * @return array  Address books array
    */
-  public function get_address_sources($writeable = false)
+  public function get_address_sources($writeable = false, $skip_hidden = false)
   {
     $abook_type = strtolower($this->config->get('address_book_type'));
     $ldap_config = $this->config->get('ldap_public');
@@ -292,11 +302,17 @@
 
     foreach ($list as $idx => $item) {
       // register source for shutdown function
-      if (!is_object($this->address_books[$item['id']]))
+      if (!is_object($this->address_books[$item['id']])) {
         $this->address_books[$item['id']] = $item;
+      }
       // remove from list if not writeable as requested
-      if ($writeable && $item['readonly'])
+      if ($writeable && $item['readonly']) {
           unset($list[$idx]);
+      }
+      // remove from list if hidden as requested
+      else if ($skip_hidden && $item['hidden']) {
+          unset($list[$idx]);
+      }
     }
 
     return $list;
diff --git a/program/steps/addressbook/edit.inc b/program/steps/addressbook/edit.inc
index b216a7c..d175c05 100644
--- a/program/steps/addressbook/edit.inc
+++ b/program/steps/addressbook/edit.inc
@@ -52,7 +52,7 @@
 
     // find writable addressbook
     if (!$CONTACTS || $CONTACTS->readonly)
-        $source = rcmail_default_source(true);
+        $source = $RCMAIL->get_address_book(-1, true);
 
     // Initialize addressbook
     $CONTACTS = rcmail_contact_source($source, true);
@@ -239,7 +239,7 @@
 {
     global $RCMAIL, $SOURCE_ID;
 
-    $sources_list = $RCMAIL->get_address_sources(true);
+    $sources_list = $RCMAIL->get_address_sources(true, true);
 
     if (count($sources_list) < 2) {
         $source = $sources_list[$SOURCE_ID];
diff --git a/program/steps/addressbook/func.inc b/program/steps/addressbook/func.inc
index e42e26e..fded9a8 100644
--- a/program/steps/addressbook/func.inc
+++ b/program/steps/addressbook/func.inc
@@ -160,17 +160,6 @@
 }
 
 
-function rcmail_default_source($writable=false)
-{
-    global $RCMAIL;
-
-    // get list of address sources
-    $first = reset($RCMAIL->get_address_sources($writable));
-
-    // use first directory by default
-    return $first['id'];
-}
-
 function rcmail_set_sourcename($abook)
 {
     global $OUTPUT;
diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc
index 6d60f82..df07d64 100644
--- a/program/steps/addressbook/import.inc
+++ b/program/steps/addressbook/import.inc
@@ -30,7 +30,7 @@
 
   $attrib += array('id' => "rcmImportForm");
 
-  $writable_books = $RCMAIL->get_address_sources(true);
+  $writable_books = $RCMAIL->get_address_sources(true, true);
 
   $upload = new html_inputfield(array(
     'type' => 'file',
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index ffc1c75..92ec88f 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -1615,7 +1615,7 @@
             'rel' => '%s',
             'onclick' => "return ".JS_OBJECT_NAME.".command('list-adresses','%s',this)"), '%s'));
 
-    foreach ($RCMAIL->get_address_sources() as $j => $source) {
+    foreach ($RCMAIL->get_address_sources(false, true) as $j => $source) {
         $id = strval(strlen($source['id']) ? $source['id'] : $j);
         $js_id = JQ($id);
 
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 2c19d88..3bcca21 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -695,7 +695,7 @@
     );
 
     if (!isset($no_override['default_addressbook'])
-      && ($books = $RCMAIL->get_address_sources(true))
+      && ($books = $RCMAIL->get_address_sources(true, true))
     ) {
       $field_id = 'rcmfd_default_addressbook';
       $select_abook = new html_select(array('name' => '_default_addressbook', 'id' => $field_id));

--
Gitblit v1.9.1