source: github/program/steps/mail/autocomplete.inc @ 710b1bd

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since 710b1bd was 710b1bd, checked in by alecpl <alec@…>, 20 months ago
  • Add option to skip alternative email addresses in autocompletion
  • Property mode set to 100644
File size: 4.7 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-2011, Roundcube Dev Team                           |
9 | Copyright (C) 2011, Kolab Systems AG                                  |
10 | Licensed under the GNU GPL                                            |
11 |                                                                       |
12 | PURPOSE:                                                              |
13 |   Perform a search on configured address books for the address        |
14 |   autocompletion of the message compose screen                        |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17 +-----------------------------------------------------------------------+
18
19 $Id$
20
21*/
22
23if ($RCMAIL->action == 'group-expand') {
24  $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC));
25  if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) {
26    $members = array();
27    $abook->set_group($gid);
28    $abook->set_pagesize(1000);  // TODO: limit number of group members by config
29    $result = $abook->list_records(array('email','name'));
30    while ($result && ($sql_arr = $result->iterate())) {
31      foreach ((array)$sql_arr['email'] as $email)
32        $members[] = format_email_recipient($email, $sql_arr['name']);
33    }
34
35    $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
36    $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
37  }
38
39  $OUTPUT->send();
40}
41
42
43$MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
44$mode   = (int) $RCMAIL->config->get('addressbook_search_mode');
45$single = (bool) $RCMAIL->config->get('autocomplete_single');
46$search = get_input_value('_search', RCUBE_INPUT_GPC, true);
47$source = get_input_value('_source', RCUBE_INPUT_GPC);
48$sid    = get_input_value('_id', RCUBE_INPUT_GPC);
49
50if (strlen($source))
51  $book_types = array($source);
52else
53  $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
54
55if (!empty($book_types) && strlen($search)) {
56  $contacts  = array();
57  $books_num = count($book_types);
58  $search_lc = mb_strtolower($search);
59
60  foreach ($book_types as $id) {
61    $abook = $RCMAIL->get_address_book($id);
62    $abook->set_pagesize($MAXNUM);
63
64    if ($result = $abook->search(array('email','name'), $search, $mode, true, true, 'email')) {
65      while ($sql_arr = $result->iterate()) {
66        // Contact can have more than one e-mail address
67        $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
68        $email_cnt = count($email_arr);
69        foreach ($email_arr as $email) {
70          if (empty($email)) {
71            continue;
72          }
73
74          $contact = format_email_recipient($email, $sql_arr['name']);
75
76          // skip entries that don't match
77          if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
78            continue;
79          }
80
81          // skip duplicates
82          if (!in_array($contact, $contacts)) {
83            $contacts[] = $contact;
84            if (count($contacts) >= $MAXNUM)
85              break 2;
86          }
87
88          // skip redundant entries (show only first email address)
89          if ($single) {
90            break;
91          }
92        }
93      }
94    }
95
96    // also list matching contact groups
97    if ($abook->groups && count($contacts) < $MAXNUM) {
98      foreach ($abook->list_groups($search) as $group) {
99        $abook->reset();
100        $abook->set_group($group['ID']);
101        $group_prop = $abook->get_group($group['ID']);
102
103        // group (distribution list) with email address(es)
104        if ($group_prop['email']) {
105            foreach ((array)$group_prop['email'] as $email) {
106                $contacts[] = format_email_recipient($email, $group['name']);
107                if (count($contacts) >= $MAXNUM)
108                  break 2;
109            }
110        }
111        // show group with count
112        else if (($result = $abook->count()) && $result->count) {
113          $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id);
114          if (count($contacts) >= $MAXNUM)
115            break;
116        }
117      }
118    }
119  }
120
121  usort($contacts, 'contact_results_sort');
122}
123
124$OUTPUT->command('ksearch_query_results', $contacts, $search, $sid);
125$OUTPUT->send();
126
127
128function contact_results_sort($a, $b)
129{
130  $name_a = is_array($a) ? $a['name'] : $a;
131  $name_b = is_array($b) ? $b['name'] : $b;
132  return strcoll(trim($name_a, '" '), trim($name_b, '" '));
133}
134
Note: See TracBrowser for help on using the repository browser.