thomascube
2011-08-18 fbe54043cf598b19a753dc2b21a7ed558d23fd15
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                     |
A 8  | Copyright (C) 2008-2010, Roundcube Dev Team                           |
938e96 9  | Licensed under the GNU GPL                                            |
T 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
1b5f98 18  $Id$
938e96 19
T 20 */
21
c0297f 22 if ($RCMAIL->action == 'group-expand') {
T 23   $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC));
24   if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) {
25     $members = array();
53d626 26     $abook->set_group($gid);
c0297f 27     $abook->set_pagesize(1000);  // TODO: limit number of group members by config
T 28     $result = $abook->list_records(array('email','name'));
0501b6 29     while ($result && ($sql_arr = $result->iterate())) {
T 30       foreach ((array)$sql_arr['email'] as $email)
31         $members[] = format_email_recipient($email, $sql_arr['name']);
32     }
25fdec 33
c0297f 34     $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
T 35   }
0213f8 36
A 37   $OUTPUT->send();
c0297f 38 }
0213f8 39
A 40
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);
45
46 if (strlen($source))
47   $book_types = array($source);
48 else
49   $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
50
51 if (!empty($book_types) && strlen($search)) {
c0297f 52   $contacts = array();
dbe498 53   $books_num = count($book_types);
938e96 54
ebf96e 55   foreach ($book_types as $id) {
938e96 56     $abook = $RCMAIL->get_address_book($id);
1553b3 57     $abook->set_pagesize($MAXNUM);
938e96 58
25fdec 59     if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
938e96 60       while ($sql_arr = $result->iterate()) {
111a6f 61         // Contact can have more than one e-mail address
A 62         $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
63         $email_cnt = count($email_arr);
64         foreach ($email_arr as $email) {
930d38 65           if (empty($email))
T 66             continue;
0501b6 67           $contact = format_email_recipient($email, $sql_arr['name']);
111a6f 68           // skip entries that don't match
09c59a 69           if ($email_cnt > 1 && stripos($contact, $search) === false) {
111a6f 70             continue;
A 71           }
0501b6 72           // when we've got more than one book, we need to skip duplicates
T 73           if ($books_num == 1 || !in_array($contact, $contacts)) {
74             $contacts[] = $contact;
75             if (count($contacts) >= $MAXNUM)
76               break 2;
77           }
dbe498 78         }
a61bbb 79       }
T 80     }
25fdec 81
a61bbb 82     // also list matching contact groups
T 83     if ($abook->groups) {
84       foreach ($abook->list_groups($search) as $group) {
85         $abook->reset();
86         $abook->set_group($group['ID']);
c0297f 87         $result = $abook->count();
25fdec 88
c0297f 89         if ($result->count) {
T 90           $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
a61bbb 91           if (count($contacts) >= $MAXNUM)
T 92             break;
93         }
938e96 94       }
T 95     }
96   }
25fdec 97
a61bbb 98   usort($contacts, 'contact_results_sort');
938e96 99 }
T 100
0213f8 101 $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);
938e96 102 $OUTPUT->send();
T 103
a61bbb 104
T 105 function contact_results_sort($a, $b)
106 {
107   $name_a = is_array($a) ? $a['name'] : $a;
108   $name_b = is_array($b) ? $b['name'] : $b;
1ac779 109   return strcoll(trim($name_a, '" '), trim($name_b, '" '));
a61bbb 110 }
T 111