From 920e144c0ce518d08efa366d56b7cccaf7f83411 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 5 Sep 2012 15:41:47 -0400
Subject: [PATCH] Incorporated search manager into advanced facet generation.

---
 .../VuFind/Controller/SearchController.php    | 16 ++++---
 .../VuFind/src/VuFind/Search/Base/Params.php  | 17 +++++++-
 .../VuFind/src/VuFind/Search/Base/Results.php | 42 ++++++++++++++++++-
 3 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php
index e1ad5c66e3d..05be63182b1 100644
--- a/module/VuFind/src/VuFind/Controller/SearchController.php
+++ b/module/VuFind/src/VuFind/Controller/SearchController.php
@@ -30,9 +30,7 @@ namespace VuFind\Controller;
 use VuFind\Cache\Manager as CacheManager, VuFind\Config\Reader as ConfigReader,
     VuFind\Connection\Manager as ConnectionManager,
     VuFind\Db\Table\Search as SearchTable, VuFind\Exception\Mail as MailException,
-    VuFind\Mailer,
-    VuFind\Search\Memory, VuFind\Search\Solr\Params, VuFind\Search\Solr\Results,
-    VuFind\Solr\Utils as SolrUtils;
+    VuFind\Mailer, VuFind\Search\Memory, VuFind\Solr\Utils as SolrUtils;
 
 /**
  * Redirects the user to the appropriate default VuFind action.
@@ -507,17 +505,25 @@ class SearchController extends AbstractSearch
             // we may want to make this more flexible later.  Also keep in mind that
             // the template is currently looking for certain hard-coded fields; this
             // should also be made smarter.
-            $params = new Params();
+            $sm = $this->getSearchManager()->setSearchClassId('Solr');
+            $params = $sm->getParams();
             $params->initAdvancedFacets();
 
             // We only care about facet lists, so don't get any results (this helps
             // prevent problems with serialized File_MARC objects in the cache):
             $params->setLimit(0);
 
-            $results = new Results($params);
+            $results = $sm->getResults($params);
             $results->getResults();                     // force processing for cache
+
+            // Temporarily remove the service manager so we can cache the
+            // results (otherwise we'll get errors about serializing closures):
+            $results->unsetServiceLocator();
             $cache->setItem('solrSearchHomeFacets', $results);
         }
+
+        // Restore the real service locator to the object:
+        $results->restoreServiceLocator($this->getServiceLocator());
         return $results;
     }
 
diff --git a/module/VuFind/src/VuFind/Search/Base/Params.php b/module/VuFind/src/VuFind/Search/Base/Params.php
index 0375dea9cef..171bcdd26e7 100644
--- a/module/VuFind/src/VuFind/Search/Base/Params.php
+++ b/module/VuFind/src/VuFind/Search/Base/Params.php
@@ -1505,12 +1505,27 @@ class Params implements ServiceLocatorAwareInterface
         return $this->selectedShards;
     }
 
+    /**
+     * Unset the service locator.
+     *
+     * @return Params
+     */
+    public function unsetServiceLocator()
+    {
+        $this->serviceLocator = null;
+        $options = $this->getOptions();
+        if (method_exists($options, 'unsetServiceLocator')) {
+            $params->unsetServiceLocator();
+        }
+        return $this;
+    }
+
     /**
      * Set the service locator.
      *
      * @param ServiceLocatorInterface $serviceLocator Locator to register
      *
-     * @return Manager
+     * @return Params
      */
     public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
     {
diff --git a/module/VuFind/src/VuFind/Search/Base/Results.php b/module/VuFind/src/VuFind/Search/Base/Results.php
index 2378b1982ed..2c57073ee56 100644
--- a/module/VuFind/src/VuFind/Search/Base/Results.php
+++ b/module/VuFind/src/VuFind/Search/Base/Results.php
@@ -525,7 +525,7 @@ abstract class Results implements ServiceLocatorAwareInterface
      *
      * @param ServiceLocatorInterface $serviceLocator Locator to register
      *
-     * @return Manager
+     * @return Results
      */
     public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
     {
@@ -533,6 +533,46 @@ abstract class Results implements ServiceLocatorAwareInterface
         return $this;
     }
 
+    /**
+     * Restore the service locator (a cascading version of setServiceLocator()).
+     *
+     * @param ServiceLocatorInterface $serviceLocator Locator to register
+     *
+     * @return Results
+     */
+    public function restoreServiceLocator(ServiceLocatorInterface $serviceLocator)
+    {
+        $this->setServiceLocator($serviceLocator);
+        $params = $this->getParams();
+        if (method_exists($params, 'setServiceLocator')) {
+            $params->setServiceLocator($serviceLocator);
+        }
+        $options = $this->getOptions();
+        if (method_exists($options, 'setServiceLocator')) {
+            $params->setServiceLocator($serviceLocator);
+        }
+        return $this;
+    }
+
+    /**
+     * Unset the service locator.
+     *
+     * @return Results
+     */
+    public function unsetServiceLocator()
+    {
+        $this->serviceLocator = null;
+        $params = $this->getParams();
+        if (method_exists($params, 'unsetServiceLocator')) {
+            $params->unsetServiceLocator();
+        }
+        $options = $this->getOptions();
+        if (method_exists($options, 'unsetServiceLocator')) {
+            $params->unsetServiceLocator();
+        }
+        return $this;
+    }
+
     /**
      * Get the service locator.
      *
-- 
GitLab