-
Notifications
You must be signed in to change notification settings - Fork 17
/
search.php
299 lines (268 loc) · 10 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Turba search.php.
*
* Copyright 2000-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @author Chuck Hagenbuch <[email protected]>
* @author Jan Schneider <[email protected]>
*/
require_once __DIR__ . '/lib/Application.php';
Horde_Registry::appInit('turba');
$vars = Horde_Variables::getDefaultVariables();
/* Verify if the search mode variable is passed in form or is registered in
* the session. Always use basic search by default. */
if ($vars->search_mode) {
$session->set('turba', 'search_mode', $vars->search_mode);
}
if (!in_array($session->get('turba', 'search_mode'), array('basic', 'advanced', 'duplicate'))) {
$session->set('turba', 'search_mode', 'basic');
}
$search_mode = $session->get('turba', 'search_mode');
/* Get the current source. */
$addressBooks = Turba::getAddressBooks();
$editableAddressBooks = Turba::getAddressBooks(Horde_Perms::EDIT & Horde_Perms::DELETE,
array('require_add' => true));
if ($search_mode == 'duplicate') {
$addressBooks = $editableAddressBooks;
}
$source = $vars->get('source', Turba::$source);
if (!isset($addressBooks[$source])) {
$source = key($addressBooks);
/* If there are absolutely no valid sources, abort. */
if (!isset($addressBooks[$source])) {
$notification->push(_("No Address Books are currently available. Searching is disabled."), 'horde.error');
$page_output->header();
$notification->notify(array('listeners' => 'status'));
$page_output->footer();
exit;
}
}
/* Build all available search criteria. */
$allCriteria = $shareSources = array();
foreach ($addressBooks as $key => $entry) {
$allCriteria[$key] = array('' => _("All"));
foreach ($entry['search'] as $field) {
$allCriteria[$key][$field] = $GLOBALS['attributes'][$field]['label'];
}
/* Remember vbooks and sources that are using shares. */
$shareSources[$key] = $entry['type'] != 'vbook';
}
// Criteria is from "basic" search only.
$criteria = $vars->criteria;
$val = $vars->val;
try {
$driver = $injector->getInstance('Turba_Factory_Driver')->create($source);
} catch (Turba_Exception $e) {
$notification->push($e, 'horde.error');
$driver = null;
$map = array();
}
if ($driver) {
$map = $driver->getCriteria();
$do_search = false;
$submitted = Horde_Util::getFormData('search');
$browsable = !empty($cfgSources[$source]['browse']) && !empty($submitted);
switch ($search_mode) {
case 'advanced':
$criteria = array();
foreach (array_keys($map) as $key) {
if ($key != '__key') {
$value = $vars->get($key);
if (strlen($value)) {
$criteria[$key] = $value;
}
}
}
if ($conf['tags']['enabled']) {
$criteria['tags'] = $vars->tags;
}
if (count($criteria) || $browsable) {
$do_search = true;
}
break;
case 'basic':
$t_val = trim($val);
if (empty($t_val)) {
if ($browsable) {
$do_search = true;
}
$criteria = array();
break;
}
if (!strlen($criteria)) {
/* Searching all fields in basic search. */
$criteria = array_combine(
array_filter(array_keys($allCriteria[$source])),
array_fill(0, count($allCriteria[$source]) - 1, $val)
);
} else {
$criteria = array($criteria => $val);
}
$do_search = true;
break;
case 'duplicate':
$do_search = $vars->search || $vars->dupe || count($addressBooks) == 1;
break;
}
/* Check for updated sort criteria */
Turba::setPreferredSortOrder($vars, $source);
/* Only try to perform a search if we actually have search criteria. */
if ($do_search) {
if ($vars->save_vbook) {
try {
$session->checkToken($vars->token);
} catch (Horde_Exception $e) {
$notification->push($e);
Horde::url('search.php', true)->redirect();
}
/* We create the vbook and redirect before we try to search
* since we are not displaying the search results on this page
* anyway. */
$vname = $vars->vbook_name;
if (empty($vname)) {
$notification->push(_("You must provide a name for virtual address books."), 'horde.error');
Horde::url('search.php', true)->redirect();
}
/* Create the vbook. */
$params = array(
'name' => $vname,
'params' => serialize(array(
'type' => 'vbook',
'source' => $source,
'criteria' => $criteria
))
);
try {
$share = Turba::createShare(strval(new Horde_Support_Randomid()), $params);
$vid = $share->getName();
} catch (Horde_Share_Exception $e) {
$notification->push(sprintf(_("There was a problem creating the virtual address book: %s"), $e->getMessage()), 'horde.error');
Horde::url('search.php', true)->redirect();
}
$notification->push(sprintf(_("Successfully created virtual address book \"%s\""), $vname), 'horde.success');
Horde::url('browse.php', true)
->add('source', $vid)
->redirect();
}
/* Perform a search. */
if ($search_mode == 'duplicate') {
try {
$duplicates = $driver->searchDuplicates();
$view = new Turba_View_Duplicates($duplicates, $driver, $vars->type, $vars->dupe);
$page_output->addScriptFile('tables.js', 'horde');
} catch (Exception $e) {
$notification->push($e);
}
} else {
try {
if (($search_mode == 'basic' &&
($results = $driver->search($criteria, null, 'OR'))) ||
(($search_mode == 'advanced') &&
($results = $driver->search($criteria)))) {
/* Read the columns to display from the preferences. */
$sources = Turba::getColumns();
$columns = isset($sources[$source])
? $sources[$source]
: array();
$results->sort(Turba::getPreferredSortOrder());
$view = new Turba_View_List($results, null, $columns);
$view->setType('search');
} else {
$notification->push(_("Failed to search the address book"), 'horde.error');
}
} catch (Turba_Exception $e) {
$notification->push($e, 'horde.error');
}
}
}
}
/* Build search mode tabs. */
$sUrl = Horde::url('search.php');
$tabs = new Horde_Core_Ui_Tabs('search_mode', $vars);
$tabs->addTab(_("Basic Search"), $sUrl, 'basic');
$tabs->addTab(_("Advanced Search"), $sUrl, 'advanced');
if (count($editableAddressBooks)) {
$tabs->addTab(_("Duplicate Search"), $sUrl, 'duplicate');
}
/* The form header. */
$headerView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
if (count($addressBooks) == 1) {
$headerView->uniqueSource = key($addressBooks);
}
/* The search forms. */
$searchView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
new Horde_View_Helper_Text($searchView);
$searchView->addressBooks = $addressBooks;
$searchView->attributes = $GLOBALS['attributes'];
$searchView->map = $map;
$searchView->blobs = $driver->getBlobs();
$searchView->source = $source;
$searchView->criteria = $criteria;
$searchView->value = $val;
/* The form footer and vbook section. */
if ($search_mode != 'duplicate' && $session->get('turba', 'has_share')) {
$vbookView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
$vbookView->hasShare = true;
$vbookView->shareSources = $shareSources;
$vbookView->source = $source;
$vbookView->token = $session->getToken();
$page_output->addInlineScript('$(\'vbook_name\').observe(\'keyup\', function() { $(\'save-vbook\').checked = !!$F(\'vbook_name\'); });');
}
switch ($search_mode) {
case 'basic':
$title = _("Basic Search");
$page_output->addInlineScript(array(
'$("val").focus()'
), true);
$page_output->addInlineJsVars(array(
'TurbaSearch.criteria' => $allCriteria,
'TurbaSearch.shareSources' => $shareSources));
break;
case 'advanced':
$title = _("Advanced Search");
/* Include the tag field? */
if (($tagger = $injector->getInstance('Turba_Tagger')) &&
!($tagger instanceof Horde_Core_Tagger_Null)) {
$searchView->tag = true;
$injector->getInstance('Horde_Core_Factory_Imple')->create('Turba_Ajax_Imple_TagAutoCompleter', array('id' => 'tags'));
}
if (isset($results)) {
$page_output->addInlineJsVars(array(
'TurbaSearch.advanced' => true
));
}
$page_output->addInlineScript(array(
'$("name").focus()'
), true);
break;
case 'duplicate':
$title = _("Duplicate Search");
break;
}
$page_output->addScriptFile('search.js');
$page_output->addScriptFile('quickfinder.js', 'horde');
$page_output->addScriptFile('scriptaculous/effects.js', 'horde');
$page_output->addScriptFile('redbox.js', 'horde');
if (isset($view) && is_object($view)) {
Turba::addBrowseJs();
}
$page_output->header(array(
'title' => $title
));
$notification->notify(array('listeners' => 'status'));
echo $tabs->render($search_mode);
echo $headerView->render('header');
echo $searchView->render($search_mode);
if (!empty($vbookView)) {
echo $vbookView->render('vbook');
}
echo $searchView->render('footer');
if (isset($view) && is_object($view)) {
require TURBA_TEMPLATES . '/browse/header.inc';
$view->display();
}
$page_output->footer();