forked from yhahn/searchlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchlight.module
1039 lines (977 loc) · 32.4 KB
/
searchlight.module
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once 'includes/SearchlightDatasource.inc';
include_once 'includes/SearchlightEnvironment.inc';
/**
* Implements hook_init().
* Forcibly disable core search indexing if present.
*/
function searchlight_init() {
if (module_exists('search') && !variable_get('searchlight_buddysystem', FALSE)) {
global $conf;
$conf['search_cron_limit'] = 0;
}
}
/**
* Implements hook_menu().
*/
function searchlight_menu() {
$items = array();
$items['admin/config/search/settings/datasource'] = array(
'title' => 'Datasource',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_datasource'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/settings/datasource/%searchlight_datasource'] = array(
'title' => 'Datasource',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_datasource_edit', 5),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/settings/datasource/%searchlight_datasource/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_datasource_edit', 5),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/settings/datasource/%searchlight_datasource/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_confirm', 'searchlight_datasource', 5, 'revert'),
'access callback' => 'searchlight_task_access',
'access arguments' => array(5, 'delete'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/search/settings/datasource/%searchlight_datasource/revert'] = array(
'title' => 'Revert',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_confirm', 'searchlight_datasource', 5, 'revert'),
'access callback' => 'searchlight_task_access',
'access arguments' => array(5, 'revert'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/search/settings/environment'] = array(
'title' => 'Environment',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_environment'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -5,
);
$items['admin/config/search/settings/environment/%searchlight_environment'] = array(
'title' => 'Environment',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_environment_edit', 5),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/settings/environment/%searchlight_environment/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_environment_edit', 5),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/search/settings/environment/%searchlight_environment/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_confirm', 'searchlight_environment', 5, 'revert'),
'access callback' => 'searchlight_task_access',
'access arguments' => array(5, 'delete'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/search/settings/environment/%searchlight_environment/revert'] = array(
'title' => 'Revert',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_confirm', 'searchlight_environment', 5, 'revert'),
'access callback' => 'searchlight_task_access',
'access arguments' => array(5, 'revert'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/search/settings/backend'] = array(
'title' => 'Backend',
'page callback' => 'drupal_get_form',
'page arguments' => array('searchlight_admin_backend'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'searchlight.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function searchlight_menu_alter(&$items) {
$items['admin/config/search/settings'] = $items['admin/config/search/settings/datasource'];
$items['admin/config/search/settings']['title'] = 'Search';
$items['admin/config/search/settings']['description'] = 'Configure relevance settings for search and other indexing options.';
$items['admin/config/search/settings']['type'] = MENU_NORMAL_ITEM;
unset($items['admin/config/search/settings']['weight']);
}
/**
* Implements hook_block_info().
*/
function searchlight_block_info() {
if (TRUE) {
$list = array();
foreach (searchlight_environment_load() as $environment) {
$list['facets_' . $environment->name] = array('info' => t('@name: Search facets', array('@name' => $environment->name)));
}
return $list;
}
}
/**
* Implements hook_block_view().
*/
function searchlight_block_view($delta) {
if (TRUE && strpos($delta, '_') !== FALSE) {
list($delta, $name) = explode('_', $delta, 2);
if (in_array($delta, array('facets'), TRUE) && $environment = searchlight_environment_load($name)) {
return $environment->getBlock($delta);
}
}
}
/**
* Implements hook_node_insert().
*/
function searchlight_node_insert($node) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("INSERT INTO {searchlight_search} (type, id, status) VALUES ('node', %d, %d)", $node->nid, 0) */
$id = db_insert('searchlight_search')
->fields(array(
'type' => 'node',
'id' => $node->nid,
'status' => 0,
))
->execute();
}
/**
* Implements hook_node_update().
*/
function searchlight_node_update($node) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("UPDATE {searchlight_search} SET status = 0 WHERE type='node' and id=%d", $node->nid) */
db_update('searchlight_search')
->fields(array( 'status' => 0,))
->condition('type', 'node')
->condition('id', $node->nid)
->execute();
}
/**
* Implements hook_node_delete().
*/
function searchlight_node_delete($node) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("UPDATE {searchlight_search} SET status = -1 WHERE type='node' and id=%d", $node->nid) */
db_update('searchlight_search')
->fields(array( 'status' => -1,))
->condition('type', 'node')
->condition('id', $node->nid)
->execute();
}
/**
* Implements hook_comment_insert().
*/
function searchlight_comment_insert($comment) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("INSERT INTO {searchlight_search} (type, id, status) VALUES ('comments', %d, %d)", $a1['cid'], 0) */
$id = db_insert('searchlight_search')
->fields(array(
'type' => 'comments',
'id' => $comment->cid,
'status' => 0,
))
->execute();
$update_node = TRUE;
}
/**
* Implements hook_comment_update().
*/
function searchlight_comment_update($comment) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("UPDATE {searchlight_search} SET status = 0 WHERE type = 'comments' AND id = %d", $a1['cid']) */
db_update('searchlight_search')
->fields(array('status' => 0,))
->condition('type', 'comments')
->condition('id', $comment->cid)
->execute();
$update_node = TRUE;
}
/**
* Implements hook_comment_delete().
*/
function searchlight_comment_delete($comment) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("UPDATE {searchlight_search} SET status = -1 WHERE type = 'comments' and id = %d", $a1['cid']) */
db_update('searchlight_search')
->fields(array('status' => -1, ))
->condition('type', 'comments')
->condition('id', $comment->cid)
->execute();
$update_node = TRUE;
}
/**
* Implements hook_theme().
*/
function searchlight_theme() {
return array(
'searchlight_search_form' => array(
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_sphinx_conf' => array(
'template' => 'searchlight-sphinx-conf',
'variables' => array('searchd' => array()),
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_sphinx_index_conf' => array(
'template' => 'searchlight-sphinx-index-conf',
'variables' => array('datasource' => array()),
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_solr_schema' => array(
'template' => 'searchlight-solr-schema',
'variables' => array('datasource' => array()),
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_solr_config' => array(
'template' => 'searchlight-solr-config',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_solr_cores' => array(
'template' => 'searchlight-solr-cores',
'variables' => array('cores' => array()),
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_plugin_display_datasource' => array(
'render element' => 'form',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_facet' => array(
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'variables' => array('facet' => array()),
'file' => 'searchlight.theme.inc',
),
'searchlight_facet_link' => array(
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'variables' => array('field' => NULL, 'item' => array()),
'file' => 'searchlight.theme.inc',
),
'searchlight_facet_active' => array(
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'variables' => array('field' => NULL, 'item' => array()),
'file' => 'searchlight.theme.inc',
),
'searchlight_admin_list' => array(
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'render element' => 'form',
'file' => 'searchlight.theme.inc',
),
'searchlight_admin_datasource' => array(
'template' => 'searchlight-admin-datasource',
'render element' => 'form',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_admin_datasource_fields' => array(
'render element' => 'form',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_admin_datasource_relations' => array(
'render element' => 'form',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
'searchlight_admin_environment' => array(
'template' => 'searchlight-admin-environment',
'render element' => 'form',
'path' => drupal_get_path('module', 'searchlight') . '/theme',
'file' => 'searchlight.theme.inc',
),
);
}
/**
* Preprocessor for theme('page').
function searchlight_preprocess_page(&$vars) {
if ($global_search = variable_get('searchlight_global_search', FALSE)) {
list($path, $identifier) = explode(':', $global_search);
$form_state = array(
'path' => $path,
'identifier' => $identifier,
'method' => 'get',
'rerender' => TRUE,
'no_redirect' => TRUE,
'input' => array(),
);
// $vars['search_box'] = drupal_build_form('searchlight_search_form', $form_state);
}
}
*/
/**
* Form builder for Searchlight GET form. Should be used with
* drupal_build_form(), the function used in Views for exposed filter forms.
*/
function searchlight_search_form(&$form_state) {
$form['#info'] = array();
$form['#action'] = url($form_state['path']);
$form['#id'] = 'searchlight-search-form';
if (!variable_get('clean_url', FALSE)) {
$form['q'] = array(
'#type' => 'hidden',
'#value' => $form_state['path'],
);
}
$form[$form_state['identifier']] = array(
'#type' => 'textfield',
'#default_value' => '',
);
$form['submit'] = array(
'#name' => '', // prevent from showing up in $_GET.
'#type' => 'submit',
'#value' => t('Search'),
'#id' => drupal_clean_css_identifier('edit-submit-searchlight-search-form'),
);
return $form;
}
/**
* Implements hook_views_api().
*/
function searchlight_views_api() {
return array(
'api' => '3.0-alpha1',
'path' => drupal_get_path('module', 'searchlight') . '/views',
);
}
/**
* Implements hook_ctools_plugin_api().
*/
function searchlight_ctools_plugin_api($module = NULL, $api = NULL) {
if ($module === 'searchlight') {
switch ($api) {
case 'plugins':
return array('version' => 1);
case 'datasource':
return array('version' => 1);
case 'environment':
return array('version' => 1);
}
}
}
/**
* Implements hook_ctools_plugin_plugins().
*/
function searchlight_ctools_plugin_type() {
return array(
'plugins' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
),
);
}
/**
* Implements hook_searchlight_plugins().
* This is a CTools plugin API hook.
*/
function searchlight_searchlight_plugins() {
return array(
'SearchlightBackend' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightBackend.inc',
'class' => 'SearchlightBackend',
),
),
'SearchlightBackendSphinx' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightBackendSphinx.inc',
'class' => 'SearchlightBackendSphinx',
'parent' => 'SearchlightBackend',
),
),
'SearchlightBackendSolr' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightBackendSolr.inc',
'class' => 'SearchlightBackendSolr',
'parent' => 'SearchlightBackend',
),
),
'SearchlightFacet' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightFacet.inc',
'class' => 'SearchlightFacet',
),
),
'SearchlightFacetSearchQuery' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightFacetSearchQuery.inc',
'class' => 'SearchlightFacetSearchQuery',
'parent' => 'SearchlightFacet',
),
),
'SearchlightFacetTerm' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightFacetTerm.inc',
'class' => 'SearchlightFacetTerm',
'parent' => 'SearchlightFacet',
),
),
'SearchlightFacetDatatypeTimestamp' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightFacetDatatypeTimestamp.inc',
'class' => 'SearchlightFacetDatatypeTimestamp',
'parent' => 'SearchlightFacet',
),
),
'SearchlightFacetLanguage' => array(
'handler' => array(
'path' => drupal_get_path('module', 'searchlight') . '/plugins',
'file' => 'SearchlightFacetLanguage.inc',
'class' => 'SearchlightFacetLanguage',
'parent' => 'SearchlightFacet',
),
),
);
}
/**
* Implements hook_searchlight_registry().
*/
function searchlight_searchlight_registry() {
return array(
'backend' => array(
'sphinx' => array(
'title' => t('Sphinx'),
'plugin' => 'SearchlightBackendSphinx',
),
'solr' => array(
'title' => t('Solr'),
'plugin' => 'SearchlightBackendSolr',
),
),
'facet' => array(
'default' => array(
'title' => t('Default'),
'plugin' => 'SearchlightFacet',
),
'search_query' => array(
'title' => t('Search query'),
'plugin' => 'SearchlightFacetSearchQuery',
),
'taxonomy_index_tid' => array(
'title' => t('Taxonomy term'),
'plugin' => 'SearchlightFacetTerm',
),
'datatype_timestamp' => array(
'title' => t('Date'),
'plugin' => 'SearchlightFacetDatatypeTimestamp',
),
'users_language' => array(
'title' => t('Language'),
'plugin' => 'SearchlightFacetLanguage',
),
),
);
}
/**
* Implements hook_form_alter() for 'views_exposed_form'().
* Ensure that Searchlight facet components are preserved when Views exposed
* filters are submitted.
*/
function searchlight_form_views_exposed_form_alter(&$form, &$form_state) {
$key = variable_get('searchlight_facet_key', 'sl');
if (!isset($form[$key]) && isset($_GET[$key])) {
$form[$key] = array(
'#type' => 'hidden',
'#value' => $_GET[$key],
);
}
}
/**
* Implements hook_ajax_render_alter().
*/
function searchlight_ajax_render_alter(&$commands) {
$environment = searchlight_environment_active();
if ($environment) {
$block = $environment->getBlock('facets');
$commands[] = ajax_command_replace(".searchlight-environment-{$environment->name}", $block['content']);
}
}
/**
* Implements hook_searchlight_node_access_realms_alter().
* Provide realms for commonly used node_access modules.
*/
function searchlight_searchlight_node_access_realms_alter(&$realms) {
// Standard node access realm.
$realms['all'] = 'all';
// OG access.
if (module_exists('og_access')) {
$realms['og_admin'] = 'og_admin';
$realms['og_public'] = 'og_public';
$realms['og_subscriber'] = 'og_subscriber';
}
}
/**
* Retrieves & caches the searchlight registry.
*/
function searchlight_registry($key = NULL, $reset = FALSE) {
static $registry;
if (!isset($registry) || $reset) {
if (!$reset && $cache = cache_get('searchlight_registry')) {
$registry = $cache->data;
}
else {
$registry = module_invoke_all('searchlight_registry');
drupal_alter('searchlight_registry', $registry);
cache_set('searchlight_registry', $registry);
}
}
if (isset($key)) {
return isset($registry[$key]) ? $registry[$key] : array();
}
return $registry;
}
/**
* Retrieve an instance of a backend class. Note: this is a singleton.
* If no backend is specified by $id, the default backend is returned.
*/
function searchlight_get_backend($id = NULL, $reset = FALSE) {
static $loaded;
ctools_include('plugins');
// Use default backend if no backend was specified.
$id = isset($id) ? $id : variable_get('searchlight_backend', 'sphinx');
if (!isset($loaded[$id]) || $reset) {
$loaded[$id] = FALSE;
$registry = searchlight_registry('backend');
if (isset($registry[$id]) && $info = $registry[$id]) {
$plugins = ctools_get_plugins('searchlight', 'plugins');
if (isset($plugins[$info['plugin']]) && $class = ctools_plugin_get_class($plugins[$info['plugin']], 'handler')) {
$loaded[$id] = new $class($id);
}
}
}
return $loaded[$id];
}
/**
* Retrieve an instance of a facet plugin class.
*/
function searchlight_get_facet($datasource, $name) {
// Build array of suggestion plugin registry names.
$suggestions = array();
$field = isset($datasource->fields[$name]) ? $datasource->fields[$name] : NULL;
// Allow facets to be defined by table/field.
if ($field) {
$suggestions[] = "{$field['table']}_{$field['field']}";
}
// Allow facets to be defined by alias.
$suggestions[] = $name;
// Allow facets to be defined by datatype.
if ($field) {
$suggestions[] = "datatype_{$field['datatype']}";
}
// Fallback to default plugin.
$suggestions[] = 'default';
// Iterate through facet registry until a suitable plugin class is found.
ctools_include('plugins');
$registry = searchlight_registry('facet');
foreach ($suggestions as $id) {
if (isset($registry[$id]) && $info = $registry[$id]) {
$plugins = ctools_get_plugins('searchlight', 'plugins');
if (isset($plugins[$info['plugin']]) && $class = ctools_plugin_get_class($plugins[$info['plugin']], 'handler')) {
return new $class($id);
}
}
}
return FALSE;
}
/**
* Retrieve full datasource information for a given base table.
*/
function searchlight_get_datasource($base_table = NULL) {
if (!isset($base_table)) {
views_include('admin');
$active_datasources = array();
foreach (array_keys(views_fetch_base_tables()) as $base_table) {
$name = variable_get("searchlight_datasource_{$base_table}", "search_{$base_table}");
if (!empty($name) && $datasource = searchlight_datasource_load($name)) {
$active_datasources[$name] = $datasource;
}
}
return $active_datasources;
}
else {
$name = variable_get("searchlight_datasource_{$base_table}", "search_{$base_table}");
if (!empty($name) && $datasource = searchlight_datasource_load($name)) {
return $datasource;
}
}
return FALSE;
}
/**
* Retrieve a new inited Searchlight query.
*/
function searchlight_get_query($base_table, $base_field, $backend = NULL) {
$backend = !isset($backend) ? searchlight_get_backend() : $backend;
if ($backend) {
$query = views_get_plugin('query', 'searchlight');
$query->init($base_table, $base_field, $backend);
return $query;
}
return FALSE;
}
/**
* Parse and build a view from a "$view_name:$view_display" string.
* @TODO: Deprecate this function. It's primarily used atm for building a view
* associated with a multivalue field. Once the multivalue field is represented
* as a proper class object, it should handle this itself.
*/
function searchlight_build_view($identifier) {
$split = explode(':', $identifier);
if (count($split) === 2 && $view = views_get_view($split[0])) {
$view->set_display($split[1]);
$view->build();
return $view;
}
return FALSE;
}
/**
* Load & crud functions ==============================================
*/
/**
* Create a new datasource object.
*/
function searchlight_datasource_new($name, $base_table) {
ctools_include('export');
$datasource = ctools_export_new_object('searchlight_datasource');
$datasource->name = $name;
$datasource->base_table = $base_table;
$datasource->construct();
return $datasource;
}
/**
* Datasource loader.
*
* @param $name
* The name for this datasource.
*
* @return
* Returns a fully-loaded datasource.
*/
function searchlight_datasource_load($name = NULL, $reset = FALSE) {
ctools_include('export');
static $datasources;
if (!isset($datasources) || $reset) {
if ($reset) {
ctools_export_load_object_reset('searchlight_datasource');
}
$datasources = ctools_export_load_object('searchlight_datasource', 'all');
foreach ($datasources as $datasource) {
// Would love to use __construct() here but CTools export.inc has already
// blown away properties in its unpack step.
$datasource->construct();
}
}
if (isset($name)) {
return isset($datasources[$name]) ? $datasources[$name] : FALSE;
}
return $datasources;
}
/**
* Inserts or updates a datasource object into the database.
*
* @param $datasource
* The datasource object to be saved.
* @param $editing
* Boolean flag for whether this datasource is still being edited. Allows for
* quick saves without invalidating the datasource index repeatedly.
*
* @return
* Returns true on success, false on failure.
*/
function searchlight_datasource_save($datasource, $editing = FALSE) {
$existing = searchlight_datasource_load($datasource->name, TRUE);
if ($existing && ($existing->export_type & EXPORT_IN_DATABASE)) {
drupal_write_record('searchlight_datasource', $datasource, 'name');
}
else {
drupal_write_record('searchlight_datasource', $datasource);
}
searchlight_datasource_load(NULL, TRUE);
if (!$editing) {
// Invalidate configuration, cache, and index.
variable_set('searchlight_config_changed', TRUE);
searchlight_invalidate_cache();
searchlight_invalidate_index($datasource->base_table);
}
return TRUE;
}
/**
* Deletes an existing datasource.
*
* @param $datasource
* The datasource to be deleted.
*
* @return
* Returns true on success, false on failure.
*/
function searchlight_datasource_delete($datasource) {
if (isset($datasource->name) && ($datasource->export_type & EXPORT_IN_DATABASE)) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("DELETE FROM {searchlight_datasource} WHERE name = '%s'", $datasource->name) */
db_delete('searchlight_datasource')
->condition('name', $datasource->name)
->execute();
searchlight_invalidate_cache();
return TRUE;
}
return FALSE;
}
/**
* Environment facet querystring packer.
*/
function searchlight_environment_pack($value) {
$keyvals = array();
foreach ($value as $k => $v) {
$keyvals[] = "{$k}-{$v}";
}
return urlencode(implode(',', $keyvals));
}
/**
* Environment facet querystring unpacker.
*/
function searchlight_environment_unpack($value) {
$value = urldecode($value);
$parsed = array();
$split = explode(',', $value);
foreach ($split as $chunk) {
$keyval = explode('-', $chunk, 2);
if (count($keyval) === 2) {
$parsed[$keyval[0]] = $keyval[1];
}
}
return $parsed;
}
/**
* Environment init.
*/
function searchlight_environment_init($packed = NULL) {
// Retrieve facet query string from $_GET if no input is provided.
if (!isset($packed)) {
$key = variable_get('searchlight_facet_key', 'sl');
$packed = isset($_GET[$key]) ? $_GET[$key] : NULL;
}
if (!empty($packed) && is_string($packed)) {
$values = searchlight_environment_unpack($packed);
if (!empty($values['environment']) && $environment = searchlight_environment_load($values['environment'])) {
$environment->initValues($values);
searchlight_environment_active($environment);
}
}
}
/**
* Static cache/lazy retrieval of the current active environment.
* If an environment is already active , return that environment.
* Otherwise, try to locate an environment "relevant" to the specified view
* using the view name and display id.
*/
function searchlight_environment_active($environment = NULL, $view = NULL, $display = NULL, $reset = FALSE) {
static $active;
// Setting.
if (!isset($active) || $reset) {
$active = NULL;
}
if (isset($environment)) {
$active = $environment;
}
// Retrieval.
if (isset($active)) {
return $active;
}
// Match both view & display. Note that a lazy matched environment is not
// static cached as multiple may be active on the same page.
else if (isset($view, $display)) {
foreach (searchlight_environment_load() as $environment) {
list($environment_view, $environment_display) = explode(':', $environment->view_display);
if ($environment->view_display === "{$view}:{$display}") {
return $environment;
}
else if ($environment_view === $view) {
return $environment;
}
}
}
return FALSE;
}
/**
* Create a new environment object.
*/
function searchlight_environment_new($name, $view_display) {
ctools_include('export');
$environment = ctools_export_new_object('searchlight_environment');
$environment->name = $name;
$environment->view_display = $view_display;
$environment->construct();
return $environment;
}
/**
* Datasource loader.
*
* @param $name
* The name for this environment.
*
* @return
* Returns a fully-loaded environment.
*/
function searchlight_environment_load($name = NULL, $reset = FALSE) {
ctools_include('export');
static $environments;
if (!isset($environments) || $reset) {
if ($reset) {
ctools_export_load_object_reset('searchlight_environment');
}
$environments = ctools_export_load_object('searchlight_environment', 'all');
foreach ($environments as $environment) {
// Would love to use __construct() here but CTools export.inc has already
// blown away properties in its unpack step.
$environment->construct();
}
}
if (isset($name)) {
return isset($environments[$name]) ? $environments[$name] : FALSE;
}
return $environments;
}
/**
* Inserts or updates a environment object into the database.
*
* @param $environment
* The environment object to be saved.
*
* @return
* Returns true on success, false on failure.
*/
function searchlight_environment_save($environment) {
$existing = searchlight_environment_load($environment->name, TRUE);
if ($existing && ($existing->export_type & EXPORT_IN_DATABASE)) {
drupal_write_record('searchlight_environment', $environment, 'name');
}
else {
drupal_write_record('searchlight_environment', $environment);
}
searchlight_environment_load(NULL, TRUE);
searchlight_invalidate_cache();
return TRUE;
}
/**
* Deletes an existing environment.
*
* @param $environment
* The environment to be deleted.
*
* @return
* Returns true on success, false on failure.
*/
function searchlight_environment_delete($environment) {
if (isset($environment->name) && ($environment->export_type & EXPORT_IN_DATABASE)) {
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* db_query("DELETE FROM {searchlight_environment} WHERE name = '%s'", $environment->name) */
db_delete('searchlight_environment')
->condition('name', $environment->name)
->execute();
searchlight_invalidate_cache();
return TRUE;
}
return FALSE;
}
/**
* Menu access callback for config object-related tasks.
*/
function searchlight_task_access($object, $op) {
if (user_access('administer site configuration')) {
switch ($op) {
case 'revert':
return ($object->export_type & EXPORT_IN_DATABASE) && ($object->export_type & EXPORT_IN_CODE);
case 'delete':
return ($object->export_type & EXPORT_IN_DATABASE) && !($object->export_type & EXPORT_IN_CODE);
case 'disable':
return empty($object->disabled);
case 'enable':
return !empty($object->disabled);
}
return TRUE;
}
return FALSE;
}