You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
roundcubemail/program/steps/mail/autocomplete.inc

198 lines
8.0 KiB
PHTML

<?php
10 years ago
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Perform a search on configured address books for the address |
| autocompletion of the message compose screen |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
if ($RCMAIL->action == 'group-expand') {
11 years ago
$abook = $RCMAIL->get_address_book(rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC));
if ($gid = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GET)) {
11 years ago
$abook->set_group($gid);
$abook->set_pagesize(9999); // TODO: limit number of group members by config?
11 years ago
$result = $abook->list_records($RCMAIL->config->get('contactlist_fields'));
$members = array();
11 years ago
while ($result && ($record = $result->iterate())) {
$emails = (array) $abook->get_col_values('email', $record, true);
11 years ago
if (!empty($emails) && ($email = array_shift($emails))) {
$members[] = format_email_recipient($email, rcube_addressbook::compose_list_name($record));
11 years ago
}
}
$OUTPUT->command('replace_group_recipients', $gid, join(', ', array_unique($members)));
11 years ago
}
11 years ago
$OUTPUT->send();
}
$MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
$mode = (int) $RCMAIL->config->get('addressbook_search_mode');
$single = (bool) $RCMAIL->config->get('autocomplete_single');
$search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
$source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
$reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
11 years ago
if (strlen($source)) {
$book_types = array($source);
}
else {
$book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
}
$contacts = array();
if (!empty($book_types) && strlen($search)) {
11 years ago
$sort_keys = array();
$books_num = count($book_types);
$search_lc = mb_strtolower($search);
$mode |= rcube_addressbook::SEARCH_GROUPS;
11 years ago
foreach ($book_types as $abook_id) {
$abook = $RCMAIL->get_address_book($abook_id);
11 years ago
$abook->set_pagesize($MAXNUM);
if ($result = $abook->search($RCMAIL->config->get('contactlist_fields'), $search, $mode, true, true, 'email')) {
while ($record = $result->iterate()) {
11 years ago
// Contact can have more than one e-mail address
$email_arr = (array)$abook->get_col_values('email', $record, true);
11 years ago
$email_cnt = count($email_arr);
$idx = 0;
foreach ($email_arr as $email) {
if (empty($email)) {
continue;
}
$name = rcube_addressbook::compose_list_name($record);
11 years ago
$contact = format_email_recipient($email, $name);
// skip entries that don't match
if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
continue;
}
$index = $contact;
11 years ago
// skip duplicates
if (empty($contacts[$index])) {
$contact = array(
'name' => $contact,
'type' => $record['_type'],
'id' => $record['ID'],
'source' => $abook_id,
);
if (($display = rcube_addressbook::compose_search_name($record, $email, $name)) && $display != $contact['name']) {
$contact['display'] = $display;
}
// groups with defined email address will not be expanded to its members' addresses
if ($record['_type'] == 'group') {
$contact['email'] = $email;
}
$contacts[$index] = $contact;
$sort_keys[$index] = sprintf('%s %03d', $contact['display'] ?: $name, $idx++);
11 years ago
if (count($contacts) >= $MAXNUM) {
break 2;
}
}
// skip redundant entries (show only first email address)
if ($single) {
break;
}
}
}
}
11 years ago
// also list matching contact groups
if ($abook->groups && count($contacts) < $MAXNUM) {
foreach ($abook->list_groups($search, $mode) as $group) {
$abook->reset();
$abook->set_group($group['ID']);
$group_prop = $abook->get_group($group['ID']);
// group (distribution list) with email address(es)
if ($group_prop['email']) {
$idx = 0;
foreach ((array)$group_prop['email'] as $email) {
$index = format_email_recipient($email, $group['name']);
if (empty($contacts[$index])) {
$sort_keys[$index] = sprintf('%s %03d', $group['name'] , $idx++);
$contacts[$index] = array(
'name' => $index,
'email' => $email,
'type' => 'group',
'id' => $group['ID'],
'source' => $abook_id,
);
if (count($contacts) >= $MAXNUM) {
break 2;
}
}
}
}
// show group with count
else if (($result = $abook->count()) && $result->count) {
if (empty($contacts[$group['name']])) {
$sort_keys[$group['name']] = $group['name'];
$contacts[$group['name']] = array(
'name' => $group['name'] . ' (' . intval($result->count) . ')',
'type' => 'group',
'id' => $group['ID'],
'source' => $abook_id,
);
11 years ago
if (count($contacts) >= $MAXNUM) {
break;
11 years ago
}
}
}
}
}
11 years ago
}
11 years ago
if (count($contacts)) {
// sort contacts index
asort($sort_keys, SORT_LOCALE_STRING);
// re-sort contacts according to index
foreach ($sort_keys as $idx => $val) {
$sort_keys[$idx] = $contacts[$idx];
}
11 years ago
$contacts = array_values($sort_keys);
}
}
// Allow autocomplete result optimization via plugin
$pluginResult = $RCMAIL->plugins->exec_hook('contacts_autocomplete_after', array(
'search' => $search,
'contacts' => $contacts, // Provide already-found contacts to plugin if they are required
));
$contacts = $pluginResult['contacts'];
$OUTPUT->command('ksearch_query_results', $contacts, $search, $reqid);
$OUTPUT->send();