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 5426 2011-11-15 10:50:30Z 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']);
35 $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
36 $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
43 $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
44 $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
45 $single = (bool) $RCMAIL->config->get('autocomplete_single');
46 $search = get_input_value('_search', RCUBE_INPUT_GPC, true);
47 $source = get_input_value('_source', RCUBE_INPUT_GPC);
48 $sid = get_input_value('_id', RCUBE_INPUT_GPC);
51 $book_types = array($source);
53 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
55 if (!empty($book_types) && strlen($search)) {
58 $books_num = count($book_types);
59 $search_lc = mb_strtolower($search);
61 foreach ($book_types as $id) {
62 $abook = $RCMAIL->get_address_book($id);
63 $abook->set_pagesize($MAXNUM);
65 if ($result = $abook->search(array('email','name'), $search, $mode, true, true, 'email')) {
66 while ($sql_arr = $result->iterate()) {
67 // Contact can have more than one e-mail address
68 $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
69 $email_cnt = count($email_arr);
71 foreach ($email_arr as $email) {
76 $contact = format_email_recipient($email, $sql_arr['name']);
78 // skip entries that don't match
79 if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
84 if (!in_array($contact, $contacts)) {
85 $contacts[] = $contact;
86 $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
88 if (count($contacts) >= $MAXNUM)
92 // skip redundant entries (show only first email address)
100 // also list matching contact groups
101 if ($abook->groups && count($contacts) < $MAXNUM) {
102 foreach ($abook->list_groups($search) as $group) {
104 $abook->set_group($group['ID']);
105 $group_prop = $abook->get_group($group['ID']);
107 // group (distribution list) with email address(es)
108 if ($group_prop['email']) {
110 foreach ((array)$group_prop['email'] as $email) {
111 $contacts[] = format_email_recipient($email, $group['name']);
112 $sort_keys[] = sprintf('%s %03d', $group['name'] , $idx++);
114 if (count($contacts) >= $MAXNUM)
118 // show group with count
119 else if (($result = $abook->count()) && $result->count) {
120 $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
121 $sort_keys[] = $group['name'];
123 if (count($contacts) >= $MAXNUM)
130 if (count($contacts)) {
131 // sort contacts index
132 asort($sort_keys, SORT_LOCALE_STRING);
133 // re-sort contacts according to index
134 foreach ($sort_keys as $idx => $val) {
135 $sort_keys[$idx] = $contacts[$idx];
137 $contacts = array_values($sort_keys);
141 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);