From 99f4dd0a0e8836866feebda36ad193dc5304a78a Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 25 Jun 2012 15:37:20 -0400
Subject: [PATCH] Progress on jQueryMobile theme's MobileMenu helper and its
 dependencies (but not fully tested/functional yet).

---
 .../src/VuFind/Theme/Root/Helper/Context.php  | 26 +++++----
 .../Theme/jQueryMobile/Helper/MobileMenu.php  | 55 ++++---------------
 .../jquerymobile/templates/footer.phtml       |  2 +-
 .../jquerymobile/templates/header.phtml       |  2 +-
 themes/vufind/jquerymobile/theme.ini          |  3 +-
 themes/vufind/root/theme.ini                  |  1 +
 6 files changed, 32 insertions(+), 57 deletions(-)

diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/Context.php b/module/VuFind/src/VuFind/Theme/Root/Helper/Context.php
index 8b7ac08f96a..9da5e0dc025 100644
--- a/module/VuFind/src/VuFind/Theme/Root/Helper/Context.php
+++ b/module/VuFind/src/VuFind/Theme/Root/Helper/Context.php
@@ -27,6 +27,8 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
+namespace VuFind\Theme\Root\Helper;
+use Zend\View\Helper\AbstractHelper, Zend\View\Renderer\RendererInterface;
 
 /**
  * Context manager (useful for using render() instead of partial() for better
@@ -39,7 +41,7 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class VuFind_Theme_Root_Helper_Context extends Zend_View_Helper_Abstract
+class Context extends AbstractHelper
 {
     /**
      * Set an array of variables in the view; return the previous values of those
@@ -51,10 +53,12 @@ class VuFind_Theme_Root_Helper_Context extends Zend_View_Helper_Abstract
      */
     public function apply($vars)
     {
+        $view = $this->getView();
+
         $oldVars = array();
         foreach ($vars as $k => $v) {
-            $oldVars[$k] = isset($this->view->$k) ? $this->view->$k : null;
-            $this->view->$k = $v;
+            $oldVars[$k] = isset($view->$k) ? $view->$k : null;
+            $view->$k = $v;
         }
         return $oldVars;
     }
@@ -68,11 +72,13 @@ class VuFind_Theme_Root_Helper_Context extends Zend_View_Helper_Abstract
      */
     public function restore($vars)
     {
+        $view = $this->getView();
+
         foreach ($vars as $k => $v) {
             if (is_null($v)) {
-                unset($this->view->$k);
+                unset($view->$k);
             } else {
-                $this->view->$k = $v;
+                $view->$k = $v;
             }
         }
     }
@@ -91,7 +97,7 @@ class VuFind_Theme_Root_Helper_Context extends Zend_View_Helper_Abstract
     public function renderInContext($template, $context)
     {
         $oldContext = $this->apply($context);
-        $html = $this->view->render($template);
+        $html = $this->getView()->render($template);
         $this->restore($oldContext);
         return $html;
     }
@@ -99,14 +105,14 @@ class VuFind_Theme_Root_Helper_Context extends Zend_View_Helper_Abstract
     /**
      * Grab the helper object so we can call methods on it.
      *
-     * @param Zend_View_Abstract $view View object to modify.
+     * @param Renderer $view View object to modify.
      *
-     * @return VuFind_Theme_Root_Helper_Context
+     * @return Context
      */
-    public function context($view = null)
+    public function __invoke(RendererInterface $view = null)
     {
         if (!is_null($view)) {
-            $this->view = $view;
+            $this->setView($view);
         }
         return $this;
     }
diff --git a/module/VuFind/src/VuFind/Theme/jQueryMobile/Helper/MobileMenu.php b/module/VuFind/src/VuFind/Theme/jQueryMobile/Helper/MobileMenu.php
index 6c82ced581f..20db406753b 100644
--- a/module/VuFind/src/VuFind/Theme/jQueryMobile/Helper/MobileMenu.php
+++ b/module/VuFind/src/VuFind/Theme/jQueryMobile/Helper/MobileMenu.php
@@ -25,6 +25,8 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
+namespace VuFind\Theme\jQueryMobile\Helper;
+use Zend\View\Helper\AbstractHelper;
 
 /**
  * MobileMenu view helper
@@ -35,36 +37,8 @@
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class VuFind_Theme_Root_Helper_MobileMenu extends Zend_View_Helper_Abstract
+class MobileMenu extends AbstractHelper
 {
-    protected $controller;
-    protected $action;
-
-    /**
-     * Constructor
-     */
-    public function __construct()
-    {
-        $request = Zend_Controller_Front::getInstance()->getRequest();
-        // TODO: is there a better way to make controller/action names consistent?
-        $this->controller = preg_replace(
-            '/[^\w]/', '', strtolower($request->getControllerName())
-        );
-        $this->action = preg_replace(
-            '/[^\w]/', '', strtolower($request->getActionName())
-        );
-    }
-
-    /**
-     * Get access to the helper object.
-     *
-     * @return VuFind_Theme_Root_Helper_MobileMenu
-     */
-    public function mobileMenu()
-    {
-        return $this;
-    }
-
     /**
      * Display the top menu.
      *
@@ -75,13 +49,11 @@ class VuFind_Theme_Root_Helper_MobileMenu extends Zend_View_Helper_Abstract
      */
     public function header($extras = array())
     {
-        $context = array(
-            'controller' => $this->controller,
-            'action' => $this->action
-        ) + $extras;
-        return $this->view->context($this->view)->renderInContext(
-            'header.phtml', $context
-        );
+        // TODO: this assumes that the view has controller and action variables set;
+        // we either need to figure out how to set them here or else set them up
+        // globally as part of the bootstrap process.
+        $context = $this->getView()->plugin('context');
+        return $context($this->getView())->renderInContext('header.phtml', $extras);
     }
 
     /**
@@ -94,13 +66,8 @@ class VuFind_Theme_Root_Helper_MobileMenu extends Zend_View_Helper_Abstract
      */
     public function footer($extras = array())
     {
-        $context = array(
-            'controller' => $this->controller,
-            'action' => $this->action,
-            'account' => VF_Account_Manager::getInstance()
-        ) + $extras;
-        return $this->view->context($this->view)->renderInContext(
-            'footer.phtml', $context
-        );
+        // TODO: same as header()
+        $context = $this->getView()->plugin('context');
+        return $context($this->getView())->renderInContext('footer.phtml', $extras);
     }
 }
\ No newline at end of file
diff --git a/themes/vufind/jquerymobile/templates/footer.phtml b/themes/vufind/jquerymobile/templates/footer.phtml
index fd8909c1b7f..6421d8e0046 100644
--- a/themes/vufind/jquerymobile/templates/footer.phtml
+++ b/themes/vufind/jquerymobile/templates/footer.phtml
@@ -5,7 +5,7 @@
     // if a module has footer-navbar.tpl, then use it, otherwise use default
     try {
         echo $this->render("{$this->controller}/footer-navbar.phtml");
-    } catch (Zend_View_Exception $e) {
+    } catch (\Zend\View\Exception\RuntimeException $e) {
         // no module-specific footer navbar found -- use default.
         echo $this->render('default-footer-navbar.phtml');
     }
diff --git a/themes/vufind/jquerymobile/templates/header.phtml b/themes/vufind/jquerymobile/templates/header.phtml
index 6dd9e1cacfb..97a24978b35 100644
--- a/themes/vufind/jquerymobile/templates/header.phtml
+++ b/themes/vufind/jquerymobile/templates/header.phtml
@@ -21,7 +21,7 @@
     // if a module has header-navbar.tpl, then use it
     try {
         echo $this->render("{$this->controller}/header-navbar.phtml");
-    } catch (Zend_View_Exception $e) {
+    } catch (\Zend\View\Exception\RuntimeException $e) {
         // if this is a record view, load the record header; otherwise, do nothing:
         if (substr($this->controller, -6) == 'record') {
             echo $this->render('record/header-navbar.phtml');
diff --git a/themes/vufind/jquerymobile/theme.ini b/themes/vufind/jquerymobile/theme.ini
index 12e79b2ee5c..6d38d16cc4c 100644
--- a/themes/vufind/jquerymobile/theme.ini
+++ b/themes/vufind/jquerymobile/theme.ini
@@ -11,4 +11,5 @@ js[] = "cart.js"
 js[] = "scripts.js"
 favicon = "vufind-favicon.ico"
 
-helper_namespace = "VuFind\Theme\jQueryMobile\Helper"
\ No newline at end of file
+helper_namespace = "VuFind\Theme\jQueryMobile\Helper"
+helpers_to_register[] = "MobileMenu"
\ No newline at end of file
diff --git a/themes/vufind/root/theme.ini b/themes/vufind/root/theme.ini
index 9827ee2b7d8..e681ac4e3bf 100644
--- a/themes/vufind/root/theme.ini
+++ b/themes/vufind/root/theme.ini
@@ -1,6 +1,7 @@
 extends = false
 
 helper_namespace = "VuFind\Theme\Root\Helper"
+helpers_to_register[] = "Context"
 helpers_to_register[] = "HeadLink"
 helpers_to_register[] = "HeadScript"
 helpers_to_register[] = "HeadThemeResources"
-- 
GitLab