4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/search.inc |
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 |
13 | Search action (and form) for address book contacts |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 | Author: Aleksander Machniak <machniak@kolabsys.com> |
18 +-----------------------------------------------------------------------+
20 $Id: search.inc 456 2007-01-10 12:34:33Z thomasb $
24 if ($RCMAIL->action == 'search-create') {
25 $id = get_input_value('_search', RCUBE_INPUT_POST);
26 $name = get_input_value('_name', RCUBE_INPUT_POST, true);
28 if (($params = $_SESSION['search_params']) && $params['id'] == $id) {
31 'type' => rcube_user::SEARCH_ADDRESSBOOK,
34 'fields' => $params['data'][0],
35 'search' => $params['data'][1],
39 $plugin = $RCMAIL->plugins->exec_hook('saved_search_create', array('data' => $data));
41 if (!$plugin['abort'])
42 $result = $RCMAIL->user->insert_search($plugin['data']);
44 $result = $plugin['result'];
48 $OUTPUT->show_message('savedsearchcreated', 'confirmation');
49 $OUTPUT->command('insert_saved_search', Q($name), Q($result));
52 $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchcreateerror', 'error');
57 if ($RCMAIL->action == 'search-delete') {
58 $id = get_input_value('_sid', RCUBE_INPUT_POST);
60 $plugin = $RCMAIL->plugins->exec_hook('saved_search_delete', array('id' => $id));
62 if (!$plugin['abort'])
63 $result = $RCMAIL->user->delete_search($id);
65 $result = $plugin['result'];
68 $OUTPUT->show_message('savedsearchdeleted', 'confirmation');
69 $OUTPUT->command('remove_search_item', Q($id));
70 // contact list will be cleared, clear also page counter
71 $OUTPUT->command('set_rowcount', rcube_label('nocontactsfound'));
72 $OUTPUT->set_env('pagecount', 0);
75 $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'savedsearchdeleteerror', 'error');
81 if (!isset($_GET['_form'])) {
82 rcmail_contact_search();
85 $OUTPUT->add_handler('searchform', 'rcmail_contact_search_form');
86 $OUTPUT->send('contactsearch');
89 function rcmail_contact_search()
91 global $RCMAIL, $OUTPUT, $CONFIG, $SEARCH_MODS_DEFAULT;
93 $adv = isset($_POST['_adv']);
94 $sid = get_input_value('_sid', RCUBE_INPUT_GET);
96 // get search criteria from saved search
97 if ($sid && ($search = $RCMAIL->user->get_search($sid))) {
98 $fields = $search['data']['fields'];
99 $search = $search['data']['search'];
101 // get fields/values from advanced search form
103 foreach (array_keys($_POST) as $key) {
104 $s = trim(get_input_value($key, RCUBE_INPUT_POST, true));
105 if (strlen($s) && preg_match('/^_search_([a-zA-Z0-9_-]+)$/', $key, $m)) {
111 if (empty($fields)) {
112 // do nothing, show the form again
118 $search = trim(get_input_value('_q', RCUBE_INPUT_GET, true));
119 $fields = explode(',', get_input_value('_headers', RCUBE_INPUT_GET));
121 if (empty($fields)) {
122 $fields = array_keys($SEARCH_MODS_DEFAULT);
125 $fields = array_filter($fields);
128 // update search_mods setting
129 $old_mods = $RCMAIL->config->get('addressbook_search_mods');
130 $search_mods = array_fill_keys($fields, 1);
131 if ($old_mods != $search_mods) {
132 $RCMAIL->user->save_prefs(array('addressbook_search_mods' => $search_mods));
135 if (in_array('*', $fields)) {
140 // Values matching mode
141 $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
144 $sources = $RCMAIL->get_address_sources();
145 $search_set = array();
148 foreach ($sources as $s) {
149 $source = $RCMAIL->get_address_book($s['id']);
151 // check if search fields are supported....
152 if (is_array($fields)) {
153 $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
156 foreach ($fields as $f) {
157 if (array_key_exists($f, $cols)) {
162 // in advanced search we require all fields (AND operator)
163 // in quick search we require at least one field (OR operator)
164 if (($adv && $supported < count($fields)) || (!$adv && !$supported)) {
170 $source->set_page(1);
171 $source->set_pagesize(9999);
173 // get contacts count
174 $result = $source->search($fields, $search, $mode, false);
176 if (!$result->count) {
181 $result = $source->list_records(array('name', 'email'));
183 while ($row = $result->next()) {
184 $row['sourceid'] = $s['id'];
185 $key = $row['name'] . ':' . $row['sourceid'];
186 $records[$key] = $row;
190 $search_set[$s['id']] = $source->get_search_set();
194 ksort($records, SORT_LOCALE_STRING);
196 // create resultset object
197 $count = count($records);
198 $result = new rcube_result_set($count);
200 // cut first-page records
201 if ($CONFIG['pagesize'] < $count) {
202 $records = array_slice($records, 0, $CONFIG['pagesize']);
205 $result->records = array_values($records);
208 $search_request = md5('addr'
209 .(is_array($fields) ? implode($fields, ',') : $fields)
210 .(is_array($search) ? implode($search, ',') : $search));
212 // save search settings in session
213 $_SESSION['search'][$search_request] = $search_set;
214 $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($fields, $search));
215 $_SESSION['page'] = 1;
218 $OUTPUT->command('list_contacts_clear');
220 if ($result->count > 0) {
221 // create javascript list
222 rcmail_js_contacts_list($result);
223 $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
226 $OUTPUT->show_message('nocontactsfound', 'notice');
229 // update message count display
230 $OUTPUT->command('set_env', 'search_request', $search_request);
231 $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $CONFIG['pagesize']));
232 $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
233 // Re-set current source
234 $OUTPUT->command('set_env', 'search_id', $sid);
235 $OUTPUT->command('set_env', 'source', '');
236 $OUTPUT->command('set_env', 'group', '');
238 // unselect currently selected directory/group
240 $OUTPUT->command('unselect_directory');
241 $OUTPUT->command('update_group_commands');
244 $OUTPUT->send($adv ? 'iframe' : null);
247 function rcmail_contact_search_form($attrib)
249 global $RCMAIL, $CONTACT_COLTYPES;
251 $i_size = !empty($attrib['size']) ? $attrib['size'] : 30;
255 'name' => rcube_label('properties'),
260 'name' => rcube_label('personalinfo'),
265 'name' => rcube_label('other'),
271 // get supported coltypes from all address sources
272 $sources = $RCMAIL->get_address_sources();
275 foreach ($sources as $s) {
276 $CONTACTS = $RCMAIL->get_address_book($s['id']);
278 if (is_array($CONTACTS->coltypes)) {
279 $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
280 $coltypes = array_merge($coltypes, $contact_cols);
284 // merge supported coltypes with $CONTACT_COLTYPES
285 foreach ($coltypes as $col => $colprop) {
286 $coltypes[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], (array)$colprop) : (array)$colprop;
289 // build form fields list
290 foreach ($coltypes as $col => $colprop)
292 if ($colprop['type'] != 'image' && !$colprop['nosearch'])
294 $ftype = $colprop['type'] == 'select' ? 'select' : 'text';
295 $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
296 $category = $colprop['category'] ? $colprop['category'] : 'other';
298 if ($ftype == 'text')
299 $colprop['size'] = $i_size;
301 $content = html::div('row', html::div('contactfieldlabel label', Q($label))
302 . html::div('contactfieldcontent', rcmail_get_edit_field('search_'.$col, '', $colprop, $ftype)));
304 $form[$category]['content'][] = $content;
308 $hiddenfields = new html_hiddenfield();
309 $hiddenfields->add(array('name' => '_adv', 'value' => 1));
311 $out = $RCMAIL->output->request_form(array(
312 'name' => 'form', 'method' => 'post',
313 'task' => $RCMAIL->task, 'action' => 'search',
314 'noclose' => true) + $attrib, $hiddenfields->show());
316 $RCMAIL->output->add_gui_object('editform', $attrib['id']);
318 unset($attrib['name']);
319 unset($attrib['id']);
321 foreach ($form as $f) {
322 if (!empty($f['content'])) {
323 $content = html::div('contactfieldgroup', join("\n", $f['content']));
325 $out .= html::tag('fieldset', $attrib,
326 html::tag('legend', null, Q($f['name']))
331 return $out . '</form>';