forked from ding2/ting_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting_search.module
977 lines (865 loc) · 27.1 KB
/
ting_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
<?php
/**
* @file
* Enables search integration with Ting.
*/
require_once 'includes/TingSearchCqlDoctor.class.inc';
/**
* Implements hook_init().
*
* Make the ting search overlay available for all other JavaScripts around the
* site.
*/
function ting_search_init() {
drupal_add_js(drupal_get_path('module', 'ting_search') . '/js/ting_search_overlay.js');
}
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types.
*/
function ting_search_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function ting_search_ctools_plugin_api($module, $api) {
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 1);
}
}
/**
* Implements hook_menu().
*/
function ting_search_menu() {
$items = array();
$items['admin/config/ting/search'] = array(
'title' => 'Search settings',
'description' => 'Settings avialable for ting search.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_search_admin_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'includes/ting_search.admin.inc',
);
$items['search-blank'] = array(
'callback' => '_blank_page',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu callback to fix the twice search issue.
*
* When the search form is submitted back to Drupal it's submitted back to the
* same page. So a search performed on the search page will trigger the first
* search and the new search (twice search).
*
* @see http://platform.dandigbib.org/issues/372
*/
function _blank_page() {
return '';
}
/**
* Implements hook_menu_alter().
*
* Temporary hack to alter titles.
*/
function ting_search_menu_alter(&$items) {
$items['search/node']['title'] = 'Homepage';
$items['search/node/%menu_tail']['title'] = 'Homepage';
$items['search/node/%menu_tail']['load arguments'] = array('%map', '%index');
$items['search/ting']['title'] = 'Data well';
$items['search/ting/%menu_tail']['title'] = 'Data well';
$items['search/meta/%menu_tail']['load arguments'] = array('%map', '%index');
$items['search/meta']['title'] = 'Universal Search';
$items['search/meta/%menu_tail']['title'] = 'Universal Search';
$items['search/meta/%menu_tail']['load arguments'] = array('%map', '%index');
}
/**
* Implements hook_search_info().
*/
function ting_search_search_info() {
return array(
'title' => t('Ting'),
'path' => 'ting',
'conditions_callback' => 'ting_search_conditions_callback',
);
}
/**
* Implements hook_ding_facetbrowser().
*/
function ting_search_ding_facetbrowser() {
$results = new stdClass();
$results->show_empty = FALSE;
$search_result = drupal_static('ting_search_results');
if ($search_result) {
$results->facets = ($search_result instanceof TingClientSearchResult) ? $search_result->facets : array();
$results->searchkey = $search_result->search_key;
return $results;
}
}
/**
* Implements hook_theme().
*/
function ting_search_theme($existing, $type, $theme, $path) {
return array(
'ting_search_results' => array(
'variables' => array('results' => NULL, 'module' => NULL),
'file' => 'ting_search.pages.inc',
'template' => 'templates/ting-search-results',
),
'ting_search_mini_pager' => array(
'variables' => array(
'tags' => array(),
'element' => 0,
'parameters' => array(),
'quantity' => 9,
),
),
'ting_search_pager' => array(
'variables' => array(
'tags' => array(),
'element' => 0,
'parameters' => array(),
'quantity' => 9,
),
),
'ting_search_display_extended_query' => array(
'variables' => array('query_label' => NULL,'query_string' => NULL),
'template' => 'ting-search-display-extended-query',
'path' => $path . '/templates',
),
'ting_search_extended_search' => array(
'template' => 'ting-search-extended-search',
'path' => $path . '/templates',
),
);
}
/**
* Returns a themed list of extended actions.
*
* @return string
* HTML ting_search_extended_search.
*/
function ting_search_get_extended_actions() {
return theme('ting_search_extended_search');
}
/**
* Implements hook_form_FORM_ID_alter().
*
* For the form search_block_form.
*/
function ting_search_form_search_block_form_alter(&$form, &$form_state, $form_id) {
// Define advanced fields.
$advanced_fields = array(
'term.creator' => array(
'key' => 'creator',
'title' => t('Author'),
'description' => t('Enter the author name'),
),
'term.title' => array(
'key' => 'title',
'title' => t('Title'),
'description' => t('Enter title'),
),
'term.subject' => array(
'key' => 'subject',
'title' => t('Subject'),
'description' => t('Enter subject keywords'),
),
);
// We're going to disable advanced search in
// the first version, and implement later on.
// When implementing againg, set
// $advanced = TRUE - just below
// and re-enable asserts in testTingExtended() in ting_search.test.
$advanced = FALSE;
// Parse extended search query parameters.
if (arg(0) == 'search') {
$parts = explode('/', $_GET['q']);
// Lose 'search' and the search type.
array_shift($parts);
$type = array_shift($parts);
$search_query = implode('/', $parts);
$indexes = ting_search_extract_keys($search_query, array_keys($advanced_fields));
$search_query = $indexes['q'];
unset($indexes['q']);
if ($type != 'ting' and !empty($indexes)) {
$search_query .= " " . implode(' ', $indexes);
$indexes = array();
$advanced = FALSE;
}
// Set default value to the current search query.
$form['search_block_form']['#default_value'] = $search_query;
}
// Set max length on search input. This is need when users uses CQL search
// string as they can get very long. This length is base on experience from
// ding1 as seams to be long enough.
$form['search_block_form']['#maxlength'] = 256;
// Add JS to handle spinner in the search form.
$form['search_block_form']['#attached']['js'] = array(
drupal_get_path('module', 'ting_search') . '/js/ting_search.js',
);
$form['sort'] = array(
'#type' => 'hidden',
'#default_value' => isset($_GET['sort']) ? check_plain($_GET['sort']) : FALSE,
'#attributes' => array('id' => 'controls_search_sort'),
);
$form['size'] = array(
'#type' => 'hidden',
'#default_value' => isset($_GET['size']) ? (int) $_GET['size'] : FALSE,
'#attributes' => array('id' => 'controls_search_size'),
);
// See line 127-130 - disabled in the first version.
//$form['form_id']['#suffix'] = ting_search_get_extended_actions();
if ($advanced) {
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced search'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 101,
'#prefix' => '<div id="advanced-search-wrapper">',
'#suffix' => '</div>',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'ting_search') . '/js/ting_search_extendform.js',
),
),
);
foreach ($advanced_fields as $name => $field) {
$form['advanced'][$field['key']] = array(
'#type' => 'textfield',
'#title' => check_plain($field['title']),
'#size' => 30,
'#maxlength' => 64,
'#description' => check_plain($field['description']),
'#default_value' => isset($indexes[$name]) ? $indexes[$name] : '',
);
}
}
$form['#submit'] = array('ting_search_submit');
// This submits the form to a blank page and prevents the search to be
// executed twice. See http://platform.dandigbib.org/issues/372 for more
// information.
$form['#action'] = '?q=search-blank';
return $form;
}
/**
* Extract special field keys from search string.
*
* @param string $search_query
* The search query.
* @param array $keys
* Keys to extract.
*
* @return array
* Where the array keys are the search keys, and the remainder
* search string in 'q'.
*/
function ting_search_extract_keys($search_query, $keys) {
$indexes = &drupal_static('ting_search_extract_indexes', array());
ting_search_extract_indexes(NULL, $keys);
$regexpes = array(
'/(^|\s+and\s+)\(([^)]+)\)$/i',
);
foreach ($regexpes as $regexp) {
$search_query = preg_replace_callback($regexp, 'ting_search_extract_indexes', $search_query);
}
$indexes['q'] = $search_query;
return $indexes;
}
/**
* Preg_replace_callback function.
*/
function ting_search_extract_indexes($matches, $set_keys = NULL) {
static $keys;
if ($set_keys) {
$keys = array_flip($set_keys);
}
$return = array();
$subexps = preg_split('/\s+and\s+/i', $matches[2], NULL, PREG_SPLIT_NO_EMPTY);
$indexes = &drupal_static(__FUNCTION__, array());
foreach ($subexps as $subexp) {
if ((preg_match('/^([^=]+)\=([^"]*)$/', $subexp, $rx) || preg_match('/^([^=]+)\="(.*)"$/', $subexp, $rx)) && array_key_exists(trim($rx[1]), $keys)) {
$indexes[trim($rx[1])] = trim($rx[2]);
}
else {
$return[] = $subexp;
}
}
// Re-append unknown stuff.
if (count($return)) {
return " and (" . implode(' and ', $return) . ")";
}
return "";
}
/**
* Search conditions callback.
*/
function ting_search_conditions_callback($keys) {
$conditions = array();
if (!empty($_REQUEST['keys'])) {
$conditions['keys'] = check_plain($_REQUEST['keys']);
}
if (!empty($_REQUEST['size'])) {
$conditions['size'] = (int) $_REQUEST['size'];
}
if (!empty($_REQUEST['sort'])) {
$conditions['sort'] = check_plain($_REQUEST['sort']);
}
else {
$conditions['sort'] = variable_get('ting_search_default_sort', '');
}
// If facets is set, check if we have to remove any, if so,
// reload the page.
if (!empty($_REQUEST['facets'])) {
$remove = array();
$redirect = FALSE;
foreach ($_REQUEST['facets'] as $key => $facet) {
if (preg_match('/^-facet/', $facet)) {
$remove[] = preg_replace('/^-/', '', $facet);
$redirect = TRUE;
unset($_REQUEST['facets'][$key]);
}
}
foreach ($_REQUEST['facets'] as $key => $facet) {
foreach ($remove as $rem) {
if ($facet == $rem) {
unset($_REQUEST['facets'][$key]);
continue;
}
}
}
$conditions['facets'] = $_REQUEST['facets'];
if ($redirect === TRUE) {
$facets = array();
foreach ($conditions['facets'] as $facet) {
$facets['facets'][] = $facet;
}
drupal_goto(rawurldecode($_GET['q']), array('query' => $facets));
}
}
return $conditions;
}
/**
* Implements hook_search_execute().
*/
function ting_search_search_execute($keys = NULL, $conditions = NULL) {
// Use static variable to ensure that this is only executed once.
$results = &drupal_static(__FUNCTION__);
if (!isset($results)) {
$results = array();
$options = array();
$results = array();
$facet_array = array();
$query = '(' . _ting_search_quote($keys) . ')';
$options['numFacets'] = variable_get('ting_search_number_of_facets', 25);
// Extend query with selected facets.
if (isset($conditions['facets']) && $conditions['facets'] != NULL) {
$facets = $conditions['facets'];
foreach ($facets as $facet) {
$facet = explode(':', $facet, 2);
if ($facet[0]) {
$facet_array[] = $facet[0] . '="' . rawurldecode($facet[1]) . '"';
}
}
$query .= ' AND ' . implode(' AND ', $facet_array);
}
$search_result = NULL;
try {
$page = pager_find_page();
// Set results per page.
$results_per_page = variable_get('ting_search_results_per_page', 10);
if (!empty($conditions['size'])) {
$results_per_page = $conditions['size'];
}
// Set sort order.
if (!empty($conditions['sort'])) {
$options['sort'] = $conditions['sort'];
}
module_load_include('client.inc', 'ting');
$search_result = ting_do_search($query, $page + 1, $results_per_page, $options);
if (isset($search_result->collections)) {
$search_result->search_key = $keys;
// So we got the total amount of results, but the joker here is that
// we have no clue how many collections we got.
// Rounded up, this will display 'one more page' if $search-result->more
// is true.
$total_results = ($page + 1) * $results_per_page + ($search_result->more ? 1 : 0);
pager_default_initialize($total_results, $results_per_page);
foreach ($search_result->collections as &$collection) {
$build = ting_collection_view($collection, 'teaser');
$uri = entity_uri('ting_collection', $collection);
$results[] = array(
'link' => url($uri['path'], $uri['options']),
'type' => '',
'title' => $collection->title,
'user' => '',
'date' => '',
'snippet' => drupal_render($build),
);
}
}
}
catch (TingClientException $e) {
watchdog_exception('ting_search', $e);
$results = array();
}
// Save search result for usage by facet modules etc.
$ting_search_results = &drupal_static('ting_search_results');
$ting_search_results = $search_result;
// Add support for facet api.
if (module_exists('ding_facets')) {
// @todo ITK Aarhus finish implementation of facet api to use a standard
// facet module, that allows usage of modules from D.O. to improved facet
// usage.
$adapter = facetapi_adapter_load('ding_facets');
if ($adapter) {
$adapter->addActiveFilters($query);
}
}
}
return $results;
}
/**
* Implements hook_search_page().
*/
function ting_search_search_page($results) {
return array(
'#theme' => 'ting_search_results',
'#results' => $results,
'#module' => 'ting_search',
);
}
/**
* Theme a pager.
*/
function theme_ting_search_pager($variables) {
$tags = $variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
$hide_list = isset($variables['hide_list']) ? $variables['hide_list'] : FALSE;
global $pager_page_array, $pager_total;
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
// Current is the page we are currently paged to.
$pager_current = $pager_page_array[$element] + 1;
// First is the first page listed by this pager piece (re quantity).
$pager_first = $pager_current - $pager_middle + 1;
// Last is the last page listed by this pager piece (re quantity).
$pager_last = $pager_current + $quantity - $pager_middle;
// Max is the maximum page number.
$pager_max = $pager_total[$element];
// Prepare for generation loop.
$i = $pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + ($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
$li_previous = theme('pager_previous', array(
'text' => isset($tags[1]) ? $tags[1] : t('‹ previous'),
'element' => $element,
'interval' => 1,
'parameters' => $parameters,
));
if (empty($li_previous)) {
$li_previous = " ";
}
$li_first = theme('pager_first', array(
'text' => isset($tags[0]) ? $tags[0] : t('« first'),
'element' => $element,
'parameters' => $parameters,
));
if (empty($li_first)) {
$li_first = " ";
}
$li_next = theme('pager_next', array(
'text' => isset($tags[3]) ? $tags[3] : t('next ›'),
'element' => $element,
'interval' => 1,
'parameters' => $parameters,
));
if (empty($li_next)) {
$li_next = " ";
}
$li_last = theme('pager_last', array(
'text' => isset($tags[4]) ? $tags[4] : t('last »'),
'element' => $element,
'parameters' => $parameters,
));
if (empty($li_last)) {
$li_last = " ";
}
if ($pager_total[$element] > 1) {
if ($pager_current > 2) {
$items[] = array(
'class' => array('pager-first'),
'data' => $li_first,
);
}
$items[] = array(
'class' => array('pager-previous'),
'data' => $li_previous,
);
// When there is more than one page, create the pager list.
if (!$hide_list && $i != $pager_max) {
if ($i > 1) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
$items[] = array(
'class' => array('pager-item'),
'data' => theme('pager_previous', array(
'text' => $i,
'element' => $element,
'interval' => ($pager_current - $i),
'parameters' => $parameters,
)),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => array('pager-current'),
'data' => $i,
);
}
if ($i > $pager_current) {
$items[] = array(
'class' => array('pager-item'),
'data' => theme('pager_next', array(
'text' => $i,
'element' => $element,
'interval' => ($i - $pager_current),
'parameters' => $parameters,
)),
);
}
}
if ($i < $pager_max) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
}
else {
$items[] = array(
'class' => array('pager-current'),
'data' => $pager_current,
);
}
$items[] = array(
'class' => array('pager-next'),
'data' => $li_next,
);
if ($pager_current + 1 < $pager_max && $li_last) {
$items[] = array(
'class' => array('pager-last'),
'data' => $li_last,
);
}
return theme('item_list', array(
'items' => $items,
'type' => 'ul',
'attributes' => array(
'class' => array('pager'),
),
));
}
}
/**
* Theme a mini pager with only first, previous and next links.
*/
function theme_ting_search_mini_pager($variables) {
$tags = $variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
global $pager_total;
$li_previous = theme('pager_previous', array(
'text' => isset($tags[1]) ? $tags[1] : t('‹ previous'),
'element' => $element,
'interval' => 1,
'parameters' => $parameters,
));
if (empty($li_previous)) {
$li_previous = " ";
}
$li_first = theme('pager_first', array(
'text' => isset($tags[0]) ? $tags[0] : t('« first'),
'element' => $element,
'parameters' => $parameters,
));
if (empty($li_first)) {
$li_first = " ";
}
$li_next = theme('pager_next', array(
'text' => isset($tags[3]) ? $tags[3] : t('next ›'),
'element' => $element,
'interval' => 1,
'parameters' => $parameters,
));
if (empty($li_next)) {
$li_next = " ";
}
if ($pager_total[$element] > 1) {
$items[] = array(
'class' => array('pager-first'),
'data' => $li_first,
);
$items[] = array(
'class' => array('pager-previous'),
'data' => $li_previous,
);
$items[] = array(
'class' => array('pager-next'),
'data' => $li_next,
);
return theme('item_list', array(
'items' => $items,
'type' => 'ul',
'attributes' => array(
'class' => array('pager'),
),
));
}
}
/**
* Implements hook_block_info().
*/
function ting_search_block_info() {
return array(
'sort_form' => array(
'info' => t('Ting search "sort by" form'),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
'search_display_extended_query' => array(
'info' => t('Ting search extended query display'),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
);
}
/**
* Implements hook_block_view().
*/
function ting_search_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'sort_form':
drupal_add_js(drupal_get_path('module', 'ting_search') . '/js/ting_search_extendform.js');
$block['subject'] = t('Ting search sort controls');
// Only display form if there are any search results.
$search_result = drupal_static('ting_search_results');
if (isset($search_result->numTotalObjects) && $search_result->numTotalObjects > 0) {
$block['content'] = drupal_get_form('ting_search_sort_form');
}
break;
case 'search_display_extended_query':
drupal_add_js(drupal_get_path('module', 'ting_search') . '/js/ting_search_extendform.js');
$block['content'] = theme('ting_search_display_extended_query', array('query_label' => t('Your query:'), 'query_string' => NULL));
break;
}
return $block;
}
function search_extend_content() {
return drupal_get_form('search_extend_form');
}
/**
* Create form for sorting search result.
*/
function ting_search_sort_form($form_state) {
$form = array();
$form['sort'] = array(
'#type' => 'select',
'#default_value' => isset($_GET['sort']) ? check_plain($_GET['sort']) : variable_get('ting_search_default_sort', ''),
'#options' => ting_search_sort_options(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sort'),
'#states' => array(
'visible' => array(':input[name="op"]' => array('value' => '')),
),
);
return $form;
}
/**
* Set default sort options.
*
* @return array
* Returns an array of sort options.
*/
function ting_search_sort_options() {
$options = array(
'' => t('Ranking'),
'title_ascending' => t('Title (Ascending)'),
'title_descending' => t('Title (Descending)'),
'creator_ascending' => t('Creator (Ascending)'),
'creator_descending' => t('Creator (Descending)'),
'date_ascending' => t('Date (Ascending)'),
'date_descending' => t('Date (Descending)'),
);
// Add label to the front of the options.
foreach ($options as $key => $option) {
$options[$key] = t('Sort by: !sort', array('!sort' => $option));
}
return $options;
}
/**
* Process submitted data for sorting order form.
*/
function ting_search_sort_form_submit($form, &$form_state) {
$query = drupal_get_query_parameters();
if (isset($form_state['input']['sort'])) {
$query = array('sort' => $form_state['input']['sort']) + $query;
}
$form_state['redirect'] = array($_GET['q'], array('query' => $query));
}
/**
* Create links for changing how many records per page.
*
* This function uses the static variable "ting_search_results", so if a search
* result have not been render yet it will be empty. So you have to ensure that
* the search have been executed.
*
* In panels "render last" option can help in the plugin array.
*/
function ting_search_records_per_page_controls_content() {
// Get search results.
$search_result = drupal_static('ting_search_results');
// Use static as panels may call this more than once.
$output = &drupal_static(__FUNCTION__, array());
// Don't show anything if the search result is empty.
if (!empty($search_result->collections) && empty($output)) {
// Get the default results per page.
$def_size = variable_get('ting_search_results_per_page', 10);
// Set default as the first value.
$sizes = array($def_size => $def_size);
// Increment the default size by 2.5 and 5 to provide the user with three
// different options. Defaults to 10, 25, 50 posts per search page.
$size = (int) round($def_size * 2.5);
$sizes[$size] = $size;
$size = (int) round($def_size * 5);
$sizes[$size] = $size;
// Iterate through sizes and build the options.
foreach ($sizes as $number => $text) {
// Set the current pager page to first page (used in query).
$pager_page = array('page' => 0);
// Find the currently select size.
$keys = array_keys($sizes);
$selected = FALSE;
if (isset($_GET['size']) && $_GET['size'] == $number) {
$selected = TRUE;
}
elseif (!isset($_GET['size']) && $number == $keys[0]) {
$selected = TRUE;
}
// Add the result to the output.
$output[$text] = array(
'size' => $number,
'text' => $text,
'query' => array('size' => $number) + $pager_page + drupal_get_query_parameters(),
'selected' => $selected,
);
}
return $output;
}
}
/**
* Process a block search form submission.
*/
function ting_search_submit($form, &$form_state) {
$controls = array();
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request,
// for example by drupal_access_denied() or drupal_not_found()
// (see http://drupal.org/node/292565).
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
$form_id = $form['form_id']['#value'];
$keys = $form_state['values'][$form_id];
$fields = array();
$extended_fields = array(
'creator' => 'term.creator',
'title' => 'term.title',
'subject' => 'term.subject',
);
foreach ($extended_fields as $name => $index) {
if (!empty($form_state['values'][$name])) {
$value = $form_state['values'][$name];
$fields[] = $index . '=' . (strpos($value, ' ') ? '"' . $value . '"' : $value);
}
}
$q = array();
if (!empty($keys)) {
$q[] = $keys;
}
if (!empty($fields)) {
$q[] = '(' . implode(' AND ', $fields) . ')';
}
$q = implode(' AND ', $q);
$s = $form_state['values']['sort'];
if ($s != "") {
$controls['sort'] = $s;
}
$s = $form_state['values']['size'];
if ($s != "") {
$controls['size'] = $s;
}
// Check to see if the form was submitted empty.
// If it is empty, display an error message.
// (This method is used instead of setting #required to TRUE for this field
// because that results in a confusing error message. It would say a plain
// "field is required" because the search keywords field has no title.
// The error message would also complain about a missing #title field.)
if ($q == '') {
form_set_error('keys', t('Please enter some keywords.'));
}
$search_info = array();
$request_path = arg();
if ($request_path[0] != 'search' || !isset($request_path[1])) {
$search_info = search_get_default_module_info();
}
else {
foreach (search_get_info() as $search_engine) {
if ($search_engine['path'] == $request_path[1]) {
$search_info = $search_engine;
break;
}
}
}
if (!empty($search_info['path']) && in_array($search_info['module'], variable_get('search_active_modules', array()))) {
$form_state['redirect'] = FALSE;
$url = 'search/' . $search_info['path'] . '/' . trim($q);
drupal_goto($url, array('query' => $controls));
}
else {
form_set_error(NULL, t('Search is currently disabled.'), 'error');
}
}
/**
* Make sure string is strict cql
*/
function _ting_search_quote($string) {
$cqlDoc = new TingSearchCqlDoctor($string);
return $cqlDoc->string_to_cql($string);
}