]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/autocomplete.inc
70072910c7e917b563843561a56bff423a529e46
[roundcube.git] / program / steps / mail / autocomplete.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/autocomplete.inc                                   |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2008-2010, Roundcube Dev Team                           |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
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  +-----------------------------------------------------------------------+
17
18  $Id: autocomplete.inc 3989 2010-09-25 13:03:53Z alec $
19
20 */
21
22 $MAXNUM = 15;
23 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
24
25 if ($RCMAIL->action == 'group-expand') {
26   $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC));
27   if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) {
28     $members = array();
29     $abook->set_group($gid);
30     $abook->set_pagesize(1000);  // TODO: limit number of group members by config
31     $result = $abook->list_records(array('email','name'));
32     while ($result && ($sql_arr = $result->iterate()))
33       $members[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
34
35     $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
36   }
37 }
38 else if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) {
39   $contacts = array();
40   $books_num = count($book_types);
41
42   foreach ($book_types as $id) {
43     $abook = $RCMAIL->get_address_book($id);
44     $abook->set_pagesize($MAXNUM);
45
46     if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
47       while ($sql_arr = $result->iterate()) {
48         $contact = format_email_recipient($sql_arr['email'], $sql_arr['name']);
49         // when we've got more than one book, we need to skip duplicates
50         if ($books_num == 1 || !in_array($contact, $contacts)) {
51           $contacts[] = $contact;
52           if (count($contacts) >= $MAXNUM)
53             break 2;
54         }
55       }
56     }
57
58     // also list matching contact groups
59     if ($abook->groups) {
60       foreach ($abook->list_groups($search) as $group) {
61         $abook->reset();
62         $abook->set_group($group['ID']);
63         $result = $abook->count();
64
65         if ($result->count) {
66           $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
67           if (count($contacts) >= $MAXNUM)
68             break;
69         }
70       }
71     }
72   }
73
74   usort($contacts, 'contact_results_sort');
75 }
76
77 $OUTPUT->command('ksearch_query_results', $contacts, $search);
78 $OUTPUT->send();
79
80
81 function contact_results_sort($a, $b)
82 {
83   $name_a = is_array($a) ? $a['name'] : $a;
84   $name_b = is_array($b) ? $b['name'] : $b;
85   return strcoll(trim($name_a, '" '), trim($name_b, '" '));
86 }
87