thomascube
2010-06-03 05a631a43c1950fc99f817cb50e4184dc0696663
commit | author | age
938e96 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/autocomplete.inc                                   |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
a61bbb 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'));
32     while ($result && ($sql_arr = $result->iterate()))
33       $members[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
25fdec 34
c0297f 35     $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
T 36   }
37 }
38 else if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) {
39   $contacts = array();
938e96 40
ebf96e 41   foreach ($book_types as $id) {
938e96 42     $abook = $RCMAIL->get_address_book($id);
1553b3 43     $abook->set_pagesize($MAXNUM);
938e96 44
25fdec 45     if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
938e96 46       while ($sql_arr = $result->iterate()) {
T 47           $contacts[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
a61bbb 48           if (count($contacts) >= $MAXNUM)
80e227 49             break 2;
a61bbb 50       }
T 51     }
25fdec 52
a61bbb 53     // also list matching contact groups
T 54     if ($abook->groups) {
55       foreach ($abook->list_groups($search) as $group) {
56         $abook->reset();
57         $abook->set_group($group['ID']);
c0297f 58         $result = $abook->count();
25fdec 59
c0297f 60         if ($result->count) {
T 61           $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
a61bbb 62           if (count($contacts) >= $MAXNUM)
T 63             break;
64         }
938e96 65       }
T 66     }
67   }
25fdec 68
a61bbb 69   usort($contacts, 'contact_results_sort');
938e96 70 }
T 71
aaffbe 72 $OUTPUT->command('ksearch_query_results', $contacts, $search);
938e96 73 $OUTPUT->send();
T 74
a61bbb 75
T 76 function contact_results_sort($a, $b)
77 {
78   $name_a = is_array($a) ? $a['name'] : $a;
79   $name_b = is_array($b) ? $b['name'] : $b;
1ac779 80   return strcoll(trim($name_a, '" '), trim($name_b, '" '));
a61bbb 81 }
T 82
92f522 83 ?>