]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/search.inc
352556de0e4d9ee5c8dea96a9555a5a6e33b6950
[roundcube.git] / program / steps / addressbook / search.inc
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  | Licensed under the GNU GPL                                            |
11  |                                                                       |
12  | PURPOSE:                                                              |
13  |   Search action (and form) for address book contacts                  |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
18  +-----------------------------------------------------------------------+
19
20  $Id: search.inc 456 2007-01-10 12:34:33Z thomasb $
21
22 */
23
24 if (!isset($_GET['_form'])) {
25     rcmail_contact_search();
26 }
27
28 $OUTPUT->add_handler('searchform', 'rcmail_contact_search_form');
29 $OUTPUT->send('contactsearch');
30
31
32 function rcmail_contact_search()
33 {
34     global $RCMAIL, $OUTPUT, $CONFIG, $SEARCH_MODS_DEFAULT;
35
36     $adv = isset($_POST['_adv']);
37
38     // get fields/values from advanced search form
39     if ($adv) {
40         foreach (array_keys($_POST) as $key) {
41             $s = trim(get_input_value($key, RCUBE_INPUT_POST, true));
42             if (strlen($s) && preg_match('/^_search_([a-zA-Z0-9_-]+)$/', $key, $m)) {
43                 $search[] = $s;
44                 $fields[] = $m[1];
45             }
46         }
47
48         if (empty($fields)) {
49             // do nothing, show the form again
50             return;
51         }
52     }
53     // quick-search
54     else {
55         $search = trim(get_input_value('_q', RCUBE_INPUT_GET, true));
56         $fields = explode(',', get_input_value('_headers', RCUBE_INPUT_GET));
57
58         if (empty($fields)) {
59             $fields = array_keys($SEARCH_MODS_DEFAULT);
60         }
61         else {
62             $fields = array_filter($fields);
63         }
64
65         // update search_mods setting
66         $old_mods = $RCMAIL->config->get('addressbook_search_mods');
67         $search_mods = array_fill_keys($fields, 1);
68         if ($old_mods != $search_mods) {
69             $RCMAIL->user->save_prefs(array('addressbook_search_mods' => $search_mods));
70         }
71
72         if (in_array('*', $fields)) {
73             $fields = '*';
74         }
75     }
76
77     // get sources list
78     $sources    = $RCMAIL->get_address_sources();
79     $search_set = array();
80     $records    = array();
81
82     foreach ($sources as $s) {
83         $source = $RCMAIL->get_address_book($s['id']);
84
85         // check if search fields are supported....
86         if (is_array($fields)) {
87             $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
88             $supported = 0;
89
90             foreach ($fields as $f) {
91                 if (array_key_exists($f, $cols)) {
92                     $supported ++;
93                 }
94             }
95
96             // in advanced search we require all fields (AND operator)
97             // in quick search we require at least one field (OR operator)
98             if (($adv && $supported < count($fields)) || (!$adv && !$supported)) {
99                 continue;
100             }
101         }
102
103         // reset page
104         $source->set_page(1);
105         $source->set_pagesize(9999);
106
107         // get contacts count
108         $result = $source->search($fields, $search, false, false);
109
110         if (!$result->count) {
111             continue;
112         }
113
114         // get records
115         $result = $source->list_records(array('name', 'email'));
116
117         while ($row = $result->next()) {
118             $row['sourceid'] = $s['id'];
119             $key = $row['name'] . ':' . $row['sourceid'];
120             $records[$key] = $row;
121         }
122
123         unset($result);
124         $search_set[$s['id']] = $source->get_search_set();
125     }
126
127     // sort the records
128     ksort($records, SORT_LOCALE_STRING);
129
130     // create resultset object
131     $count  = count($records);
132     $result = new rcube_result_set($count);
133
134     // cut first-page records
135     if ($CONFIG['pagesize'] < $count) {
136         $records = array_slice($records, 0, $CONFIG['pagesize']);
137     }
138
139     $result->records = array_values($records);
140
141     // search request ID
142     $search_request = md5('addr'
143         .(is_array($fields) ? implode($fields, ',') : $fields)
144         .(is_array($search) ? implode($search, ',') : $search));
145
146     // save search settings in session
147     $_SESSION['search'][$search_request] = $search_set;
148     $_SESSION['page'] = 1;
149
150     if ($adv)
151         $OUTPUT->command('list_contacts_clear');
152
153     if ($result->count > 0) {
154         // create javascript list
155         rcmail_js_contacts_list($result);
156     }
157     else {
158         $OUTPUT->show_message('nocontactsfound', 'notice');
159     }
160
161     // update message count display
162     $OUTPUT->command('set_env', 'search_request', $search_request);
163     $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $CONFIG['pagesize']));
164     $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
165
166     // unselect currently selected directory/group
167     $OUTPUT->command('unselect_directory');
168     $OUTPUT->command('update_group_commands');
169
170     // send response
171     $OUTPUT->send($adv ? 'iframe' : null);
172 }
173
174 function rcmail_contact_search_form($attrib)
175 {
176     global $RCMAIL, $CONTACT_COLTYPES;
177
178     $i_size = !empty($attrib['size']) ? $attrib['size'] : 30;
179
180     $form = array(
181         'main' => array(
182             'name'    => rcube_label('contactproperties'),
183             'content' => array(
184             ),
185         ),
186         'personal' => array(
187             'name'    => rcube_label('personalinfo'),
188             'content' => array(
189             ),
190         ),
191         'other' => array(
192             'name'    => rcube_label('other'),
193             'content' => array(
194             ),
195         ),
196     );
197
198     // get supported coltypes from all address sources
199     $sources  = $RCMAIL->get_address_sources();
200     $coltypes = array();
201
202     foreach ($sources as $s) {
203         $CONTACTS = $RCMAIL->get_address_book($s['id']);
204
205         if (is_array($CONTACTS->coltypes)) {
206             $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
207             $coltypes = array_merge($coltypes, $contact_cols);
208         }
209     }
210
211     // merge supported coltypes with $CONTACT_COLTYPES
212     foreach ($coltypes as $col => $colprop) {
213         $coltypes[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], (array)$colprop) : (array)$colprop;
214     }
215
216     // build form fields list
217     foreach ($coltypes as $col => $colprop)
218     {
219         if ($colprop['type'] != 'image' && !$colprop['nosearch'])
220         {
221             $ftype    = $colprop['type'] == 'select' ? 'select' : 'text';
222             $label    = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
223             $category = $colprop['category'] ? $colprop['category'] : 'other';
224
225             if ($ftype == 'text')
226                 $colprop['size'] = $i_size;
227
228             $content  = html::div('row', html::div('contactfieldlabel label', Q($label))
229                 . html::div('contactfieldcontent', rcmail_get_edit_field('search_'.$col, '', $colprop, $ftype)));
230
231             $form[$category]['content'][] = $content;
232         }
233     }
234
235     $hiddenfields = new html_hiddenfield();
236     $hiddenfields->add(array('name' => '_adv', 'value' => 1));
237
238     $out = $RCMAIL->output->request_form(array(
239         'name' => 'form', 'method' => 'post',
240         'task' => $RCMAIL->task, 'action' => 'search',
241         'noclose' => true) + $attrib, $hiddenfields->show());
242
243     $RCMAIL->output->add_gui_object('editform', $attrib['id']);
244
245     unset($attrib['name']);
246     unset($attrib['id']);
247
248     foreach ($form as $f) {
249         if (!empty($f['content'])) {
250             $content = html::div('contactfieldgroup', join("\n", $f['content']));
251
252             $out .= html::tag('fieldset', $attrib,
253                 html::tag('legend', null, Q($f['name']))
254                 . $content) . "\n";
255         }
256     }
257
258     return $out . '</form>';
259 }