From 323457ab65d78c3965f90ed559a1078fc7662373 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 28 Jun 2012 12:08:52 -0400
Subject: [PATCH] Override routing to send user to error page when system is
 disabled.

---
 module/VuFind/config/module.config.php        |  3 +-
 module/VuFind/src/VuFind/Bootstrap.php        | 24 ++++++++-
 .../src/VuFind/Controller/ErrorController.php | 52 +++++++++++++++++++
 3 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Controller/ErrorController.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 645d928a8d3..aa74b05e731 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -22,6 +22,7 @@ $config = array(
     ),
     'controller' => array(
         'classes' => array(
+            'error' => 'VuFind\Controller\ErrorController',
             'index' => 'VuFind\Controller\IndexController',
             'search' => 'VuFind\Controller\SearchController'
         ),
@@ -56,7 +57,7 @@ $staticRoutes = array(
     'Browse/Author', 'Browse/Dewey', 'Browse/Era', 'Browse/Genre', 'Browse/Home',
     'Browse/LCC', 'Browse/Region', 'Browse/Tag', 'Browse/Topic',
     'Cart/Email', 'Cart/Export', 'Cart/Home', 'Cart/MyResearchBulk', 'Cart/Save',
-    'Cover/Unavailable', 'Help/Home',
+    'Cover/Unavailable', 'Error/Unavailable', 'Help/Home',
     'Install/Done', 'Install/FixBasicConfig', 'Install/FixCache',
     'Install/FixDatabase', 'Install/FixDependencies', 'Install/FixILS',
     'Install/FixSolr', 'Install/Home',
diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php
index 9c676460e88..973e151efce 100644
--- a/module/VuFind/src/VuFind/Bootstrap.php
+++ b/module/VuFind/src/VuFind/Bootstrap.php
@@ -29,7 +29,7 @@ namespace VuFind;
 use VuFind\Account\Manager as AccountManager,
     VuFind\Config\Reader as ConfigReader,
     VuFind\Theme\Initializer as ThemeInitializer,
-    Zend\Mvc\MvcEvent;
+    Zend\Mvc\MvcEvent, Zend\Mvc\Router\Http\RouteMatch;
 /**
  * VuFind Bootstrapper
  *
@@ -73,6 +73,28 @@ class Bootstrap
         }
     }
 
+    /**
+     * If the system is offline, set up a handler to override the routing output.
+     *
+     * @return void
+     */
+    protected function initSystemStatus()
+    {
+        // If the system is unavailable, forward to a different place:
+        if (isset($this->config->System->available)
+            && !$this->config->System->available
+        ) {
+            $callback = function($e) {
+                $routeMatch = new RouteMatch(
+                    array('controller' => 'Error', 'action' => 'Unavailable'), 1
+                );
+                $routeMatch->setMatchedRouteName('error-unavailable');
+                $e->setRouteMatch($routeMatch);
+            };
+            $this->events->attach('route', $callback);
+        }
+    }
+
     /**
      * Initializes locale and timezone values
      *
diff --git a/module/VuFind/src/VuFind/Controller/ErrorController.php b/module/VuFind/src/VuFind/Controller/ErrorController.php
new file mode 100644
index 00000000000..f96cbb8b5ad
--- /dev/null
+++ b/module/VuFind/src/VuFind/Controller/ErrorController.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Error Controller
+ *
+ * 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  Controller
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+namespace VuFind\Controller;
+
+use Zend\Mvc\Controller\ActionController;
+
+/**
+ * Error Controller
+ *
+ * @category VuFind2
+ * @package  Controller
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+class ErrorController extends ActionController
+{
+    /**
+     * Display unavailable message.
+     *
+     * @return void
+     */
+    public function unavailableAction()
+    {
+        // no special action necessary
+    }
+}
-- 
GitLab