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 (!isset($_GET['_form'])) {
25 rcmail_contact_search();
28 $OUTPUT->add_handler('searchform', 'rcmail_contact_search_form');
29 $OUTPUT->send('contactsearch');
32 function rcmail_contact_search()
34 global $RCMAIL, $OUTPUT, $CONFIG, $SEARCH_MODS_DEFAULT;
36 $adv = isset($_POST['_adv']);
38 // get fields/values from advanced search form
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)) {
49 // do nothing, show the form again
55 $search = trim(get_input_value('_q', RCUBE_INPUT_GET, true));
56 $fields = explode(',', get_input_value('_headers', RCUBE_INPUT_GET));
59 $fields = array_keys($SEARCH_MODS_DEFAULT);
62 $fields = array_filter($fields);
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));
72 if (in_array('*', $fields)) {
78 $sources = $RCMAIL->get_address_sources();
79 $search_set = array();
82 foreach ($sources as $s) {
83 $source = $RCMAIL->get_address_book($s['id']);
85 // check if search fields are supported....
86 if (is_array($fields)) {
87 $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
90 foreach ($fields as $f) {
91 if (array_key_exists($f, $cols)) {
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)) {
104 $source->set_page(1);
105 $source->set_pagesize(9999);
107 // get contacts count
108 $result = $source->search($fields, $search, false, false);
110 if (!$result->count) {
115 $result = $source->list_records(array('name', 'email'));
117 while ($row = $result->next()) {
118 $row['sourceid'] = $s['id'];
119 $key = $row['name'] . ':' . $row['sourceid'];
120 $records[$key] = $row;
124 $search_set[$s['id']] = $source->get_search_set();
128 ksort($records, SORT_LOCALE_STRING);
130 // create resultset object
131 $count = count($records);
132 $result = new rcube_result_set($count);
134 // cut first-page records
135 if ($CONFIG['pagesize'] < $count) {
136 $records = array_slice($records, 0, $CONFIG['pagesize']);
139 $result->records = array_values($records);
142 $search_request = md5('addr'
143 .(is_array($fields) ? implode($fields, ',') : $fields)
144 .(is_array($search) ? implode($search, ',') : $search));
146 // save search settings in session
147 $_SESSION['search'][$search_request] = $search_set;
148 $_SESSION['page'] = 1;
151 $OUTPUT->command('list_contacts_clear');
153 if ($result->count > 0) {
154 // create javascript list
155 rcmail_js_contacts_list($result);
158 $OUTPUT->show_message('nocontactsfound', 'notice');
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));
166 // unselect currently selected directory/group
167 $OUTPUT->command('unselect_directory');
168 $OUTPUT->command('update_group_commands');
171 $OUTPUT->send($adv ? 'iframe' : null);
174 function rcmail_contact_search_form($attrib)
176 global $RCMAIL, $CONTACT_COLTYPES;
178 $i_size = !empty($attrib['size']) ? $attrib['size'] : 30;
182 'name' => rcube_label('contactproperties'),
187 'name' => rcube_label('personalinfo'),
192 'name' => rcube_label('other'),
198 // get supported coltypes from all address sources
199 $sources = $RCMAIL->get_address_sources();
202 foreach ($sources as $s) {
203 $CONTACTS = $RCMAIL->get_address_book($s['id']);
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);
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;
216 // build form fields list
217 foreach ($coltypes as $col => $colprop)
219 if ($colprop['type'] != 'image' && !$colprop['nosearch'])
221 $ftype = $colprop['type'] == 'select' ? 'select' : 'text';
222 $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col);
223 $category = $colprop['category'] ? $colprop['category'] : 'other';
225 if ($ftype == 'text')
226 $colprop['size'] = $i_size;
228 $content = html::div('row', html::div('contactfieldlabel label', Q($label))
229 . html::div('contactfieldcontent', rcmail_get_edit_field('search_'.$col, '', $colprop, $ftype)));
231 $form[$category]['content'][] = $content;
235 $hiddenfields = new html_hiddenfield();
236 $hiddenfields->add(array('name' => '_adv', 'value' => 1));
238 $out = $RCMAIL->output->request_form(array(
239 'name' => 'form', 'method' => 'post',
240 'task' => $RCMAIL->task, 'action' => 'search',
241 'noclose' => true) + $attrib, $hiddenfields->show());
243 $RCMAIL->output->add_gui_object('editform', $attrib['id']);
245 unset($attrib['name']);
246 unset($attrib['id']);
248 foreach ($form as $f) {
249 if (!empty($f['content'])) {
250 $content = html::div('contactfieldgroup', join("\n", $f['content']));
252 $out .= html::tag('fieldset', $attrib,
253 html::tag('legend', null, Q($f['name']))
258 return $out . '</form>';