forked from Islandora/islandora_solr_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_solr_search.module
1259 lines (1069 loc) · 40 KB
/
islandora_solr_search.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
/**
* @file
* Implementation of Solr search for the Islandora fedora_search module.
*/
//$islandora_query;
$queryClass;
// set current display
$current_display;
/**
* islandora solr search implentation of hook_init()
* @global IslandoraSolrQueryProcessor $queryClass
* @staticvar boolean $islandora_solr_search_init
*/
function islandora_solr_search_init() {
//Including in the global scope (as was done previously) doesn't quite seem correct.
module_load_include('inc', 'islandora_solr_search', 'islandora_solr_search.admin');
module_load_include('inc', 'islandora_solr_search', 'includes/common');
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrQueryProcessor');
static $islandora_solr_search_init = false;
if (!$islandora_solr_search_init) {
drupal_add_css(drupal_get_path('module', 'islandora_solr_search') . '/css/islandora_solr_search.css');
global $queryClass;
if (empty($queryClass)) {
$queryClass = new IslandoraSolrQueryProcessor();
}
$islandora_solr_search_init = true;
}
}
/**
*
* @global type $conf
*/
function islandora_solr_search_boot() {
global $conf;
// set timezone to suppress strototime strict errors
ini_set('date.timezone', @date_default_timezone_get());
// Allow i18n, by using multilingual variables.
if (module_exists('i18n')) {
$vars = array(
'islandora_solr_search_block_facets',
'islandora_solr_search_result_fields',
'islandora_solr_searchterms'
);
$conf['i18n_variables'] = (isset($conf['i18n_variables']) && is_array($conf['i18n_variables'])) ?
array_merge($vars, $conf['i18n_variables']):
$vars;
}
}
/**
* Implementation of hook_menu().
*/
function islandora_solr_search_menu() {
$items['islandora/solr/search'] = array(
'page callback' => 'islandora_solr_search',
'access arguments' => array('view islandora solr search results'),
'type' => MENU_CALLBACK,
);
$items['islandora/solr/process'] = array(
'page callback' => 'update_solr_url_div',
'access arguments' => array('administer site configuration'),
'file' => 'islandora_solr_search.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/islandora_solr_search'] = array(
'title' => 'Islandora Solr Client',
'description' => 'Managing Islandora Solr Searching',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_solr_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'islandora_solr_search.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/islandora_solr_search/config'] = array(
'title' => 'Islandora Solr Client',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => drupal_get_path('module', 'islandora_solr_search'),
'weight' => -1,
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function islandora_solr_search_theme() {
// set path
$path = drupal_get_path('module', 'islandora_solr_search');
$file = 'islandora_solr_search.theme.inc';
return array(
'islandora_solr_search_block_form' => array(
'arguments' => array(
'form' => NULL,
),
),
// theme admin form
'islandora_solr_search_admin_settings' => array(
'path' => $path,
'file' => 'islandora_solr_search.admin.inc',
'arguments' => array('form' => NULL),
),
// results page
'islandora_solr_wrapper' => array(
'path' => $path . '/theme',
'file' => $file,
'template' => 'islandora-solr-wrapper',
'arguments' => array('results' => NULL, 'secondary_profiles' => NULL, 'elements' => array()),
),
// default results display
'islandora_solr_search' => array(
'path' => $path . '/theme',
'file' => $file,
'template' => 'islandora-solr-search',
'arguments' => array('results' => NULL, 'elements' => array()),
),
);
}
/**
* Implements hook_perm().
*/
function islandora_solr_search_perm() {
return array('view islandora solr search results');
}
/**
* Implements hook_islandora_solr_primary_display()
*/
function islandora_solr_search_islandora_solr_primary_display() {
return array(
// 'machine-name' = array(
// 'name' => 'Human Readable Name',
// 'module' => 'module_name',
// 'file' => 'FileName.inc',
// 'class' => 'ClassName',
// 'function' => 'function_name',
// 'description' => 'A description of the display profile',
// 'configuration' => 'path/to/configuration/page',
// );
//
// Note: this class should either be, or extend, the class IslandoraSolrResults.
//
'default' => array(
'name' => t('Fields (default)'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => "IslandoraSolrResults",
'function' => "displayResults",
'description' => t("A simple output."),
),
);
}
/**
* Implements hook_islandora_solr_query_blocks()
*/
function islandora_solr_search_islandora_solr_query_blocks() {
return array(
// 'machine_name' => array(
// 'name' => 'Human Readable Name',
// 'module' => 'module_name',
// 'file' => 'FileName.inc',
// 'class' => 'ClassName',
// 'function' => 'method_name',
// 'form' => 'form_function_name',
// ),
//
// Note: As in the examples below, it is valid to specify
// *either* a class and method *or* a form. The latter implies no
// class needs to be instantiated.
//
'advanced' => array(
'name' => t('Advanced Search'),
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => NULL,
'form' => 'islandora_solr_search_block_form',
),
'simple' => array(
'name' => t('Simple Search'),
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => NULL,
'form' => 'islandora_solr_simple_search_form',
),
'basic_facets' => array(
'name' => t('Islandora Facet Block'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => 'IslandoraSolrResults',
'function' => 'displayFacets',
'form' => NULL,
),
'current_query' => array(
'name' => t('Current Query'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => 'IslandoraSolrResults',
'function' => 'currentQuery',
'form' => NULL,
),
'display_switch' => array(
'name' => 'Islandora Display Switch',
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => 'islandora_solr_search_display',
'form' => NULL,
),
'limit_results' => array(
'name' => 'Islandora Limit Results',
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => 'islandora_solr_search_limit',
'form' => NULL,
),
'sort' => array(
'name' => t('Islandora Sort'),
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => 'islandora_solr_search_sort',
'form' => NULL,
),
);
}
/**
* Implementation of hook_block().
*/
function islandora_solr_search_block($op = 'list', $delta = 0, $edit = array()) {
global $queryClass;
/*
* Here I need to call a hook which will return module/file/class/method/name/description
* for blocks which need a queryClass to render.
*/
$solr_blocks = module_invoke_all("islandora_solr_query_blocks");
// The $op parameter determines what piece of information is being requested.
switch ($op) {
case 'list':
// If $op is "list", we just need to return a list of block descriptions.
// This is used to provide a list of possible blocks to the administrator,
// end users will not see these descriptions.
foreach ($solr_blocks as $name => $block) {
$blocks[$name] = array(
'info' => $block['name'],
'cache' => BLOCK_NO_CACHE,
);
}
return $blocks;
case 'configure':
// If $op is "configure", we need to provide the administrator with a
// configuration form. The $delta parameter tells us which block is being
// configured. In this example, we'll allow the administrator to customize
// the text of the first block.
$form = array();
return $form;
case 'save':
// If $op is "save", we need to save settings from the configuration form.
// Since the first block is the only one that allows configuration, we
// need to check $delta to make sure we only save it.
case 'view': default:
// If $op is "view", then we need to generate the block for display
// purposes. The $delta parameter tells us which block is being requested.
if (!empty($solr_blocks[$delta])) {
// First we'll set the block title.
$block['subject'] = $solr_blocks[$delta]['name'];
// Include the file from which the block originates.
require_once( drupal_get_path('module', $solr_blocks[$delta]['module']) . '/' . $solr_blocks[$delta]['file'] );
// If a class is present, instantiate it and proceed from there.
// The variable $queryClass (the IslandoraSolrQueryProcessor, containing
// the Solr search result), is fed as an argument.
if (!empty($solr_blocks[$delta]['class'])) {
$displayClass = new $solr_blocks[$delta]['class']();
$block_function = $solr_blocks[$delta]['function'];
if (method_exists($displayClass, $block_function)) {
$content = $displayClass->$block_function($queryClass);
$block['content'] = !empty($content) ? $content : NULL;
}
// Otherwise, simply load the form.
}
elseif (!empty($solr_blocks[$delta]['form'])) {
$block['content'] = drupal_get_form($solr_blocks[$delta]['form']);
}
// else if only a function is given (no class, no form)
elseif (!empty($solr_blocks[$delta]['function'])) {
$function = $solr_blocks[$delta]['function'];
$block['content'] = $function();
}
}
return $block;
}
}
/**
* islandora solr simple search form
* @return type
*/
function islandora_solr_simple_search_form() {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->build_simple_solr_form();
}
/**
* islandora solr simple search form submit
* @param type $form
* @param type $form_state
*/
function islandora_solr_simple_search_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$searchString = $form_state['values']['islandora_simple_search_query'];
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
// set query
$query = array('type' => 'dismax');
drupal_goto("islandora/solr/search/$searchString", $query);
}
/**
* islandora solr search block form
* @global type $queryClass
* @return type
*/
function islandora_solr_search_block_form() {
global $queryClass;
islandora_solr_search_init();
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->build_solr_search_form(NULL, NULL, $queryClass->solrQuery);
}
/**
* theme islandora solr search block form
* @global type $queryClass
* @param type $form
* @return type
*/
function theme_islandora_solr_search_block_form($form) {
global $queryClass;
islandora_solr_search_init();
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->theme_solr_search_form($form);
}
/**
* islandora solr search block form validate
* @param type $form
* @param type $form_state
*/
function islandora_solr_search_block_form_validate($form, &$form_state) {
$repeat = variable_get('islandora_solr_search_block_repeat', '3');
$found = FALSE;
$message = t("Please enter search term.");
for ($fieldNum = 1; $fieldNum <= $repeat; $fieldNum++) {
if (!empty($form_state['values']["fedora_terms$fieldNum"])) {
if($form_state['values']["fedora_terms$fieldNum"][0]== '*'){
unset($form_state['values']["fedora_terms$fieldNum"]);
$message = t("If !char is used in search field it must follow other characters.", array('!char' => '<bold>*</bold>'));
continue;
}
$found = TRUE;
}
}
if (!$found) {
form_set_error('edit_fedora_terms1', $message);
}
}
/**
* islandora solr search block form submit
* @global type $queryClass
* @param type $form
* @param type $form_state
*/
function islandora_solr_search_block_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
global $queryClass;
$type_id = trim($form_state['values']['type']);
$repeat = variable_get('islandora_solr_search_block_repeat', '3');
$fedora_terms = array();
$types = array();
$booleans = array();
for ($fieldNum = 1; $fieldNum <= $repeat; $fieldNum++) {
if ($form_state['values']["fedora_terms$fieldNum"]) {
$types[] = trim($form_state['values']["type$fieldNum"]);
$fedora_terms[] = lesser_escape(trim($form_state['values']["fedora_terms$fieldNum"]));
}
$next = $fieldNum + 1;
if ($form_state['values']["fedora_terms$next"] && $form_state['values']["fedora_terms$fieldNum"]) {
$booleans[] = trim($form_state['values']["andor$fieldNum"]);
}
}
for ($iteration = 0; $iteration < count($fedora_terms); $iteration++) {
//FIXME (minor): t() should be changed to format_string() in this instance, in Drupal 7
// The string isn't really translatable, just templated.
$searchString .= t("!field:(!query) !bool ", array(
'!field' => $types[$iteration],
'!query' => $fedora_terms[$iteration],
'!bool' => $booleans[$iteration]
));
}
$searchString = trim($searchString);
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
drupal_goto("islandora/solr/search/$searchString");
}
/**
* @return display switch
*/
function islandora_solr_search_display() {
global $queryClass;
// set variables
$output = '';
// check if the page is dealing with search results
if (isset($queryClass->display)) {
// parameters set in url
$params = $queryClass->internalSolrParams;
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($queryClass->solrQuery);
// get profiles
$profiles = module_invoke_all("islandora_solr_primary_display");
// get the table settings
$primary_display_array = variable_get('islandora_solr_primary_display_table', array());
if (!empty($primary_display_array)) {
$weight = $primary_display_array['weight'];
$enabled = $primary_display_array['enabled'];
$default = $primary_display_array['default'];
// sort by weight
array_multisort(array_values($weight), SORT_ASC, array_keys($weight), SORT_ASC, $weight);
// set variables
$list_items = array();
foreach ($weight as $key => $value) {
if ($enabled[$key] !== 0) {
// current display
if ($params['display']) {
$current_display = $params['display'];
}
else {
$current_display = $default;
}
// title
$display_name = $profiles[$key]['name'];
// query
$query = array_merge($params, array('display' => $key));
// set attributes
$attr = array();
// set class
if ($key == $current_display) {
$attr['class'] = 'active';
}
// nofollow
$attr['rel'] = 'nofollow';
// title
$attr['title'] = $display_name;
// url
$attr['href'] = url($path, array('query' => $query));
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$item = '<a' . drupal_attributes($attr) . '>' . check_plain($display_name) . '</a>';
// add link to list
$list_items[] = $item;
}
}
// theme item list (if more than one displays are available)
if (count($list_items) > 1) {
$output = theme('item_list', $list_items, NULL, "ul", array('class' => 'islandora-solr-search-display'));
}
}
}
return $output;
}
/**
* Callback function for Islandora Result Limit block. Returns a styled list of
* links to limit the search results per page.
*
* @global IslandoraSolrQueryProcessor $queryClass
* @return string
* Returns a themed unordered list containing links to limit search results
* per page.
*/
function islandora_solr_search_limit() {
global $queryClass;
// set variables
$output = '';
// check if the page is dealing with search results
if (isset($queryClass->display)) {
// parameters set in url
$params = $queryClass->internalSolrParams;
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($queryClass->solrQuery);
// get limit results values
$limit_results = variable_get('islandora_solr_search_limit_results_exposed', '10, 25, 50, 100');
// conver to array
$limit_results = explode(',', $limit_results);
if (!empty($limit_results)) {
// set variables
$list_items = array();
foreach ($limit_results as $limit) {
// trim value
$limit = trim($limit);
if (is_numeric($limit)) {
// current display
if ($params['limit']) {
$current_limit = $params['limit'];
}
else {
$current_limit = variable_get('islandora_solr_search_num_of_results', '25');
}
// query
$query = array_merge($params, array('limit' => $limit));
// set attributes
$attr = array();
// set class
if ($limit == $current_limit) {
$attr['class'] = 'active';
}
// nofollow
$attr['rel'] = 'nofollow';
// title
$attr['title'] = t('Show @limit results', array('@limit' => $limit));
// url
$attr['href'] = url($path, array('query' => $query));
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$item = '<a' . drupal_attributes($attr) . '>' . $limit . '</a>';
// add link to list
$list_items[] = $item;
}
}
// theme item list (if more than one displays are available)
if (count($list_items) > 1) {
$styling = (variable_get('islandora_solr_search_limit_results_exposed_styling', 0) == 1) ? ' horizontal' : '';
$output = theme('item_list', $list_items, NULL, "ul", array('class' => 'islandora-solr-search-limit' . $styling));
}
}
}
return $output;
}
/**
* Provides a list to change the sorting on a current search query.
*
* @global type $queryClass
* the IslandoraSolrQueryProcessor object which includes the current query
* settings and the raw Solr results.
* @return
* a rendered list containing Solr sort links
*
*/
function islandora_solr_search_sort() {
global $queryClass;
// set variables
$output = '';
// check if the page is dealing with search results
if (isset($queryClass->display)) {
// parameters set in url
$params = isset($queryClass->internalSolrParams) ? $queryClass->internalSolrParams : array();
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($queryClass->solrQuery);
// get sort terms
$sort_terms = islandora_build_substitution_list(variable_get('islandora_solr_search_sort_terms', 'score ~ Relevance, PID ~ PID'));
// get current sort (check in the actual parameters used for the query, because modules might have altered it)
if (isset($queryClass->solrParams['sort'])) {
$sort_explode = explode(' ', $queryClass->solrParams['sort']);
$current_sort_term = $sort_explode[0];
$current_sort_order = isset($sort_explode[1]) ? $sort_explode[1] : NULL;
}
else {
// if no parameters are given, internally solr defaults to 'score desc'
// http://wiki.apache.org/solr/CommonQueryParameters#sort
$current_sort_term = 'score';
$current_sort_order = 'desc';
}
// set variables
$list_items = array();
// loop over sort terms
foreach ($sort_terms as $term => $label) {
// set indicator variable
$indicator = '';
// create query and indicator arrow
if ($term == $current_sort_term) {
if (isset($current_sort_order) AND $current_sort_order == 'asc') {
// set order
$order = 'desc';
// create indicator
$indicator = theme('tablesort_indicator', 'desc');
}
else {
// set order
$order = 'asc';
// create indicator
$indicator = theme('tablesort_indicator', 'asc');
}
}
// the term is score or a date, we want descending order by default
elseif ($term == 'score' OR stripos($term, 'date')) {
// set order
$order = 'desc';
}
else {
// set order
$order = 'asc';
}
// set sort parameter
$sort_param = $term . ' ' . $order;
// set query
$query = array_merge($params, array('sort' => $sort_param));
// set attributes
$attr = array();
// set class
if ($term == $current_sort_term) {
$attr['class'] = 'active';
}
// nofollow
$attr['rel'] = 'nofollow';
// title
$attr['title'] = $label;
// url
$attr['href'] = url($path, array('query' => $query));
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$item = '<a' . drupal_attributes($attr) . '>' . check_plain($label) . ' ' . $indicator . '</a>';
// add link to list
$list_items[] = $item;
}
// theme item list
if (!empty($list_items)) {
$output = theme('item_list', $list_items, NULL, 'ul', array('class' => 'islandora-solr-search-sort'));
}
}
return $output;
}
/**
* Implements hook_forms().
*/
function islandora_solr_search_forms($form_id, $args) {
// Check if the form_id passed to drupal_get_form() contains the string 'islandora_solr_search_date_filter_form'
if (strpos($form_id, 'islandora_solr_search_date_filter_form') !== FALSE) {
// Lets attach islandora_solr_search_date_filter_form to $forms[$form_id]. This effectively allows you to use the same form builder function to build a form with any form_id of your choice.
$forms[$form_id] = array(
'callback' => 'islandora_solr_search_date_filter_form',
'callback arguments' => array_slice($args, 0, 3),
);
return $forms;
}
// Check if the form_id passed to drupal_get_form() contains the string 'islandora_solr_search_date_filter_form'
if (strpos($form_id, 'islandora_solr_search_range_slider_form') !== FALSE) {
// Lets attach islandora_solr_search_date_filter_form to $forms[$form_id]. This effectively allows you to use the same form builder function to build a form with any form_id of your choice.
$forms[$form_id] = array(
'callback' => 'islandora_solr_search_range_slider_form',
'callback arguments' => array_slice($args, 0, 6),
);
return $forms;
}
}
function islandora_solr_search_date_filter_form(&$form_state, $form_key, $solr_field, $noslider) {
date_default_timezone_set('UTC');
global $queryClass;
// set variables
$form = array();
$form['#tree'] = TRUE;
// Field
$form['date_filter_term'] = array(
'#type' => 'hidden',
'#value' => $solr_field,
'#name' => 'date_filter_term_' . $form_key,
);
// check if default value is possible
// parameters set in url
$params = isset($queryClass->internalSolrParams) ? $queryClass->internalSolrParams : array();
if (isset($params['f'])) {
$format = 'Y/m/d';
$term_count = 0;
foreach ($params['f'] as $key => $filter) {
if (strpos($filter, $solr_field) === 0) {
$term_count++;
// split the filter into field and value
$filter_split = explode(':', $filter, 2);
// trim brackets
$filter_split[1] = trim($filter_split[1], "\"");
// split range filter string to return formatted date values
$filter_str = $filter_split[1];
$filter_str = trim($filter_str, '[');
$filter_str = trim($filter_str, ']');
$filter_array = explode(' TO ', $filter_str);
// get timestamps
$from_unix = strtotime(trim($filter_array[0]));
$to_unix = strtotime(trim($filter_array[1]));
// only set default times if from date is lower than to date
if ($from_unix < $to_unix) {
$from_default = (strpos($filter_array[0], '*') !== FALSE) ? '*' : format_date($from_unix + 1, 'custom', $format, 0);
$to_default = (strpos($filter_array[1], '*') !== FALSE) ? '*' : format_date($to_unix, 'custom', $format, 0);
}
else {
$from_default = NULL;
$to_default = NULL;
}
}
}
}
if ($term_count != 1) {
$from_default = NULL;
$to_default = NULL;
}
if ($from_default != NULL OR $to_default != NULL OR $noslider) {
$class = 'date-range-expanded';
$value = t('Hide');
}
else {
$class = 'date-range-collapsed';
$value = t('Show');
}
// Show/hide link
$form['date_range_expand'] = array(
'#type' => 'markup',
'#value' => t('Specify date range: <a href="#" class="toggle-date-range-filter @class">@value</a>', array('@class' => $class, '@value' => $value)),
);
// wrapper around form elements
$form[$form_key] = array(
'#type' => 'markup',
'#prefix' => '<div class="date-range-filter-wrapper">',
'#suffix' => '</div>',
);
// description
$form[$form_key]['date_filter'] = array(
'#type' => 'markup',
'#value' => '<div class="description">' . t('Format: @date', array('@date' => date("Y/m/d"))) . '</div>',
);
// From
$form[$form_key]['date_filter_from'] = array(
'#type' => 'textfield',
'#title' => t('From'),
'#default_value' => ($from_default) ? $from_default : '',
'#size' => 10,
'#maxsize' => 10,
'#attributes' => array('class' => 'islandora-solr-search-datepicker'),
);
// To
$form[$form_key]['date_filter_to'] = array(
'#type' => 'textfield',
'#title' => t('To'),
'#default_value' => ($to_default) ? $to_default : '',
'#size' => 10,
'#maxsize' => 10,
'#attributes' => array('class' => 'islandora-solr-search-datepicker'),
);
// Submit
$form[$form_key]['date_filter_submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
'#name' => 'date_filter_term_' . $form_key,
);
$form['#submit'][] = 'islandora_solr_search_date_filter_form_submit';
// Validate
$form['#validate'][] = 'islandora_solr_search_date_filter_form_validate';
return $form;
}
function islandora_solr_search_date_filter_form_validate($form, &$form_state) {
// set variables
$form_key = $form_state['clicked_button']['#array_parents'][0];
$from = explode('/', $form_state['values'][$form_key]['date_filter_from']);
$to = explode('/', $form_state['values'][$form_key]['date_filter_to']);
// default for month and day
$default = '01';
// if the 'from' value is '*' just skip all checks
if (trim($from[0]) != '*' ) {
// Apply some defaults
if (!isset($from[1])) {
$from[1] = $default;
}
if (!isset($from[2])) {
$from[2] = $default;
}
// check from date
if (!checkdate(intval($from[1]), intval($from[2]), intval($from[0]))) {
form_set_error($form_key . '][date_filter_from', t('From date isn\'t formatted correctly.'));
}
}
// if the 'to' value is '*' just skip all checks
if (trim($to[0]) != '*' ) {
// Apply some defaults
if (!isset($to[1])) {
$to[1] = $default;
}
if (!isset($to[2])) {
$to[2] = $default;
}
// check to date
if (!checkdate(intval($to[1]), intval($to[2]), intval($to[0]))) {
form_set_error($form_key . '][date_filter_to', t('To date isn\'t formatted correctly.'));
}
}
}
function islandora_solr_search_date_filter_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
// set variables
global $queryClass;
$params = isset($queryClass->internalSolrParams) ? $queryClass->internalSolrParams : array();
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($queryClass->solrQuery);
// form key
$form_key = $form_state['clicked_button']['#array_parents'][0];
// term
$term = $form_state['values']['date_filter_term'];
// date
$from = explode('/', $form_state['values'][$form_key]['date_filter_from']);
$to = explode('/', $form_state['values'][$form_key]['date_filter_to']);
// if the 'from' value is '*' just skip all checks
if (trim($from[0]) != '*' ) {
// Apply some defaults
$default = '01';
if (!isset($from[1])) {
$from[1] = $default;
}
if (!isset($from[2])) {
$from[2] = $default;
}
// create date string
// example:
// timestamp:[1900-01-01T00:00:00Z TO 1990-01-01T00:00:00Z]
$from_str = "$from[0]-$from[1]-$from[2]T00:00:00Z";
}
else {
$from_str = $from[0];
}
// if the 'to' value is '*' just skip all checks
if (trim($to[0]) != '*' ) {
// Apply some defaults
if (!isset($to[1])) {
$to[1] = $default;
}
if (!isset($to[2])) {
$to[2] = $default;
}
// create date string
// example:
// timestamp:[1900-01-01T00:00:00Z TO 1990-01-01T00:00:00Z]
$to_str = "$to[0]-$to[1]-$to[2]T00:00:00Z";
}
else {
$to_str = $to[0];
}
// create filter
$filter = "$term:[$from_str TO $to_str]";
// set date filter key if there are no date filters included
if (isset($params['f'])) {
foreach ($params['f'] as $key => $f) {
if (strpos($f, $term) !== FALSE) {
array_splice($params['f'], $key);
}
}
$params['f'][] = $filter;
$query = $params;
}
else {
$query = array_merge_recursive($params, array('f' => array($filter)));
}
// redirect
drupal_goto($path, $query);
}
function islandora_solr_search_range_slider_form(&$form_state, $form_key, $solr_field, $facet_data, $slider_color, $gap, $date_format) {