/** * Returns whether page should be considered active or not * * This method will compare the page's route name and mvc params with the * matched route's name and params * * @param bool $recursive [optional] whether page should be considered * active if any child pages are active. Default is * false. * @return bool whether page should be considered active or not */ public function isActive($recursive = false) { if (!$this->active) { //do we have a matched route? if ($this->routeMatch instanceof \Zend\Mvc\Router\RouteMatch) { if (null !== $this->getRoute() && $this->routeMatch->getMatchedRouteName() === $this->getRoute()) { //get default params and set defaults $myParams = $this->params; if (!isset($myParams['controller'])) { $myParams['controller'] = 'index'; } if (!isset($myParams['action'])) { $myParams['action'] = 'index'; } //check the controller and action params if (strtolower($this->routeMatch->getParam('__CONTROLLER__')) == strtolower($myParams['controller']) && strtolower($this->routeMatch->getParam('action')) == strtolower($myParams['action'])) { $this->active = true; return true; } } //if recursive check the sub pages if ($recursive) { /** @var $page Mvc */ foreach ($this->pages as $page) { $page->setRouteMatch($this->routeMatch); if ($page->isActive(true)) { return true; } } } return false; } //no matched route so try parent logic return parent::isActive($recursive); } return true; }
public function testIsActiveReturnsFalseWhenRequestHasLessParams() { $page = new Page\Mvc(array( 'label' => 'foo', 'action' => 'view', 'controller' => 'post', 'params' => array( 'id' => '1337' ) )); $routeMatch = new RouteMatch(array( 'controller' => 'post', 'action' => 'view', 'id' => null )); $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); }
public function testIsActiveReturnsFalseWhenRequestHasLessParams() { $page = new Page\Mvc(array('label' => 'foo', 'action' => 'view', 'controller' => 'post', 'module' => 'blog', 'params' => array('id' => '1337'))); $this->_front->getRequest()->setParams(array('module' => 'blog', 'controller' => 'post', 'action' => 'view', 'id' => null)); $this->assertFalse($page->isActive()); }
/** * {@inheritDoc} */ public function isActive($recursive = false) { //if (!$this->active) { if (null === $this->active) { $reqParams = array(); /**#@+ * Added by Taiwen Jiang */ $this->routeMatch = $this->getRouteMatch(); /**#@-*/ if ($this->routeMatch instanceof RouteMatch) { $reqParams = $this->routeMatch->getParams(); $originalController = ModuleRouteListener::ORIGINAL_CONTROLLER; if (isset($reqParams[$originalController])) { $reqParams['controller'] = $reqParams[$originalController]; } $myParams = $this->params; /**#@+ * Added by Taiwen Jiang */ if (null !== $this->section) { $myParams['section'] = $this->section; } if (null !== $this->module) { $myParams['module'] = $this->module; } /**#@-*/ if (null !== $this->controller) { $myParams['controller'] = $this->controller; } if (null !== $this->action) { $myParams['action'] = $this->action; } if (null !== $this->getRoute()) { /**#@+ * Added by Taiwen Jiang */ if (!empty($myParams['module']) && $myParams['module'] === $reqParams['module'] && empty($myParams['controller']) && empty($myParams['index'])) { $section = isset($myParams['section']) ? $myParams['section'] : ''; if ($section == $this->routeMatch->getParam('section')) { $this->active = true; return $this->active; } } if ($this->routeMatch->getMatchedRouteName() === $this->getRoute() && count(array_intersect_assoc($reqParams, $myParams)) == count($myParams)) { $this->active = true; return $this->active; } else { return $this->isAbstractActive($recursive); } /**#@-*/ if ($this->routeMatch->getMatchedRouteName() === $this->getRoute() && count(array_intersect_assoc($reqParams, $myParams)) == count($myParams)) { $this->active = true; return $this->active; } else { return parent::isActive($recursive); } } /**#@+ * Added by Taiwen Jiang */ return $this->isAbstractActive($recursive); /**#@-*/ } $myParams = $this->params; /**#@+ * Added by Taiwen Jiang */ if (null !== $this->module) { $myParams['module'] = $this->module; } /**#@-*/ if (null !== $this->controller) { $myParams['controller'] = $this->controller; } else { /** * @todo In ZF1, this was configurable and pulled * from the front controller */ $myParams['controller'] = 'index'; } if (null !== $this->action) { $myParams['action'] = $this->action; } else { /** * @todo In ZF1, this was configurable and pulled * from the front controller */ $myParams['action'] = 'index'; } if (count(array_intersect_assoc($reqParams, $myParams)) == count($myParams)) { $this->active = true; return true; } /**#@+ * Added by Taiwen Jiang */ return $this->isAbstractActive($recursive); /*#@-*/ } /**#@+ * Modified by Taiwen Jiang */ return $this->active; /**#@-*/ return parent::isActive($recursive); }
public function testRecursiveDetectIsActiveWhenRouteNameIsKnown() { $parentPage = new Page\Mvc(array('label' => 'some Label', 'route' => 'parentPageRoute')); $childPage = new Page\Mvc(array('label' => 'child', 'route' => 'childPageRoute')); $parentPage->addPage($childPage); $router = new TreeRouteStack(); $router->addRoutes(array('parentPageRoute' => array('type' => 'literal', 'options' => array('route' => '/foo', 'defaults' => array('controller' => 'fooController', 'action' => 'fooAction'))), 'childPageRoute' => array('type' => 'literal', 'options' => array('route' => '/bar', 'defaults' => array('controller' => 'barController', 'action' => 'barAction'))))); $routeMatch = new RouteMatch(array(ModuleRouteListener::MODULE_NAMESPACE => 'Application\\Controller', 'controller' => 'barController', 'action' => 'barAction')); $routeMatch->setMatchedRouteName('childPageRoute'); $event = new MvcEvent(); $event->setRouter($router)->setRouteMatch($routeMatch); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->onRoute($event); $parentPage->setRouter($event->getRouter()); $parentPage->setRouteMatch($event->getRouteMatch()); $childPage->setRouter($event->getRouter()); $childPage->setRouteMatch($event->getRouteMatch()); $this->assertTrue($childPage->isActive(true)); $this->assertTrue($parentPage->isActive(true)); }