From 67d3f0e91469f511869447f68651ecb53093b993 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 30 Aug 2017 13:51:14 -0400
Subject: [PATCH] Add support for AlphaBrowse options in main search box.
 (#1019)

---
 config/vufind/config.ini                      |  2 +
 config/vufind/searchbox.ini                   |  5 ++
 .../src/VuFind/View/Helper/Root/Factory.php   | 10 +++-
 .../src/VuFind/View/Helper/Root/SearchBox.php | 49 +++++++++++++++++--
 .../templates/alphabrowse/home.phtml          | 37 +++++++++-----
 5 files changed, 86 insertions(+), 17 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 004ddd76093..0b32a20878b 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -1409,6 +1409,8 @@ page_size = 20
 rows_before = 0
 ; highlight the match row (or spot where match would have been)? default false
 highlighting = false
+; SEE ALSO: the General/includeAlphaBrowse setting in searchbox.ini, for including
+; alphabrowse options in the main search drop-down options.
 
 ; This section controls the order and content of the browse type menu in the
 ; Alphabetic Browse module.  The key is the browse index to use, the value is the
diff --git a/config/vufind/searchbox.ini b/config/vufind/searchbox.ini
index d6f8e48ee8e..43afb28473b 100644
--- a/config/vufind/searchbox.ini
+++ b/config/vufind/searchbox.ini
@@ -5,6 +5,11 @@
 ; in the [CombinedHandlers] section below.
 combinedHandlers = false
 
+; Should we include AlphaBrowse options below the configured combined search options?
+; (This setting is ignored unless combinedHandlers is set to true). See also the
+; [AlphaBrowse] section of config.ini.
+includeAlphaBrowse = false
+
 ; This section controls the "combined handlers" drop-down. It must contain groups
 ; of settings with the following keys:
 ;
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
index 75f036f557c..682a83d2e70 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
@@ -452,11 +452,17 @@ class Factory
     {
         $config = $sm->getServiceLocator()->get('VuFind\Config');
         $mainConfig = $config->get('config');
+        $searchboxConfig = $config->get('searchbox')->toArray();
+        $includeAlphaOptions
+            = isset($searchboxConfig['General']['includeAlphaBrowse'])
+            && $searchboxConfig['General']['includeAlphaBrowse'];
         return new SearchBox(
             $sm->getServiceLocator()->get('VuFind\SearchOptionsPluginManager'),
-            $config->get('searchbox')->toArray(),
+            $searchboxConfig,
             isset($mainConfig->SearchPlaceholder)
-                ? $mainConfig->SearchPlaceholder->toArray() : []
+                ? $mainConfig->SearchPlaceholder->toArray() : [],
+            $includeAlphaOptions && isset($mainConfig->AlphaBrowse_Types)
+                ? $mainConfig->AlphaBrowse_Types->toArray() : []
         );
     }
 
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php b/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
index ad912bf51d4..49b4b20a592 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
@@ -46,6 +46,13 @@ class SearchBox extends \Zend\View\Helper\AbstractHelper
      */
     protected $config;
 
+    /**
+     * Alphabrowse settings for search box.
+     *
+     * @var array
+     */
+    protected $alphabrowseConfig;
+
     /**
      * Placeholders from config.ini
      *
@@ -70,15 +77,19 @@ class SearchBox extends \Zend\View\Helper\AbstractHelper
     /**
      * Constructor
      *
-     * @param OptionsManager $optionsManager Search options plugin manager
-     * @param array          $config         Configuration for search box
-     * @param array          $placeholders   Array of placeholders keyed by backend
+     * @param OptionsManager $optionsManager    Search options plugin manager
+     * @param array          $config            Configuration for search box
+     * @param array          $placeholders      Array of placeholders keyed by
+     * backend
+     * @param array          $alphabrowseConfig source => label config for
+     * alphabrowse options to display in combined box (empty for none)
      */
     public function __construct(OptionsManager $optionsManager, $config = [],
-        $placeholders = []
+        $placeholders = [], $alphabrowseConfig = []
     ) {
         $this->optionsManager = $optionsManager;
         $this->config = $config;
+        $this->alphabrowseConfig = $alphabrowseConfig;
         $this->placeholders = $placeholders;
     }
 
@@ -114,6 +125,18 @@ class SearchBox extends \Zend\View\Helper\AbstractHelper
         return false;
     }
 
+    /**
+     * Are alphabrowse options configured to display in the search options
+     * drop-down?
+     *
+     * @return bool
+     */
+    public function alphaBrowseOptionsEnabled()
+    {
+        // Alphabrowse options depend on combined handlers:
+        return $this->combinedHandlersActive() && !empty($this->alphabrowseConfig);
+    }
+
     /**
      * Are combined handlers enabled?
      *
@@ -314,6 +337,24 @@ class SearchBox extends \Zend\View\Helper\AbstractHelper
             }
         }
 
+        // Should we add alphabrowse links?
+        if ($this->alphaBrowseOptionsEnabled()) {
+            $alphaBrowseBase = $this->getView()->plugin('url')
+                ->__invoke('alphabrowse-home');
+            $labelPrefix = $this->getView()->translate('Browse Alphabetically')
+                . ': ';
+            foreach ($this->alphabrowseConfig as $source => $label) {
+                $alphaBrowseUrl = $alphaBrowseBase . '?source=' . urlencode($source)
+                    . '&from=';
+                $handlers[] = [
+                    'value' => 'External:' . $alphaBrowseUrl,
+                    'label' => $labelPrefix . $this->getView()->translate($label),
+                    'indent' => false,
+                    'selected' => $activeHandler == 'AlphaBrowse:' . $source
+                ];
+            }
+        }
+
         // If we didn't find an exact match for a selected index, use a fuzzy
         // match:
         if (!$selectedFound && $backupSelectedIndex !== false) {
diff --git a/themes/bootstrap3/templates/alphabrowse/home.phtml b/themes/bootstrap3/templates/alphabrowse/home.phtml
index f8359720822..45190f6d2c4 100644
--- a/themes/bootstrap3/templates/alphabrowse/home.phtml
+++ b/themes/bootstrap3/templates/alphabrowse/home.phtml
@@ -2,6 +2,17 @@
   $this->headTitle($this->translate('Browse the Collection Alphabetically'));
   $this->layout()->breadcrumbs = '<a href="' . $this->url('alphabrowse-home') . '">' . $this->transEsc('Browse Alphabetically') . '</a>';
   $baseQuery = ['source' => $this->source, 'from' => $this->from];
+
+  // Set up upper search box if necessary:
+  if ($this->searchbox()->alphaBrowseOptionsEnabled()) {
+    $this->layout()->searchbox = $this->context($this)->renderInContext(
+        'search/searchbox.phtml',
+        [
+          'lookfor' => $this->from,
+          'searchIndex' => 'AlphaBrowse:' . $this->source,
+        ]
+    );
+  }
 ?>
 
 <? /* LOAD THE LINK INFORMATION INTO $pageLinks, similar to smarty's {capture} */ ?>
@@ -22,17 +33,21 @@
 <? $pageLinks = ob_get_contents(); ?>
 <? ob_end_clean(); ?>
 
-<form class="form-inline" method="get" action="<?=$this->url('alphabrowse-home')?>" name="alphaBrowseForm" id="alphaBrowseForm">
-  <label for="alphaBrowseForm_source"><?=$this->transEsc('Browse Alphabetically') ?></label>
-  <select id="alphaBrowseForm_source" name="source" class="form-control">
-    <? foreach ($this->alphaBrowseTypes as $key => $item): ?>
-      <option value="<?=$this->escapeHtmlAttr($key) ?>"<? if ($this->source == $key): ?> selected="selected"<? endif; ?>><?=$this->transEsc($item) ?></option>
-    <? endforeach; ?>
-  </select>
-  <label for="alphaBrowseForm_from"><?=$this->transEsc('starting from') ?></label>
-  <input type="text" name="from" id="alphaBrowseForm_from" value="<?=$this->escapeHtmlAttr($this->from) ?>" class="form-control"/>
-  <input class="btn btn-primary" type="submit" value="<?=$this->transEsc('Browse') ?>"/>
-</form>
+<? /* If the top search box is not configured to show alphabrowse, or if no option
+      is selected yet, set up a separate form: */ ?>
+<? if (!$this->searchbox()->alphaBrowseOptionsEnabled() || empty($this->source)): ?>
+  <form class="form-inline" method="get" action="<?=$this->url('alphabrowse-home')?>" name="alphaBrowseForm" id="alphaBrowseForm">
+    <label for="alphaBrowseForm_source"><?=$this->transEsc('Browse Alphabetically') ?></label>
+    <select id="alphaBrowseForm_source" name="source" class="form-control">
+      <? foreach ($this->alphaBrowseTypes as $key => $item): ?>
+        <option value="<?=$this->escapeHtmlAttr($key) ?>"<? if ($this->source == $key): ?> selected="selected"<? endif; ?>><?=$this->transEsc($item) ?></option>
+      <? endforeach; ?>
+    </select>
+    <label for="alphaBrowseForm_from"><?=$this->transEsc('starting from') ?></label>
+    <input type="text" name="from" id="alphaBrowseForm_from" value="<?=$this->escapeHtmlAttr($this->from) ?>" class="form-control"/>
+    <input class="btn btn-primary" type="submit" value="<?=$this->transEsc('Browse') ?>"/>
+  </form>
+<? endif; ?>
 
 <? if ($this->result): ?>
   <?=$pageLinks ?>
-- 
GitLab