From 97ecf4707f01a98797d719c53c5376b5c48f660d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 13 Nov 2019 15:38:07 -0500
Subject: [PATCH] Support reversed checkbox facet sections in label loading.

---
 config/vufind/EDS.ini                          | 12 +++++++-----
 config/vufind/Primo.ini                        |  4 +++-
 config/vufind/Summon.ini                       |  4 +++-
 config/vufind/facets.ini                       |  4 +++-
 .../VuFind/src/VuFind/Search/Base/Params.php   | 18 +++++++++++++-----
 5 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/config/vufind/EDS.ini b/config/vufind/EDS.ini
index 7edd57c981c..0f98a8b23cf 100644
--- a/config/vufind/EDS.ini
+++ b/config/vufind/EDS.ini
@@ -122,7 +122,9 @@ labelSections[] = Facets
 
 ; This setting lists configuration settings defining checkbox facets. If you use
 ; a custom section to configure additional facets, be sure to add it to this list
-; so labels display correctly in history, the advanced search editor, etc.
+; so labels display correctly in history, the advanced search editor, etc. If you
+; are using the reverse label => filter format rather than filter => label, you
+; should prefix the section name with a ~ character to ensure proper loading.
 checkboxSections[] = CheckboxFacets
 
 ; Facet display settings
@@ -229,14 +231,14 @@ organization_id = "VuFind 2.x from MyUniversity"
 [List]
 view=full
 
-; This section controls the behavior of the Autocomplete of EDS 
+; This section controls the behavior of the Autocomplete of EDS
 ; If enabled the option "autocomplete" is send for UIDAuth to get the token
-; and the url. 
+; and the url.
 [Autocomplete]
 ; Set this to false to disable all autocomplete behavior
 enabled = true
 
-; Define a default_handler 
+; Define a default_handler
 default_handler = Eds
 
 ; Auto-submit autocomplete on click or enter
@@ -247,7 +249,7 @@ auto_submit = true
 ; behavior when that search type is selected. (default: Eds:rawqueries)
 ; These values are available: None, Eds:rawqueries and Eds:holdings
 ; Use None to disable autocomplete for a specific search type
-; Use Eds:holdings for title completion in PubFinder. 
+; Use Eds:holdings for title completion in PubFinder.
 ; Use Eds:rawqueries for completion of basic textual queries.
 [Autocomplete_Types]
 ;AllFields = Eds:rawqueries
diff --git a/config/vufind/Primo.ini b/config/vufind/Primo.ini
index 6823989cbd4..5d662f863d9 100644
--- a/config/vufind/Primo.ini
+++ b/config/vufind/Primo.ini
@@ -108,7 +108,9 @@ labelSections[] = Facets
 
 ; This setting lists configuration settings defining checkbox facets. If you use
 ; a custom section to configure additional facets, be sure to add it to this list
-; so labels display correctly in history, the advanced search editor, etc.
+; so labels display correctly in history, the advanced search editor, etc. If you
+; are using the reverse label => filter format rather than filter => label, you
+; should prefix the section name with a ~ character to ensure proper loading.
 checkboxSections[] = CheckboxFacets
 
 ; Checkbox facets are facets, which are getting displayed as checkboxes
diff --git a/config/vufind/Summon.ini b/config/vufind/Summon.ini
index 26d25143dfb..d703c0e8224 100644
--- a/config/vufind/Summon.ini
+++ b/config/vufind/Summon.ini
@@ -164,7 +164,9 @@ labelSections[] = Facets
 
 ; This setting lists configuration settings defining checkbox facets. If you use
 ; a custom section to configure additional facets, be sure to add it to this list
-; so labels display correctly in history, the advanced search editor, etc.
+; so labels display correctly in history, the advanced search editor, etc. If you
+; are using the reverse label => filter format rather than filter => label, you
+; should prefix the section name with a ~ character to ensure proper loading.
 checkboxSections[] = CheckboxFacets
 
 ; Facet display settings
diff --git a/config/vufind/facets.ini b/config/vufind/facets.ini
index e3c1492f9bf..4dae529e5c8 100644
--- a/config/vufind/facets.ini
+++ b/config/vufind/facets.ini
@@ -39,7 +39,9 @@ labelSections[] = ExtraFacetLabels
 
 ; This setting lists configuration settings defining checkbox facets. If you use
 ; a custom section to configure additional facets, be sure to add it to this list
-; so labels display correctly in history, the advanced search editor, etc.
+; so labels display correctly in history, the advanced search editor, etc. If you
+; are using the reverse label => filter format rather than filter => label, you
+; should prefix the section name with a ~ character to ensure proper loading.
 checkboxSections[] = CheckboxFacets
 
 ; This section is used to specify labels for facets that may be applied by parts
diff --git a/module/VuFind/src/VuFind/Search/Base/Params.php b/module/VuFind/src/VuFind/Search/Base/Params.php
index 8c848b62654..502f88d6b84 100644
--- a/module/VuFind/src/VuFind/Search/Base/Params.php
+++ b/module/VuFind/src/VuFind/Search/Base/Params.php
@@ -1797,11 +1797,19 @@ class Params
     ) {
         $config = $this->configLoader
             ->get($cfgFile ?? $this->getOptions()->getFacetsIni());
-        if (empty($config->$facetList)) {
-            return false;
-        }
-        foreach ($config->$facetList as $key => $value) {
-            $this->addCheckboxFacet($key, $value);
+        $retVal = false;
+        // If the section is in reverse order, the tilde will flag this:
+        if (substr($facetList, 0, 1) == '~') {
+            foreach ($config->{substr($facetList, 1)} as $value => $key) {
+                $this->addCheckboxFacet($key, $value);
+                $retVal = true;
+            }
+        } else {
+            foreach ($config->$facetList as $key => $value) {
+                $this->addCheckboxFacet($key, $value);
+                $retVal = true;
+            }
         }
+        return $retVal;
     }
 }
-- 
GitLab