Thomas Bruederli
2014-01-16 2baeac116abef9d5bcb748c687577d16dce868a0
commit | author | age
938e96 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/autocomplete.inc                                   |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
c97625 8  | Copyright (C) 2008-2013, Roundcube Dev Team                           |
AM 9  | Copyright (C) 2011-2013, Kolab Systems AG                             |
7fe381 10  |                                                                       |
T 11  | Licensed under the GNU General Public License version 3 or            |
12  | any later version with exceptions for skins & plugins.                |
13  | See the README file for a full license statement.                     |
938e96 14  |                                                                       |
T 15  | PURPOSE:                                                              |
16  |   Perform a search on configured address books for the address        |
17  |   autocompletion of the message compose screen                        |
18  +-----------------------------------------------------------------------+
19  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
20  +-----------------------------------------------------------------------+
21 */
22
c0297f 23 if ($RCMAIL->action == 'group-expand') {
c97625 24     $abook = $RCMAIL->get_address_book(rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC));
AM 25     if ($gid = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GPC)) {
26         $abook->set_group($gid);
27         $abook->set_pagesize(1000);  // TODO: limit number of group members by config
28
29         $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
30         $result    = $abook->list_records($RCMAIL->config->get('contactlist_fields'));
31         $members   = array();
32
33         while ($result && ($sql_arr = $result->iterate())) {
34             $emails = (array) $abook->get_col_values('email', $sql_arr, true);
35             if (!empty($emails) && ($email = array_shift($emails))) {
36                 $members[] = format_email_recipient($email, rcube_addressbook::compose_list_name($sql_arr));
37             }
38         }
39
40         $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
0501b6 41     }
25fdec 42
c97625 43     $OUTPUT->send();
c0297f 44 }
0213f8 45
A 46
710b1b 47 $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
f21a04 48 $mode   = (int) $RCMAIL->config->get('addressbook_search_mode');
710b1b 49 $single = (bool) $RCMAIL->config->get('autocomplete_single');
6b2b2e 50 $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
AM 51 $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
017c4f 52 $reqid  = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
0213f8 53
c97625 54 if (strlen($source)) {
AM 55     $book_types = array($source);
56 }
57 else {
58     $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
59 }
0213f8 60
A 61 if (!empty($book_types) && strlen($search)) {
c97625 62     $contacts  = array();
AM 63     $sort_keys = array();
64     $books_num = count($book_types);
65     $search_lc = mb_strtolower($search);
938e96 66
c97625 67     foreach ($book_types as $id) {
AM 68         $abook = $RCMAIL->get_address_book($id);
69         $abook->set_pagesize($MAXNUM);
938e96 70
c97625 71         if ($result = $abook->search($RCMAIL->config->get('contactlist_fields'), $search, $mode, true, true, 'email')) {
AM 72             while ($sql_arr = $result->iterate()) {
73                 // Contact can have more than one e-mail address
74                 $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
75                 $email_cnt = count($email_arr);
76                 $idx       = 0;
710b1b 77
c97625 78                 foreach ($email_arr as $email) {
AM 79                     if (empty($email)) {
80                         continue;
81                     }
710b1b 82
c97625 83                     $name    = rcube_addressbook::compose_list_name($sql_arr);
AM 84                     $contact = format_email_recipient($email, $name);
710b1b 85
c97625 86                     // skip entries that don't match
AM 87                     if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
88                         continue;
89                     }
2f9fdb 90
c97625 91                     // skip duplicates
AM 92                     if (!in_array($contact, $contacts)) {
93                         $contacts[]  = $contact;
94                         $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++);
710b1b 95
c97625 96                         if (count($contacts) >= $MAXNUM) {
AM 97                             break 2;
98                         }
99                     }
25fdec 100
c97625 101                     // skip redundant entries (show only first email address)
AM 102                     if ($single) {
103                         break;
104                     }
105                 }
dc6c4f 106             }
T 107         }
2f9fdb 108
c97625 109         // also list matching contact groups
AM 110         if ($abook->groups && count($contacts) < $MAXNUM) {
111             foreach ($abook->list_groups($search, $mode) as $group) {
112                 $abook->reset();
113                 $abook->set_group($group['ID']);
114
115                 $group_prop = $abook->get_group($group['ID']);
116
117                 // group (distribution list) with email address(es)
118                 if ($group_prop['email']) {
119                     $idx = 0;
120                     foreach ((array)$group_prop['email'] as $email) {
121                         $contacts[]  = format_email_recipient($email, $group['name']);
122                         $sort_keys[] = sprintf('%s %03d', $group['name'] , $idx++);
123
124                         if (count($contacts) >= $MAXNUM) {
125                             break 2;
126                         }
127                     }
128                 }
129                 // show group with count
130                 else if (($result = $abook->count()) && $result->count) {
131                     $sort_keys[] = $group['name'];
132                     $contacts[]  = array(
133                         'name'   => $group['name'] . ' (' . intval($result->count) . ')',
134                         'id'     => $group['ID'],
135                         'source' => $id
136                     );
137
138                     if (count($contacts) >= $MAXNUM) {
139                         break;
140                     }
141                 }
142             }
a61bbb 143         }
938e96 144     }
25fdec 145
c97625 146     if (count($contacts)) {
AM 147         // sort contacts index
148         asort($sort_keys, SORT_LOCALE_STRING);
149         // re-sort contacts according to index
150         foreach ($sort_keys as $idx => $val) {
151             $sort_keys[$idx] = $contacts[$idx];
152         }
153         $contacts = array_values($sort_keys);
2f9fdb 154     }
938e96 155 }
T 156
017c4f 157 $OUTPUT->command('ksearch_query_results', $contacts, $search, $reqid);
938e96 158 $OUTPUT->send();