-
Notifications
You must be signed in to change notification settings - Fork 3
/
facetapi.admin.inc
1352 lines (1186 loc) · 45 KB
/
facetapi.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
* Admin page callbacks for the Facet API module.
*/
/**
* Tests whether a configuration setting is overridden or not.
*
* @param stdClass $settings
* The configuration settings being tested.
*
* @return boolean
* TRUE if the settings are overridden, FALSE otherwise.
*/
function facetapi_is_overridden(stdClass $settings) {
return ($settings->export_type & EXPORT_IN_CODE && $settings->export_type & EXPORT_IN_DATABASE);
}
/**
* Form constructor for the realm settings form.
*
* @param $searcher
* The machine readable name of the searcher.
* @param $realm_name
* The machine readable name of the realm.
*
* @see facetapi_realm_settings_form_submit()
* @ingroup forms
*/
function facetapi_realm_settings_form($form, &$form_state, $searcher, $realm_name) {
// Instantiates adapter, loads realm.
$adapter = facetapi_adapter_load($searcher);
$realm = facetapi_realm_load($realm_name);
$form['#facetapi'] = array(
'adapter' => $adapter,
'realm' => $realm,
'facet_info' => facetapi_get_facet_info($searcher),
);
$form['description'] = array(
'#prefix' => '<div class="facetapi-realm-description">',
'#markup' => filter_xss_admin($realm['description']),
'#suffix' => "</div>\n",
);
$form['performance'] = array(
'#prefix' => '<div class="facetapi-performance-note">',
'#markup' => t('For performance reasons, you should only enable facets that you intend to have available to users on the search page.'),
'#suffix' => "</div>\n",
);
$form['table'] = array(
'#theme' => 'facetapi_realm_settings_table',
'#facetapi' => &$form['#facetapi'],
'operations' => array('#tree' => TRUE),
'weight' => array('#tree' => TRUE),
);
// Builds "enabled_facets" options.
$options = $default_value = array();
foreach ($form['#facetapi']['facet_info'] as $facet_name => $facet) {
$settings = $adapter->getFacetSettings($facet, $realm);
$global_settings = $adapter->getFacetSettingsGlobal($facet);
// Builds array of operations to use in the dropbutton.
$operations = array();
$operations[] = array(
'title' => t('Configure display'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'edit'),
);
if ($facet['dependency plugins']) {
$operations[] = array(
'title' => t('Configure dependencies'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'dependencies'),
);
}
if (facetapi_filters_load($facet_name, $searcher, $realm['name'])) {
$operations[] = array(
'title' => t('Configure filters'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'filters'),
);
}
$operations[] = array(
'title' => t('Export configuration'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'export'),
);
if (facetapi_is_overridden($settings) || facetapi_is_overridden($global_settings)) {
$operations[] = array(
'title' => t('Revert configuration'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'revert'),
);
}
$form['table']['operations'][$facet_name] = array(
'#type' => 'dropbutton',
'#links' => $operations,
'#attributes' => array(
'class' => array('dropbutton'),
),
);
// Adds weight if sortable.
if ($realm['sortable']) {
$form['#facetapi']['facet_info'][$facet_name]['weight'] = $settings->settings['weight'];
$form['table']['weight'][$facet_name] = array(
'#type' => 'select',
'#title' => t('Weight for @title', array('@title' => $facet['label'])),
'#title_display' => 'invisible',
'#options' => backdrop_map_assoc(range(-50, 50)),
'#default_value' => $settings->settings['weight'],
'#attributes' => array('class' => array('facetapi-facet-weight')),
);
}
$options[$facet_name] = '';
$default_value[$facet_name] = (!$settings->enabled) ? 0 : $facet_name;
}
// Sorts by the weight appended above.
uasort($form['#facetapi']['facet_info'], 'facetapi_sort_weight');
$form['table']['enabled_facets'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $default_value,
);
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 20,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['#submit'][] = 'facetapi_realm_settings_form_submit';
return $form;
}
/**
* Form submission handler for facetapi_realm_settings_form().
*/
function facetapi_realm_settings_form_submit($form, &$form_state) {
$success = TRUE;
// Pulls variables for code readability.
$adapter = $form['#facetapi']['adapter'];
$realm = $form['#facetapi']['realm'];
$facet_info = $form['#facetapi']['facet_info'];
// Builds settings object, saves to database.
foreach ($facet_info as $facet_name => $facet) {
$status = empty($form_state['values']['enabled_facets'][$facet_name]) ? 0 : 1;
$weight = $realm['sortable'] ? $form_state['values']['weight'][$facet_name] : 0;
if (!facetapi_save_facet_status($adapter, $realm, $facet, $status, $weight, TRUE)) {
$success = FALSE;
$message = t(
'Error saving configuration options for the %label facet.',
array('%label' => $facet['label'])
);
backdrop_set_message($message, 'error');
}
}
// Sets message if all configurations were saved.
if ($success) {
backdrop_set_message(t('The configuration options have been saved.'));
if ('block' == $form['#facetapi']['realm']['name']) {
backdrop_set_message(t(
'To enable or arrange the facet blocks, add a block to a <a href="@layout-page">layout</a>.',
array('@layout-page' => url('admin/structure/layouts', array('query' => array('destination' => current_path()))))
));
}
}
// Clears delta map cache.
cache_clear_all();
}
/**
* Returns the realm settings table.
*
* @param $variables
* An associative array containing:
* - element: A render element representing the form.
*
* @ingroup themable
*/
function theme_facetapi_realm_settings_table($variables) {
$output = '';
// Adds CSS to ensure that the dropbutton looks nice.
$path = backdrop_get_path('module', 'facetapi');
backdrop_add_css($path . '/facetapi.admin.css', array('weight' => CSS_THEME - 1));
// Gets variales for code readability.
$searcher = $variables['element']['#facetapi']['adapter']->getSearcher();
$realm = $variables['element']['#facetapi']['realm'];
$header = array(
'enabled' => array('data' => t('Enabled')),
'label' => array('data' => t('Facet')),
'operations' => array('data' => t('Operations')),
);
if ($realm['sortable']) {
$header['weight'] = array('data' => t('Weight'));
}
// Builds field options.
$rows = array();
$facet_info = $variables['element']['#facetapi']['facet_info'];
foreach ($facet_info as $facet_name => $facet) {
// Builds rows.
$rows[$facet_name] = array(
'class' => $realm['sortable'] ? array('draggable') : array(),
'data' => array(
backdrop_render($variables['element']['enabled_facets'][$facet_name]),
check_plain($facet['label']) . "<div class='description'>" . filter_xss($facet['description']) . '</div>',
array(
'class' => 'facetapi-operations',
'data' => backdrop_render($variables['element']['operations'][$facet_name]),
),
),
);
if ($realm['sortable']) {
$rows[$facet_name]['data'][] = backdrop_render($variables['element']['weight'][$facet_name]);
}
}
if ($realm['sortable']) {
backdrop_add_tabledrag('facetapi-realm-settings', 'order', 'sibling', 'facetapi-facet-weight');
}
$output .= backdrop_render_children($variables['element']);
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'facetapi-realm-settings')));
return $output;
}
/**
* Returns the path to a facet's settings page.
*
* @param $searcher
* The machine readable name of the searcher.
* @param $realm_name
* The machine readable name of the realm.
* @param $facet_name
* The machine readable name of the realm.
* @param $op
* The operation being requested, one of the following values:
* - edit: The display settings form.
* - dependencies: The dependency settings form.
* - filters: The filters settings form.
* - export: The settings export form.
*
* @return
* The path to the settings page.
*/
function facetapi_get_settings_path($searcher, $realm_name, $facet_name, $op) {
$path_parts = array('admin', 'config', 'search', 'facetapi');
$path_parts[] = $searcher;
$path_parts[] = $realm_name;
$path_parts[] = $facet_name;
$path_parts[] = $op;
return join('/', $path_parts);
}
/**
* Returns the widget plugin definitions available to the facet.
*
* All widget plugins are checked against their requirements. Only widgets that
* pass all requirements are returned by this function.
*
* @param array $realm
* The realm definition.
* @param array $facet
* The facet definition.
*
* @return array
* An array of available widget plugins.
*/
function facetapi_get_widgets(array $realm, array $facet) {
$plugins = array();
// Iterates over all defined plugins, initializes requirements.
foreach (facetapi_get_plugins('facetapi', 'widgets') as $id => $plugin) {
$plugin['handler'] += array(
'requirements' => array(
'facetapi_requirement_realm_property' => array('element type' => 'links'),
),
'requirements operator' => 'AND',
);
// Checks requirements, only saves widgets that pass all requirements.
if (facetapi_check_requirements($plugin['handler']['requirements'], $realm, $facet, $plugin['handler']['requirements operator'])) {
$plugins[$id] = $plugin;
}
}
return $plugins;
}
/**
* Returns the sorts available to the facet.
*
* All sorts are checked against their requirements. Only sorts that pass all
* requirements are returned by this function.
*
* @param array $realm
* The realm definition.
* @param array $facet
* The facet definition.
*
* @return array
* An array of available sorts plugins.
*/
function facetapi_get_available_sorts(array $realm, array $facet) {
$sort_info = array();
foreach (facetapi_get_sort_info() as $name => $info) {
if (!$info['requirements'] || facetapi_check_requirements($info['requirements'], $realm, $facet)) {
$sort_info[$name] = $info;
}
}
return $sort_info;
}
/**
* Form constructor for the facet display settings form.
*
* @param array $realm
* The realm definition.
* @param array $facet
* The facet definition.
*
* @see facetapi_facet_display_form_submit()
* @ingroup forms
*/
function facetapi_facet_display_form($form, &$form_state, FacetapiAdapter $adapter, array $realm, array $facet) {
$path = backdrop_get_path('module', 'facetapi');
backdrop_add_css($path . '/facetapi.admin.css', array('weight' => CSS_THEME - 1));
backdrop_add_js($path . '/facetapi.admin.js');
// We have to set the title due to contextual link magic.
// @see http://drupal.org/node/1147588#comment-4428940
backdrop_set_title(t('Configure facet display for @label', array('@label' => $facet['label'])));
// Captures variables and settings for code readability.
$searcher = $adapter->getSearcher();
$facet_settings = $adapter->getFacet($facet)->getSettings($realm);
$global_settings = $adapter->getFacet($facet)->getSettings();
$form['#facetapi'] = array(
'adapter' => $adapter,
'realm' => $realm,
'facet' => $facet,
'sort_info' => array(),
'excluded_values' => array(
'form_build_id',
'form_token',
'form_id',
'op',
'submit',
'submit_list',
'submit_realm',
'settings__active_tab',
),
);
////
////
//// Widget settings
////
////
$form['widget'] = array(
'#type' => 'fieldset',
'#title' => t('Display settings'),
);
// Builds select options for widgets, allows widgets to add settings.
$widget_options = array();
foreach (facetapi_get_widgets($realm, $facet) as $id => $plugin) {
$widget_options[$id] = $plugin['handler']['label'];
$class = $plugin['handler']['class'];
$plugin = new $class($id, $realm, $adapter->getFacet($facet), $facet_settings);
$plugin->settingsForm($form, $form_state);
}
$form['widget']['title_override'] = array(
'#type' => 'checkbox',
'#title' => t('Override Facet Title %default_title', array('%default_title' => '(' . t('Default') . ': ' . $facet['label'] . ')')),
'#default_value' => $facet_settings->settings['title_override'],
'#weight' => -12,
);
$form['widget']['title'] = array(
'#type' => 'textfield',
'#title' => t('Facet Title'),
'#description' => t('To remove or alter the default title prefix: "Filter by @title:", implement the following hook in your theme: !url.', array(
'!url' => l('THEME_facetapi_title()', 'http://backdropcontrib.org/api/backdrop/contributions!facetapi!facetapi.theme.inc/function/theme_facetapi_title/7', array(
'attributes' => array('target' => '_blank'),
)),
)),
'#default_value' => $facet_settings->settings['title'],
'#states' => array(
'visible' => array(
'input[name="title_override"]' => array('checked' => TRUE),
),
),
'#weight' => -11,
);
$form['widget']['widget'] = array(
'#type' => 'select',
'#title' => t('Display widget'),
'#default_value' => $facet_settings->settings['widget'],
'#options' => $widget_options,
'#weight' => -10,
'#description' => t('Select the display widget used to render this facet.'),
);
////
////
//// Sort settings
////
////
$form['widget']['sort'] = array(
'#prefix' => '<div class="facetapi-sort-table">',
'#suffix' => '</div>',
'#weight' => -10,
);
$form['widget']['sort']['table'] = array(
'#theme' => 'facetapi_sort_settings_table',
'#facetapi' => &$form['#facetapi'],
'sort_weight' => array('#tree' => TRUE),
'sort_order' => array('#tree' => TRUE),
);
// Initializes sorts with default settings, orders by default weight. Ordering
// the weights allows us to iterate over them in order when building the
// form elements in the foreach() loop below.
$sort_weight = $facet_settings->settings['sort_weight'];
$sort_order = $facet_settings->settings['sort_order'];
foreach (facetapi_get_available_sorts($realm, $facet) as $sort_name => $sort_info) {
$weight = (isset($sort_weight[$sort_name])) ? $sort_weight[$sort_name] : 0;
$form['#facetapi']['sort_info'][$sort_name] = $sort_info;
$form['#facetapi']['sort_info'][$sort_name]['weight'] = $weight;
}
// Orders the sorts by the default weights set above.
uasort($form['#facetapi']['sort_info'], 'facetapi_sort_weight');
// Builds checkbox options and weight dropboxes.
$sort_options = array();
foreach ($form['#facetapi']['sort_info'] as $sort_name => $sort) {
$sort_options[$sort_name] = '';
$order_default = (isset($sort_order[$sort_name])) ? $sort_order[$sort_name] : SORT_ASC;
$form['widget']['sort']['table']['sort_order'][$sort_name] = array(
'#type' => 'select',
'#title' => t('Order for sort @title', array('@title' => $sort['label'])),
'#title_display' => 'invisible',
'#options' => array(
SORT_DESC => t('Descending'),
SORT_ASC => t('Ascending'),
),
'#default_value' => $order_default,
);
$weight_default = (isset($sort_weight[$sort_name])) ? $sort_weight[$sort_name] : 0;
$form['widget']['sort']['table']['sort_weight'][$sort_name] = array(
'#type' => 'weight',
'#title' => t('Weight for sort @title', array('@title' => $sort['label'])),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $weight_default,
'#attributes' => array('class' => array('facetapi-sort-weight')),
);
}
$form['widget']['sort']['table']['active_sorts'] = array(
'#type' => 'checkboxes',
'#options' => $sort_options,
'#default_value' => $facet_settings->settings['active_sorts'],
);
$form['widget']['empty'] = array(
'#prefix' => '<div class="facetapi-empty-setting">',
'#suffix' => '</div>',
'#weight' => 10,
);
$empty_options = array();
foreach (facetapi_get_plugins('facetapi', 'empty_behaviors') as $id => $plugin) {
$empty_options[$id] = $plugin['handler']['label'];
$class = $plugin['handler']['class'];
$plugin = new $class($facet_settings);
$plugin->settingsForm($form, $form_state);
}
$form['widget']['empty']['empty_behavior'] = array(
'#type' => 'select',
'#title' => t('Empty facet behavior'),
'#default_value' => $facet_settings->settings['empty_behavior'],
'#options' => $empty_options,
'#weight' => -10,
'#description' => t('The action to take when a facet has no items.'),
);
$form['widget']['display_count'] = array(
'#type' => 'checkbox',
'#prefix' => '<div class="facetapi-display-count-setting">',
'#suffix' => '</div>',
'#title' => t('Display the number of matching items'),
'#description' => t('If checked, the number of matching items will be output in brackets.'),
'#default_value' => $facet_settings->settings['display_count'],
);
$form['widget']['facet_more_text'] = array(
'#type' => 'textfield',
'#prefix' => '<div class="facetapi-more-text-setting">',
'#suffix' => '</div>',
'#title' => t('Facet "more" link text'),
'#default_value' => $facet_settings->settings['facet_more_text'],
);
$form['widget']['facet_fewer_text'] = array(
'#type' => 'textfield',
'#prefix' => '<div class="facetapi-fewer-text-setting">',
'#suffix' => '</div>',
'#title' => t('Facet "fewer" link text'),
'#default_value' => $facet_settings->settings['facet_fewer_text'],
);
////
////
//// Global settings
////
////
$form['global'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Global settings'),
'#description' => t('The configuration options below apply to this facet across <em>all</em> realms.'),
);
// Loop through the query types so the key is the same as the value
$query_types = backdrop_map_assoc($facet['query types'], 'check_plain');
$form['global']['query_type'] = array(
'#type' => 'select',
'#access' => count($facet['query types']) > 1,
'#title' => t('Query type'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['query_type'],
'#options' => $query_types,
'#description' => t('Select the query type this facet will use.'),
);
$all_options = array(
FACETAPI_OPERATOR_AND => t('AND'),
FACETAPI_OPERATOR_OR => t('OR'),
);
$options = array_intersect_key($all_options, array_filter($facet['allowed operators']));
$form['global']['operator'] = array(
'#type' => 'radios',
'#access' => count($options) > 1,
'#title' => t('Operator'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['operator'],
'#options' => $options,
'#description' => t('AND filters are exclusive and narrow the result set. OR filters are inclusive and widen the result set.'),
);
$hard_limit_options = backdrop_map_assoc(array(3, 5, 10, 15, 20, 30, 40, 50, 75, 100));
$hard_limit_options[-1] = t('No limit');
$form['global']['hard_limit'] = array(
'#type' => 'select',
'#title' => t('Hard limit'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['hard_limit'],
'#options' => $hard_limit_options,
'#description' => t('Display no more than this number of facet items.'),
);
$form['global']['flatten'] = array(
'#type' => 'radios',
'#access' => $facet['hierarchy callback'],
'#title' => t('Flatten hierarchy'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['flatten'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Do not process hierarchical relationships and display facet items as a flat list.'),
);
$form['global']['individual_parent'] = array(
'#type' => 'radios',
'#access' => $facet['hierarchy callback'],
'#title' => t('Treat parent items as individual facet items'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['individual_parent'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Parent items will not be active when selecting a children and if active, they will not clear their children facet selection.'),
'#states' => array(
'visible' => array(
':input[name="global[flatten]"]' => array('value' => 0),
),
),
);
$form['global']['facet_mincount'] = array(
'#type' => 'textfield',
'#access' => $facet['facet mincount allowed'] && $adapter->supportsFacetMincount(),
'#title' => t('Minimum facet count'),
'#size' => 5,
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['facet_mincount'],
'#description' => t('Only display facets that are matched in at least this many documents.'),
'#element_validate' => array('facetapi_element_validate_integer'),
);
$form['global']['facet_missing'] = array(
'#type' => 'radios',
'#access' => $facet['facet missing allowed'] && $adapter->supportsFacetMissing(),
'#title' => t('Add facet for missing values'),
'#prefix' => '<div class="facetapi-global-setting">',
'#suffix' => '</div>',
'#default_value' => $global_settings->settings['facet_missing'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Adds an extra facet matching documents with no value at all for this field.'),
);
////
////
//// Finalizes form
////
////
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 20,
);
// Gets destination from query string which is set when the page is navigated
// to via a contextual link. Builds messages based on where user came from.
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$submit_text = t('Save and go back to search page');
$cancel_title = t('Return to the search page without saving configuration changes.');
$url = backdrop_parse_url($_GET['destination']);
}
else {
$submit_text = t('Save configuration');
$cancel_title = t('Return to the realm settings page without saving configuration changes.');
$url = array();
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $submit_text,
);
// Do not show the button if the page was navigated to via a contextual link
// because it would redirect the user back to the search page.
$form['actions']['submit_realm'] = array(
'#type' => 'submit',
'#access' => (!$url),
'#value' => t('Save and go back to realm settings'),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => (!$url) ? $adapter->getPath($realm['name']) : $url['path'],
'#options' => (!$url) ? array() : array('query' => $url['query']),
'#attributes' => array('title' => $cancel_title),
);
// Adds form submissions and validation handlers.
$form['#submit'][] = 'facetapi_facet_display_form_submit';
$form['#validate'][] = 'facetapi_facet_display_form_validate';
// Allow the realm, adapter, and URL to alter the form.
if ($realm['settings callback']) {
$realm['settings callback']($form, $form_state);
}
$adapter->settingsForm($form, $form_state);
$adapter->getUrlProcessor()->settingsForm($form, $form_state);
return $form;
}
/**
* Helper form element validator: integer.
*
* @see _element_validate_integer
*/
function facetapi_element_validate_integer($element, &$form_state) {
$value = $element['#value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value)) {
form_error($element, t('%name must be an integer.', array('%name' => $element['#title'])));
}
}
/**
* Returns facet sort order table.
*
* @param $variables
* An associative array containing:
* - element: A render element representing the form.
*
* @ingroup themeable
*/
function theme_facetapi_sort_settings_table($variables) {
$output = '';
// Gets variales for code readability.
$searcher = $variables['element']['#facetapi']['adapter']->getSearcher();
$header = array(
'active' => array(),
'label' => array('data' => t('Sort')),
'order' => array('data' => t('Order')),
'weight' => array('data' => t('Weight')),
);
// Builds field options.
$rows = array();
$sort_info = $variables['element']['#facetapi']['sort_info'];
foreach ($sort_info as $sort_name => $sort) {
$rows[$sort_name] = array(
'class' => array('draggable'),
'data' => array(
backdrop_render($variables['element']['active_sorts'][$sort_name]),
check_plain($sort['label']) . "<div class='description'>" . filter_xss($sort['description']) . '</div>',
backdrop_render($variables['element']['sort_order'][$sort_name]),
backdrop_render($variables['element']['sort_weight'][$sort_name]),
),
);
}
backdrop_add_tabledrag('facetapi-sort-settings', 'order', 'sibling', 'facetapi-sort-weight');
$output .= backdrop_render_children($variables['element']);
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'facetapi-sort-settings')));
return $output;
}
/**
* Form validation handler for facetapi_facet_display_form().
*/
function facetapi_facet_display_form_validate($form, &$form_state) {
// Gets the selected query type and widget.
$query_type = $form_state['values']['global']['query_type'];
$widget_name = $form_state['values']['widget'];
// Loads the widget plugin, makes sure it supports the selected type.
if ($widget = facetapi_get_plugins('facetapi', 'widgets', $widget_name)) {
$widget_types = backdrop_map_assoc($widget['handler']['query types']);
if (!isset($widget_types[$query_type])) {
$error = t('The widget does not support the %type query type', array('%type' => $query_type));
form_set_error('widget', $error);
}
}
else {
$error = t('Widget %name not valid.', array('%name' => $widget_name));
form_set_error('widget', $error);
}
}
/**
* Form submission handler for facetapi_facet_display_form().
*/
function facetapi_facet_display_form_submit($form, &$form_state) {
// Pulls variables for code readability.
$adapter = $form['#facetapi']['adapter'];
$realm = $form['#facetapi']['realm'];
$facet = $form['#facetapi']['facet'];
$global_values = $form_state['values']['global'];
unset($form_state['values']['global']);
// Loads settings, saves all form values as settings other than excluded.
$facet_settings = $adapter->getFacet($facet)->getSettings($realm);
$facet_settings->settings = array_merge($facet_settings->settings, array_diff_key(
$form_state['values'],
array_flip($form['#facetapi']['excluded_values'])
));
$global_settings = $adapter->getFacet($facet)->getSettings();
foreach ($global_values as $key => $value) {
$global_settings->settings[$key] = $value;
}
$success = TRUE;
if (FALSE === facetapi_export_crud_save('facetapi', $facet_settings)) {
backdrop_set_message(t('Error saving configuration options.'), 'error');
$success = FALSE;
}
if (FALSE === facetapi_export_crud_save('facetapi', $global_settings)) {
backdrop_set_message(t('Error saving configuration options.'), 'error');
$success = FALSE;
}
// Sets message if both sets of configurations were saved.
if ($success) {
backdrop_set_message(t('The configuration options have been saved.'));
}
// Redirects back to the realm settings page if necessary.
$clicked = $form_state['clicked_button']['#value'];
if (t('Save and go back to realm settings') == $clicked) {
$form_state['redirect'] = $adapter->getPath($realm['name']);
}
}
/**
* Form constructor for the facet filter settings form.
*
* @param FacetapiAdapter $adapter
* The adapter object.
* @param array $realm
* The realm definition.
* @param array $filter_options
* The available filter options.
*
* @see facetapi_facet_filters_form_submit()
* @ingroup forms
*/
function facetapi_facet_filters_form($form, &$form_state, FacetapiAdapter $adapter, array $realm, array $filters) {
// Loads variables for code readability.
$facet = $filters['facet'];
$settings = $adapter->getFacet($facet)->getSettings($realm);
// We have to set the title due to contextual link magic.
// @see http://drupal.org/node/1147588#comment-4428940
backdrop_set_title(t('Configure facet filters for @label', array('@label' => $facet['label'])));
// Adds Facet API settings, excluded values aren't saved.
$form['#facetapi'] = array(
'adapter' => $adapter,
'realm' => $realm,
'facet' => $facet,
'settings' => $settings,
);
// Filter order (tabledrag).
$form['settings']['filters']['table'] = array(
'#type' => 'item',
'#title' => t('Active filters'),
'#theme' => 'facetapi_filter_settings_table',
);
$form['settings']['filters']['filter_settings_title'] = array(
'#type' => 'item',
'#title' => t('Filter settings'),
'#prefix' => '<div id="filters-status-wrapper">',
'#suffix' => '</div>',
'#weight' => 10,
);
$form['settings']['filters']['filter_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 20,
);
// Builds plugins.
$weight = -50;
$plugins = array();
foreach ($filters['plugins'] as $id => $plugin) {
if ($class = facetapi_plugin_load_class('facetapi', 'filters', $id, 'handler')) {
// Gets filter defaults.
$filter_plugin = new $class($id, $adapter, $settings);
$default = $filter_plugin->getDefaultSettings() + array(
'status' => 0,
'weight' => $weight++,
);
// Merges in default settings.
if (empty($settings->settings['filters'][$id])) {
$settings->settings['filters'][$id] = array();
}
$settings->settings['filters'][$id] += $default;
// Adds weight to plugin so we can sort it.
$plugins[$id] = $plugin + array(
'weight' => $settings->settings['filters'][$id]['weight'],
);
}
}
uasort($plugins, 'facetapi_sort_weight');
// Builds table, adds settings to vertical tabs.
$has_settings = FALSE;
foreach ($plugins as $id => $plugin) {
// Instantiates filter plugins.
$filter_plugin = new $class($id, $adapter, $settings);
$defaults = $filter_plugin->getDefaultSettings();
// Table elements.
$form['settings']['filters']['table'][$id]['status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable @title', array('@title' => $plugin['handler']['label'])),
'#title_display' => 'invisible',
'#default_value' => $settings->settings['filters'][$id]['status'],
'#parents' => array('settings', 'filters', $id, 'status'),
);
$form['settings']['filters']['table'][$id]['filter'] = array(
'#markup' => filter_xss_admin($plugin['handler']['label']),
);
$form['settings']['filters']['table'][$id]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $plugin['handler']['label'])),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $settings->settings['filters'][$id]['weight'],
'#parents' => array('settings', 'filters', $id, 'weight'),
'#weight' => 0,
);
// Initializes vertical tab.
$form['settings']['filters']['filter_settings'][$id] = array(
'#type' => 'fieldset',
'#title' => check_plain($plugin['handler']['label']),
'#group' => 'settings',
'#tree' => TRUE,
);
// Allows plugin to add settings to the form.
if ($class = facetapi_plugin_load_class('facetapi', 'filters', $id, 'handler')) {
$filter = new $class($id, $adapter, $settings);
$filter->settingsForm($form['settings']['filters']['filter_settings'][$id], $form_state);
}
// Removes vertical tab if no settings were added.
if (element_children($form['settings']['filters']['filter_settings'][$id])) {
$has_settings = TRUE;
}
else {
unset($form['settings']['filters']['filter_settings'][$id]);
}
}
// Removes title and fieldset if there aren't any settings.
if (!$has_settings) {
unset($form['settings']['filters']['filter_settings']);
unset($form['settings']['filters']['filter_settings_title']);
}
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 20,
);
// Gets destination from query string which is set when the page is navigated
// to via a contextual link. Builds messages based on where user came from.
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$submit_text = t('Save and go back to search page');
$cancel_title = t('Return to the search page without saving configuration changes.');
$url = backdrop_parse_url($_GET['destination']);
}
else {
$submit_text = t('Save configuration');
$cancel_title = t('Return to the realm settings page without saving configuration changes.');
$url = array();
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $submit_text,
);
// Do not show the button if the page was navigated to via a contextual link
// because it would redirect the user back to the search page.
$form['actions']['submit_realm'] = array(
'#type' => 'submit',
'#access' => (!$url),
'#value' => t('Save and go back to realm settings'),
);