From 2f5ca5c0d9c3b247bb0bf9d33e4811e83612eebf Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 5 Sep 2013 09:24:11 -0400
Subject: [PATCH] Resolving VUFIND-882 (Combined search conflicts with search
 memory)

---
 .../src/VuFind/Controller/AbstractSearch.php  | 24 ++++++++++++++-----
 .../VuFind/Controller/CombinedController.php  | 12 +++++++++-
 module/VuFind/src/VuFind/Search/Memory.php    | 23 ++++++++++++++++++
 3 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
index be906c0b794..02f64ba6a86 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
@@ -164,6 +164,23 @@ class AbstractSearch extends AbstractBase
         return false;
     }
 
+    /**
+     * Store the URL of the provided search (if appropriate).
+     *
+     * @param \VuFind\Search\Base\Results $results Search results object
+     *
+     * @return void
+     */
+    protected function rememberSearch($results)
+    {
+        if ($this->rememberSearch) {
+            $searchUrl = $this->url()->fromRoute(
+                $results->getOptions()->getSearchAction()
+            ) . $results->getUrlQuery()->getParams(false);
+            Memory::rememberSearch($searchUrl);
+        }
+    }
+
     /**
      * Send search results to results view
      *
@@ -209,12 +226,7 @@ class AbstractSearch extends AbstractBase
             // Send results to the view and remember the current URL as the last
             // search.
             $view->results = $results;
-            if ($this->rememberSearch) {
-                $searchUrl = $this->url()->fromRoute(
-                    $results->getOptions()->getSearchAction()
-                ) . $results->getUrlQuery()->getParams(false);
-                Memory::rememberSearch($searchUrl);
-            }
+            $this->rememberSearch($results);
 
             // Add to search history:
             if ($this->saveToHistory) {
diff --git a/module/VuFind/src/VuFind/Controller/CombinedController.php b/module/VuFind/src/VuFind/Controller/CombinedController.php
index 40a47f336a2..b29cef82240 100644
--- a/module/VuFind/src/VuFind/Controller/CombinedController.php
+++ b/module/VuFind/src/VuFind/Controller/CombinedController.php
@@ -26,7 +26,7 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Controller;
-use Zend\Stdlib\Parameters;
+use VuFind\Search\Memory, Zend\Stdlib\Parameters;
 
 /**
  * Redirects the user to the appropriate default VuFind action.
@@ -65,6 +65,11 @@ class CombinedController extends AbstractSearch
      */
     public function resultAction()
     {
+        $this->writeSession();  // avoid session write timing bug
+
+        // Turn off search memory -- not relevant in this context:
+        Memory::disable();
+
         // Validate configuration:
         $searchClassId = $this->params()->fromQuery('id');
         $config = $this->getServiceLocator()->get('VuFind\Config')->get('combined')
@@ -113,6 +118,11 @@ class CombinedController extends AbstractSearch
             )
         );
 
+        // Remember the current URL, then disable memory so multi-search results
+        // don't overwrite it:
+        $this->rememberSearch($results);
+        Memory::disable();
+
         // Gather combined results:
         $combinedResults = array();
         $options = $this->getServiceLocator()
diff --git a/module/VuFind/src/VuFind/Search/Memory.php b/module/VuFind/src/VuFind/Search/Memory.php
index 574c8b965ca..fe04d8749f9 100644
--- a/module/VuFind/src/VuFind/Search/Memory.php
+++ b/module/VuFind/src/VuFind/Search/Memory.php
@@ -39,6 +39,24 @@ use Zend\Session\Container as SessionContainer;
  */
 class Memory
 {
+    /**
+     * Is memory currently active? (i.e. will we save new URLs?)
+     *
+     * @var bool
+     */
+    protected static $active = true;
+
+    /**
+     * Stop updating the URL in memory -- used in combined search to prevent
+     * multiple search URLs from overwriting one another.
+     *
+     * @return void
+     */
+    public function disable()
+    {
+        self::$active = false;
+    }
+
     /**
      * Clear the last accessed search URL in the session.
      *
@@ -59,6 +77,11 @@ class Memory
      */
     public static function rememberSearch($url)
     {
+        // Do nothing if disabled.
+        if (!self::$active) {
+            return;
+        }
+
         // Only remember URL if string is non-empty... otherwise clear the memory.
         if (strlen(trim($url)) > 0) {
             $session = new SessionContainer('Search');
-- 
GitLab