source: github/program/steps/addressbook/search.inc @ 0203f16

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since 0203f16 was 0203f16, checked in by alecpl <alec@…>, 16 months ago
  • Fix duplicate names handling in addressbook searches (#1488375)
  • Property mode set to 100644
File size: 11.0 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/search.inc                                  |
6 |                                                                       |
7 | This file is part of the Roundcube Webmail client                     |
8 | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
9 | Copyright (C) 2011, Kolab Systems AG                                  |
10 |                                                                       |
11 | Licensed under the GNU General Public License version 3 or            |
12 | any later version with exceptions for skins & plugins.                |
13 | See the README file for a full license statement.                     |
14 |                                                                       |
15 | PURPOSE:                                                              |
16 |   Search action (and form) for address book contacts                  |
17 |                                                                       |
18 +-----------------------------------------------------------------------+
19 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
20 | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
21 +-----------------------------------------------------------------------+
22
23 $Id: search.inc 456 2007-01-10 12:34:33Z thomasb $
24
25*/
26
27if ($RCMAIL->action == 'search-create') {
28    $id   = get_input_value('_search', RCUBE_INPUT_POST);
29    $name = get_input_value('_name', RCUBE_INPUT_POST, true);
30
31    if (($params = $_SESSION['search_params']) && $params['id'] == $id) {
32
33        $data = array(
34            'type' => rcube_user::SEARCH_ADDRESSBOOK,
35            'name' => $name,
36            'data' => array(
37                'fields' => $params['data'][0],
38                'search' => $params['data'][1],
39            ),
40        );
41
42        $plugin = $RCMAIL->plugins->exec_hook('saved_search_create', array('data' => $data));
43
44        if (!$plugin['abort'])
45            $result = $RCMAIL->user->insert_search($plugin['data']);
46        else
47            $result = $plugin['result'];
48    }
49
50    if ($result) {
51        $OUTPUT->show_message('savedsearchcreated', 'confirmation');
52        $OUTPUT->command('insert_saved_search', Q($name), Q($result));
53    }
54    else
55        $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchcreateerror', 'error');
56
57    $OUTPUT->send();
58}
59
60if ($RCMAIL->action == 'search-delete') {
61    $id = get_input_value('_sid', RCUBE_INPUT_POST);
62
63    $plugin = $RCMAIL->plugins->exec_hook('saved_search_delete', array('id' => $id));
64
65    if (!$plugin['abort'])
66        $result = $RCMAIL->user->delete_search($id);
67    else
68        $result = $plugin['result'];
69
70    if ($result) {
71        $OUTPUT->show_message('savedsearchdeleted', 'confirmation');
72        $OUTPUT->command('remove_search_item', Q($id));
73        // contact list will be cleared, clear also page counter
74        $OUTPUT->command('set_rowcount', rcube_label('nocontactsfound'));
75        $OUTPUT->set_env('pagecount', 0);
76    }
77    else
78        $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchdeleteerror', 'error');
79
80    $OUTPUT->send();
81}
82
83
84if (!isset($_GET['_form'])) {
85    rcmail_contact_search();
86}
87
88$OUTPUT->add_handler('searchform', 'rcmail_contact_search_form');
89$OUTPUT->send('contactsearch');
90
91
92function rcmail_contact_search()
93{
94    global $RCMAIL, $OUTPUT, $SEARCH_MODS_DEFAULT, $PAGE_SIZE;
95
96    $adv = isset($_POST['_adv']);
97    $sid = get_input_value('_sid', RCUBE_INPUT_GET);
98
99    // get search criteria from saved search
100    if ($sid && ($search = $RCMAIL->user->get_search($sid))) {
101        $fields = $search['data']['fields'];
102        $search = $search['data']['search'];
103    }
104    // get fields/values from advanced search form
105    else if ($adv) {
106        foreach (array_keys($_POST) as $key) {
107            $s = trim(get_input_value($key, RCUBE_INPUT_POST, true));
108            if (strlen($s) && preg_match('/^_search_([a-zA-Z0-9_-]+)$/', $key, $m)) {
109                $search[] = $s;
110                $fields[] = $m[1];
111            }
112        }
113
114        if (empty($fields)) {
115            // do nothing, show the form again
116            return;
117        }
118    }
119    // quick-search
120    else {
121        $search = trim(get_input_value('_q', RCUBE_INPUT_GET, true));
122        $fields = explode(',', get_input_value('_headers', RCUBE_INPUT_GET));
123
124        if (empty($fields)) {
125            $fields = array_keys($SEARCH_MODS_DEFAULT);
126        }
127        else {
128            $fields = array_filter($fields);
129        }
130
131        // update search_mods setting
132        $old_mods = $RCMAIL->config->get('addressbook_search_mods');
133        $search_mods = array_fill_keys($fields, 1);
134        if ($old_mods != $search_mods) {
135            $RCMAIL->user->save_prefs(array('addressbook_search_mods' => $search_mods));
136        }
137
138        if (in_array('*', $fields)) {
139            $fields = '*';
140        }
141    }
142
143    // Values matching mode
144    $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
145
146    // get sources list
147    $sources    = $RCMAIL->get_address_sources();
148    $search_set = array();
149    $records    = array();
150    $sort_col   = $RCMAIL->config->get('addressbook_sort_col', 'name');
151
152    foreach ($sources as $s) {
153        $source = $RCMAIL->get_address_book($s['id']);
154
155        // check if search fields are supported....
156        if (is_array($fields)) {
157            $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
158            $supported = 0;
159
160            foreach ($fields as $f) {
161                if (array_key_exists($f, $cols)) {
162                    $supported ++;
163                }
164            }
165
166            // in advanced search we require all fields (AND operator)
167            // in quick search we require at least one field (OR operator)
168            if (($adv && $supported < count($fields)) || (!$adv && !$supported)) {
169                continue;
170            }
171        }
172
173        // reset page
174        $source->set_page(1);
175        $source->set_pagesize(9999);
176
177        // get contacts count
178        $result = $source->search($fields, $search, $mode, false);
179
180        if (!$result->count) {
181            continue;
182        }
183
184        // get records
185        $result = $source->list_records(array('name', 'email'));
186
187        while ($row = $result->next()) {
188            $row['sourceid'] = $s['id'];
189            $key = rcmail_contact_key($row, $sort_col);
190            $records[$key] = $row;
191        }
192
193        unset($result);
194        $search_set[$s['id']] = $source->get_search_set();
195    }
196
197    // sort the records
198    ksort($records, SORT_LOCALE_STRING);
199
200    // create resultset object
201    $count  = count($records);
202    $result = new rcube_result_set($count);
203
204    // cut first-page records
205    if ($PAGE_SIZE < $count) {
206        $records = array_slice($records, 0, $PAGE_SIZE);
207    }
208
209    $result->records = array_values($records);
210
211    // search request ID
212    $search_request = md5('addr'
213        .(is_array($fields) ? implode($fields, ',') : $fields)
214        .(is_array($search) ? implode($search, ',') : $search));
215
216    // save search settings in session
217    $_SESSION['search'][$search_request] = $search_set;
218    $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($fields, $search));
219    $_SESSION['page'] = 1;
220
221    if ($adv)
222        $OUTPUT->command('list_contacts_clear');
223
224    if ($result->count > 0) {
225        // create javascript list
226        rcmail_js_contacts_list($result);
227        $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
228    }
229    else {
230        $OUTPUT->show_message('nocontactsfound', 'notice');
231    }
232
233    // update message count display
234    $OUTPUT->command('set_env', 'search_request', $search_request);
235    $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $PAGE_SIZE));
236    $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
237    // Re-set current source
238    $OUTPUT->command('set_env', 'search_id', $sid);
239    $OUTPUT->command('set_env', 'source', '');
240    $OUTPUT->command('set_env', 'group', '');
241
242    // unselect currently selected directory/group
243    if (!$sid)
244        $OUTPUT->command('unselect_directory');
245    $OUTPUT->command('update_group_commands');
246
247    // send response
248    $OUTPUT->send($adv ? 'iframe' : null);
249}
250
251function rcmail_contact_search_form($attrib)
252{
253    global $RCMAIL, $CONTACT_COLTYPES;
254
255    $i_size = !empty($attrib['size']) ? $attrib['size'] : 30;
256
257    $form = array(
258        'main' => array(
259            'name'    => rcube_label('properties'),
260            'content' => array(
261            ),
262        ),
263        'personal' => array(
264            'name'    => rcube_label('personalinfo'),
265            'content' => array(
266            ),
267        ),
268        'other' => array(
269            'name'    => rcube_label('other'),
270            'content' => array(
271            ),
272        ),
273    );
274
275    // get supported coltypes from all address sources
276    $sources  = $RCMAIL->get_address_sources();
277    $coltypes = array();
278
279    foreach ($sources as $s) {
280        $CONTACTS = $RCMAIL->get_address_book($s['id']);
281
282        if (is_array($CONTACTS->coltypes)) {
283            $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
284            $coltypes = array_merge($coltypes, $contact_cols);
285        }
286    }
287
288    // merge supported coltypes with $CONTACT_COLTYPES
289    foreach ($coltypes as $col => $colprop) {
290        $coltypes[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], (array)$colprop) : (array)$colprop;
291    }
292
293    // build form fields list
294    foreach ($coltypes as $col => $colprop)
295    {
296        if ($colprop['type'] != 'image' && !$colprop['nosearch'])
297        {
298            $ftype    = $colprop['type'] == 'select' ? 'select' : 'text';
299            $label    = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
300            $category = $colprop['category'] ? $colprop['category'] : 'other';
301
302            if ($ftype == 'text')
303                $colprop['size'] = $i_size;
304
305            $content  = html::div('row', html::div('contactfieldlabel label', Q($label))
306                . html::div('contactfieldcontent', rcmail_get_edit_field('search_'.$col, '', $colprop, $ftype)));
307
308            $form[$category]['content'][] = $content;
309        }
310    }
311
312    $hiddenfields = new html_hiddenfield();
313    $hiddenfields->add(array('name' => '_adv', 'value' => 1));
314
315    $out = $RCMAIL->output->request_form(array(
316        'name' => 'form', 'method' => 'post',
317        'task' => $RCMAIL->task, 'action' => 'search',
318        'noclose' => true) + $attrib, $hiddenfields->show());
319
320    $RCMAIL->output->add_gui_object('editform', $attrib['id']);
321
322    unset($attrib['name']);
323    unset($attrib['id']);
324
325    foreach ($form as $f) {
326        if (!empty($f['content'])) {
327            $content = html::div('contactfieldgroup', join("\n", $f['content']));
328
329            $out .= html::tag('fieldset', $attrib,
330                html::tag('legend', null, Q($f['name']))
331                . $content) . "\n";
332        }
333    }
334
335    return $out . '</form>';
336}
Note: See TracBrowser for help on using the repository browser.