source: github/program/steps/mail/autocomplete.inc @ 930d385

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 930d385 was 930d385, checked in by thomascube <thomas@…>, 2 years ago

Skip empty e-mail addresses in autocompletion

  • Property mode set to 100644
File size: 3.6 KB
Line 
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$MAXNUM = 15;
23$book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
24
25if ($RCMAIL->action == 'group-expand') {
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();
29    $abook->set_group($gid);
30    $abook->set_pagesize(1000);  // TODO: limit number of group members by config
31    $result = $abook->list_records(array('email','name'));
32    while ($result && ($sql_arr = $result->iterate())) {
33      foreach ((array)$sql_arr['email'] as $email)
34        $members[] = format_email_recipient($email, $sql_arr['name']);
35    }
36
37    $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members));
38  }
39}
40else if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) {
41  $contacts = array();
42  $books_num = count($book_types);
43
44  foreach ($book_types as $id) {
45    $abook = $RCMAIL->get_address_book($id);
46    $abook->set_pagesize($MAXNUM);
47
48    if ($result = $abook->search(array('email','name'), $search, false, true, true, 'email')) {
49      while ($sql_arr = $result->iterate()) {
50        // Contact can have more than one e-mail address
51        $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
52        $email_cnt = count($email_arr);
53        foreach ($email_arr as $email) {
54          if (empty($email))
55            continue;
56          $contact = format_email_recipient($email, $sql_arr['name']);
57          // skip entries that don't match
58          if ($email_cnt > 1 && stripos($contact, $search) === false) {
59            continue;
60          }
61          // when we've got more than one book, we need to skip duplicates
62          if ($books_num == 1 || !in_array($contact, $contacts)) {
63            $contacts[] = $contact;
64            if (count($contacts) >= $MAXNUM)
65              break 2;
66          }
67        }
68      }
69    }
70
71    // also list matching contact groups
72    if ($abook->groups) {
73      foreach ($abook->list_groups($search) as $group) {
74        $abook->reset();
75        $abook->set_group($group['ID']);
76        $result = $abook->count();
77
78        if ($result->count) {
79          $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
80          if (count($contacts) >= $MAXNUM)
81            break;
82        }
83      }
84    }
85  }
86
87  usort($contacts, 'contact_results_sort');
88}
89
90$OUTPUT->command('ksearch_query_results', $contacts, $search);
91$OUTPUT->send();
92
93
94function contact_results_sort($a, $b)
95{
96  $name_a = is_array($a) ? $a['name'] : $a;
97  $name_b = is_array($b) ? $b['name'] : $b;
98  return strcoll(trim($name_a, '" '), trim($name_b, '" '));
99}
100
Note: See TracBrowser for help on using the repository browser.