From 761afb1973cb55fca6c717cbca11465041f7f834 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 20 Nov 2015 14:12:23 -0500
Subject: [PATCH] Allow saved searches to be disabled by configuration.

---
 config/vufind/config.ini                      |  2 +
 .../src/VuFind/Config/AccountCapabilities.php | 12 ++++
 .../Controller/MyResearchController.php       |  6 ++
 .../View/Helper/Root/AccountCapabilities.php  | 68 +++++++++++++++++++
 .../src/VuFind/View/Helper/Root/Factory.php   | 14 ++++
 .../templates/myresearch/menu.phtml           | 10 +--
 .../templates/search/history-table.phtml      | 19 +++---
 .../bootstrap3/templates/search/history.phtml | 22 +++---
 .../bootstrap3/templates/search/results.phtml |  2 +-
 .../templates/search/header-navbar.phtml      |  2 +-
 .../templates/search/history-table.phtml      | 11 +--
 .../templates/search/history.phtml            |  5 ++
 themes/root/theme.config.php                  |  1 +
 13 files changed, 147 insertions(+), 27 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/View/Helper/Root/AccountCapabilities.php

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 0622661cee5..3f9b3c490cb 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -97,6 +97,8 @@ showBookBag = false
 bookBagMaxSize = 100
 ; Display bulk items (export, save, etc.) and checkboxes on search result screens?
 showBulkOptions = false
+; Should users be allowed to save searches in their accounts?
+allowSavedSearches = true
 ; Generator value to display in an HTML header <meta> tag:
 generator = "VuFind 2.5"
 
diff --git a/module/VuFind/src/VuFind/Config/AccountCapabilities.php b/module/VuFind/src/VuFind/Config/AccountCapabilities.php
index 50108eee7a2..74b9cadee8c 100644
--- a/module/VuFind/src/VuFind/Config/AccountCapabilities.php
+++ b/module/VuFind/src/VuFind/Config/AccountCapabilities.php
@@ -89,6 +89,18 @@ class AccountCapabilities
         return $setting;
     }
 
+    /**
+     * Get saved search setting.
+     *
+     * @return string
+     */
+    public function getSavedSearchSetting()
+    {
+        return isset($this->config->Site->allowSavedSearches)
+            && !$this->config->Site->allowSavedSearches
+            ? 'disabled' : 'enabled';
+    }
+
     /**
      * Get tag setting.
      *
diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php
index d69baad7771..0ab2057c642 100644
--- a/module/VuFind/src/VuFind/Controller/MyResearchController.php
+++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php
@@ -284,6 +284,12 @@ class MyResearchController extends AbstractBase
      */
     public function savesearchAction()
     {
+        // Fail if saved searches are disabled.
+        $check = $this->getServiceLocator()->get('VuFind\AccountCapabilities');
+        if ($check->getSavedSearchSetting() === 'disabled') {
+            throw new \Exception('Saved searches disabled.');
+        }
+
         $user = $this->getUser();
         if ($user == false) {
             return $this->forceLogin();
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/AccountCapabilities.php b/module/VuFind/src/VuFind/View/Helper/Root/AccountCapabilities.php
new file mode 100644
index 00000000000..8e86605c316
--- /dev/null
+++ b/module/VuFind/src/VuFind/View/Helper/Root/AccountCapabilities.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * AccountCapabilities view helper
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2015.
+ *
+ * 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  View_Helpers
+ * @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:developer_manual Wiki
+ */
+namespace VuFind\View\Helper\Root;
+use VuFind\Config\AccountCapabilities as Helper;
+
+/**
+ * AccountCapabilities view helper
+ *
+ * @category VuFind2
+ * @package  View_Helpers
+ * @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:developer_manual Wiki
+ */
+class AccountCapabilities extends \Zend\View\Helper\AbstractHelper
+{
+    /**
+     * Capabilities helper
+     *
+     * @var Helper
+     */
+    protected $helper;
+
+    /**
+     * Constructor
+     *
+     * @param Helper $connection Capabilities helper
+     */
+    public function __construct(Helper $helper)
+    {
+        $this->helper = $helper;
+    }
+
+    /**
+     * Get the capabilities helper.
+     *
+     * @return Helper
+     */
+    public function __invoke()
+    {
+        return $this->helper;
+    }
+}
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
index b59993d75a5..12769c3fe12 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
@@ -56,6 +56,20 @@ class Factory
         );
     }
 
+    /**
+     * Construct the AccountCapabilities helper.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return AccountCapabilities
+     */
+    public static function getAccountCapabilities(ServiceManager $sm)
+    {
+        return new AccountCapabilities(
+            $sm->getServiceLocator()->get('VuFind\AccountCapabilities')
+        );
+    }
+
     /**
      * Construct the AlphaBrowse helper.
      *
diff --git a/themes/bootstrap3/templates/myresearch/menu.phtml b/themes/bootstrap3/templates/myresearch/menu.phtml
index d56da0d5d19..14ebab3eb0f 100644
--- a/themes/bootstrap3/templates/myresearch/menu.phtml
+++ b/themes/bootstrap3/templates/myresearch/menu.phtml
@@ -50,10 +50,12 @@
       </a>
     <? endif; ?>
   <? endif; ?>
-  <a href="<?=$this->url('search-history')?>?require_login" class="list-group-item<?=$this->active == 'history' ? ' active' : ''?>">
-    <?=$this->transEsc('history_saved_searches')?>
-    <span class="pull-right flip"><i class="fa fa-fw fa-search"></i></span>
-  </a>
+  <? if ($this->accountCapabilities()->getSavedSearchSetting() === 'enabled'): ?>
+    <a href="<?=$this->url('search-history')?>?require_login" class="list-group-item<?=$this->active == 'history' ? ' active' : ''?>">
+      <?=$this->transEsc('history_saved_searches')?>
+      <span class="pull-right flip"><i class="fa fa-fw fa-search"></i></span>
+    </a>
+  <? endif; ?>
   <? if ($user = $this->auth()->isLoggedIn()): ?>
     <a href="<?=$this->url('myresearch-logout')?>" class="list-group-item">
       <?=$this->transEsc("Log Out")?>
diff --git a/themes/bootstrap3/templates/search/history-table.phtml b/themes/bootstrap3/templates/search/history-table.phtml
index 9a3fa5f1836..9daf184a3e2 100644
--- a/themes/bootstrap3/templates/search/history-table.phtml
+++ b/themes/bootstrap3/templates/search/history-table.phtml
@@ -1,10 +1,11 @@
+<? $saveSupported = $this->accountCapabilities()->getSavedSearchSetting() === 'enabled'; ?>
 <table class="table table-striped">
   <tr>
     <th width="20%"><?=$this->transEsc("history_time")?></th>
     <th><?=$this->transEsc("history_search")?></th>
     <th><?=$this->transEsc("history_limits")?></th>
     <th><?=$this->transEsc("history_results")?></th>
-    <th><?=$this->transEsc($this->showSaved ? "history_delete" : "history_save")?></th>
+    <? if ($saveSupported): ?><th><?=$this->transEsc($this->showSaved ? "history_delete" : "history_save")?></th><? endif; ?>
   </tr>
   <? foreach (($this->showSaved ? array_reverse($this->saved) : array_reverse($this->unsaved)) as $iteration => $info): ?>
     <tr class="<?=$iteration % 2 == 1 ? 'even' : 'odd'?>row">
@@ -25,13 +26,15 @@
         <? endforeach; ?>
       </td>
       <td><?=$this->escapeHtml($this->localizedNumber($info->getResultTotal()))?></td>
-      <td>
-        <? if ($this->showSaved): ?>
-          <a href="<?=$this->url('myresearch-savesearch')?>?delete=<?=urlencode($info->getSearchId())?>&amp;mode=history"><i class="fa fa-remove"></i> <?=$this->transEsc("history_delete_link")?></a>
-        <? else: ?>
-          <a href="<?=$this->url('myresearch-savesearch')?>?save=<?=urlencode($info->getSearchId())?>&amp;mode=history"><i class="fa fa-save"></i> <?=$this->transEsc("history_save_link")?></a>
-        <? endif; ?>
-      </td>
+      <? if ($saveSupported): ?>
+        <td>
+          <? if ($this->showSaved): ?>
+            <a href="<?=$this->url('myresearch-savesearch')?>?delete=<?=urlencode($info->getSearchId())?>&amp;mode=history"><i class="fa fa-remove"></i> <?=$this->transEsc("history_delete_link")?></a>
+          <? else: ?>
+            <a href="<?=$this->url('myresearch-savesearch')?>?save=<?=urlencode($info->getSearchId())?>&amp;mode=history"><i class="fa fa-save"></i> <?=$this->transEsc("history_save_link")?></a>
+          <? endif; ?>
+        </td>
+      <? endif; ?>
     </tr>
   <? endforeach; ?>
 </table>
diff --git a/themes/bootstrap3/templates/search/history.phtml b/themes/bootstrap3/templates/search/history.phtml
index efec1177841..39f4e36a209 100644
--- a/themes/bootstrap3/templates/search/history.phtml
+++ b/themes/bootstrap3/templates/search/history.phtml
@@ -5,11 +5,13 @@
   // Set up breadcrumbs:
   $this->layout()->breadcrumbs = '<li><a href="' .  $this->url('myresearch-home') . '">' . $this->transEsc('Your Account') . '</a></li>'
     . '<li class="active">' . $this->transEsc('History') . '</li>';
+
+  $saveSupported = $this->accountCapabilities()->getSavedSearchSetting() === 'enabled';
 ?>
 
 <div class="row">
   <div class="<?=$this->layoutClass('mainbody')?>">
-    <? if (!empty($this->saved)): ?>
+    <? if ($saveSupported && !empty($this->saved)): ?>
       <h2><?=$this->transEsc("history_saved_searches")?></h2>
       <?=$this->context()->renderInContext('search/history-table.phtml', array('showSaved' => true));?>
     <? endif; ?>
@@ -23,12 +25,14 @@
     <? endif; ?>
   </div>
 
-  <div class="<?=$this->layoutClass('sidebar')?>">
-    <?=$this->context($this)->renderInContext(
-        "myresearch/menu.phtml",
-        // Only activate search history in account menu if user is logged in.
-        $this->auth()->isLoggedIn() ? array('active' => 'history') : array()
-      );
-    ?>
-  </div>
+  <? if ($saveSupported): ?>
+    <div class="<?=$this->layoutClass('sidebar')?>">
+      <?=$this->context($this)->renderInContext(
+          "myresearch/menu.phtml",
+          // Only activate search history in account menu if user is logged in.
+          $this->auth()->isLoggedIn() ? array('active' => 'history') : array()
+       );
+       ?>
+    </div>
+  <? endif; ?>
 </div>
\ No newline at end of file
diff --git a/themes/bootstrap3/templates/search/results.phtml b/themes/bootstrap3/templates/search/results.phtml
index 1ed0fdced7d..97930e8618d 100644
--- a/themes/bootstrap3/templates/search/results.phtml
+++ b/themes/bootstrap3/templates/search/results.phtml
@@ -111,7 +111,7 @@
         <a href="<?=$this->results->getUrlQuery()->setViewParam('rss')?>"><i class="fa fa-bell"></i> <?=$this->transEsc('Get RSS Feed')?></a>
         &mdash;
         <a href="<?=$this->url('search-email')?>" class="mailSearch modal-link" id="mailSearch<?=$this->escapeHtmlAttr($this->results->getSearchId())?>" title="<?=$this->transEsc('Email this Search')?>"><i class="fa fa-envelope"></i> <?=$this->transEsc('Email this Search')?></a>
-        <? if (($account = $this->auth()->getManager()) && $account->loginEnabled()): // hide save option if login disabled ?>
+        <? if ($this->accountCapabilities()->getSavedSearchSetting() === 'enabled'): ?>
           &mdash;
           <? if (is_numeric($this->results->getSearchId())): ?>
             <? if ($this->results->isSavedSearch()): ?>
diff --git a/themes/jquerymobile/templates/search/header-navbar.phtml b/themes/jquerymobile/templates/search/header-navbar.phtml
index eb7daaad86c..6bb71b162e2 100644
--- a/themes/jquerymobile/templates/search/header-navbar.phtml
+++ b/themes/jquerymobile/templates/search/header-navbar.phtml
@@ -4,7 +4,7 @@
   <div data-role="navbar">
     <ul>
       <li><a href="#Search-narrow" data-rel="dialog" data-transition="flip"><?=$this->transEsc('Narrow Search')?></a></li>
-      <? if (($account = $this->auth()->getManager()) && $account->loginEnabled()): // hide save option if login disabled ?>
+      <? if ($this->accountCapabilities()->getSavedSearchSetting() === 'enabled'): ?>
         <li>
           <? if (isset($this->results) && is_numeric($this->results->getSearchId())): ?>
             <? if ($this->results->isSavedSearch()): ?>
diff --git a/themes/jquerymobile/templates/search/history-table.phtml b/themes/jquerymobile/templates/search/history-table.phtml
index 24b1f392560..c43cabf5643 100644
--- a/themes/jquerymobile/templates/search/history-table.phtml
+++ b/themes/jquerymobile/templates/search/history-table.phtml
@@ -1,3 +1,4 @@
+<? $saveSupported = $this->accountCapabilities()->getSavedSearchSetting() === 'enabled'; ?>
 <? foreach (($this->showSaved ? array_reverse($this->saved) : array_reverse($this->unsaved)) as $iteration => $info): ?>
   <li>
     <a rel="external" href="<?=$this->url($info->getOptions()->getSearchAction()) . $info->getUrlQuery()->getParams()?>">
@@ -16,10 +17,12 @@
     <? endforeach; ?>
     </div>
     </a>
-    <? if ($this->showSaved): ?>
-      <a rel="external" href="<?=$this->url('myresearch-savesearch')?>?delete=<?=urlencode($info->getSearchId())?>&amp;mode=history" class="delete"><?=$this->transEsc("history_delete_link")?></a>
-    <? else: ?>
-      <a rel="external" href="<?=$this->url('myresearch-savesearch')?>?save=<?=urlencode($info->getSearchId())?>&amp;mode=history" class="add"><?=$this->transEsc("history_save_link")?></a>
+    <? if ($saveSupported): ?>
+      <? if ($this->showSaved): ?>
+        <a rel="external" href="<?=$this->url('myresearch-savesearch')?>?delete=<?=urlencode($info->getSearchId())?>&amp;mode=history" class="delete"><?=$this->transEsc("history_delete_link")?></a>
+      <? else: ?>
+        <a rel="external" href="<?=$this->url('myresearch-savesearch')?>?save=<?=urlencode($info->getSearchId())?>&amp;mode=history" class="add"><?=$this->transEsc("history_save_link")?></a>
+      <? endif; ?>
     <? endif; ?>
   </li>
 <? endforeach; ?>
\ No newline at end of file
diff --git a/themes/jquerymobile/templates/search/history.phtml b/themes/jquerymobile/templates/search/history.phtml
index e9e9ea37a3f..66d3f206bbb 100644
--- a/themes/jquerymobile/templates/search/history.phtml
+++ b/themes/jquerymobile/templates/search/history.phtml
@@ -1,6 +1,11 @@
 <?
     // Set page title.
     $this->headTitle($this->translate('Search History'));
+
+    $saveSupported = $this->accountCapabilities()->getSavedSearchSetting() === 'enabled';
+    if (!$saveSupported) {
+        $this->saved = [];
+    }
 ?>
 <div data-role="page" id="Search-history">
   <?=$this->mobileMenu()->header()?>
diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php
index 16488fec1f1..41041b6f7f4 100644
--- a/themes/root/theme.config.php
+++ b/themes/root/theme.config.php
@@ -3,6 +3,7 @@ return array(
     'extends' => false,
     'helpers' => array(
         'factories' => array(
+            'accountcapabilities' => 'VuFind\View\Helper\Root\Factory::getAccountCapabilities',
             'addthis' => 'VuFind\View\Helper\Root\Factory::getAddThis',
             'alphabrowse' => 'VuFind\View\Helper\Root\Factory::getAlphaBrowse',
             'auth' => 'VuFind\View\Helper\Root\Factory::getAuth',
-- 
GitLab