7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Navigation
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Mvc.php 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_Navigation_Page
26 require_once 'Zend/Navigation/Page.php';
29 * @see Zend_Controller_Action_HelperBroker
31 require_once 'Zend/Controller/Action/HelperBroker.php';
34 * Used to check if page is active
36 * @see Zend_Controller_Front
38 require_once 'Zend/Controller/Front.php';
41 * Represents a page that is defined using module, controller, action, route
42 * name and route params to assemble the href
45 * @package Zend_Navigation
47 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
50 class Zend_Navigation_Page_Mvc
extends Zend_Navigation_Page
53 * Action name to use when assembling URL
60 * Controller name to use when assembling URL
64 protected $_controller;
67 * Module name to use when assembling URL
74 * Params to use when assembling URL
79 protected $_params = array();
82 * Route name to use when assembling URL
90 * Whether params should be reset when assembling URL
95 protected $_resetParams = true;
100 * The use of this variable minimizes execution time when getHref() is
101 * called more than once during the lifetime of a request. If a property
102 * is updated, the cache is invalidated.
106 protected $_hrefCache;
109 * Action helper for assembling URLs
112 * @var Zend_Controller_Action_Helper_Url
114 protected static $_urlHelper = null;
119 * Returns whether page should be considered active or not
121 * This method will compare the page properties against the request object
122 * that is found in the front controller.
124 * @param bool $recursive [optional] whether page should be considered
125 * active if any child pages are active. Default is
127 * @return bool whether page should be considered active or not
129 public function isActive($recursive = false)
131 if (!$this->_active
) {
132 $front = Zend_Controller_Front
::getInstance();
133 $reqParams = $front->getRequest()->getParams();
135 if (!array_key_exists('module', $reqParams)) {
136 $reqParams['module'] = $front->getDefaultModule();
139 $myParams = $this->_params
;
141 if (null !== $this->_module
) {
142 $myParams['module'] = $this->_module
;
144 $myParams['module'] = $front->getDefaultModule();
147 if (null !== $this->_controller
) {
148 $myParams['controller'] = $this->_controller
;
150 $myParams['controller'] = $front->getDefaultControllerName();
153 if (null !== $this->_action
) {
154 $myParams['action'] = $this->_action
;
156 $myParams['action'] = $front->getDefaultAction();
159 if (count(array_intersect_assoc($reqParams, $myParams)) ==
161 $this->_active
= true;
166 return parent
::isActive($recursive);
170 * Returns href for this page
172 * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
173 * the href based on the page's properties.
175 * @return string page href
177 public function getHref()
179 if ($this->_hrefCache
) {
180 return $this->_hrefCache
;
183 if (null === self
::$_urlHelper) {
185 Zend_Controller_Action_HelperBroker
::getStaticHelper('Url');
188 $params = $this->getParams();
190 if ($param = $this->getModule()) {
191 $params['module'] = $param;
194 if ($param = $this->getController()) {
195 $params['controller'] = $param;
198 if ($param = $this->getAction()) {
199 $params['action'] = $param;
202 $url = self
::$_urlHelper->url($params,
204 $this->getResetParams());
206 return $this->_hrefCache
= $url;
210 * Sets action name to use when assembling URL
214 * @param string $action action name
215 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
216 * @throws Zend_Navigation_Exception if invalid $action is given
218 public function setAction($action)
220 if (null !== $action && !is_string($action)) {
221 require_once 'Zend/Navigation/Exception.php';
222 throw new Zend_Navigation_Exception(
223 'Invalid argument: $action must be a string or null');
226 $this->_action
= $action;
227 $this->_hrefCache
= null;
232 * Returns action name to use when assembling URL
236 * @return string|null action name
238 public function getAction()
240 return $this->_action
;
244 * Sets controller name to use when assembling URL
248 * @param string|null $controller controller name
249 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
250 * @throws Zend_Navigation_Exception if invalid controller name is given
252 public function setController($controller)
254 if (null !== $controller && !is_string($controller)) {
255 require_once 'Zend/Navigation/Exception.php';
256 throw new Zend_Navigation_Exception(
257 'Invalid argument: $controller must be a string or null');
260 $this->_controller
= $controller;
261 $this->_hrefCache
= null;
266 * Returns controller name to use when assembling URL
270 * @return string|null controller name or null
272 public function getController()
274 return $this->_controller
;
278 * Sets module name to use when assembling URL
282 * @param string|null $module module name
283 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
284 * @throws Zend_Navigation_Exception if invalid module name is given
286 public function setModule($module)
288 if (null !== $module && !is_string($module)) {
289 require_once 'Zend/Navigation/Exception.php';
290 throw new Zend_Navigation_Exception(
291 'Invalid argument: $module must be a string or null');
294 $this->_module
= $module;
295 $this->_hrefCache
= null;
300 * Returns module name to use when assembling URL
304 * @return string|null module name or null
306 public function getModule()
308 return $this->_module
;
312 * Sets params to use when assembling URL
316 * @param array|null $params [optional] page params. Default is null
317 * which sets no params.
318 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
320 public function setParams(array $params = null)
322 if (null === $params) {
323 $this->_params
= array();
325 // TODO: do this more intelligently?
326 $this->_params
= $params;
329 $this->_hrefCache
= null;
334 * Returns params to use when assembling URL
338 * @return array page params
340 public function getParams()
342 return $this->_params
;
346 * Sets route name to use when assembling URL
350 * @param string $route route name to use when assembling URL
351 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
352 * @throws Zend_Navigation_Exception if invalid $route is given
354 public function setRoute($route)
356 if (null !== $route && (!is_string($route) ||
strlen($route) < 1)) {
357 require_once 'Zend/Navigation/Exception.php';
358 throw new Zend_Navigation_Exception(
359 'Invalid argument: $route must be a non-empty string or null');
362 $this->_route
= $route;
363 $this->_hrefCache
= null;
368 * Returns route name to use when assembling URL
372 * @return string route name
374 public function getRoute()
376 return $this->_route
;
380 * Sets whether params should be reset when assembling URL
384 * @param bool $resetParams whether params should be reset when
386 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
388 public function setResetParams($resetParams)
390 $this->_resetParams
= (bool) $resetParams;
391 $this->_hrefCache
= null;
396 * Returns whether params should be reset when assembling URL
400 * @return bool whether params should be reset when assembling URL
402 public function getResetParams()
404 return $this->_resetParams
;
408 * Sets action helper for assembling URLs
412 * @param Zend_Controller_Action_Helper_Url $uh URL helper
415 public static function setUrlHelper(Zend_Controller_Action_Helper_Url
$uh)
417 self
::$_urlHelper = $uh;
423 * Returns an array representation of the page
425 * @return array associative array containing all page properties
427 public function toArray()
432 'action' => $this->getAction(),
433 'controller' => $this->getController(),
434 'module' => $this->getModule(),
435 'params' => $this->getParams(),
436 'route' => $this->getRoute(),
437 'reset_params' => $this->getResetParams()