| 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-2010, Roundcube Dev Team | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 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 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | if ($RCMAIL->action == 'group-expand') { |
|---|
| 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(); |
|---|
| 26 | $abook->set_group($gid); |
|---|
| 27 | $abook->set_pagesize(1000); // TODO: limit number of group members by config |
|---|
| 28 | $result = $abook->list_records(array('email','name')); |
|---|
| 29 | while ($result && ($sql_arr = $result->iterate())) { |
|---|
| 30 | foreach ((array)$sql_arr['email'] as $email) |
|---|
| 31 | $members[] = format_email_recipient($email, $sql_arr['name']); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members)); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | $OUTPUT->send(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 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)) { |
|---|
| 52 | $contacts = array(); |
|---|
| 53 | $books_num = count($book_types); |
|---|
| 54 | |
|---|
| 55 | foreach ($book_types as $id) { |
|---|
| 56 | $abook = $RCMAIL->get_address_book($id); |
|---|
| 57 | $abook->set_pagesize($MAXNUM); |
|---|
| 58 | |
|---|
| 59 | if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) { |
|---|
| 60 | while ($sql_arr = $result->iterate()) { |
|---|
| 61 | // Contact can have more than one e-mail address |
|---|
| 62 | $email_arr = (array)$abook->get_col_values('email', $sql_arr, true); |
|---|
| 63 | $email_cnt = count($email_arr); |
|---|
| 64 | foreach ($email_arr as $email) { |
|---|
| 65 | if (empty($email)) |
|---|
| 66 | continue; |
|---|
| 67 | $contact = format_email_recipient($email, $sql_arr['name']); |
|---|
| 68 | // skip entries that don't match |
|---|
| 69 | if ($email_cnt > 1 && stripos($contact, $search) === false) { |
|---|
| 70 | continue; |
|---|
| 71 | } |
|---|
| 72 | // when we've got more than one book, we need to skip duplicates |
|---|
| 73 | if ($books_num == 1 || !in_array($contact, $contacts)) { |
|---|
| 74 | $contacts[] = $contact; |
|---|
| 75 | if (count($contacts) >= $MAXNUM) |
|---|
| 76 | break 2; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // also list matching contact groups |
|---|
| 83 | if ($abook->groups) { |
|---|
| 84 | foreach ($abook->list_groups($search) as $group) { |
|---|
| 85 | $abook->reset(); |
|---|
| 86 | $abook->set_group($group['ID']); |
|---|
| 87 | $result = $abook->count(); |
|---|
| 88 | |
|---|
| 89 | if ($result->count) { |
|---|
| 90 | $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id); |
|---|
| 91 | if (count($contacts) >= $MAXNUM) |
|---|
| 92 | break; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | usort($contacts, 'contact_results_sort'); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid); |
|---|
| 102 | $OUTPUT->send(); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 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; |
|---|
| 109 | return strcoll(trim($name_a, '" '), trim($name_b, '" ')); |
|---|
| 110 | } |
|---|
| 111 | |
|---|