4 +-----------------------------------------------------------------------+
5 | program/steps/mail/autocomplete.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2011, Roundcube Dev Team |
9 | Copyright (C) 2011, Kolab Systems AG |
10 | Licensed under the GNU GPL |
13 | Perform a search on configured address books for the address |
14 | autocompletion of the message compose screen |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
19 $Id: autocomplete.inc 5635 2011-12-21 10:07:42Z alec $
23 if ($RCMAIL->action == 'group-expand') {
24 $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC));
25 if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) {
27 $abook->set_group($gid);
28 $abook->set_pagesize(1000); // TODO: limit number of group members by config
29 $result = $abook->list_records(array('email','name'));
30 while ($result && ($sql_arr = $result->iterate())) {
31 foreach ((array)$sql_arr['email'] as $email) {
32 $members[] = format_email_recipient($email, $sql_arr['name']);
33 break; // only expand one email per contact
37 $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
38 $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
45 $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
46 $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
47 $single = (bool) $RCMAIL->config->get('autocomplete_single');
48 $search = get_input_value('_search', RCUBE_INPUT_GPC, true);
49 $source = get_input_value('_source', RCUBE_INPUT_GPC);
50 $sid = get_input_value('_id', RCUBE_INPUT_GPC);
53 $book_types = array($source);
55 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
57 if (!empty($book_types) && strlen($search)) {
60 $books_num = count($book_types);
61 $search_lc = mb_strtolower($search);
63 foreach ($book_types as $id) {
64 $abook = $RCMAIL->get_address_book($id);
65 $abook->set_pagesize($MAXNUM);
67 if ($result = $abook->search(array('email','name'), $search, $mode, true, true, 'email')) {
68 while ($sql_arr = $result->iterate()) {
69 // Contact can have more than one e-mail address
70 $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
71 $email_cnt = count($email_arr);
73 foreach ($email_arr as $email) {
78 $contact = format_email_recipient($email, $sql_arr['name']);
80 // skip entries that don't match
81 if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
86 if (!in_array($contact, $contacts)) {
87 $contacts[] = $contact;
88 $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
90 if (count($contacts) >= $MAXNUM)
94 // skip redundant entries (show only first email address)
102 // also list matching contact groups
103 if ($abook->groups && count($contacts) < $MAXNUM) {
104 foreach ($abook->list_groups($search) as $group) {
106 $abook->set_group($group['ID']);
107 $group_prop = $abook->get_group($group['ID']);
109 // group (distribution list) with email address(es)
110 if ($group_prop['email']) {
112 foreach ((array)$group_prop['email'] as $email) {
113 $contacts[] = format_email_recipient($email, $group['name']);
114 $sort_keys[] = sprintf('%s %03d', $group['name'] , $idx++);
116 if (count($contacts) >= $MAXNUM)
120 // show group with count
121 else if (($result = $abook->count()) && $result->count) {
122 $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
123 $sort_keys[] = $group['name'];
125 if (count($contacts) >= $MAXNUM)
132 if (count($contacts)) {
133 // sort contacts index
134 asort($sort_keys, SORT_LOCALE_STRING);
135 // re-sort contacts according to index
136 foreach ($sort_keys as $idx => $val) {
137 $sort_keys[$idx] = $contacts[$idx];
139 $contacts = array_values($sort_keys);
143 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);