From ec433114236f9e1871ec5ff9d5d19de50ab236fd Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 01 May 2013 03:46:29 -0400
Subject: [PATCH] Fix so addressbook_search_mode works also for group search (#1489079)

---
 CHANGELOG                                   |    1 +
 program/lib/Roundcube/rcube_addressbook.php |    7 ++++++-
 program/steps/mail/autocomplete.inc         |    2 +-
 program/lib/Roundcube/rcube_ldap.php        |   11 ++++++++---
 program/lib/Roundcube/rcube_contacts.php    |   22 ++++++++++++++++++++--
 5 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 9d1a621..4dc83ac 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Fix so addressbook_search_mode works also for group search (#1489079)
 - Fix removal of a contact from a group in LDAP addressbook (#1489081)
 - Support CSV import from Atmail (#1489045)
 - Inlcude SQL query in the log on SQL error (#1489064)
diff --git a/program/lib/Roundcube/rcube_addressbook.php b/program/lib/Roundcube/rcube_addressbook.php
index 5fbcb72..84bd4bf 100644
--- a/program/lib/Roundcube/rcube_addressbook.php
+++ b/program/lib/Roundcube/rcube_addressbook.php
@@ -309,9 +309,14 @@
      * List all active contact groups of this source
      *
      * @param string  Optional search string to match group name
+     * @param int     Matching mode:
+     *                0 - partial (*abc*),
+     *                1 - strict (=),
+     *                2 - prefix (abc*)
+     *
      * @return array  Indexed list of contact groups, each a hash array
      */
-    function list_groups($search = null)
+    function list_groups($search = null, $mode = 0)
     {
         /* empty for address books don't supporting groups */
         return array();
diff --git a/program/lib/Roundcube/rcube_contacts.php b/program/lib/Roundcube/rcube_contacts.php
index 6693e55..3919cdc 100644
--- a/program/lib/Roundcube/rcube_contacts.php
+++ b/program/lib/Roundcube/rcube_contacts.php
@@ -137,16 +137,34 @@
      * List all active contact groups of this source
      *
      * @param string  Search string to match group name
+     * @param int     Matching mode:
+     *                0 - partial (*abc*),
+     *                1 - strict (=),
+     *                2 - prefix (abc*)
+     *
      * @return array  Indexed list of contact groups, each a hash array
      */
-    function list_groups($search = null)
+    function list_groups($search = null, $mode = 0)
     {
         $results = array();
 
         if (!$this->groups)
             return $results;
 
-        $sql_filter = $search ? " AND " . $this->db->ilike('name', '%'.$search.'%') : '';
+        if ($search) {
+            switch (intval($mode)) {
+            case 1:
+                $sql_filter = $this->db->ilike('name', $search);
+                break;
+            case 2:
+                $sql_filter = $this->db->ilike('name', $search . '%');
+                break;
+            default:
+                $sql_filter = $this->db->ilike('name', '%' . $search . '%');
+            }
+
+            $sql_filter = " AND $sql_filter";
+        }
 
         $sql_result = $this->db->query(
             "SELECT * FROM ".$this->db->table_name($this->db_groups).
diff --git a/program/lib/Roundcube/rcube_ldap.php b/program/lib/Roundcube/rcube_ldap.php
index bae72b7..47e96c3 100644
--- a/program/lib/Roundcube/rcube_ldap.php
+++ b/program/lib/Roundcube/rcube_ldap.php
@@ -1715,9 +1715,14 @@
      * List all active contact groups of this source
      *
      * @param string  Optional search string to match group name
+     * @param int     Matching mode:
+     *                0 - partial (*abc*),
+     *                1 - strict (=),
+     *                2 - prefix (abc*)
+     *
      * @return array  Indexed list of contact groups, each a hash array
      */
-    function list_groups($search = null)
+    function list_groups($search = null, $mode = 0)
     {
         if (!$this->groups)
             return array();
@@ -1729,10 +1734,10 @@
 
         $groups = array();
         if ($search) {
-            $search = mb_strtolower($search);
             foreach ($group_cache as $group) {
-                if (strpos(mb_strtolower($group['name']), $search) !== false)
+                if ($this->compare_search_value('name', $group['name'], $search, $mode)) {
                     $groups[] = $group;
+                }
             }
         }
         else
diff --git a/program/steps/mail/autocomplete.inc b/program/steps/mail/autocomplete.inc
index 5557981..f9e8d71 100644
--- a/program/steps/mail/autocomplete.inc
+++ b/program/steps/mail/autocomplete.inc
@@ -102,7 +102,7 @@
 
     // also list matching contact groups
     if ($abook->groups && count($contacts) < $MAXNUM) {
-      foreach ($abook->list_groups($search) as $group) {
+      foreach ($abook->list_groups($search, $mode) as $group) {
         $abook->reset();
         $abook->set_group($group['ID']);
         $group_prop = $abook->get_group($group['ID']);

--
Gitblit v1.9.1