forked from Islandora/islandora_solr_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IslandoraSolrResults.inc
1445 lines (1245 loc) · 51.1 KB
/
IslandoraSolrResults.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
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
* Contains methods to search solr and display results. depends on Apache_Solr_Php client.
*/
/**
* Islandora Solr Results
*/
class IslandoraSolrResults {
public static $SEARCH_CLASS_ADVANCED_SEARCH_NUMBER_FIELDS = 5;
public $facetFieldArray = array();
public $searchFieldArray = array();
public $resultFieldArray = array();
public $allSubsArray = array();
/**
* Constructor
*/
function IslandoraSolrResults() {
if (module_exists('apachesolr')) {
module_load_include('php', 'apachesolr', 'SolrPhpClient/Apache/Solr/Service');
}
else {
module_load_include('php', 'islandora_solr_search', 'SolrPhpClient/Apache/Solr/Service');
}
module_load_include('inc', 'islandora_solr_search', 'includes/common');
$this->prepFieldSubstitutions();
}
/**
* Output the main body of the search results
*
* @param type $solrQueryProcessor
* @param type $title
* @param type $output
* @return string
*/
function displayResults($solrQueryProcessor, $title = NULL, $output = '') {
// set variables to collect returned data.
$results = NULL;
$secondary_profiles = NULL;
$elements = array();
// set title
if (!is_null($title)) {
drupal_set_title($title);
}
// set breadcrumbs
$this->setBreadcrumbs($solrQueryProcessor);
// raw solr results
$apacheSolrResult = $solrQueryProcessor->solrResult;
// solr results count
// total solr results
$total = (int) $apacheSolrResult->response->numFound;
$elements['solr_total'] = $total;
// solr start
// to display: $solrQueryProcessor->solrStart + ($total > 0 ? 1 : 0)
$elements['solr_start'] = $solrQueryProcessor->solrStart;
// solr results end
$end = min(($solrQueryProcessor->solrLimit + $solrQueryProcessor->solrStart), $total);
$elements['solr_end'] = $end;
// pager
islandora_solr_search_pager_init($total, $solrQueryProcessor->solrLimit);
$elements['solr_pager'] = theme('pager', NULL, $solrQueryProcessor->solrLimit, 0, NULL, 5);
// debug (will be removed)
if (variable_get('islandora_solr_search_debug_mode', FALSE)) {
$elements['solr_debug'] = $this->printDebugOutput($apacheSolrResult);
}
// rendered secondary display profiles
$secondary_profiles = $this->addSecondaries($solrQueryProcessor);
// rendered results
$results = $this->printResults($apacheSolrResult);
// return themed layout
return theme('islandora_solr_wrapper', $results, $secondary_profiles, $elements);
}
function addSecondaries($solrQueryProcessor) {
$query_list = array();
// Get secondary display profiles
$secondary_display_profiles = module_invoke_all('islandora_solr_secondary_display');
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($solrQueryProcessor->solrQuery); // $_GET['q'] didn't seem to work here
// parameters set in url
$params = $solrQueryProcessor->internalSolrParams;
// Expected return:
//
// 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',
// 'logo' => 'Some text, some <html>, an <img>, etc used to link to this output,
// );
$secondary_array = variable_get('islandora_solr_secondary_display', array());
foreach ($secondary_array as $name => $status) {
if ($status === $name) {
// generate url
$query_secondary = array_merge($params, array('solr_profile' => $name));
// set attributes variable for remove link
$attr = array();
// set title
$attr['title'] = $secondary_display_profiles[$name]['description'];
// set rel
$attr['rel'] = 'nofollow';
// set url
$attr['href'] = url($path, array('query' => $query_secondary));
// logo
$logo = $secondary_display_profiles[$name]['logo'];
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$query_list[] = '<a' . drupal_attributes($attr) . '>' . $logo . '</a>';
}
}
return theme('item_list', $query_list, NULL, "ul", array('class' => 'islandora-solr-search-secondary'));
}
/**
* Function: printResults
*
* Description: translates a solr query result into a basic in-browser search result.
*
* @param Apache_Solr_Object $results
*/
function printResults($results) {
// set variables
$results_array = array();
$elements = array();
// total
$elements['solr_total'] = $results->response->numFound;
// start
$elements['solr_start'] = $results->response->start;
// get prepared search results
$docs = $this->prep_results($results);
// optionally limit results to values given
$limitResults = variable_get('islandora_solr_search_limit_result_fields', 0);
$pids = array();
// loop over results array
foreach ($docs as $doc) {
$pids[] = $doc['PID'];
$rows = array();
$row = 0; // keeping $row in there for now until the snippet issue is cleared out
$snippet = NULL; // @TODO: how is this suposed to work if the $snippet always is set to NULL ?
// loop over individual results to process fields
foreach ($doc as $field => $value) {
// check if this field should be included
if ($limitResults && empty($this->resultFieldArray[$field])) {
continue;
}
// check for field label substitutions
$translated_field_name = isset($this->allSubsArray[$field]) ? $this->allSubsArray[$field] : $field;
// add label
$rows[$field]['label'] = $translated_field_name;
// add class: render solr field name to string that can be used as class name
$rows[$field]['class'] = strtolower(preg_replace('/[^A-Za-z0-9]/', '-', $field));
// split value if the result value is an array
if (is_array($value)) {
$value = implode(", ", $value);
}
// add value
$rows[$field]['value'] = $value;
$row++; // keeping $row in there for now until the snippet issue is cleared out
}
// @TODO: how is this suposed to work if the $snippet always is set to NULL ?
if ($snippet) {
$rows[$row][] = array(
'data' => 'Full Text',
'header' => TRUE,
);
$rows[$row][] = $snippet[0];
}
// append array to results array
$results_array[] = $rows;
}
// make pids accessible without interfering with results
$elements['pids'] = $pids;
// return themed search results
return theme('islandora_solr_search', $results_array, $elements);
}
function printDebugOutput($results) {
// debug dump
$results_r .= "<pre>Results: " . print_r($results, TRUE) . "</pre>";
$fieldset_r = array(
'#title' => t("Raw Results"),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $results_r,
);
return theme('fieldset', $fieldset_r);
}
const FIELD_ALL = 0, FIELD_SEARCH = 1, FIELD_FACET = 2, FIELD_RESULT = 3;
function _make_pattern($string) {
return "/^" . preg_quote($string, '/') . "/";
}
function getSubstitutedFields($string_in, $type = FIELD_SEARCH) {
$replacements = NULL;
switch ($type) {
case FIELD_SEARCH:
$replacements = $this->searchFieldArray;
break;
case FIELD_FACET:
$replacements = $this->facetFieldArray;
break;
case FIELD_RESULT:
$replacements = $this->resultFieldArray;
break;
case FIELD_ALL:
default:
$replacements = $this->allSubsArray;
}
return preg_replace(array_map(array($this, '_make_pattern'), array_keys($replacements)), array_values($replacements), $string_in);
}
/**
* Function: currentQuery
*
* Description: Displays elements of the current solr query (with enabled filters).
* This functionality was previously rolled into the facet block, but it makes
* sense to separate the two functions, particularly as the current query is now
* imprinted in the system's breadcrumb.
*
* @param IslandoraSolrQueryProcessor $solrQueryProcessor
*/
function currentQuery($solrQueryProcessor) {
date_default_timezone_set('UTC');
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($solrQueryProcessor->solrQuery); // $_GET['q'] didn't seem to work here
// get date format
$format = variable_get('islandora_solr_facet_date_format', 'Y');
// set output variable
$output = '';
// get user provided filter parameters
$fq = $solrQueryProcessor->internalSolrParams['f'];
// parameters set in url
$params = $solrQueryProcessor->internalSolrParams;
// Get Query values
if (!in_array($solrQueryProcessor->solrQuery, $solrQueryProcessor->different_kinds_of_nothing)) {
// get query value
$query_value = $solrQueryProcessor->solrQuery;
// set list variables
$query_list = array();
// remove link keeps all parameters (query gets removed instead)
$query_minus = array();
$query_minus = $params;
// remove query from path
$path_minus = implode('/', explode('/', $path, -1)) . '/ ';
// set attributes variable for remove link
$attr_minus = array();
// set title
$attr_minus['title'] = t('Remove') . ' ' . $query_value;
// set rel
$attr_minus['rel'] = 'nofollow';
// set url
$attr_minus['href'] = url($path_minus, array('query' => $query_minus));
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$query_list[] = '<a' . drupal_attributes($attr_minus) . '>(-)</a> ' . check_plain($query_value);
// add wrap and list
$output .= '<div class="islandora_solr_search_query_wrap">';
$output .= theme_item_list($query_list, t('Query'), "ul", array('class' => 'islandora_solr_search_query_list query_list'));
$output .= '</div>'; // 'islandora_solr_search_query_wrap'
}
// Get Filter values
if (!empty($fq)) {
// set list variables
$filter_list = array();
// loop over filters
foreach ($fq as $key => $filter) {
// check for exclude filter
if ($filter[0] == '-') {
// $symbol = 'ࣔ';
$symbol = '≠';
}
else {
$symbol = '=';
}
// split the filter into field and value
$filter_split = explode(':', $filter, 2);
// trim brackets
// $filter_split[1] = trim($filter_split[1], "\"");
$filter_value = trim($filter_split[1], "\"");
// if value is date
if (isset($solrQueryProcessor->solrParams['facet.date']) AND in_array(ltrim($filter_split[0], '-'), $solrQueryProcessor->solrParams['facet.date'])) {
// split range filter string to return formatted date values
$filter_str = $filter_value;
$filter_str = trim($filter_str, '[');
$filter_str = trim($filter_str, ']');
$filter_array = explode(' TO ', $filter_str);
$from_str = (strpos($filter_array[0], '*') !== FALSE) ? '*' : format_date(strtotime(trim($filter_array[0])) + 1, 'custom', $format, 0);
$to_str = (strpos($filter_array[1], '*') !== FALSE) ? '*' : format_date(strtotime(trim($filter_array[1])), 'custom', $format, 0);
if ($from_str == $to_str OR trim($filter_array[0]) == trim($filter_array[1])) {
$filter_value = $from_str;
}
else {
$filter_value = $from_str . ' - ' . $to_str;
}
}
// pull out filter (for exclude link)
$query_minus = array();
$f_x['f'] = array_diff($params['f'], array($filter));
$query_minus = array_merge($params, $f_x);
// resetting the filter key's order // @TODO find a cleaner way to do this.
if ($query_minus['f']) {
$query_minus['f'] = array_merge(array(), $query_minus['f']);
}
// remove 'f' if empty
if (empty($query_minus['f'])) {
unset($query_minus['f']);
}
// set attributes variable for remove link
$attr_minus = array();
// set title
$attr_minus['title'] = t('Remove') . ' ' . $filter;
// set rel
$attr_minus['rel'] = 'nofollow';
// set url
$attr_minus['href'] = url($path, array('query' => $query_minus));
// create link
// we're not using l() because of active classes: http://drupal.org/node/41595
$filter_list[] = '<a' . drupal_attributes($attr_minus) . '>(-)</a> ' . $symbol . ' ' . check_plain($filter_value);
}
// return filter list
$output .= '<div class="islandora_solr_search_filter_wrap">';
$output .= theme_item_list($filter_list, t("Enabled Filters"), "ul", array('class' => 'islandora_solr_search_filter_list filter_list'));
$output .= '</div>'; // 'islandora_solr_search_filter_wrap'
}
return $output;
}
/**
* Function: setBreadcrumbs
*
* Description: sets the drupal breadcrumbs based on the current query and filters.
*/
function setBreadcrumbs($solrQueryProcessor) {
// set path
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($solrQueryProcessor->solrQuery); // $_GET['q'] didn't seem to work here
// get date format
$format = variable_get('islandora_solr_facet_date_format', 'Y');
// set breadcrumb variable
$breadcrumb = array();
// get user provided filter parameters
$fq = $solrQueryProcessor->internalSolrParams['f'];
// parameters set in url
$params = $solrQueryProcessor->internalSolrParams;
// set filter key if there are no filters included
if (empty($params['f'])) {
$params['f'] = array();
}
// loop to create filter breadcrumbs if available
if (!empty($fq)) {
// set var
$f['f'] = array();
foreach ($fq as $key => $filter) {
// check for exclude filter
$exclude = FALSE;
if ($filter[0] == '-') {
$exclude = TRUE;
}
// split the filter into field and value
$filter_split = explode(':', $filter, 2);
// trim brackets
$filter_value = trim($filter_split[1], "\"");
// if value is date
if (isset($solrQueryProcessor->solrParams['facet.date']) AND in_array(ltrim($filter_split[0], '-'), $solrQueryProcessor->solrParams['facet.date'])) {
// split range filter string to return formatted date values
$filter_str = $filter_value;
$filter_str = trim($filter_str, '[');
$filter_str = trim($filter_str, ']');
$filter_array = explode(' TO ', $filter_str);
$from_str = (strpos($filter_array[0], '*') !== FALSE) ? '*' : format_date(strtotime(trim($filter_array[0])) + 1, 'custom', $format, 0);
$to_str = (strpos($filter_array[1], '*') !== FALSE) ? '*' : format_date(strtotime(trim($filter_array[1])), 'custom', $format, 0);
if ($from_str == $to_str OR trim($filter_array[0]) == trim($filter_array[1])) {
$filter_value = $from_str;
}
else {
$filter_value = $from_str . ' - ' . $to_str;
}
}
// increment filter array with current filter (for breadcrumb link)
$query = array();
$query_diff = array_diff($params, array('f' => array()));
$f = array_merge_recursive($f, array('f' => array($filter)));
$query = array_merge($query_diff, $f);
// pull out filter (for x link)
$query_x = array();
$f_x['f'] = array_diff($params['f'], array($filter));
$query_x = array_merge($params, $f_x);
// resetting the filter key's order // @TODO find a cleaner way to do this.
if ($query_x['f']) {
$query_x['f'] = array_merge(array(), $query_x['f']);
}
// remove 'f' if empty
if (empty($query_x['f'])) {
unset($query_x['f']);
}
// set attributes variable
$attr = array();
// set title
$attr['title'] = $filter;
// set rel
$attr['rel'] = 'nofollow';
// exclude filter: add class "strikethrough"
if ($exclude) {
$attr['class'] = 'strikethrough';
}
// set url
$attr['href'] = url($path, array('query' => $query));
// set attributes variable for remove link
$attr_x = array();
// set title
$attr_x['title'] = t('Remove') . ' ' . $filter;
// set rel
$attr_x['rel'] = 'nofollow';
// set url
$attr_x['href'] = url($path, array('query' => $query_x));
// create links
// we're not using l() because of active classes: http://drupal.org/node/41595
$breadcrumb[] = '<a' . drupal_attributes($attr) . '>' . check_plain($filter_value) . '</a>'
. '<span class="islandora_solr_search_breadcrumb_super"> <a' . drupal_attributes($attr_x) . '>(' . t('x') . ')</a></span>';
}
// at this point reverse the breadcrumbs array (only contains filters)
$breadcrumb = array_reverse($breadcrumb);
}
// create Query breadcrumb
if (!in_array($solrQueryProcessor->solrQuery, $solrQueryProcessor->different_kinds_of_nothing)) {
// get query value
$query_value = $solrQueryProcessor->solrQuery;
// remove all filters for this breadcrumb
$query = array();
$query = array_diff($params, array('f' => array()));
// remove button keeps all parameters (query gets removed instead)
$query_x = array();
$query_x = $params;
if (empty($params['f'])) {
unset($query_x['f']);
}
// remove query from path
$path_x = implode('/', explode('/', $path, -1)) . '/ ';
// set attributes variable
$attr = array();
// set title
$attr['title'] = $query_value;
// set rel
$attr['rel'] = 'nofollow';
// set url
$attr['href'] = url($path, array('query' => $query));
// set attributes variable for remove link
$attr_x = array();
// set title
$attr_x['title'] = t('Remove') . ' ' . $query_value;
// set rel
$attr_x['rel'] = 'nofollow';
// set url
$attr_x['href'] = url($path_x, array('query' => $query_x));
// remove solr fields from breadcrumb value
$query_explode = explode(' ', $query_value);
$query_implode = array();
foreach ($query_explode as $value) {
// check for first colon to split the string
if (strpos($value, ':') != FALSE) {
// split the filter into field and value
$value_split = explode(':', $value, 2);
// trim whitespace
$value_split[1] = trim($value_split[1]);
// trim brackets
$value = str_replace(array('(', ')'), '', $value_split[1]);
}
// no colon is found
else {
$value = trim($value);
// strip brackets
$value = str_replace(array('(', ')'), '', $value);
}
$query_implode[] = $value;
}
$query_value = implode(" ", $query_implode);
// create links
// we're not using l() because of active classes: http://drupal.org/node/41595
$breadcrumb[] = '<a' . drupal_attributes($attr) . '>' . check_plain($query_value) . '</a>'
. '<span class="islandora_solr_search_breadcrumb_super"> <a' . drupal_attributes($attr_x) . '>(' . t('x') . ')</a></span>';
}
$breadcrumb[] = l(t('Home'), '<front>', array('attributes' => array('title' => t('Home'))));
if (!empty($breadcrumb))
$breadcrumb = array_reverse($breadcrumb);
drupal_set_breadcrumb($breadcrumb);
}
/**
* Function: prepFieldSubstitutions
*
* Description: reads configuration values and preps a number of key=>value
* arrays for output substitution
*/
function prepFieldSubstitutions() {
$rawFacetVals = variable_get("islandora_solr_search_block_facets", 'dc.subject ~ Subject,dc.type ~ Type');
$this->facetFieldArray = islandora_build_substitution_list($rawFacetVals);
// populate with date facet readable names
$rawDateFacetVals = variable_get('islandora_solr_facet_date', NULL);
$rawDateFacetVals = islandora_build_substitution_list($rawDateFacetVals);
if ($rawDateFacetVals != NULL) {
$this->facetFieldArray = array_merge($this->facetFieldArray, $rawDateFacetVals);
}
$rawSearchTerms = variable_get('islandora_solr_searchterms', 'dc.title ~ Title');
$this->searchFieldArray = islandora_build_substitution_list($rawSearchTerms);
$rawResultFields = variable_get('islandora_solr_search_result_fields', 'dc.subject ~ Subject,dc.type ~ Type');
$this->resultFieldArray = islandora_build_substitution_list($rawResultFields);
$this->allSubsArray = array_merge($this->facetFieldArray, $this->searchFieldArray, $this->resultFieldArray);
}
/**
* Function: displayFacets
*
* Description: Displays basic facets based on an apache solr query response,
* as contained with the IslandoraSolrQueryProcessor.
*
* @global string $base_url
* @param IslandoraSolrQueryProcessor $solrQueryProcessor
* @return string
*/
function displayFacets($solrQueryProcessor) {
// set variables
$facet_output = $output = '';
$range_slider_key = 0;
$date_filter_key = 0;
// shown limit
$facet_min_count = 1;
$min_result = variable_get('islandora_solr_search_block_facet_min_count', '2');
$initial_limit = variable_get('islandora_solr_search_block_facet_shown_limit', '0');
$facet_limit = variable_get('islandora_solr_search_block_facet_limit', '20');
// get facet values returned from solr
$facets = isset($solrQueryProcessor->solrResult->facet_counts->facet_fields) ? $solrQueryProcessor->solrResult->facet_counts->facet_fields : array();
// object to json
$facets_json = json_encode($facets);
// json to array
$facets = json_decode($facets_json, true);
// get date facet values
$date_facets = isset($solrQueryProcessor->solrResult->facet_counts->facet_dates) ? $solrQueryProcessor->solrResult->facet_counts->facet_dates : array();
// check if we need to set a variable range gap
if (variable_get('islandora_solr_facet_date_gap_variable', 0) == 1 AND isset($solrQueryProcessor->solrResult->facet_counts->facet_dates)) {
$variable_range_data = $this->variableRangeGap($solrQueryProcessor);
$date_facets = $variable_range_data['date_facets'];
$solr_call = $variable_range_data['solr_call'];
}
// object to json
$date_facets_json = json_encode($date_facets);
// json to array
$date_facets_arr = json_decode($date_facets_json, true);
// get date format
$format = variable_get('islandora_solr_facet_date_format', 'Y');
// set variable
$date_facets = array();
// loop over each date field
foreach ($date_facets_arr as $key => $fields) {
// loop over each field
$date_facets[$key] = array();
foreach ($fields as $date => $bucket_size) {
// facet.date includes other values in the facet results - excluding these
// we're turning the facet results in an array with multiple values
if (!in_array($date, array('gap', 'end', 'other', 'hardend', 'include'))) {
// set bucket size or documents
$date_facets[$key][$date]['bucket_size'] = $bucket_size;
// logic to get the next range key (next date)
$field_keys = array_keys($fields);
$field_key = array_search($date, $field_keys);
$field_key++;
$date_next = (!in_array($field_keys[$field_key], array('gap', 'end', 'other', 'hardend', 'include'))) ? $field_keys[$field_key] : $fields['end'];
// set date range filter for facet url
$date_facets[$key][$date]['range'] = '[' . $date . ' TO ' . $date_next . ']';
// set formatted value for facet link
$from_str = format_date(strtotime(trim($date)) + 1, 'custom', $format, 0);
$to_str = format_date(strtotime(trim($date_next)), 'custom', $format, 0);
if ($from_str == $to_str) {
$filter_value = $from_str;
}
else {
$filter_value = $from_str . ' - ' . $to_str;
}
$date_facets[$key][$date]['value'] = $filter_value;
}
}
}
// merge facet fields with date facets
$facets = array_merge($date_facets, $facets);
// return if no facets are available
if (empty($facets)) {
return $output;
}
// set show more variable
$show_more = FALSE;
// loop over returned facet objects
foreach ($facets as $facet_key => $facet) {
$list_items = array();
$list_type = "ul";
$list_title = NULL;
// if the facet contains the minimum amount of values
// @TODO: added the range slider rule, but should be solved more elegantly
if (count($facet) >= $facet_min_count AND !(variable_get('islandora_solr_range_slider_enabled', 0) == 1 AND array_key_exists($facet_key, $date_facets_arr))) {
// set facet count
$facet_count = 0;
// render facet values
foreach ($facet as $value => $data) {
// add facet
$this->_addFacets($facet_key, $value, $data, $solrQueryProcessor, $facet_count, $list_items, $min_result);
}
// check for minumum returned values again, because some might have been disabled
if (count($list_items) >= $facet_min_count) {
// opening facet div // @TODO: this should go in theme function
$facet_output .='<div class="islandora_solr_search_facet">';
// set title
$list_title = $this->facetFieldArray[$facet_key];
// set attributes
$list_attributes = array('class' => 'islandora_solr_search_facet_list facet_list');
// check if facet value amount is larger than the initial limit
if ($facet_count > $initial_limit AND $initial_limit < $facet_limit AND $initial_limit > $facet_min_count) {
$split_facet = TRUE;
}
else {
$split_facet = FALSE;
}
if ($split_facet) {
$show_more = TRUE; //There exists a split array...
$list_items_hidden = array_slice($list_items, $initial_limit);
$list_items = array_slice($list_items, 0, $initial_limit);
}
// theme the facet as a list
$facet_output .= theme('item_list', $list_items, $list_title, $list_type, $list_attributes);
// render the hidden facets
if ($split_facet) {
$facet_output .= '<a href="#" class="shown-toggle">' . t('Show more') . '</a>';
$facet_output .= '<a href="#" class="shown-toggle hidden">' . t('Show less') . '</a>';
$facet_output .= '<div class="toggle-wrapper hidden">';
$facet_output .= theme('item_list', $list_items_hidden, NULL, $list_type, $list_attributes);
$facet_output .= '</div>';
}
$facet_output .='</div>'; //div.islandora_solr_search_facet
}
}
// check if we need range slider.
if (variable_get('islandora_solr_range_slider_enabled', 0) == 1
AND array_key_exists($facet_key, $date_facets_arr)
AND $solrQueryProcessor->solrResult->response->numFound > 0
AND module_exists('jquery_ui')
AND jquery_ui_get_version() >= 1.7
AND !islandora_solr_is_ie_less_then(9)) {
// facet data
// @TODO: this is pretty bad code. Clean this up.
// From here
$facet_data = $date_facets_arr[$facet_key];
$facet_gap = $facet_data['gap'];
$facet_end = $facet_data['end'];
unset($facet_data['gap']);
unset($facet_data['start']);
unset($facet_data['end']); // 'gap', 'end', 'other', 'hardend', 'include'
unset($facet_data['other']);
unset($facet_data['hardend']);
unset($facet_data['include']);
// strip empty buckets left and right when no date filters are set
if (isset($solr_call) AND !in_array($facet_key, $solr_call)) {
foreach ($facet_data as $key => $value) {
if ($value == 0) {
unset($facet_data[$key]);
}
else {
break;
}
}
$facet_data = array_reverse($facet_data);
$end = array();
foreach ($facet_data as $date => $bucket_size) {
if ($bucket_size == 0) {
unset($facet_data[$date]);
$end = array('date' => $date, 'bucket_size' => $bucket_size);
}
else {
break;
}
}
$facet_data = array_reverse($facet_data);
// add end date
if (isset($end['bucket_size']) AND $end['bucket_size'] == 0) {
$end_date = $end['date'];
$facet_data[$end_date] = NULL;
}
else {
$facet_data[$facet_end] = NULL;
}
}
// do not strip empty buckets left and right when filters are set
else {
// add end date
$facet_data[$facet_end] = NULL;
}
// calculate gap
$calc_from = strtotime(key($facet_data));
next($facet_data);
$calc_to = strtotime(key($facet_data));
// calculate difference between from and to date
$calc_diff = abs($calc_from - $calc_to);
// total difference in days
$calc_total_days = floor($calc_diff / 60 / 60 / 24);
$gap = NULL;
if ($calc_total_days == 1) {
$gap = t('days');
}
elseif ($calc_total_days == 7) {
$gap = t('weeks');
}
elseif ($calc_total_days >= 28 AND $calc_total_days <= 32) {
$gap = t('months');
}
elseif ($calc_total_days >= 360 AND $calc_total_days <= 370) {
$gap = t('years');
}
elseif ($calc_total_days >= 720 AND $calc_total_days <= 740) {
$gap = t('2 years');
}
elseif ($calc_total_days >= 1800 AND $calc_total_days <= 1850) {
$gap = t('5 years');
}
elseif ($calc_total_days >= 3600 AND $calc_total_days <= 3700) {
$gap = t('decades');
}
elseif ($calc_total_days >= 36000 AND $calc_total_days <= 37000) {
$gap = t('centuries');
}
elseif ($calc_total_days >= 360000 AND $calc_total_days <= 370000) {
$gap = t('millennia');
}
if (strpos($gap, t('year')) !== FALSE OR strpos($gap, t('decades')) !== FALSE OR strpos($gap, t('centuries')) !== FALSE OR strpos($gap, t('millennia')) !== FALSE) {
$date_format = 'Y';
}
elseif (strpos($gap, t('months')) !== FALSE) {
$date_format = 'M Y';
}
else {
$date_format = 'M j, Y';
}
// create a nice array with our data
$data = array();
foreach ($facet_data as $date => $bucket_size) {
$format_date_str = format_date(strtotime(trim($date)) + 1, 'custom', $date_format, 0);
$format_date_str = str_replace(' ', ' ', $format_date_str);
$data[] = array(
'date' => $date,
'format_date' => $format_date_str,
'bucket_size' => $bucket_size,
);
}
// add range slider color
$slider_color = variable_get('islandora_solr_range_slider_color', '#edc240');
// To here
// if values are available
$noslider = FALSE;
if (count($data) > 1) {
// render form
// opening facet div // @TODO: this should go in theme function
$facet_output .='<div class="islandora_solr_search_range_slider">';
// add title
$facet_output .='<h3>' . $this->facetFieldArray[$facet_key] . '</h3>';
$facet_output .= drupal_get_form('islandora_solr_search_range_slider_form_' . $range_slider_key, $range_slider_key, $facet_key, $data, $slider_color, $gap, $date_format);
$range_slider_key++;
$facet_output .='</div>';
}
else {
$noslider = TRUE;
}
}
// add datefilter
if (variable_get('islandora_solr_date_filter_enabled', 0) == 1 AND array_key_exists($facet_key, $date_facets_arr) AND module_exists('jquery_ui') AND $solrQueryProcessor->solrResult->response->numFound > 0) {
// opening facet div // @TODO: this should go in theme function
$facet_output .='<div class="islandora_solr_search_date_filter">';
// @TODO: set title when the range slider isn't active
if (count($list_items) < $facet_min_count AND !(variable_get('islandora_solr_range_slider_enabled', 0) == 1 AND array_key_exists($facet_key, $date_facets_arr)) OR $noslider) {
// add title
$facet_output .='<h3>' . $this->facetFieldArray[$facet_key] . '</h3>';
}
$facet_output .= drupal_get_form('islandora_solr_search_date_filter_form_' . $date_filter_key, $date_filter_key, $facet_key, $noslider);
$date_filter_key++;
$facet_output .='</div>';
}
}
// include jquery ui datepicker
if ($date_filter_key > 0 AND module_exists('jquery_ui')) {
// include datepicker js
jquery_ui_add(array('ui.datepicker'));
// add datepicker css
drupal_add_css('sites/all/libraries/jquery.ui/themes/base/ui.all.css');
$date_range = variable_get('islandora_solr_search_datepicker_range', '-100:+3');
// add js settings
drupal_add_js(array('islandora_solr_search_datepicker_range' => trim($date_range)), 'setting');
}
// add js for facets
if ($show_more OR $date_filter_key > 0) {
// if there is at least one occurence of a split array, we add js to the page.
drupal_add_js(drupal_get_path('module', 'islandora_solr_search') . '/js/islandora_solr_search_facets.js');
}
//////////////////////////////////////////////////////////////////////////////////////
// as we add additional facets, we're repeatedly URL-encoding old facet //
// strings. when we double-encode quotation marks they're incomprehensible to solr. //
// This is a quick workaround: //
//////////////////////////////////////////////////////////////////////////////////////
$facet_output = str_replace('%2B', '%252B', $facet_output);
if (!empty($facet_output)) {
$output .='<div class="islandora_solr_search_facet_wrap">';
$output .= $facet_output;
$output .= '</div>';
}
return $output;
}
/**
* Add facet callback
* @param type $key
* @param type $name
* @param type $number
* @param type $solrQueryProcessor
* @param int $facet_count
* @param string $list
*/
function _addFacets(&$key, &$value, &$data, &$solrQueryProcessor, &$facet_count, &$list, $min_result) {
// value of date facet is an array, normal facets is a string.
if (is_array($data)) {
// set value
$value = $data['value'];
// set filter to include in url
$filter = $key . ':' . $data['range'];
// set number of found solr documents
$data = $data['bucket_size'];
// don't include facet fields with a document count lower than the configured minimum
if ($data < $min_result) {
return;
}
}
else {
// @TODO: Should be changed to format_string for Drupal 7
// @TODO: does this check for exclude filters? -PID:"foo"
$filter = $key . ':"' . $value . '"'; // PID:"foo"
}
//FIXME: This isn't quite right... It can make sense to facet on
// the same field multiple times, provided it is multi-valued...
$fq = (array) $solrQueryProcessor->solrParams['fq'];
// check if item should show up
// 1. check if the filter isn't active
// 2. if the results of the facet items equals the total of current returned results, that means it's useless to include or exclude the filter.
$disable_link = (array_search($filter, $fq) != FALSE) || $data == $solrQueryProcessor->solrResult->response->numFound;
// if link isn't disabled
if (!$disable_link) {
// set variables
$path = SOLR_SEARCH_PATH . '/' . replaceSlashes($solrQueryProcessor->solrQuery); // $_GET['q'] didn't seem to work here
$query_plus = array();
$query_minus = array();
// parameters set in url
$params = $solrQueryProcessor->internalSolrParams;
// set filter key if there are no filters included
if (empty($params['f'])) {
$params['f'] = array();
}
// merge recursively to add new filter parameter
$query_plus = array_merge_recursive($params, array('f' => array($filter)));
$query_minus = array_merge_recursive($params, array('f' => array('-' . $filter)));