From 48baa130d0b8109c7a980d5258970dafbb959060 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 17 Jun 2015 09:51:54 -0400
Subject: [PATCH] Allow top recommendations to display (and be independently
 configured) in combined search. - Resolves VUFIND-1095

---
 config/vufind/combined.ini                    |  6 ++++++
 .../src/VuFind/Controller/AbstractSearch.php  | 12 +++++++++++
 .../VuFind/Controller/CombinedController.php  | 20 ++++++++++++++++---
 .../templates/combined/results-list.phtml     |  3 +++
 .../templates/combined/results-list.phtml     |  3 +++
 5 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/config/vufind/combined.ini b/config/vufind/combined.ini
index 68dc1ac5e5b..1e1f1245022 100644
--- a/config/vufind/combined.ini
+++ b/config/vufind/combined.ini
@@ -16,6 +16,12 @@
 ; limit = The maximum number of search results to show in this column; note
 ;         that this must be a legal limit value for the chosen search backend.
 ;         (may sometimes require config changes -- e.g. searches.ini, Summon.ini).
+; include_recommendations = If set to true, standard 'top' recommendations will be
+;                           displayed at the top of the column. If set to an array
+;                           of recommendation settings (as per searches.ini), the
+;                           specified recommendations will be loaded into the top
+;                           area of the column. If set to false, recommendations
+;                           will be suppressed (default).
 ;
 ; All display text is subject to translation and may be added to the language
 ; .ini files.
diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
index b1dfb71f350..955150fe9bb 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
@@ -231,6 +231,18 @@ class AbstractSearch extends AbstractBase
         }
 
         $rManager = $this->getServiceLocator()->get('VuFind\RecommendPluginManager');
+
+        // Special case: override recommend settings through parameter (used by
+        // combined search)
+        if ($override = $this->params()->fromQuery('recommendOverride')) {
+            return function ($runner, $p, $searchId) use ($rManager, $override) {
+                $listener = new RecommendListener($rManager, $searchId);
+                $listener->setConfig($override);
+                $listener->attach($runner->getEventManager()->getSharedManager());
+            };
+        }
+
+        // Standard case: retrieve recommend settings from params object:
         return function ($runner, $params, $searchId) use ($rManager, $activeRecs) {
             $listener = new RecommendListener($rManager, $searchId);
             $config = [];
diff --git a/module/VuFind/src/VuFind/Controller/CombinedController.php b/module/VuFind/src/VuFind/Controller/CombinedController.php
index dd0fa333d0b..fb319fda7d4 100644
--- a/module/VuFind/src/VuFind/Controller/CombinedController.php
+++ b/module/VuFind/src/VuFind/Controller/CombinedController.php
@@ -256,8 +256,22 @@ class CombinedController extends AbstractSearch
         $query = $this->getRequest()->getQuery();
         $query->limit = isset($settings['limit']) ? $settings['limit'] : null;
 
-        // Disable top/side recommendations but leave noresults active:
-        $query->noRecommend = 'top,side';
+        // Reset override to avoid bleed-over from one section to the next!
+        $query->recommendOverride = false;
+
+        // Always leave noresults active (useful for 0-hit searches) and
+        // side inactive (no room to display) but display or hide top based
+        // on include_recommendations setting.
+        if (isset($settings['include_recommendations'])
+            && $settings['include_recommendations']
+        ) {
+            $query->noRecommend = 'side';
+            if (is_array($settings['include_recommendations'])) {
+                $query->recommendOverride
+                    = ['top' => $settings['include_recommendations']];
+            }
+        } else {
+            $query->noRecommend = 'top,side';
+        }
     }
 }
-
diff --git a/themes/blueprint/templates/combined/results-list.phtml b/themes/blueprint/templates/combined/results-list.phtml
index f2e3a9409e0..1dff16015e1 100644
--- a/themes/blueprint/templates/combined/results-list.phtml
+++ b/themes/blueprint/templates/combined/results-list.phtml
@@ -25,6 +25,9 @@
 <div class="resulthead">
   <div class="floatleft">
     <? if ($recordTotal > 0): ?>
+      <? foreach (($top = $results->getRecommendations('top')) as $current): ?>
+        <?=$this->recommend($current)?>
+      <? endforeach; ?>
       <?=$this->transEsc("Showing")?>
       <strong><?=$this->localizedNumber($results->getStartRecord())?></strong> - <strong><?=$this->localizedNumber($results->getEndRecord())?></strong>
       <? if (!isset($view->skipTotalCount)): ?>
diff --git a/themes/bootstrap3/templates/combined/results-list.phtml b/themes/bootstrap3/templates/combined/results-list.phtml
index f0ced1b2dd9..abbda2156e4 100644
--- a/themes/bootstrap3/templates/combined/results-list.phtml
+++ b/themes/bootstrap3/templates/combined/results-list.phtml
@@ -25,6 +25,9 @@
 <div class="clearfix">
   <div class="pull-left help-block">
     <? if ($recordTotal > 0): ?>
+      <? foreach (($top = $results->getRecommendations('top')) as $current): ?>
+        <?=$this->recommend($current)?>
+      <? endforeach; ?>
       <?=$this->transEsc("Showing")?>
       <strong><?=$this->localizedNumber($results->getStartRecord())?></strong> - <strong><?=$this->localizedNumber($results->getEndRecord())?></strong>
       <? if (!isset($view->skipTotalCount)): ?>
-- 
GitLab