This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
searchlight.admin.inc
473 lines (435 loc) · 16 KB
/
searchlight.admin.inc
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
/**
* Datasource management form.
*/
function searchlight_admin_environment($form_state) {
$form = array(
'#theme' => 'searchlight_admin_list',
'#objects' => array(searchlight_environment_load(NULL, TRUE)),
);
$form['new']['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
'#maxlength' => 255,
'#size' => 15,
'#element_validate' => array('searchlight_admin_environment_validate_name'),
);
$form['new']['view_display'] = array(
'#title' => t('View'),
'#type' => 'select',
'#options' => searchlight_admin_environment_view_displays(),
);
$form['new']['submit'] = array(
'#value' => t('Create new environment'),
'#type' => 'submit',
'#submit' => array('searchlight_admin_environment_new'),
);
return $form;
}
/**
* Retrieve an array of Views that can be used with searchlight environments.
*/
function searchlight_admin_environment_view_displays() {
// Gather all views that have the active facets filter.
$views = views_get_all_views();
$usable = array();
foreach ($views as $view) {
foreach ($view->display as $display) {
if ($display->display_plugin === 'page') {
$view->set_display($display->id);
$filters = $view->display_handler->get_option('filters');
if (!empty($filters)) {
foreach ($filters as $filter) {
if ($filter['table'] === 'searchlight' && $filter['field'] === 'facets') {
$usable[$view->base_table]["{$view->name}:{$display->id}"] = "{$view->name}: {$display->display_title}";
}
}
}
}
}
}
return $usable;
}
/**
* Validate environment name values.
*/
function searchlight_admin_environment_validate_name($element, &$form_state) {
// Check for string identifier sanity
if (!preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
form_set_error('name', t('The environment name can only consist of lowercase letters, dashes, underscores, and numbers.'));
}
// Check for name collision
else if ($exists = searchlight_environment_load($element['#value'], TRUE)) {
form_set_error('name', t('A environment with this name already exists. Please choose another name or delete the existing environment before creating a new one.'));
}
}
/**
* Searchlight datasource form submit handler.
*/
function searchlight_admin_environment_new(&$form, &$form_state) {
$environment = searchlight_environment_new($form_state['values']['name'], $form_state['values']['view_display']);
if (searchlight_environment_save($environment)) {
drupal_set_message(t('Saved environment %name.', array('%name' => $environment->name)));
}
else {
drupal_set_message(t('Could not save environment %name.', array('%name' => $environment->name)), 'error');
}
}
/**
* Edit form for environment.
*/
function searchlight_admin_environment_edit($form_state, $environment) {
$form = array();
$form['#environment'] = $environment;
$form['#environment_name'] = $environment->name;
$environment->optionsForm($form, $form_state);
$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['buttons']['cancel'] = array(
'#type' => 'markup',
'#value' => l(t('Cancel'), 'admin/settings/search/environment'),
);
return $form;
}
/**
* Submit handler for searchlight_admin_environment_edit().
*/
function searchlight_admin_environment_edit_submit(&$form, $form_state) {
if (!empty($form['#environment'])) {
$form['#environment']->optionsSubmit($form, $form_state);
}
}
/**
* Datasource management form.
*/
function searchlight_admin_datasource($form_state) {
views_include('admin');
$base_tables = views_fetch_base_tables();
$form = array(
'#theme' => 'searchlight_admin_list',
'#grouped' => TRUE,
'#objects' => array(),
'active' => array('#tree' => TRUE),
);
foreach (searchlight_datasource_load(NULL, TRUE) as $datasource) {
$group = $base_tables[$datasource->base_table]['title'];
// Add in objects.
$form['#objects'][$group][] = $datasource;
// Add in active datasource radio option.
if (!isset($form['active'][$datasource->base_table])) {
$form['active'][$datasource->base_table] = array(
'#type' => 'radios',
'#options' => array(),
'#default_value' => variable_get("searchlight_datasource_{$datasource->base_table}", "searchlight_{$datasource->base_table}"),
);
}
$form['active'][$datasource->base_table]['#options'][$datasource->name] = $datasource->name;
}
$form['new']['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#maxlength' => 255,
'#size' => 15,
);
$options = array();
foreach ($base_tables as $table => $info) {
$options[$table] = $info['title'];
}
$form['new']['base_table'] = array(
'#title' => t('Base table'),
'#type' => 'select',
'#options' => $options,
);
$form['new']['submit'] = array(
'#value' => t('Create new datasource'),
'#type' => 'submit',
'#validate' => array('searchlight_admin_datasource_new_validate'),
'#submit' => array('searchlight_admin_datasource_new'),
);
$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('searchlight_admin_datasource_save'),
);
$form['buttons']['cancel'] = array(
'#type' => 'markup',
'#value' => l(t('Cancel'), 'admin/settings/search/datastore'),
);
return $form;
}
/**
* Validate datasource creation values.
*/
function searchlight_admin_datasource_new_validate(&$form, &$form_state) {
$name = $form_state['values']['name'];
// Check that the name field is populated
if (empty($name)) {
form_set_error('name', t('!name field is required.', array('!name' => $form['new']['name']['#title'])));
}
// Check for string identifier sanity
else if (!preg_match('!^[a-z0-9_-]+$!', $name)) {
form_set_error('name', t('The datasource name can only consist of lowercase letters, dashes, underscores, and numbers.'));
}
// Check for name collision
else if ($exists = searchlight_datasource_load($name, TRUE)) {
form_set_error('name', t('A datasource with this name already exists. Please choose another name or delete the existing datasource before creating a new one.'));
}
}
/**
* Searchlight datasource creation submit handler.
*/
function searchlight_admin_datasource_new(&$form, &$form_state) {
$datasource = searchlight_datasource_new($form_state['values']['name'], $form_state['values']['base_table']);
if (searchlight_datasource_save($datasource)) {
drupal_set_message(t('Created datasource %name.', array('%name' => $datasource->name)));
}
else {
drupal_set_message(t('Could not save datasource %name.', array('%name' => $datasource->name)), 'error');
}
}
/**
* Searchlight active datasource submit handler.
*/
function searchlight_admin_datasource_save(&$form, &$form_state) {
foreach ($form_state['values']['active'] as $base_table => $datasource) {
if (!empty($datasource)) {
$current = variable_get("searchlight_datasource_{$base_table}", "searchlight_{$base_table}");
variable_set('searchlight_datasource_'. $base_table, $datasource);
drupal_set_message(t('New active datasource settings have been saved.'), 'status', FALSE);
// If active datasource settings have changed a rebuild is necessary.
if ($current != $datasource) {
// Invalidate configuration, cache, and index.
variable_set('searchlight_config_changed', TRUE);
searchlight_invalidate_cache();
searchlight_invalidate_index($base_table);
drupal_set_message(t('Active datasource settings have changed. Please rebuild your configuration and search index.'), 'warning', FALSE);
}
}
}
}
/**
* Edit form for datasource.
*/
function searchlight_admin_datasource_edit($form_state, $datasource) {
$form = array();
$form['#datasource'] = $datasource;
$form['#datasource_name'] = $datasource->name;
$datasource->init();
$datasource->optionsForm($form, $form_state);
$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['buttons']['cancel'] = array(
'#type' => 'markup',
'#value' => l(t('Cancel'), 'admin/settings/search/datastore'),
);
return $form;
}
/**
* Submit handler for searchlight_admin_datasource_edit().
*/
function searchlight_admin_datasource_edit_submit(&$form, $form_state) {
if (!empty($form['#datasource'])) {
$form['#datasource']->optionsSubmit($form, $form_state);
}
}
/**
* AHAH callback & processor for datasource editor.
*/
function searchlight_admin_datasource_ahah($clicked_id, $element = 'fields') {
$form_state = array();
$form_state['clicked_button']['#id'] = $clicked_id;
$form_state['values'] = $_POST;
$form = form_get_cache($_POST['form_build_id'], $form_state);
if ($form) {
$datasource = searchlight_datasource_load($form['#datasource_name']);
$datasource->optionsSubmit($form, $form_state);
$form['#datasource'] = $datasource;
$datasource->optionsForm($form, $form_state);
form_set_cache($_POST['form_build_id'], $form, $form_state);
// Build and render the new element, then return it in JSON format.
$form_state = array();
$form['#post'] = array();
$form = form_builder($form['form_id']['#value'], $form, $form_state);
$output = drupal_render($form[$element]);
drupal_json(array('status' => TRUE, 'data' => $output));
}
else {
drupal_json(array('status' => FALSE, 'data' => ''));
}
exit();
}
/**
* Confirmation form for datasource actions.
*/
function searchlight_admin_confirm(&$form_state, $type, $object, $op) {
switch ($type) {
case 'searchlight_datasource':
$type_name = t('datasource');
break;
case 'searchlight_environment':
$type_name = t('environment');
break;
}
$form = array();
$form['type'] = array('#type' => 'value', '#value' => $type);
$form['object'] = array('#type' => 'value', '#value' => $object);
$form['action'] = array('#type' => 'value', '#value' => $op);
switch ($op) {
case 'revert':
$action = t('revert');
$message = t('This action will permanently remove any customizations made to this @type.', array('@type' => $type_name));
break;
case 'delete':
$action = t('delete');
$message = t('This action will remove this @type permanently from your site.', array('@type' => $type_name));
break;
case 'disable':
$action = t('disable');
$message = '';
break;
case 'enable':
$action = t('enable');
$message = '';
break;
}
$form = confirm_form($form,
t('Are you sure you want to !action the @type %title?', array('%title' => $object->name, '@type' => $type_name, '!action' => $action)),
'admin/settings/search',
$message,
drupal_ucfirst($action), t('Cancel')
);
return $form;
}
/**
* Submit handler for the searchlight_admin_datasource_confirm.
*/
function searchlight_admin_confirm_submit($form, &$form_state) {
ctools_include('export');
$object = $form_state['values']['object'];
switch ($form_state['values']['action']) {
case 'revert':
case 'delete':
switch ($form_state['values']['type']) {
case 'searchlight_datasource':
searchlight_datasource_delete($object);
// If reverting, display a message indicating that the index must be rebuilt.
if ($form_state['values']['action'] === 'revert') {
drupal_set_message(t('Datasource @datasource reverted. The index for this datasource needs to be rebuilt.', array('@datasource' => $object->name)));
}
$form_state['redirect'] = 'admin/settings/search/datasource';
break;
case 'searchlight_environment':
searchlight_environment_delete($object);
$form_state['redirect'] = 'admin/settings/search/environment';
break;
}
break;
}
}
/**
* System settings form for Searchlight.
*/
function searchlight_admin_backend($form_state) {
drupal_add_js(drupal_get_path('module', 'searchlight') .'/searchlight.admin.js');
$form = array();
// If core search is enabled, determine whether or not to play nice with it.
if (module_exists('search')) {
$form['searchlight_buddysystem'] = array(
'#type' => 'select',
'#title' => t('Core search compatibility'),
'#description' => t('Determine how Searchlight should interact with core search if it is enabled.'),
'#options' => array(
FALSE => t('Replace core search'),
TRUE => t('Work parallel to core search'),
),
'#default_value' => variable_get('searchlight_buddysystem', FALSE),
);
}
$form['searchlight_global_search'] = array(
'#type' => 'select',
'#title' => t('Global search'),
'#description' => t('Choose a View to power the global search block.'),
'#options' => array(FALSE => '<'. t("Don't replace global search") .'>') + searchlight_admin_global_search_views(),
'#default_value' => variable_get('searchlight_global_search', FALSE),
);
// Backend selection.
$form['backend'] = array(
'#tree' => FALSE,
'#type' => 'fieldset',
'#title' => t('Backend'),
'#description' => t('Choose a search backend to use with Searchlight.'),
);
$form['backend']['searchlight_backend'] = array(
'#type' => 'select',
'#options' => array(0 => '< '. t('Choose a backend') .' >'),
'#default_value' => variable_get('searchlight_backend', 'sphinx'),
'#attributes' => array(
'class' => 'searchlight-backend-select',
),
);
foreach (searchlight_registry('backend', TRUE) as $key => $info) {
$form['backend']['searchlight_backend']['#options'][$key] = $info['title'];
$backend = searchlight_get_backend($key);
$form["searchlight_backend_{$key}"] = $backend->settingsForm(variable_get("searchlight_backend_{$key}", array()));
$form["searchlight_backend_{$key}"]['#tree'] = TRUE;
$form["searchlight_backend_{$key}"]['#title'] = $info['title'];
$form["searchlight_backend_{$key}"]['#type'] = 'fieldset';
$form["searchlight_backend_{$key}"]['#attributes'] = array(
'class' => "searchlight-backend-settings searchlight-backend-{$key}",
);
}
$form = system_settings_form($form);
array_unshift($form['#submit'], 'searchlight_admin_backend_submit');
return $form;
}
/**
* Submit handler that runs prior to system_settings_form_submit().
*/
function searchlight_admin_backend_submit(&$form, &$form_state) {
$current = variable_get('searchlight_backend', 'sphinx');
if ($current != $form_state['values']['searchlight_backend']) {
// Switch backends. This must occur *before* we invalidate the index.
variable_set('searchlight_backend', $form_state['values']['searchlight_backend']);
// Invalidate configuration, cache, and index.
variable_set('searchlight_config_changed', TRUE);
searchlight_invalidate_cache();
searchlight_invalidate_index();
drupal_set_message(t('Searchlight backend settings have changed. Please rebuild your configuration and search index.'), 'warning', FALSE);
}
}
/**
* Retrieve an array of Views that can be used as the destination for a global
* search block. Keyed by View path + search filter key.
*/
function searchlight_admin_global_search_views() {
// Gather all views that have the active facets filter.
$views = views_get_all_views();
$usable = array();
foreach ($views as $view) {
foreach ($view->display as $display) {
if ($display->display_plugin === 'page') {
$view->set_display($display->id);
$filters = $view->display_handler->get_option('filters');
if (!empty($filters)) {
foreach ($filters as $filter) {
if (!empty($filter['exposed']) && !empty($filter['expose']['identifier'])) {
if (
($filter['table'] === 'searchlight' && $filter['field'] === 'search') ||
($filter['table'] === 'search' && $filter['field'] === 'keys')
) {
$path = $view->get_url();
$usable[$view->base_table]["{$path}:{$filter['expose']['identifier']}"] = "{$view->name}: {$display->display_title}";
}
}
}
}
}
}
}
return $usable;
}