thomascube
2011-04-12 3d8b54edf74792e3996d861a6a30c41d82976261
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
80e227 22 $MAXNUM = 15;
ebf96e 23 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
938e96 24
c0297f 25 if ($RCMAIL->action == 'group-expand') {
T 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();
53d626 29     $abook->set_group($gid);
c0297f 30     $abook->set_pagesize(1000);  // TODO: limit number of group members by config
T 31     $result = $abook->list_records(array('email','name'));
0501b6 32     while ($result && ($sql_arr = $result->iterate())) {
T 33       foreach ((array)$sql_arr['email'] as $email)
34         $members[] = format_email_recipient($email, $sql_arr['name']);
35     }
25fdec 36
c0297f 37     $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
T 38   }
39 }
40 else if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) {
41   $contacts = array();
dbe498 42   $books_num = count($book_types);
938e96 43
ebf96e 44   foreach ($book_types as $id) {
938e96 45     $abook = $RCMAIL->get_address_book($id);
1553b3 46     $abook->set_pagesize($MAXNUM);
938e96 47
25fdec 48     if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
938e96 49       while ($sql_arr = $result->iterate()) {
111a6f 50         // Contact can have more than one e-mail address
A 51         $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
52         $email_cnt = count($email_arr);
53         foreach ($email_arr as $email) {
0501b6 54           $contact = format_email_recipient($email, $sql_arr['name']);
111a6f 55           // skip entries that don't match
A 56           if ($email_cnt > 1 && strpos($contact, $search) === false) {
57             continue;
58           }
0501b6 59           // when we've got more than one book, we need to skip duplicates
T 60           if ($books_num == 1 || !in_array($contact, $contacts)) {
61             $contacts[] = $contact;
62             if (count($contacts) >= $MAXNUM)
63               break 2;
64           }
dbe498 65         }
a61bbb 66       }
T 67     }
25fdec 68
a61bbb 69     // also list matching contact groups
T 70     if ($abook->groups) {
71       foreach ($abook->list_groups($search) as $group) {
72         $abook->reset();
73         $abook->set_group($group['ID']);
c0297f 74         $result = $abook->count();
25fdec 75
c0297f 76         if ($result->count) {
T 77           $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
a61bbb 78           if (count($contacts) >= $MAXNUM)
T 79             break;
80         }
938e96 81       }
T 82     }
83   }
25fdec 84
a61bbb 85   usort($contacts, 'contact_results_sort');
938e96 86 }
T 87
aaffbe 88 $OUTPUT->command('ksearch_query_results', $contacts, $search);
938e96 89 $OUTPUT->send();
T 90
a61bbb 91
T 92 function contact_results_sort($a, $b)
93 {
94   $name_a = is_array($a) ? $a['name'] : $a;
95   $name_b = is_array($b) ? $b['name'] : $b;
1ac779 96   return strcoll(trim($name_a, '" '), trim($name_b, '" '));
a61bbb 97 }
T 98