From 43b225d18333530abf17f5ca39140d417cc69490 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 08 Jul 2014 04:10:16 -0400
Subject: [PATCH] Support contact_search_name option in ACL plugin autocompletion

---
 plugins/acl/acl.php     |   27 +++++++++++++++++++--------
 plugins/acl/acl.js      |    6 +-----
 plugins/acl/package.xml |    4 ++--
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/plugins/acl/acl.js b/plugins/acl/acl.js
index d82725c..e59ac72 100644
--- a/plugins/acl/acl.js
+++ b/plugins/acl/acl.js
@@ -24,11 +24,7 @@
                     if (e.field.id != 'acluser')
                         return;
 
-                    var value = e.insert;
-                    // get UID from the entry value
-                    if (value.match(/\s*\(([^)]+)\)[, ]*$/))
-                        value = RegExp.$1;
-                    e.field.value = value;
+                    e.field.value = e.insert.replace(/[ ,;]+$/, '');
                 });
             }
         }
diff --git a/plugins/acl/acl.php b/plugins/acl/acl.php
index 95d4eda..3e3bca9 100644
--- a/plugins/acl/acl.php
+++ b/plugins/acl/acl.php
@@ -88,6 +88,7 @@
         $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
         $reqid  = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
         $users  = array();
+        $keys   = array();
 
         if ($this->init_ldap()) {
             $max  = (int) $this->rc->config->get('autocomplete_max', 15);
@@ -105,15 +106,23 @@
                 }
 
                 if ($user) {
-                    if ($record['name'])
-                        $user = $record['name'] . ' (' . $user . ')';
-
+                    $display = rcube_addressbook::compose_search_name($record);
+                    $user    = array('name' => $user, 'display' => $display);
                     $users[] = $user;
+                    $keys[]  = $display ?: $user['name'];
                 }
             }
         }
 
-        sort($users, SORT_LOCALE_STRING);
+        if (count($users)) {
+            // sort users index
+            asort($keys, SORT_LOCALE_STRING);
+            // re-sort users according to index
+            foreach ($keys as $idx => $val) {
+                $keys[$idx] = $users[$idx];
+            }
+            $users = array_values($keys);
+        }
 
         $this->rc->output->command('ksearch_query_results', $users, $search, $reqid);
         $this->rc->output->send();
@@ -653,8 +662,9 @@
      */
     private function init_ldap()
     {
-        if ($this->ldap)
+        if ($this->ldap) {
             return $this->ldap->ready;
+        }
 
         // get LDAP config
         $config = $this->rc->config->get('acl_users_source');
@@ -666,7 +676,7 @@
         // not an array, use configured ldap_public source
         if (!is_array($config)) {
             $ldap_config = (array) $this->rc->config->get('ldap_public');
-            $config = $ldap_config[$config];
+            $config      = $ldap_config[$config];
         }
 
         $uid_field = $this->rc->config->get('acl_users_field', 'mail');
@@ -692,12 +702,13 @@
         );
 
         // search in UID and name fields
-        $config['search_fields'] = array_values($config['fieldmap']);
+        $config['search_fields']   = array_values($config['fieldmap']);
         $config['required_fields'] = array($uid_field);
 
         // set search filter
-        if ($filter)
+        if ($filter) {
             $config['filter'] = $filter;
+        }
 
         // disable vlv
         $config['vlv'] = false;
diff --git a/plugins/acl/package.xml b/plugins/acl/package.xml
index a99ad4f..87e8b1c 100644
--- a/plugins/acl/package.xml
+++ b/plugins/acl/package.xml
@@ -13,9 +13,9 @@
 		<email>alec@alec.pl</email>
 		<active>yes</active>
 	</lead>
-	<date>2014-02-04</date>
+	<date>2014-07-08</date>
 	<version>
-		<release>1.3</release>
+		<release>1.4</release>
 		<api>1.0</api>
 	</version>
 	<stability>

--
Gitblit v1.9.1