From 244f70d9bbae843ea43e8172b4611584a46b34fd Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 26 Jun 2012 15:20:52 -0400 Subject: [PATCH] Make template context available to view for proper jquerymobile menu generation. --- module/VuFind/src/VuFind/Bootstrap.php | 24 +++++++++++++++++++ .../jquerymobile/templates/footer.phtml | 2 +- .../jquerymobile/templates/header.phtml | 4 ++-- .../templates/myresearch/footer-navbar.phtml | 4 ++-- .../templates/myresearch/header-navbar.phtml | 6 ++--- .../templates/search/footer-navbar.phtml | 2 +- .../templates/search/header-navbar.phtml | 4 ++-- 7 files changed, 35 insertions(+), 11 deletions(-) diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index 1c9da23e849..883f81b1da7 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -65,6 +65,7 @@ class Bootstrap public function bootstrap() { $this->initAccount(); + $this->initContext(); $this->initTheme(); } @@ -83,6 +84,29 @@ class Bootstrap $this->events->attach('dispatch', $callback); } + /** + * Set view variables representing the current context. + * + * @return void + */ + protected function initContext() + { + $callback = function($event) { + $serviceManager = $event->getApplication()->getServiceManager(); + $viewModel = $serviceManager->get('viewmanager')->getViewModel(); + + // Grab the template name from the first child -- we can use this to + // figure out the current template context. + $children = $viewModel->getChildren(); + $parts = explode('/', $children[0]->getTemplate()); + $viewModel->setVariable('templateDir', $parts[0]); + $viewModel->setVariable( + 'templateName', isset($parts[1]) ? $parts[1] : null + ); + }; + $this->events->attach('dispatch', $callback); + } + /** * Set up theme handling. * diff --git a/themes/vufind/jquerymobile/templates/footer.phtml b/themes/vufind/jquerymobile/templates/footer.phtml index 6421d8e0046..0dc231d6a2d 100644 --- a/themes/vufind/jquerymobile/templates/footer.phtml +++ b/themes/vufind/jquerymobile/templates/footer.phtml @@ -4,7 +4,7 @@ <? // if a module has footer-navbar.tpl, then use it, otherwise use default try { - echo $this->render("{$this->controller}/footer-navbar.phtml"); + echo $this->render("{$this->layout()->templateDir}/footer-navbar.phtml"); } 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 820b6f70d2c..f7d66d28f03 100644 --- a/themes/vufind/jquerymobile/templates/header.phtml +++ b/themes/vufind/jquerymobile/templates/header.phtml @@ -20,10 +20,10 @@ <? // if a module has header-navbar.tpl, then use it try { - echo $this->render("{$this->controller}/header-navbar.phtml"); + echo $this->render("{$this->layout()->templateDir}/header-navbar.phtml"); } 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') { + if (substr($this->layout()->templateDir, -6) == 'record') { echo $this->render('record/header-navbar.phtml'); } } diff --git a/themes/vufind/jquerymobile/templates/myresearch/footer-navbar.phtml b/themes/vufind/jquerymobile/templates/myresearch/footer-navbar.phtml index 5de39491f9e..2343dc6de81 100644 --- a/themes/vufind/jquerymobile/templates/myresearch/footer-navbar.phtml +++ b/themes/vufind/jquerymobile/templates/myresearch/footer-navbar.phtml @@ -1,8 +1,8 @@ <? if ($this->layout()->account->isLoggedIn()): ?> <div data-role="navbar"> <ul> - <li><a rel="external" <?=$this->action=="mylist" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-favorites')?>"><?=$this->transEsc('Favorites')?></a></li> - <li><a rel="external" <?=$this->action=="history" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('search-history')?>?require_login"><?=$this->transEsc('History')?></a></li> + <li><a rel="external" <?=$this->layout()->templateName=="mylist" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-favorites')?>"><?=$this->transEsc('Favorites')?></a></li> + <li><a rel="external" <?=$this->layout()->templateName=="history" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('search-history')?>?require_login"><?=$this->transEsc('History')?></a></li> <li><a rel="external" href="<?=$this->url('myresearch-logout')?>"><?=$this->transEsc("Log Out")?></a></li> </ul> </div> diff --git a/themes/vufind/jquerymobile/templates/myresearch/header-navbar.phtml b/themes/vufind/jquerymobile/templates/myresearch/header-navbar.phtml index 63edad2a5e7..a82b6b422fa 100644 --- a/themes/vufind/jquerymobile/templates/myresearch/header-navbar.phtml +++ b/themes/vufind/jquerymobile/templates/myresearch/header-navbar.phtml @@ -1,9 +1,9 @@ <? if (VF_Account_Manager::getInstance()->isLoggedIn()): ?> <div data-role="navbar"> <ul> - <li><a rel="external" <?=$this->action=="checkedout" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-checkedout')?>"><?=$this->transEsc('Checked Out')?></a></li> - <li><a rel="external" <?=$this->action=="holds" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-holds')?>"><?=$this->transEsc('Holds')?></a></li> - <li><a rel="external" <?=$this->action=="fines" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-fines')?>"><?=$this->transEsc('Fines')?></a></li> + <li><a rel="external" <?=$this->layout()->templateName=="checkedout" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-checkedout')?>"><?=$this->transEsc('Checked Out')?></a></li> + <li><a rel="external" <?=$this->layout()->templateName=="holds" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-holds')?>"><?=$this->transEsc('Holds')?></a></li> + <li><a rel="external" <?=$this->layout()->templateName=="fines" ? ' class="ui-btn-active"' : ''?> href="<?=$this->url('myresearch-fines')?>"><?=$this->transEsc('Fines')?></a></li> </ul> </div> <? endif; ?> \ No newline at end of file diff --git a/themes/vufind/jquerymobile/templates/search/footer-navbar.phtml b/themes/vufind/jquerymobile/templates/search/footer-navbar.phtml index 2501d59e6c0..3a8366aa1ba 100644 --- a/themes/vufind/jquerymobile/templates/search/footer-navbar.phtml +++ b/themes/vufind/jquerymobile/templates/search/footer-navbar.phtml @@ -1,4 +1,4 @@ -<? if ($this->action == 'history'): // use myresearch navbar for Search/history ?> +<? if ($this->layout()->templateName == 'history'): // use myresearch navbar for Search/history ?> <?=$this->render('myresearch/footer-navbar.phtml')?> <? else: // use default navbar for everything else ?> <?=$this->render('default-footer-navbar.phtml');?> diff --git a/themes/vufind/jquerymobile/templates/search/header-navbar.phtml b/themes/vufind/jquerymobile/templates/search/header-navbar.phtml index 4a181480510..58cf4d71d8a 100644 --- a/themes/vufind/jquerymobile/templates/search/header-navbar.phtml +++ b/themes/vufind/jquerymobile/templates/search/header-navbar.phtml @@ -1,6 +1,6 @@ -<? if ($this->action == 'history'): // use myresearch navbar for Search/history ?> +<? if ($this->layout()->templateName == 'history'): // use myresearch navbar for Search/history ?> <?=$this->render('myresearch/header-navbar.phtml')?> -<? elseif (isset($this->results) && $this->results->getResultTotal() > 0): ?> +<? elseif (isset($this->results) && is_object($this->results) && $this->results->getResultTotal() > 0): ?> <div data-role="navbar"> <ul> <li><a href="#Search-narrow" data-rel="dialog" data-transition="flip"><?=$this->transEsc('Narrow Search')?></a></li> -- GitLab