4 +-----------------------------------------------------------------------+
5 | program/steps/mail/autocomplete.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2010, Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Perform a search on configured address books for the address |
13 | autocompletion of the message compose screen |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: autocomplete.inc 4963 2011-07-25 10:49:39Z alec $
22 if ($RCMAIL->action == 'group-expand') {
23 $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC));
24 if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) {
26 $abook->set_group($gid);
27 $abook->set_pagesize(1000); // TODO: limit number of group members by config
28 $result = $abook->list_records(array('email','name'));
29 while ($result && ($sql_arr = $result->iterate())) {
30 foreach ((array)$sql_arr['email'] as $email)
31 $members[] = format_email_recipient($email, $sql_arr['name']);
34 $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
41 $MAXNUM = (int)$RCMAIL->config->get('autocomplete_max', 15);
42 $search = get_input_value('_search', RCUBE_INPUT_GPC, true);
43 $source = get_input_value('_source', RCUBE_INPUT_GPC);
44 $sid = get_input_value('_id', RCUBE_INPUT_GPC);
47 $book_types = array($source);
49 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
51 if (!empty($book_types) && strlen($search)) {
53 $books_num = count($book_types);
55 foreach ($book_types as $id) {
56 $abook = $RCMAIL->get_address_book($id);
57 $abook->set_pagesize($MAXNUM);
59 if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
60 while ($sql_arr = $result->iterate()) {
61 // Contact can have more than one e-mail address
62 $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
63 $email_cnt = count($email_arr);
64 foreach ($email_arr as $email) {
67 $contact = format_email_recipient($email, $sql_arr['name']);
68 // skip entries that don't match
69 if ($email_cnt > 1 && stripos($contact, $search) === false) {
72 // when we've got more than one book, we need to skip duplicates
73 if ($books_num == 1 || !in_array($contact, $contacts)) {
74 $contacts[] = $contact;
75 if (count($contacts) >= $MAXNUM)
82 // also list matching contact groups
84 foreach ($abook->list_groups($search) as $group) {
86 $abook->set_group($group['ID']);
87 $result = $abook->count();
90 $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
91 if (count($contacts) >= $MAXNUM)
98 usort($contacts, 'contact_results_sort');
101 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);
105 function contact_results_sort($a, $b)
107 $name_a = is_array($a) ? $a['name'] : $a;
108 $name_b = is_array($b) ? $b['name'] : $b;
109 return strcoll(trim($name_a, '" '), trim($name_b, '" '));