From 36d004e3d0ad9ff97b66b2e505f6b17fd6d23102 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 03 Jul 2014 08:25:19 -0400
Subject: [PATCH] Added 'contact_search_name' option to define autocompletion entry format

---
 CHANGELOG                                   |    1 
 program/lib/Roundcube/rcube_addressbook.php |   53 ++++++++++++++++++++++++++
 program/steps/mail/autocomplete.inc         |   10 ++++-
 program/js/app.js                           |    2 
 config/defaults.inc.php                     |    5 ++
 5 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 4730642..6b3927a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Added 'contact_search_name' option to define autocompletion entry format
 - Display quota information for current folder not INBOX only (#1487993)
 - Support images in HTML signatures (#1488676)
 - Display full quota information in popup (#1485769, #1486604)
diff --git a/config/defaults.inc.php b/config/defaults.inc.php
index e7cb1e3..18171b7 100644
--- a/config/defaults.inc.php
+++ b/config/defaults.inc.php
@@ -877,6 +877,11 @@
 // Note: For LDAP sources fuzzy_search must be enabled to use 'partial' or 'prefix' mode
 $config['addressbook_search_mode'] = 0;
 
+// Template of contact entry on the autocompletion list.
+// You can use contact fields as: name, email, organization, department, etc.
+// See program/steps/addressbook/func.inc for a list
+$config['contact_search_name'] = '{name} <{email}>';
+
 // ----------------------------------
 // USER PREFERENCES
 // ----------------------------------
diff --git a/program/js/app.js b/program/js/app.js
index a3060a8..34871f7 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -4568,7 +4568,7 @@
     // add each result line to list
     if (results && (len = results.length)) {
       for (i=0; i < len && maxlen > 0; i++) {
-        text = typeof results[i] === 'object' ? results[i].name : results[i];
+        text = typeof results[i] === 'object' ? (results[i].display || results[i].name) : results[i];
         type = typeof results[i] === 'object' ? results[i].type : '';
         id = i + this.env.contacts.length;
         $('<li>').attr('id', 'rcmkSearchItem' + id)
diff --git a/program/lib/Roundcube/rcube_addressbook.php b/program/lib/Roundcube/rcube_addressbook.php
index 4d9fa3d..7566d65 100644
--- a/program/lib/Roundcube/rcube_addressbook.php
+++ b/program/lib/Roundcube/rcube_addressbook.php
@@ -557,6 +557,59 @@
     }
 
     /**
+     * Build contact display name for autocomplete listing
+     *
+     * @param array  Hash array with contact data as key-value pairs
+     * @param string Optional email address
+     * @param string Optional name (self::compose_list_name() result)
+     *
+     * @return string Display name
+     */
+    public static function compose_search_name($contact, $email = null, $name = null)
+    {
+        static $template;
+
+        if (!isset($template)) {  // cache this
+            $template = rcube::get_instance()->config->get('contact_search_name');
+            if (empty($template)) {
+                $template = '{name} <{email}>';
+            }
+        }
+
+        $result = $template;
+
+        if (preg_match_all('/\{[a-z]+\}/', $result, $matches)) {
+            foreach ($matches[0] as $key) {
+                $key = trim($key, '{}');
+
+                switch ($key) {
+                case 'name':
+                    $value = $name ?: self::compose_list_name($contact);
+                    break;
+
+                case 'email':
+                    $value = $email;
+                    break;
+                }
+
+                if (empty($value)) {
+                    $value = strpos($key, ':') ? $contact[$key] : self::get_col_values($key, $contact, true);
+                    if (is_array($value)) {
+                        $value = $value[0];
+                    }
+                }
+
+                $result = str_replace('{' . $key . '}', $value, $result);
+            }
+        }
+
+        $result = preg_replace('/\s+/', ' ', $result);
+        $result = trim($result);
+
+        return $result;
+    }
+
+    /**
      * Create a unique key for sorting contacts
      */
     public static function compose_contact_key($contact, $sort_col)
diff --git a/program/steps/mail/autocomplete.inc b/program/steps/mail/autocomplete.inc
index 71b337a..c805cd0 100644
--- a/program/steps/mail/autocomplete.inc
+++ b/program/steps/mail/autocomplete.inc
@@ -90,8 +90,14 @@
 
                     // skip duplicates
                     if (!in_array($contact, $contacts)) {
-                        $contacts[]  = array('name' => $contact, 'type' => $sql_arr['_type']);
-                        $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
+                        $contact = array('name' => $contact, 'type' => $sql_arr['_type']);
+
+                        if (($display = rcube_addressbook::compose_search_name($sql_arr, $email, $name)) && $display != $contact['name']) {
+                            $contact['display'] = $display;
+                        }
+
+                        $contacts[]  = $contact;
+                        $sort_keys[] = sprintf('%s %03d', $contact['display'] ?: $name, $idx++);
 
                         if (count($contacts) >= $MAXNUM) {
                             break 2;

--
Gitblit v1.9.1