From 6d5db0da4b6ea92e5c7210aa057ea61d16f1fd4f Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 16 May 2014 12:56:12 -0400
Subject: [PATCH] Pre-apply default filters to advanced screen for better user
 control.

---
 .../VuFind/src/VuFind/Controller/AbstractSearch.php   | 11 +++++++++++
 .../blueprint/templates/search/advanced/layout.phtml  |  7 ++++++-
 .../bootstrap/templates/search/advanced/layout.phtml  |  7 ++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
index bfc25aa1c8f..e8bdeb640e1 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
@@ -112,6 +112,17 @@ class AbstractSearch extends AbstractBase
             $view->saved = $this->restoreAdvancedSearch($searchId);
         }
 
+        // If we have default filters, set them up as a fake "saved" search
+        // to properly populate special controls on the advanced screen.
+        if (!$view->saved && count($view->options->getDefaultFilters()) > 0) {
+            $view->saved = $this->getServiceLocator()
+                ->get('VuFind\SearchResultsPluginManager')
+                ->get($this->searchClassId);
+            $view->saved->getParams()->initFromRequest(
+                new \Zend\StdLib\Parameters(array())
+            );
+        }
+
         return $view;
     }
 
diff --git a/themes/blueprint/templates/search/advanced/layout.phtml b/themes/blueprint/templates/search/advanced/layout.phtml
index 8915c9b015c..d7ad979a757 100644
--- a/themes/blueprint/templates/search/advanced/layout.phtml
+++ b/themes/blueprint/templates/search/advanced/layout.phtml
@@ -11,7 +11,12 @@
     // Set up saved search details:
     if (isset($this->saved) && is_object($this->saved)) {
         $searchDetails = $this->saved->getParams()->getQuery();
-        $groups = $searchDetails->getQueries();
+        if ($searchDetails instanceof \VuFindSearch\Query\Query) {
+            // Not an advanced query -- ignore it.
+            $searchDetails = $groups = false;
+        } else {
+            $groups = $searchDetails->getQueries();
+        }
         $hasDefaultsApplied = $this->saved->getParams()->hasDefaultsApplied();
         $searchFilters = $this->saved->getParams()->getFilterList();
     } else {
diff --git a/themes/bootstrap/templates/search/advanced/layout.phtml b/themes/bootstrap/templates/search/advanced/layout.phtml
index fa9ae6d508f..9b224c58ee5 100644
--- a/themes/bootstrap/templates/search/advanced/layout.phtml
+++ b/themes/bootstrap/templates/search/advanced/layout.phtml
@@ -12,7 +12,12 @@
   // Set up saved search details:
   if (isset($this->saved) && is_object($this->saved)) {
     $searchDetails = $this->saved->getParams()->getQuery();
-    $groups = $searchDetails->getQueries();
+    if ($searchDetails instanceof \VuFindSearch\Query\Query) {
+        // Not an advanced query -- ignore it.
+        $searchDetails = $groups = false;
+    } else {
+        $groups = $searchDetails->getQueries();
+    }
     $hasDefaultsApplied = $this->saved->getParams()->hasDefaultsApplied();
     $searchFilters = $this->saved->getParams()->getFilterList();
   } else {
-- 
GitLab