diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index e517dadf44ccfd012f8d6d8af06c64b8bc5f5f36..33bb85ad003fa1ea45fe27a755570f59f003fd40 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -321,6 +321,7 @@ $config = array( 'invokables' => array( 'VuFind\SessionManager' => 'Zend\Session\SessionManager', 'VuFind\Search' => 'VuFindSearch\Service', + 'VuFind\Search\Memory' => 'VuFind\Search\Memory', ), 'initializers' => array( array('VuFind\ServiceManager\Initializer', 'initInstance'), diff --git a/module/VuFind/src/VuFind/Controller/AbstractBase.php b/module/VuFind/src/VuFind/Controller/AbstractBase.php index 426b5420107ed1fa59ad8fce740e6ae09aa7e99d..1d9f972e915efc2c944126e6514677708526c5b5 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractBase.php +++ b/module/VuFind/src/VuFind/Controller/AbstractBase.php @@ -379,4 +379,14 @@ class AbstractBase extends AbstractActionController { $this->getServiceLocator()->get('VuFind\SessionManager')->writeClose(); } + + /** + * Get the search memory + * + * @return \VuFind\Search\Memory + */ + public function getSearchMemory() + { + return $this->getServiceLocator()->get('VuFind\Search\Memory'); + } } diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php index 02f64ba6a862b496d54cb3053b2a4aaf81d7cce0..4264c1655ed47e38d2e3d8a6c540af5a12ac3f48 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php +++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php @@ -26,7 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Controller; -use VuFind\Search\Memory, Zend\Stdlib\Parameters; +use Zend\Stdlib\Parameters; /** * VuFind Search Controller @@ -177,7 +177,7 @@ class AbstractSearch extends AbstractBase $searchUrl = $this->url()->fromRoute( $results->getOptions()->getSearchAction() ) . $results->getUrlQuery()->getParams(false); - Memory::rememberSearch($searchUrl); + $this->getSearchMemory()->rememberSearch($searchUrl); } } diff --git a/module/VuFind/src/VuFind/Controller/CombinedController.php b/module/VuFind/src/VuFind/Controller/CombinedController.php index 0651f12d884268fa1c379d6a410040451ffd82d6..fa38c0b9e2ab97ecbb8fec321643dff8a81dcfbe 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 VuFind\Search\Memory, Zend\Stdlib\Parameters; +use Zend\Stdlib\Parameters; /** * Redirects the user to the appropriate default VuFind action. @@ -68,7 +68,7 @@ class CombinedController extends AbstractSearch $this->writeSession(); // avoid session write timing bug // Turn off search memory -- not relevant in this context: - Memory::disable(); + $this->getSearchMemory()->disable(); // Validate configuration: $searchClassId = $this->params()->fromQuery('id'); @@ -121,7 +121,7 @@ class CombinedController extends AbstractSearch // Remember the current URL, then disable memory so multi-search results // don't overwrite it: $this->rememberSearch($results); - Memory::disable(); + $this->getSearchMemory()->disable(); // Gather combined results: $combinedResults = array(); diff --git a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php index c676b2c1aa85acb3e23adc6b827f795af4c7f41d..64e609343e68e5bedd782739df0c1e9dcb1c3b07 100644 --- a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php +++ b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php @@ -26,7 +26,7 @@ * @link http://vufind.org/wiki/vufind2:developer_manual Wiki */ namespace VuFind\Controller\Plugin; -use VuFind\Search\Memory, Zend\Mvc\Controller\Plugin\AbstractPlugin, +use Zend\Mvc\Controller\Plugin\AbstractPlugin, Zend\Session\Container as SessionContainer; /** @@ -368,7 +368,7 @@ class ResultScroller extends AbstractPlugin $baseUrl = $this->getController()->url()->fromRoute( $search->getOptions()->getSearchAction() ); - Memory::rememberSearch( + $this->getController()->getSearchMemory()->rememberSearch( $baseUrl . $search->getUrlQuery()->getParams(false) ); } diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index 301689d951fe44191a3a0cb7fb3744c060f12314..ff5a531468a779899d1c72e9b90f05d886cabb5b 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -27,8 +27,7 @@ */ namespace VuFind\Controller; -use VuFind\Exception\Mail as MailException, VuFind\Search\Memory, - VuFind\Solr\Utils as SolrUtils; +use VuFind\Exception\Mail as MailException, VuFind\Solr\Utils as SolrUtils; /** * Redirects the user to the appropriate default VuFind action. @@ -265,7 +264,7 @@ class SearchController extends AbstractSearch $current->delete(); // We don't want to remember the last search after a purge: - Memory::forgetSearch(); + $this->getSearchMemory()->forgetSearch(); } else { // Otherwise add to the list $unsaved[] = $minSO->deminify($this->getResultsManager()); diff --git a/module/VuFind/src/VuFind/Search/Memory.php b/module/VuFind/src/VuFind/Search/Memory.php index fe04d8749f980f8f9ed072b5594442fff279f204..55f2769453c3921e1916c67f0e2a50e409bf39f1 100644 --- a/module/VuFind/src/VuFind/Search/Memory.php +++ b/module/VuFind/src/VuFind/Search/Memory.php @@ -26,7 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Search; -use Zend\Session\Container as SessionContainer; +use Zend\Session\Container; /** * Wrapper class to handle search memory @@ -44,7 +44,25 @@ class Memory * * @var bool */ - protected static $active = true; + protected $active = true; + + /** + * Session container + * + * @var Container + */ + protected $session; + + /** + * Constructor + * + * @param Container $session Session container for storing URLs (optional) + */ + public function __construct($session = null) + { + $this->session = (null === $session) + ? new Container('Search') : $session; + } /** * Stop updating the URL in memory -- used in combined search to prevent @@ -54,7 +72,7 @@ class Memory */ public function disable() { - self::$active = false; + $this->active = false; } /** @@ -62,10 +80,9 @@ class Memory * * @return void */ - public static function forgetSearch() + public function forgetSearch() { - $session = new SessionContainer('Search'); - unset($session->last); + unset($this->session->last); } /** @@ -75,19 +92,18 @@ class Memory * * @return void */ - public static function rememberSearch($url) + public function rememberSearch($url) { // Do nothing if disabled. - if (!self::$active) { + if (!$this->active) { return; } // Only remember URL if string is non-empty... otherwise clear the memory. if (strlen(trim($url)) > 0) { - $session = new SessionContainer('Search'); - $session->last = $url; + $this->session->last = $url; } else { - self::forgetSearch(); + $this->forgetSearch(); } } @@ -97,9 +113,8 @@ class Memory * * @return string|null */ - public static function retrieve() + public function retrieve() { - $session = new SessionContainer('Search'); - return isset($session->last) ? $session->last : null; + return isset($this->session->last) ? $this->session->last : null; } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php b/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php index 9295b6e241424ac6d650a97eee511770b4397cfd..8bc3eed1b82d83d200f1db8634bed6ebc1276aef 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/GetLastSearchLink.php @@ -39,6 +39,23 @@ use VuFind\Search\Memory, Zend\View\Helper\AbstractHelper; */ class GetLastSearchLink extends AbstractHelper { + /** + * Search memory + * + * @var Memory + */ + protected $memory; + + /** + * Constructor + * + * @param Memory $memory Search memory + */ + public function __construct(Memory $memory) + { + $this->memory = $memory; + } + /** * If a previous search is recorded in the session, return a link to it; * otherwise, return a blank string. @@ -51,7 +68,7 @@ class GetLastSearchLink extends AbstractHelper */ public function __invoke($link, $prefix = '', $suffix = '') { - $last = Memory::retrieve(); + $last = $this->memory->retrieve(); if (!empty($last)) { $escaper = $this->getView()->plugin('escapeHtml'); return $prefix . '<a href="' . $escaper($last) . '">' . $link . '</a>' diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php index 4e0670b1baa81e3102cc47f0f2ce4707b2dbe4e0..f7625e870844975ca455844019e64bb9d1a6eaff 100644 --- a/themes/root/theme.config.php +++ b/themes/root/theme.config.php @@ -66,6 +66,11 @@ return array( ? $config->GoogleAnalytics->apiKey : false; return new \VuFind\View\Helper\Root\GoogleAnalytics($key); }, + 'getlastsearchlink' => function ($sm) { + return new \VuFind\View\Helper\Root\GetLastSearchLink( + $sm->getServiceLocator()->get('VuFind\Search\Memory') + ); + }, 'historylabel' => function ($sm) { $config = $sm->getServiceLocator()->get('VuFind\Config')->get('config'); $config = isset($config->SearchHistoryLabels) @@ -160,7 +165,6 @@ return array( 'browse' => 'VuFind\View\Helper\Root\Browse', 'context' => 'VuFind\View\Helper\Root\Context', 'currentpath' => 'VuFind\View\Helper\Root\CurrentPath', - 'getlastsearchlink' => 'VuFind\View\Helper\Root\GetLastSearchLink', 'highlight' => 'VuFind\View\Helper\Root\Highlight', 'jqueryvalidation' => 'VuFind\View\Helper\Root\JqueryValidation', 'printms' => 'VuFind\View\Helper\Root\Printms',