From 74c3ea9cb06fcbb496b3c4c605d72e757e703bc6 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Thu, 31 Mar 2016 11:00:47 -0400
Subject: [PATCH] Added support for multiple checkbox facets for the same field
 - (e.g. Primo uses tlevel for both 'available online' and 'peer reviewed'). -
 Added better support for checkbox facets in search history.

---
 .../VuFind/src/VuFind/Search/Base/Params.php  | 56 +++++++++++++------
 .../VuFind/src/VuFind/Search/Primo/Params.php |  1 +
 .../VuFind/src/VuFind/Search/Solr/Params.php  |  1 +
 .../src/VuFind/Search/Summon/Params.php       |  1 +
 .../templates/search/history-table.phtml      |  7 ++-
 .../templates/search/history-table.phtml      |  7 ++-
 6 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/module/VuFind/src/VuFind/Search/Base/Params.php b/module/VuFind/src/VuFind/Search/Base/Params.php
index 5062e9579f5..f1df82a0cf7 100644
--- a/module/VuFind/src/VuFind/Search/Base/Params.php
+++ b/module/VuFind/src/VuFind/Search/Base/Params.php
@@ -909,7 +909,7 @@ class Params implements ServiceLocatorAwareInterface
         // Extract the facet field name from the filter, then add the
         // relevant information to the array.
         list($fieldName) = explode(':', $filter);
-        $this->checkboxFacets[$fieldName]
+        $this->checkboxFacets[$fieldName][]
             = ['desc' => $desc, 'filter' => $filter];
     }
 
@@ -1072,12 +1072,14 @@ class Params implements ServiceLocatorAwareInterface
     protected function getCheckboxFacetValues()
     {
         $list = [];
-        foreach ($this->checkboxFacets as $current) {
-            list($field, $value) = $this->parseFilter($current['filter']);
-            if (!isset($list[$field])) {
-                $list[$field] = [];
+        foreach ($this->checkboxFacets as $facets) {
+            foreach ($facets as $current) {
+                list($field, $value) = $this->parseFilter($current['filter']);
+                if (!isset($list[$field])) {
+                    $list[$field] = [];
+                }
+                $list[$field][] = $value;
             }
-            $list[$field][] = $value;
         }
         return $list;
     }
@@ -1091,20 +1093,18 @@ class Params implements ServiceLocatorAwareInterface
     {
         // Build up an array of checkbox facets with status booleans and
         // toggle URLs.
-        $facets = [];
-        foreach ($this->checkboxFacets as $field => $details) {
-            $facets[$field] = $details;
-            if ($this->hasFilter($details['filter'])) {
-                $facets[$field]['selected'] = true;
-            } else {
-                $facets[$field]['selected'] = false;
+        $result = [];
+        foreach ($this->checkboxFacets as $field => $facets) {
+            foreach ($facets as $facet) {
+                $facet['selected'] = $this->hasFilter($facet['filter']);
+                // Is this checkbox always visible, even if non-selected on the
+                // "no results" screen?  By default, no (may be overridden by
+                // child classes).
+                $facet['alwaysVisible'] = false;
+                $result[] = $facet;
             }
-            // Is this checkbox always visible, even if non-selected on the
-            // "no results" screen?  By default, no (may be overridden by
-            // child classes).
-            $facets[$field]['alwaysVisible'] = false;
         }
-        return $facets;
+        return $result;
     }
 
     /**
@@ -1770,4 +1770,24 @@ class Params implements ServiceLocatorAwareInterface
     {
         return $this->defaultsApplied;
     }
+
+    /**
+     * Initialize checkbox facet settings for the specified configuration sections.
+     *
+     * @param string $facetList Config section containing fields to activate
+     * @param string $cfgFile   Name of configuration to load
+     *
+     * @return bool             True if facets set, false if no settings found
+     */
+    protected function initCheckboxFacets($facetList = 'CheckboxFacets',
+        $cfgFile = 'facets'
+    ) {
+        $config = $this->getServiceLocator()->get('VuFind\Config')->get($cfgFile);
+        if (empty($config->$facetList)) {
+            return false;
+        }
+        foreach ($config->$facetList as $key => $value) {
+            $this->addCheckboxFacet($key, $value);
+        }
+    }
 }
diff --git a/module/VuFind/src/VuFind/Search/Primo/Params.php b/module/VuFind/src/VuFind/Search/Primo/Params.php
index cb371c78f8a..8de1045b1de 100644
--- a/module/VuFind/src/VuFind/Search/Primo/Params.php
+++ b/module/VuFind/src/VuFind/Search/Primo/Params.php
@@ -114,5 +114,6 @@ class Params extends \VuFind\Search\Base\Params
     {
         $this->initFacetList('Facets', 'Results_Settings', 'Primo');
         $this->initFacetList('Advanced_Facets', 'Advanced_Facet_Settings', 'Primo');
+        $this->initCheckboxFacets('CheckboxFacets', 'Primo');
     }
 }
diff --git a/module/VuFind/src/VuFind/Search/Solr/Params.php b/module/VuFind/src/VuFind/Search/Solr/Params.php
index 30661c1b240..06df69db698 100644
--- a/module/VuFind/src/VuFind/Search/Solr/Params.php
+++ b/module/VuFind/src/VuFind/Search/Solr/Params.php
@@ -363,6 +363,7 @@ class Params extends \VuFind\Search\Base\Params
             $this->initAdvancedFacets();
             $this->initBasicFacets();
         }
+        $this->initCheckboxFacets();
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Search/Summon/Params.php b/module/VuFind/src/VuFind/Search/Summon/Params.php
index 217fa24235a..17b911c0c65 100644
--- a/module/VuFind/src/VuFind/Search/Summon/Params.php
+++ b/module/VuFind/src/VuFind/Search/Summon/Params.php
@@ -339,5 +339,6 @@ class Params extends \VuFind\Search\Base\Params
     {
         $this->initFacetList('Facets', 'Results_Settings', 'Summon');
         $this->initFacetList('Advanced_Facets', 'Advanced_Facet_Settings', 'Summon');
+        $this->initCheckboxFacets('CheckboxFacets', 'Summon');
     }
 }
diff --git a/themes/bootstrap3/templates/search/history-table.phtml b/themes/bootstrap3/templates/search/history-table.phtml
index 9daf184a3e2..27126e25d9b 100644
--- a/themes/bootstrap3/templates/search/history-table.phtml
+++ b/themes/bootstrap3/templates/search/history-table.phtml
@@ -18,12 +18,17 @@
         ?></a>
       </td>
       <td>
-        <? $info->getParams()->activateAllFacets(); foreach ($info->getParams()->getFilterList() as $field => $filters): ?>
+        <? $info->getParams()->activateAllFacets(); foreach ($info->getParams()->getFilterList(true) as $field => $filters): ?>
           <? foreach ($filters as $i => $filter): ?>
             <? if ($filter['operator'] == 'NOT') echo $this->transEsc('NOT') . ' '; if ($filter['operator'] == 'OR' && $i > 0) echo $this->transEsc('OR') . ' '; ?>
             <strong><?=$this->transEsc($field)?></strong>: <?=$this->escapeHtml($filter['displayText'])?><br/>
           <? endforeach; ?>
         <? endforeach; ?>
+        <? foreach($info->getParams()->getCheckboxFacets() as $facet): ?>
+          <? if ($facet['selected']): ?>
+            <strong><?=$this->transEsc($facet['desc'])?></strong><br/>
+          <? endif; ?>
+        <? endforeach; ?>
       </td>
       <td><?=$this->escapeHtml($this->localizedNumber($info->getResultTotal()))?></td>
       <? if ($saveSupported): ?>
diff --git a/themes/jquerymobile/templates/search/history-table.phtml b/themes/jquerymobile/templates/search/history-table.phtml
index c43cabf5643..480d6d22f14 100644
--- a/themes/jquerymobile/templates/search/history-table.phtml
+++ b/themes/jquerymobile/templates/search/history-table.phtml
@@ -10,11 +10,16 @@
     ?></h3>
     <span class="ui-li-count"><?=$this->escapeHtml($this->localizedNumber($info->getResultTotal()))?></span>
     <p><strong><?=$this->transEsc("history_time")?></strong>: <?=$this->escapeHtml($this->dateTime()->convertToDisplayDateAndTime("U", $info->getStartTime()))?></p>
-    <? $info->getParams()->activateAllFacets(); foreach ($info->getParams()->getFilterList() as $field => $filters): ?>
+    <? $info->getParams()->activateAllFacets(); foreach ($info->getParams()->getFilterList(true) as $field => $filters): ?>
       <? foreach ($filters as $i => $filter): ?>
         <p><? if ($filter['operator'] == 'NOT') echo $this->transEsc('NOT') . ' '; if ($filter['operator'] == 'OR' && $i > 0) echo $this->transEsc('OR') . ' '; ?><strong><?=$this->transEsc($field)?></strong>: <?=$this->escapeHtml($filter['displayText'])?></p>
       <? endforeach; ?>
     <? endforeach; ?>
+    <? foreach($info->getParams()->getCheckboxFacets() as $facet): ?>
+      <? if ($facet['selected']): ?>
+        <p><strong><?=$this->transEsc($facet['desc'])?></strong></p>
+      <? endif; ?>
+    <? endforeach; ?>
     </div>
     </a>
     <? if ($saveSupported): ?>
-- 
GitLab