File indexing completed on 2024-12-22 05:36:52
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Navigation 0017 * @subpackage Page 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_Navigation_Page 0025 */ 0026 // require_once 'Zend/Navigation/Page.php'; 0027 0028 /** 0029 * @see Zend_Controller_Action_HelperBroker 0030 */ 0031 // require_once 'Zend/Controller/Action/HelperBroker.php'; 0032 0033 /** 0034 * Used to check if page is active 0035 * 0036 * @see Zend_Controller_Front 0037 */ 0038 // require_once 'Zend/Controller/Front.php'; 0039 0040 /** 0041 * Represents a page that is defined using module, controller, action, route 0042 * name and route params to assemble the href 0043 * 0044 * @category Zend 0045 * @package Zend_Navigation 0046 * @subpackage Page 0047 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0048 * @license http://framework.zend.com/license/new-bsd New BSD License 0049 */ 0050 class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page 0051 { 0052 /** 0053 * Action name to use when assembling URL 0054 * 0055 * @var string 0056 */ 0057 protected $_action; 0058 0059 /** 0060 * Controller name to use when assembling URL 0061 * 0062 * @var string 0063 */ 0064 protected $_controller; 0065 0066 /** 0067 * Module name to use when assembling URL 0068 * 0069 * @var string 0070 */ 0071 protected $_module; 0072 0073 /** 0074 * Params to use when assembling URL 0075 * 0076 * @see getHref() 0077 * @var array 0078 */ 0079 protected $_params = array(); 0080 0081 /** 0082 * Route name to use when assembling URL 0083 * 0084 * @see getHref() 0085 * @var string 0086 */ 0087 protected $_route; 0088 0089 /** 0090 * Whether params should be reset when assembling URL 0091 * 0092 * @see getHref() 0093 * @var bool 0094 */ 0095 protected $_resetParams = true; 0096 0097 /** 0098 * Whether href should be encoded when assembling URL 0099 * 0100 * @see getHref() 0101 * @var bool 0102 */ 0103 protected $_encodeUrl = true; 0104 0105 /** 0106 * Whether this page should be considered active 0107 * 0108 * @var bool 0109 */ 0110 protected $_active = null; 0111 0112 /** 0113 * Scheme to use when assembling URL 0114 * 0115 * @see getHref() 0116 * @var string 0117 */ 0118 protected $_scheme; 0119 0120 /** 0121 * Cached href 0122 * 0123 * The use of this variable minimizes execution time when getHref() is 0124 * called more than once during the lifetime of a request. If a property 0125 * is updated, the cache is invalidated. 0126 * 0127 * @var string 0128 */ 0129 protected $_hrefCache; 0130 0131 /** 0132 * Action helper for assembling URLs 0133 * 0134 * @see getHref() 0135 * @var Zend_Controller_Action_Helper_Url 0136 */ 0137 protected static $_urlHelper = null; 0138 0139 /** 0140 * View helper for assembling URLs with schemes 0141 * 0142 * @see getHref() 0143 * @var Zend_View_Helper_ServerUrl 0144 */ 0145 protected static $_schemeHelper = null; 0146 0147 // Accessors: 0148 0149 /** 0150 * Returns whether page should be considered active or not 0151 * 0152 * This method will compare the page properties against the request object 0153 * that is found in the front controller. 0154 * 0155 * @param bool $recursive [optional] whether page should be considered 0156 * active if any child pages are active. Default is 0157 * false. 0158 * @return bool whether page should be considered active or not 0159 */ 0160 public function isActive($recursive = false) 0161 { 0162 if (null === $this->_active) { 0163 $front = Zend_Controller_Front::getInstance(); 0164 $request = $front->getRequest(); 0165 $reqParams = array(); 0166 if ($request) { 0167 $reqParams = $request->getParams(); 0168 if (!array_key_exists('module', $reqParams)) { 0169 $reqParams['module'] = $front->getDefaultModule(); 0170 } 0171 } 0172 0173 $myParams = $this->_params; 0174 0175 if ($this->_route 0176 && method_exists($front->getRouter(), 'getRoute') 0177 ) { 0178 $route = $front->getRouter()->getRoute($this->_route); 0179 if (method_exists($route, 'getDefaults')) { 0180 $myParams = array_merge($route->getDefaults(), $myParams); 0181 } 0182 } 0183 0184 if (null !== $this->_module) { 0185 $myParams['module'] = $this->_module; 0186 } elseif (!array_key_exists('module', $myParams)) { 0187 $myParams['module'] = $front->getDefaultModule(); 0188 } 0189 0190 if (null !== $this->_controller) { 0191 $myParams['controller'] = $this->_controller; 0192 } elseif (!array_key_exists('controller', $myParams)) { 0193 $myParams['controller'] = $front->getDefaultControllerName(); 0194 } 0195 0196 if (null !== $this->_action) { 0197 $myParams['action'] = $this->_action; 0198 } elseif (!array_key_exists('action', $myParams)) { 0199 $myParams['action'] = $front->getDefaultAction(); 0200 } 0201 0202 foreach ($myParams as $key => $value) { 0203 if (null === $value) { 0204 unset($myParams[$key]); 0205 } 0206 } 0207 0208 if (count(array_intersect_assoc($reqParams, $myParams)) == 0209 count($myParams) 0210 ) { 0211 $this->_active = true; 0212 0213 return true; 0214 } 0215 0216 $this->_active = false; 0217 } 0218 0219 return parent::isActive($recursive); 0220 } 0221 0222 /** 0223 * Returns href for this page 0224 * 0225 * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble 0226 * the href based on the page's properties. 0227 * 0228 * @return string page href 0229 */ 0230 public function getHref() 0231 { 0232 if ($this->_hrefCache) { 0233 return $this->_hrefCache; 0234 } 0235 0236 if (null === self::$_urlHelper) { 0237 self::$_urlHelper = 0238 Zend_Controller_Action_HelperBroker::getStaticHelper('Url'); 0239 } 0240 0241 $params = $this->getParams(); 0242 0243 if ($param = $this->getModule()) { 0244 $params['module'] = $param; 0245 } 0246 0247 if ($param = $this->getController()) { 0248 $params['controller'] = $param; 0249 } 0250 0251 if ($param = $this->getAction()) { 0252 $params['action'] = $param; 0253 } 0254 0255 $url = self::$_urlHelper->url( 0256 $params, 0257 $this->getRoute(), 0258 $this->getResetParams(), 0259 $this->getEncodeUrl() 0260 ); 0261 0262 // Use scheme? 0263 $scheme = $this->getScheme(); 0264 if (null !== $scheme) { 0265 if (null === self::$_schemeHelper) { 0266 // require_once 'Zend/View/Helper/ServerUrl.php'; 0267 self::$_schemeHelper = new Zend_View_Helper_ServerUrl(); 0268 } 0269 0270 $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url); 0271 } 0272 0273 // Add the fragment identifier if it is set 0274 $fragment = $this->getFragment(); 0275 if (null !== $fragment) { 0276 $url .= '#' . $fragment; 0277 } 0278 0279 return $this->_hrefCache = $url; 0280 } 0281 0282 /** 0283 * Sets action name to use when assembling URL 0284 * 0285 * @see getHref() 0286 * 0287 * @param string $action action name 0288 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0289 * @throws Zend_Navigation_Exception if invalid $action is given 0290 */ 0291 public function setAction($action) 0292 { 0293 if (null !== $action && !is_string($action)) { 0294 // require_once 'Zend/Navigation/Exception.php'; 0295 throw new Zend_Navigation_Exception( 0296 'Invalid argument: $action must be a string or null' 0297 ); 0298 } 0299 0300 $this->_action = $action; 0301 $this->_hrefCache = null; 0302 0303 return $this; 0304 } 0305 0306 /** 0307 * Returns action name to use when assembling URL 0308 * 0309 * @see getHref() 0310 * 0311 * @return string|null action name 0312 */ 0313 public function getAction() 0314 { 0315 return $this->_action; 0316 } 0317 0318 /** 0319 * Sets controller name to use when assembling URL 0320 * 0321 * @see getHref() 0322 * 0323 * @param string|null $controller controller name 0324 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0325 * @throws Zend_Navigation_Exception if invalid controller name is given 0326 */ 0327 public function setController($controller) 0328 { 0329 if (null !== $controller && !is_string($controller)) { 0330 // require_once 'Zend/Navigation/Exception.php'; 0331 throw new Zend_Navigation_Exception( 0332 'Invalid argument: $controller must be a string or null' 0333 ); 0334 } 0335 0336 $this->_controller = $controller; 0337 $this->_hrefCache = null; 0338 0339 return $this; 0340 } 0341 0342 /** 0343 * Returns controller name to use when assembling URL 0344 * 0345 * @see getHref() 0346 * 0347 * @return string|null controller name or null 0348 */ 0349 public function getController() 0350 { 0351 return $this->_controller; 0352 } 0353 0354 /** 0355 * Sets module name to use when assembling URL 0356 * 0357 * @see getHref() 0358 * 0359 * @param string|null $module module name 0360 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0361 * @throws Zend_Navigation_Exception if invalid module name is given 0362 */ 0363 public function setModule($module) 0364 { 0365 if (null !== $module && !is_string($module)) { 0366 // require_once 'Zend/Navigation/Exception.php'; 0367 throw new Zend_Navigation_Exception( 0368 'Invalid argument: $module must be a string or null' 0369 ); 0370 } 0371 0372 $this->_module = $module; 0373 $this->_hrefCache = null; 0374 0375 return $this; 0376 } 0377 0378 /** 0379 * Returns module name to use when assembling URL 0380 * 0381 * @see getHref() 0382 * 0383 * @return string|null module name or null 0384 */ 0385 public function getModule() 0386 { 0387 return $this->_module; 0388 } 0389 0390 /** 0391 * Set multiple parameters (to use when assembling URL) at once 0392 * 0393 * URL options passed to the url action helper for assembling URLs. 0394 * Overwrites any previously set parameters! 0395 * 0396 * @see getHref() 0397 * 0398 * @param array|null $params [optional] paramters as array 0399 * ('name' => 'value'). Default is null 0400 * which clears all params. 0401 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0402 */ 0403 public function setParams(array $params = null) 0404 { 0405 $this->clearParams(); 0406 0407 if (is_array($params)) { 0408 $this->addParams($params); 0409 } 0410 0411 return $this; 0412 } 0413 0414 /** 0415 * Set parameter (to use when assembling URL) 0416 * 0417 * URL option passed to the url action helper for assembling URLs. 0418 * 0419 * @see getHref() 0420 * 0421 * @param string $name parameter name 0422 * @param mixed $value parameter value 0423 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0424 */ 0425 public function setParam($name, $value) 0426 { 0427 $name = (string)$name; 0428 $this->_params[$name] = $value; 0429 0430 $this->_hrefCache = null; 0431 0432 return $this; 0433 } 0434 0435 /** 0436 * Add multiple parameters (to use when assembling URL) at once 0437 * 0438 * URL options passed to the url action helper for assembling URLs. 0439 * 0440 * @see getHref() 0441 * 0442 * @param array $params paramters as array ('name' => 'value') 0443 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0444 */ 0445 public function addParams(array $params) 0446 { 0447 foreach ($params as $name => $value) { 0448 $this->setParam($name, $value); 0449 } 0450 0451 return $this; 0452 } 0453 0454 /** 0455 * Remove parameter (to use when assembling URL) 0456 * 0457 * @see getHref() 0458 * 0459 * @param string $name 0460 * @return bool 0461 */ 0462 public function removeParam($name) 0463 { 0464 if (array_key_exists($name, $this->_params)) { 0465 unset($this->_params[$name]); 0466 0467 $this->_hrefCache = null; 0468 return true; 0469 } 0470 0471 return false; 0472 } 0473 0474 /** 0475 * Clear all parameters (to use when assembling URL) 0476 * 0477 * @see getHref() 0478 * 0479 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0480 */ 0481 public function clearParams() 0482 { 0483 $this->_params = array(); 0484 0485 $this->_hrefCache = null; 0486 return $this; 0487 } 0488 0489 /** 0490 * Retrieve all parameters (to use when assembling URL) 0491 * 0492 * @see getHref() 0493 * 0494 * @return array parameters as array ('name' => 'value') 0495 */ 0496 public function getParams() 0497 { 0498 return $this->_params; 0499 } 0500 0501 /** 0502 * Retrieve a single parameter (to use when assembling URL) 0503 * 0504 * @see getHref() 0505 * 0506 * @param string $name parameter name 0507 * @return mixed 0508 */ 0509 public function getParam($name) 0510 { 0511 $name = (string) $name; 0512 0513 if (!array_key_exists($name, $this->_params)) { 0514 return null; 0515 } 0516 0517 return $this->_params[$name]; 0518 } 0519 0520 /** 0521 * Sets route name to use when assembling URL 0522 * 0523 * @see getHref() 0524 * 0525 * @param string $route route name to use when assembling URL 0526 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0527 * @throws Zend_Navigation_Exception if invalid $route is given 0528 */ 0529 public function setRoute($route) 0530 { 0531 if (null !== $route && (!is_string($route) || strlen($route) < 1)) { 0532 // require_once 'Zend/Navigation/Exception.php'; 0533 throw new Zend_Navigation_Exception( 0534 'Invalid argument: $route must be a non-empty string or null' 0535 ); 0536 } 0537 0538 $this->_route = $route; 0539 $this->_hrefCache = null; 0540 0541 return $this; 0542 } 0543 0544 /** 0545 * Returns route name to use when assembling URL 0546 * 0547 * @see getHref() 0548 * 0549 * @return string route name 0550 */ 0551 public function getRoute() 0552 { 0553 return $this->_route; 0554 } 0555 0556 /** 0557 * Sets whether params should be reset when assembling URL 0558 * 0559 * @see getHref() 0560 * 0561 * @param bool $resetParams whether params should be reset when 0562 * assembling URL 0563 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0564 */ 0565 public function setResetParams($resetParams) 0566 { 0567 $this->_resetParams = (bool) $resetParams; 0568 $this->_hrefCache = null; 0569 0570 return $this; 0571 } 0572 0573 /** 0574 * Returns whether params should be reset when assembling URL 0575 * 0576 * @see getHref() 0577 * 0578 * @return bool whether params should be reset when assembling URL 0579 */ 0580 public function getResetParams() 0581 { 0582 return $this->_resetParams; 0583 } 0584 0585 /** 0586 * Sets whether href should be encoded when assembling URL 0587 * 0588 * @see getHref() 0589 * 0590 * @param $encodeUrl 0591 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0592 */ 0593 public function setEncodeUrl($encodeUrl) 0594 { 0595 $this->_encodeUrl = (bool) $encodeUrl; 0596 $this->_hrefCache = null; 0597 0598 return $this; 0599 } 0600 0601 /** 0602 * Returns whether herf should be encoded when assembling URL 0603 * 0604 * @see getHref() 0605 * 0606 * @return bool whether herf should be encoded when assembling URL 0607 */ 0608 public function getEncodeUrl() 0609 { 0610 return $this->_encodeUrl; 0611 } 0612 0613 /** 0614 * Sets scheme to use when assembling URL 0615 * 0616 * @see getHref() 0617 * 0618 * @param string|null $scheme scheme 0619 * @throws Zend_Navigation_Exception 0620 * @return Zend_Navigation_Page_Mvc fluent interface, returns self 0621 */ 0622 public function setScheme($scheme) 0623 { 0624 if (null !== $scheme && !is_string($scheme)) { 0625 // require_once 'Zend/Navigation/Exception.php'; 0626 throw new Zend_Navigation_Exception( 0627 'Invalid argument: $scheme must be a string or null' 0628 ); 0629 } 0630 0631 $this->_scheme = $scheme; 0632 return $this; 0633 } 0634 0635 /** 0636 * Returns scheme to use when assembling URL 0637 * 0638 * @see getHref() 0639 * 0640 * @return string|null scheme or null 0641 */ 0642 public function getScheme() 0643 { 0644 return $this->_scheme; 0645 } 0646 0647 /** 0648 * Sets action helper for assembling URLs 0649 * 0650 * @see getHref() 0651 * 0652 * @param Zend_Controller_Action_Helper_Url $uh URL helper 0653 * @return void 0654 */ 0655 public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh) 0656 { 0657 self::$_urlHelper = $uh; 0658 } 0659 0660 /** 0661 * Sets view helper for assembling URLs with schemes 0662 * 0663 * @see getHref() 0664 * 0665 * @param Zend_View_Helper_ServerUrl $sh scheme helper 0666 * @return void 0667 */ 0668 public static function setSchemeHelper(Zend_View_Helper_ServerUrl $sh) 0669 { 0670 self::$_schemeHelper = $sh; 0671 } 0672 0673 // Public methods: 0674 0675 /** 0676 * Returns an array representation of the page 0677 * 0678 * @return array associative array containing all page properties 0679 */ 0680 public function toArray() 0681 { 0682 return array_merge( 0683 parent::toArray(), 0684 array( 0685 'action' => $this->getAction(), 0686 'controller' => $this->getController(), 0687 'module' => $this->getModule(), 0688 'params' => $this->getParams(), 0689 'route' => $this->getRoute(), 0690 'reset_params' => $this->getResetParams(), 0691 'encodeUrl' => $this->getEncodeUrl(), 0692 'scheme' => $this->getScheme(), 0693 ) 0694 ); 0695 } 0696 }