From 8f2e73def07ec0dfa1b30b3f2d39c72921ecbe57 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 6 Nov 2019 23:19:43 +0200
Subject: [PATCH] Fix filters displaying on home or record page when
 retain_filters_by_default = false. (#1482)

---
 .../templates/RecordTab/collectionlist.phtml        | 11 +++++++++--
 themes/bootstrap3/templates/combined/results.phtml  |  2 +-
 themes/bootstrap3/templates/search/filters.phtml    |  8 +++-----
 themes/bootstrap3/templates/search/results.phtml    |  2 +-
 themes/bootstrap3/templates/search/searchbox.phtml  | 13 +++++++++++--
 5 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/themes/bootstrap3/templates/RecordTab/collectionlist.phtml b/themes/bootstrap3/templates/RecordTab/collectionlist.phtml
index f124aeb16e0..22460757bdd 100644
--- a/themes/bootstrap3/templates/RecordTab/collectionlist.phtml
+++ b/themes/bootstrap3/templates/RecordTab/collectionlist.phtml
@@ -6,6 +6,9 @@
   $results = $this->tab->getResults();
   $params = $this->tab->getParams();
   $searchDetails = ['results' => $results, 'params' => $params, 'indexStart' => 1];
+
+  $filterList = $params->getFilterList(false);
+  $checkboxFilters = $params->getCheckboxFacets();
 ?>
 <div class="<?=$this->layoutClass('mainbody')?>">
   <?php if (($recordTotal = $results->getResultTotal()) > 0): // only display these at very top if we have results ?>
@@ -42,11 +45,15 @@
     </div>
   </div>
 
-  <?php if (!empty($params->getFilterList(false))): ?>
+
+  <?php if ($filterList || $checkboxFilters): ?>
     <?=$this->render('search/filters.phtml',
       [
-        'params' => $params,
         'urlQuery' => $results->getUrlQuery(),
+        'filterList' => $filterList,
+        'checkboxFilters' => $checkboxFilters,
+        'searchClassId' => $this->searchClassId,
+        'searchType' => 'basic',
       ]
     ); ?>
   <?php endif; ?>
diff --git a/themes/bootstrap3/templates/combined/results.phtml b/themes/bootstrap3/templates/combined/results.phtml
index 0a552a622f9..01c5a707b00 100644
--- a/themes/bootstrap3/templates/combined/results.phtml
+++ b/themes/bootstrap3/templates/combined/results.phtml
@@ -17,7 +17,7 @@
       'searchId' => $this->results->getSearchId(),
       'searchClassId' => $this->params->getSearchClassId(),
       'checkboxFilters' => $this->params->getCheckboxFacets(),
-      'filterList' => $this->params->getFilters(),
+      'filterList' => $this->params->getFilterList(),
       'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
       'selectedShards' => $this->params->getSelectedShards()
     ]
diff --git a/themes/bootstrap3/templates/search/filters.phtml b/themes/bootstrap3/templates/search/filters.phtml
index ef4e5274109..30286c0dc8d 100644
--- a/themes/bootstrap3/templates/search/filters.phtml
+++ b/themes/bootstrap3/templates/search/filters.phtml
@@ -7,10 +7,8 @@
     $params = clone $this->params;
   }
 
-  $filterList = $params->getFilterList(true);
-  $checkboxFilters = $params->getCheckboxFacets();
-  $lastSort = $this->searchMemory()->getLastSort($params->getSearchClassId());
-  $options = $this->searchOptions($params->getSearchClassId());
+  $lastSort = $this->searchMemory()->getLastSort($this->searchClassId);
+  $options = $this->searchOptions($this->searchClassId);
   $hasDefaultsApplied = $params->hasDefaultsApplied();
   $filterCount = $this->searchbox()->getFilterCount($checkboxFilters, $filterList);
 
@@ -25,7 +23,7 @@
     }
   }
 
-  $advancedSearch = $params->getSearchType() === 'advanced';
+  $advancedSearch = $this->searchType === 'advanced';
 ?>
 <?php ob_start(); ?>
   <?php foreach ($checkboxFilters as $filter): ?>
diff --git a/themes/bootstrap3/templates/search/results.phtml b/themes/bootstrap3/templates/search/results.phtml
index 5f6f4be0df1..76b09cb00e7 100644
--- a/themes/bootstrap3/templates/search/results.phtml
+++ b/themes/bootstrap3/templates/search/results.phtml
@@ -17,7 +17,7 @@
         'searchId' => $this->results->getSearchId(),
         'searchClassId' => $this->params->getSearchClassId(),
         'checkboxFilters' => $this->params->getCheckboxFacets(),
-        'filterList' => $this->params->getFilters(),
+        'filterList' => $this->params->getFilterList(),
         'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
         'selectedShards' => $this->params->getSelectedShards(),
         'ignoreHiddenFiltersInRequest' => isset($this->ignoreHiddenFiltersInRequest) ? $this->ignoreHiddenFiltersInRequest : false,
diff --git a/themes/bootstrap3/templates/search/searchbox.phtml b/themes/bootstrap3/templates/search/searchbox.phtml
index 75fd6c9e65b..1a03f5753dc 100644
--- a/themes/bootstrap3/templates/search/searchbox.phtml
+++ b/themes/bootstrap3/templates/search/searchbox.phtml
@@ -36,6 +36,7 @@
         $checkboxFilters = is_array($this->checkboxFilters) ? $this->checkboxFilters : [];
     }
     $filterDetails = $this->searchbox()->getFilterDetails($filterList, $checkboxFilters);
+    $showFilters = $filterDetails && (isset($results) || $options->getRetainFilterSetting());
 ?>
 <?php $tabConfig = $this->searchTabs()->getTabConfig($this->searchClassId, $this->lookfor, $this->searchIndex, $this->searchType, $hiddenFilters); ?>
 <?php if ($this->searchType == 'advanced'): ?>
@@ -52,7 +53,11 @@
         'search/filters.phtml',
         [
           'params' => $params ?? null,
-          'urlQuery' => isset($results) ? $results->getUrlQuery() : null
+          'urlQuery' => isset($results) ? $results->getUrlQuery() : null,
+          'filterList' => $showFilters ? $filterList : [],
+          'checkboxFilters' => $showFilters ? $checkboxFilters : [],
+          'searchClassId' => $this->searchClassId,
+          'searchType' => $this->searchType,
         ]
       );?>
     <?php if (!empty($tabs)): ?></div><?php endif; ?>
@@ -140,7 +145,11 @@
       'search/filters.phtml',
       [
         'params' => $params ?? null,
-        'urlQuery' => isset($results) ? $results->getUrlQuery() : null
+        'urlQuery' => isset($results) ? $results->getUrlQuery() : null,
+        'filterList' => $showFilters ? $filterList : [],
+        'checkboxFilters' => $showFilters ? $checkboxFilters : [],
+        'searchClassId' => $this->searchClassId,
+        'searchType' => $this->searchType,
       ]
     );?>
   </form>
-- 
GitLab