From 144683e3b71d0361b1b552f12f8fdc019d2744f0 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 3 Jul 2013 14:15:52 -0400
Subject: [PATCH] New recommendation module: SummonResultsDeferred. Resolves
 VUFIND-838.

---
 config/vufind/searches.ini                    |   2 +
 module/VuFind/config/module.config.php        |   1 +
 .../Recommend/SummonResultsDeferred.php       | 148 ++++++++++++++++++
 .../Recommend/SummonResultsDeferred.phtml     |   9 ++
 .../Recommend/SummonResultsDeferred.phtml     |   1 +
 5 files changed, 161 insertions(+)
 create mode 100644 module/VuFind/src/VuFind/Recommend/SummonResultsDeferred.php
 create mode 100644 themes/blueprint/templates/Recommend/SummonResultsDeferred.phtml
 create mode 100644 themes/jquerymobile/templates/Recommend/SummonResultsDeferred.phtml

diff --git a/config/vufind/searches.ini b/config/vufind/searches.ini
index 167e3e1247a..2582b0323b2 100644
--- a/config/vufind/searches.ini
+++ b/config/vufind/searches.ini
@@ -211,6 +211,8 @@ CallNumber = callnumber
 ;       Display Summon search results matching the terms found in the specified
 ;       GET parameter (default = "lookfor"), limited to a specified number of
 ;       matches (default = 5).
+; SummonResultsDeferred:[GET parameter]:[result limit]
+;       Same as SummonResults, but loaded via AJAX.
 ; WebResults:[GET parameter]:[result limit]
 ;       Display website search results matching the terms found in the specified
 ;       GET parameter (default = "lookfor"), limited to a specified number of
diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 16ced186f7e..e4dd9f704ff 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -636,6 +636,7 @@ $config = array(
                     'openlibrarysubjectsdeferred' => 'VuFind\Recommend\OpenLibrarySubjectsDeferred',
                     'pubdatevisajax' => 'VuFind\Recommend\PubDateVisAjax',
                     'resultgooglemapajax' => 'VuFind\Recommend\ResultGoogleMapAjax',
+                    'summonresultsdeferred' => 'VuFind\Recommend\SummonResultsDeferred',
                     'switchtype' => 'VuFind\Recommend\SwitchType',
                 ),
             ),
diff --git a/module/VuFind/src/VuFind/Recommend/SummonResultsDeferred.php b/module/VuFind/src/VuFind/Recommend/SummonResultsDeferred.php
new file mode 100644
index 00000000000..6a38f494aae
--- /dev/null
+++ b/module/VuFind/src/VuFind/Recommend/SummonResultsDeferred.php
@@ -0,0 +1,148 @@
+<?php
+/**
+ * SummonResultsDeferred Recommendations Module
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Recommendations
+ * @author   Lutz Biedinger <lutz.biedinger@gmail.com>
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:recommendation_modules Wiki
+ */
+namespace VuFind\Recommend;
+
+/**
+ * SummonResultsDeferred Recommendations Module
+ *
+ * This class sets up an AJAX call to trigger a call to the SummonResults
+ * module.  
+ *
+ * @category VuFind2
+ * @package  Recommendations
+ * @author   Lutz Biedinger <lutz.biedigner@gmail.com>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:recommendation_modules Wiki
+ */
+class SummonResultsDeferred implements RecommendInterface
+{
+    /**
+     * Raw configuration parameters
+     *
+     * @var string
+     */
+    protected $rawParams;
+
+    /**
+     * Current search query
+     *
+     * @var string
+     */
+    protected $lookfor;
+
+    /**
+     * Configuration parameters processed for submission via AJAX
+     *
+     * @var string
+     */
+    protected $processedParams;
+
+    /**
+     * setConfig
+     *
+     * Store the configuration of the recommendation module.
+     *
+     * @param string $settings Settings from searches.ini.
+     *
+     * @return void
+     */
+    public function setConfig($settings)
+    {
+        $this->rawParams = $settings;
+    }
+
+    /**
+     * init
+     *
+     * Called at the end of the Search Params objects' initFromRequest() method.
+     * This method is responsible for setting search parameters needed by the
+     * recommendation module and for reading any existing search parameters that may
+     * be needed.
+     *
+     * @param \VuFind\Search\Base\Params $params  Search parameter object
+     * @param \Zend\StdLib\Parameters    $request Parameter object representing user
+     * request.
+     *
+     * @return void
+     */
+    public function init($params, $request)
+    {
+        // Parse out parameters:
+        $settings = explode(':', $this->rawParams);
+
+        // Make sure all elements of the params array are filled in, even if just
+        // with a blank string, so we can rebuild the parameters to pass through
+        // AJAX later on!
+        for ($i = 0; $i < 2; $i++) {
+            $settings[$i] = isset($settings[$i]) ? $settings[$i] : '';
+        }
+
+        // Collect the best possible search term(s):
+        $lookforParam = empty($settings[0]) ? 'lookfor' : $settings[0];
+        $this->lookfor =  $request->get($lookforParam, '');
+        if (empty($this->lookfor) && is_object($params)) {
+            $this->lookfor = $params->getQuery()->getAllTerms();
+        }
+        $this->lookfor = trim($this->lookfor);
+
+        // In AJAX mode, the query will always be found in the 'lookfor' parameter,
+        // so override the setting:
+        $settings[0] = 'lookfor';
+
+        // Now rebuild the parameters to pass via AJAX:
+        $this->processedParams = implode(':', $settings);
+    }
+
+    /**
+     * process
+     *
+     * Called after the Search Results object has performed its main search.  This
+     * may be used to extract necessary information from the Search Results object
+     * or to perform completely unrelated processing.
+     *
+     * @param \VuFind\Search\Base\Results $results Search results object
+     *
+     * @return void
+     */
+    public function process($results)
+    {
+        // No action needed
+    }
+
+    /**
+     * Get the URL parameters needed to make the AJAX recommendation request.
+     *
+     * @return string
+     */
+    public function getUrlParams()
+    {
+        return 'mod=SummonResults&params=' . urlencode($this->processedParams)
+            . '&lookfor=' . urlencode($this->lookfor);
+    }
+}
\ No newline at end of file
diff --git a/themes/blueprint/templates/Recommend/SummonResultsDeferred.phtml b/themes/blueprint/templates/Recommend/SummonResultsDeferred.phtml
new file mode 100644
index 00000000000..80b00782cea
--- /dev/null
+++ b/themes/blueprint/templates/Recommend/SummonResultsDeferred.phtml
@@ -0,0 +1,9 @@
+<?
+    // Set up Javascript for use below:
+    $loadJs = 'var url = path + "/AJAX/Recommend?' . $this->recommend->getUrlParams() . '";'
+        . "\$('#SummonDeferredRecommend').load(url);";
+?>
+<div id="SummonDeferredRecommend">
+    <p><?=$this->transEsc("Loading")?>... <img src="<?=$this->imageLink('ajax_loading.gif')?>" /></p>
+    <?=$this->inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $loadJs)?>
+</div>
\ No newline at end of file
diff --git a/themes/jquerymobile/templates/Recommend/SummonResultsDeferred.phtml b/themes/jquerymobile/templates/Recommend/SummonResultsDeferred.phtml
new file mode 100644
index 00000000000..0df1e74df18
--- /dev/null
+++ b/themes/jquerymobile/templates/Recommend/SummonResultsDeferred.phtml
@@ -0,0 +1 @@
+<? /* Not supported in mobile theme. */ ?>
\ No newline at end of file
-- 
GitLab