From d7eda342b492dc5341731fc10201fc4f64874edc Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 18 Jul 2012 12:36:02 -0400
Subject: [PATCH] Implemented AlphaBrowse module; standardized related routes
 to separate legacy compatibility from current requirements.

---
 module/VuFind/config/module.config.php        |  14 +-
 .../Controller/AlphabrowseController.php      | 126 ++++++++++++++++++
 .../templates/alphabrowse/home.phtml          |   4 +-
 .../templates/alphabrowse/home.phtml          |   2 +-
 4 files changed, 141 insertions(+), 5 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Controller/AlphabrowseController.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 6d7b7b08183..409ead97a24 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -18,6 +18,16 @@ $config = array(
                     ),
                 ),
             ),
+            'legacy-alphabrowse-results' => array(
+                'type' => 'Zend\Mvc\Router\Http\Literal',
+                'options' => array(
+                    'route'    => '/AlphaBrowse/Results',
+                    'defaults' => array(
+                        'controller' => 'Alphabrowse',
+                        'action'     => 'Home',
+                    )
+                )
+            ),
             'legacy-bookcover' => array(
                 'type' => 'Zend\Mvc\Router\Http\Literal',
                 'options' => array(
@@ -54,6 +64,7 @@ $config = array(
         'invokables' => array(
             'admin' => 'VuFind\Controller\AdminController',
             'ajax' => 'VuFind\Controller\AjaxController',
+            'alphabrowse' => 'VuFind\Controller\AlphabrowseController',
             'cover' => 'VuFind\Controller\CoverController',
             'error' => 'VuFind\Controller\ErrorController',
             'index' => 'VuFind\Controller\IndexController',
@@ -95,8 +106,7 @@ $listRoutes = array('userList' => 'MyList', 'editList' => 'EditList');
 // Define static routes -- Controller/Action strings
 $staticRoutes = array(
     'Admin/Config', 'Admin/DeleteExpiredSearches', 'Admin/EnableAutoConfig',
-    'Admin/Home', 'Admin/Maintenance', 'Admin/Statistics', 'AlphaBrowse/Home',
-    'AlphaBrowse/Results',
+    'Admin/Home', 'Admin/Maintenance', 'Admin/Statistics', 'Alphabrowse/Home',
     'Author/Home', 'Author/Search',
     'Authority/Home', 'Authority/Record', 'Authority/Search',
     'Browse/Author', 'Browse/Dewey', 'Browse/Era', 'Browse/Genre', 'Browse/Home',
diff --git a/module/VuFind/src/VuFind/Controller/AlphabrowseController.php b/module/VuFind/src/VuFind/Controller/AlphabrowseController.php
new file mode 100644
index 00000000000..0c91ade93bc
--- /dev/null
+++ b/module/VuFind/src/VuFind/Controller/AlphabrowseController.php
@@ -0,0 +1,126 @@
+<?php
+/**
+ * AlphaBrowse Module Controller
+ *
+ * PHP Version 5
+ *
+ * Copyright (C) Villanova University 2011.
+ *
+ * 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  Controller
+ * @author   Mark Triggs <vufind-tech@lists.sourceforge.net>
+ * @author   Chris Hallberg <challber@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/alphabetical_heading_browse Wiki
+ */
+namespace VuFind\Controller;
+use VuFind\Config\Reader as ConfigReader,
+    VuFind\Connection\Manager as ConnectionManager,
+    VuFind\Exception\Solr as SolrException;
+
+/**
+ * AlphabrowseController Class
+ *
+ * Controls the alphabetical browsing feature
+ *
+ * @category VuFind2
+ * @package  Controller
+ * @author   Mark Triggs <vufind-tech@lists.sourceforge.net>
+ * @author   Chris Hallberg <challber@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/alphabetical_heading_browse Wiki
+ */
+class AlphabrowseController extends AbstractBase
+{
+    /**
+     * Gathers data for the view of the AlphaBrowser and does some initialization
+     *
+     * @return \Zend\View\Model\ViewModel
+     */
+    public function homeAction()
+    {
+        $config = ConfigReader::getConfig();
+
+        // Load browse types from config file, or use defaults if unavailable:
+        if (isset($config->AlphaBrowse_Types)
+            && !empty($config->AlphaBrowse_Types)
+        ) {
+            $types = array();
+            foreach ($config->AlphaBrowse_Types as $key => $value) {
+                $types[$key] = $value;
+            }
+        } else {
+            $types = array(
+                'topic'  => 'By Topic',
+                'author' => 'By Author',
+                'title'  => 'By Title',
+                'lcc'    => 'By Call Number'
+            );
+        }
+
+        // Connect to Solr:
+        $db = ConnectionManager::connectToIndex();
+
+        // Process incoming parameters:
+        $source = $this->params()->fromQuery('source', false);
+        $from   = $this->params()->fromQuery('from', false);
+        $page   = intval($this->params()->fromQuery('page', 0));
+        $limit  = isset($config->AlphaBrowse->page_size)
+            ? $config->AlphaBrowse->page_size : 20;
+
+
+        // Create view model:
+        $view = $this->createViewModel();
+
+        // If required parameters are present, load results:
+        if ($source && $from !== false) {
+            // Load Solr data or die trying:
+            try {
+                $result = $db->alphabeticBrowse($source, $from, $page, $limit);
+
+                // No results?    Try the previous page just in case we've gone past
+                // the end of the list....
+                if ($result['Browse']['totalCount'] == 0) {
+                    $page--;
+                    $result = $db->alphabeticBrowse($source, $from, $page, $limit);
+                }
+            } catch (SolrException $e) {
+                if ($e->isMissingBrowseIndex()) {
+                    throw new \Exception(
+                        "Alphabetic Browse index missing.    See " .
+                        "http://vufind.org/wiki/alphabetical_heading_browse for " .
+                        "details on generating the index."
+                    );
+                }
+                throw $e;
+            }
+
+            // Only display next/previous page links when applicable:
+            if ($result['Browse']['totalCount'] > $limit) {
+                $view->nextpage = $page + 1;
+            }
+            if ($result['Browse']['offset'] + $result['Browse']['startRow'] > 1) {
+                $view->prevpage = $page - 1;
+            }
+            $view->result = $result;
+        }
+
+        $view->alphaBrowseTypes = $types;
+        $view->from = $from;
+        $view->source = $source;
+        return $view;
+    }
+}
\ No newline at end of file
diff --git a/themes/blueprint/templates/alphabrowse/home.phtml b/themes/blueprint/templates/alphabrowse/home.phtml
index b8a615e6221..0bcc73be6d7 100644
--- a/themes/blueprint/templates/alphabrowse/home.phtml
+++ b/themes/blueprint/templates/alphabrowse/home.phtml
@@ -66,7 +66,7 @@
           <div class="title"><?=$this->transEsc('Use instead') ?>:</div>
           <ul>
             <? foreach ($item['useInstead'] as $heading): ?>
-            <li><a href="<?=$this->url('alphabrowse-results')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode($heading)?>"><?=$this->escapeHtml($heading)?></a></li>
+            <li><a href="<?=$this->url('alphabrowse-home')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode($heading)?>"><?=$this->escapeHtml($heading)?></a></li>
             <? endforeach; ?>
           </ul>
           </div>
@@ -77,7 +77,7 @@
           <div class="title"><?=$this->transEsc('See also') ?>:</div>
           <ul>
             <? foreach ($item['seeAlso'] as $heading): ?>
-            <li><a href="<?=$this->url('alphabrowse-results')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode($heading)?>"><?=$this->escapeHtml($heading)?></a></li>
+            <li><a href="<?=$this->url('alphabrowse-home')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode($heading)?>"><?=$this->escapeHtml($heading)?></a></li>
             <? endforeach; ?>
           </ul>
           </div>
diff --git a/themes/jquerymobile/templates/alphabrowse/home.phtml b/themes/jquerymobile/templates/alphabrowse/home.phtml
index e7acba7daff..8df60e603b3 100644
--- a/themes/jquerymobile/templates/alphabrowse/home.phtml
+++ b/themes/jquerymobile/templates/alphabrowse/home.phtml
@@ -51,7 +51,7 @@
           <? if ($item['count'] > 0 || count($item['useInstead']) > 0): ?>
           <? ob_start(); ?>
             <? if (count($item['useInstead']) > 0): ?>
-              <?=$this->url('alphabrowse-results')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode(implode($item['useInstead']))?>
+              <?=$this->url('alphabrowse-home')?>?source=<?=urlencode($this->source)?>&amp;from=<?=urlencode(implode($item['useInstead']))?>
             <? else: ?>
               <? if ($item['count'] < 5): ?>
                 <?=$this->url('search-results')?>?type=ids&amp;lookfor=<? foreach ($item['ids'] as $id): ?><?=$id ?>+<? endforeach; ?>
-- 
GitLab