]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/autocomplete.inc
Imported Upstream version 0.7.1
[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-2011, Roundcube Dev Team                           |
9  | Copyright (C) 2011, Kolab Systems AG                                  |
10  | Licensed under the GNU GPL                                            |
11  |                                                                       |
12  | PURPOSE:                                                              |
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  +-----------------------------------------------------------------------+
18
19  $Id: autocomplete.inc 5635 2011-12-21 10:07:42Z alec $
20
21 */
22
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)) {
26     $members = array();
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
34       }
35     }
36
37     $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
38     $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
39   }
40
41   $OUTPUT->send();
42 }
43
44
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);
51
52 if (strlen($source))
53   $book_types = array($source);
54 else
55   $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
56
57 if (!empty($book_types) && strlen($search)) {
58   $contacts  = array();
59   $sort_keys = array();
60   $books_num = count($book_types);
61   $search_lc = mb_strtolower($search);
62
63   foreach ($book_types as $id) {
64     $abook = $RCMAIL->get_address_book($id);
65     $abook->set_pagesize($MAXNUM);
66
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);
72         $idx = 0;
73         foreach ($email_arr as $email) {
74           if (empty($email)) {
75             continue;
76           }
77
78           $contact = format_email_recipient($email, $sql_arr['name']);
79
80           // skip entries that don't match
81           if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
82             continue;
83           }
84
85           // skip duplicates
86           if (!in_array($contact, $contacts)) {
87             $contacts[]  = $contact;
88             $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
89
90             if (count($contacts) >= $MAXNUM)
91               break 2;
92           }
93
94           // skip redundant entries (show only first email address)
95           if ($single) {
96             break;
97           }
98         }
99       }
100     }
101
102     // also list matching contact groups
103     if ($abook->groups && count($contacts) < $MAXNUM) {
104       foreach ($abook->list_groups($search) as $group) {
105         $abook->reset();
106         $abook->set_group($group['ID']);
107         $group_prop = $abook->get_group($group['ID']);
108
109         // group (distribution list) with email address(es)
110         if ($group_prop['email']) {
111             $idx = 0;
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++);
115
116                 if (count($contacts) >= $MAXNUM)
117                   break 2;
118             }
119         }
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'];
124
125           if (count($contacts) >= $MAXNUM)
126             break;
127         }
128       }
129     }
130   }
131
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];
138     }
139     $contacts = array_values($sort_keys);
140   }
141 }
142
143 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);
144 $OUTPUT->send();