]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/autocomplete.inc
Imported Upstream version 0.7
[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 5426 2011-11-15 10:50:30Z 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     }
34
35     $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
36     $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
37   }
38
39   $OUTPUT->send();
40 }
41
42
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);
49
50 if (strlen($source))
51   $book_types = array($source);
52 else
53   $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
54
55 if (!empty($book_types) && strlen($search)) {
56   $contacts  = array();
57   $sort_keys = array();
58   $books_num = count($book_types);
59   $search_lc = mb_strtolower($search);
60
61   foreach ($book_types as $id) {
62     $abook = $RCMAIL->get_address_book($id);
63     $abook->set_pagesize($MAXNUM);
64
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);
70         $idx = 0;
71         foreach ($email_arr as $email) {
72           if (empty($email)) {
73             continue;
74           }
75
76           $contact = format_email_recipient($email, $sql_arr['name']);
77
78           // skip entries that don't match
79           if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
80             continue;
81           }
82
83           // skip duplicates
84           if (!in_array($contact, $contacts)) {
85             $contacts[]  = $contact;
86             $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
87
88             if (count($contacts) >= $MAXNUM)
89               break 2;
90           }
91
92           // skip redundant entries (show only first email address)
93           if ($single) {
94             break;
95           }
96         }
97       }
98     }
99
100     // also list matching contact groups
101     if ($abook->groups && count($contacts) < $MAXNUM) {
102       foreach ($abook->list_groups($search) as $group) {
103         $abook->reset();
104         $abook->set_group($group['ID']);
105         $group_prop = $abook->get_group($group['ID']);
106
107         // group (distribution list) with email address(es)
108         if ($group_prop['email']) {
109             $idx = 0;
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++);
113
114                 if (count($contacts) >= $MAXNUM)
115                   break 2;
116             }
117         }
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'];
122
123           if (count($contacts) >= $MAXNUM)
124             break;
125         }
126       }
127     }
128   }
129
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];
136     }
137     $contacts = array_values($sort_keys);
138   }
139 }
140
141 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);
142 $OUTPUT->send();