From 36d004e3d0ad9ff97b66b2e505f6b17fd6d23102 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 3 Jul 2014 14:25:19 +0200 Subject: Added 'contact_search_name' option to define autocompletion entry format --- CHANGELOG | 1 + config/defaults.inc.php | 5 +++ program/js/app.js | 2 +- program/lib/Roundcube/rcube_addressbook.php | 53 +++++++++++++++++++++++++++++ program/steps/mail/autocomplete.inc | 10 ++++-- 5 files changed, 68 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4730642ef..6b3927a08 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 e7cb1e3aa..18171b75f 100644 --- a/config/defaults.inc.php +++ b/config/defaults.inc.php @@ -877,6 +877,11 @@ $config['address_template'] = '{street}
{locality} {zipcode}
{country} { // 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 a3060a8c3..34871f798 100644 --- a/program/js/app.js +++ b/program/js/app.js @@ -4568,7 +4568,7 @@ function rcube_webmail() // 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; $('
  • ').attr('id', 'rcmkSearchItem' + id) diff --git a/program/lib/Roundcube/rcube_addressbook.php b/program/lib/Roundcube/rcube_addressbook.php index 4d9fa3db1..7566d6546 100644 --- a/program/lib/Roundcube/rcube_addressbook.php +++ b/program/lib/Roundcube/rcube_addressbook.php @@ -556,6 +556,59 @@ abstract class rcube_addressbook return $fn; } + /** + * 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 */ diff --git a/program/steps/mail/autocomplete.inc b/program/steps/mail/autocomplete.inc index 71b337a53..c805cd009 100644 --- a/program/steps/mail/autocomplete.inc +++ b/program/steps/mail/autocomplete.inc @@ -90,8 +90,14 @@ if (!empty($book_types) && strlen($search)) { // 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; -- cgit v1.2.3