From b896b18f87064f523dca82c3fcaa5465ec8328ad Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 03 Jun 2011 08:34:48 -0400
Subject: [PATCH] - Call addressbook_get hook only if build-in addressbook doesn't match wanted ID (for better performance), other improvements

---
 program/steps/addressbook/list.inc |    4 ++
 program/include/rcmail.php         |   38 ++++++++++---------
 program/steps/addressbook/func.inc |   23 +++++++----
 3 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index e93d546..4fc1262 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -381,18 +381,12 @@
    */
   public function get_address_book($id, $writeable = false)
   {
-    $contacts = null;
+    $contacts    = null;
     $ldap_config = (array)$this->config->get('ldap_public');
-    $abook_type = strtolower($this->config->get('address_book_type'));
+    $abook_type  = strtolower($this->config->get('address_book_type'));
 
-    $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable));
-
-    // plugin returned instance of a rcube_addressbook
-    if ($plugin['instance'] instanceof rcube_addressbook) {
-      $contacts = $plugin['instance'];
-    }
     // use existing instance
-    else if (isset($this->address_books[$id]) && is_a($this->address_books[$id], 'rcube_addressbook') && (!$writeable || !$this->address_books[$id]->readonly)) {
+    if (isset($this->address_books[$id]) && is_a($this->address_books[$id], 'rcube_addressbook') && (!$writeable || !$this->address_books[$id]->readonly)) {
       $contacts = $this->address_books[$id];
     }
     else if ($id && $ldap_config[$id]) {
@@ -401,17 +395,25 @@
     else if ($id === '0') {
       $contacts = new rcube_contacts($this->db, $this->user->ID);
     }
-    else if ($abook_type == 'ldap') {
-      // Use the first writable LDAP address book.
-      foreach ($ldap_config as $id => $prop) {
-        if (!$writeable || $prop['writable']) {
-          $contacts = new rcube_ldap($prop, $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host']));
-          break;
+    else {
+      $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable));
+
+      // plugin returned instance of a rcube_addressbook
+      if ($plugin['instance'] instanceof rcube_addressbook) {
+        $contacts = $plugin['instance'];
+      }
+      else if ($abook_type == 'ldap') {
+        // Use the first writable LDAP address book.
+        foreach ($ldap_config as $id => $prop) {
+          if (!$writeable || $prop['writable']) {
+            $contacts = new rcube_ldap($prop, $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host']));
+            break;
+          }
         }
       }
-    }
-    else { // $id == 'sql'
-      $contacts = new rcube_contacts($this->db, $this->user->ID);
+      else { // $id == 'sql'
+        $contacts = new rcube_contacts($this->db, $this->user->ID);
+      }
     }
 
     // add to the 'books' array for shutdown function
diff --git a/program/steps/addressbook/func.inc b/program/steps/addressbook/func.inc
index df86fce..62fde97 100644
--- a/program/steps/addressbook/func.inc
+++ b/program/steps/addressbook/func.inc
@@ -21,15 +21,21 @@
 
 $SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
 
-// add list of address sources to client env
-$js_list = $RCMAIL->get_address_sources();
-
 // select source
 $source = get_input_value('_source', RCUBE_INPUT_GPC);
 
-// if source is not set use first directory
-if (empty($source))
-    $source = $js_list[key($js_list)]['id'];
+if (!$RCMAIL->action && !$OUTPUT->ajax_call) {
+    // add list of address sources to client env
+    $js_list = $RCMAIL->get_address_sources();
+
+    // if source is not set use first directory
+    if (empty($source))
+        $source = $js_list[key($js_list)]['id'];
+
+    $search_mods = $RCMAIL->config->get('addressbook_search_mods', $SEARCH_MODS_DEFAULT);
+    $OUTPUT->set_env('search_mods', $search_mods);
+    $OUTPUT->set_env('address_sources', $js_list);
+}
 
 // instantiate a contacts object according to the given source
 $CONTACTS = $RCMAIL->get_address_book($source);
@@ -44,18 +50,17 @@
 
 if (!empty($_REQUEST['_gid']))
     $CONTACTS->set_group(get_input_value('_gid', RCUBE_INPUT_GPC));
-
+/*
 // set message set for search result
 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
     $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
-
+*/
 // set data source env
 $OUTPUT->set_env('source', $source ? $source : '0');
 $OUTPUT->set_env('readonly', $CONTACTS->readonly, false);
 if (!$OUTPUT->ajax_call) {
     $search_mods = $RCMAIL->config->get('addressbook_search_mods', $SEARCH_MODS_DEFAULT);
     $OUTPUT->set_env('search_mods', $search_mods);
-    $OUTPUT->set_env('address_sources', $js_list);
     $OUTPUT->set_pagetitle(rcube_label('addressbook'));
 }
 
diff --git a/program/steps/addressbook/list.inc b/program/steps/addressbook/list.inc
index 0eb4b80..864ad93 100644
--- a/program/steps/addressbook/list.inc
+++ b/program/steps/addressbook/list.inc
@@ -19,6 +19,10 @@
 
 */
 
+// set message set for search result
+if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
+    $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
+
 // get contacts for this user
 $result = $CONTACTS->list_records(array('name'));
 

--
Gitblit v1.9.1