*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Navigation / Page / Mvc.php
blob90a9573e415270479faba17d2b7ac585bd04317a
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Navigation
17 * @subpackage Page
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 $
23 /**
24 * @see Zend_Navigation_Page
26 require_once 'Zend/Navigation/Page.php';
28 /**
29 * @see Zend_Controller_Action_HelperBroker
31 require_once 'Zend/Controller/Action/HelperBroker.php';
33 /**
34 * Used to check if page is active
36 * @see Zend_Controller_Front
38 require_once 'Zend/Controller/Front.php';
40 /**
41 * Represents a page that is defined using module, controller, action, route
42 * name and route params to assemble the href
44 * @category Zend
45 * @package Zend_Navigation
46 * @subpackage Page
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
52 /**
53 * Action name to use when assembling URL
55 * @var string
57 protected $_action;
59 /**
60 * Controller name to use when assembling URL
62 * @var string
64 protected $_controller;
66 /**
67 * Module name to use when assembling URL
69 * @var string
71 protected $_module;
73 /**
74 * Params to use when assembling URL
76 * @see getHref()
77 * @var array
79 protected $_params = array();
81 /**
82 * Route name to use when assembling URL
84 * @see getHref()
85 * @var string
87 protected $_route;
89 /**
90 * Whether params should be reset when assembling URL
92 * @see getHref()
93 * @var bool
95 protected $_resetParams = true;
97 /**
98 * Cached href
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.
104 * @var string
106 protected $_hrefCache;
109 * Action helper for assembling URLs
111 * @see getHref()
112 * @var Zend_Controller_Action_Helper_Url
114 protected static $_urlHelper = null;
116 // Accessors:
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
126 * false.
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;
143 } else {
144 $myParams['module'] = $front->getDefaultModule();
147 if (null !== $this->_controller) {
148 $myParams['controller'] = $this->_controller;
149 } else {
150 $myParams['controller'] = $front->getDefaultControllerName();
153 if (null !== $this->_action) {
154 $myParams['action'] = $this->_action;
155 } else {
156 $myParams['action'] = $front->getDefaultAction();
159 if (count(array_intersect_assoc($reqParams, $myParams)) ==
160 count($myParams)) {
161 $this->_active = true;
162 return 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) {
184 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,
203 $this->getRoute(),
204 $this->getResetParams());
206 return $this->_hrefCache = $url;
210 * Sets action name to use when assembling URL
212 * @see getHref()
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;
228 return $this;
232 * Returns action name to use when assembling URL
234 * @see getHref()
236 * @return string|null action name
238 public function getAction()
240 return $this->_action;
244 * Sets controller name to use when assembling URL
246 * @see getHref()
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;
262 return $this;
266 * Returns controller name to use when assembling URL
268 * @see getHref()
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
280 * @see getHref()
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;
296 return $this;
300 * Returns module name to use when assembling URL
302 * @see getHref()
304 * @return string|null module name or null
306 public function getModule()
308 return $this->_module;
312 * Sets params to use when assembling URL
314 * @see getHref()
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();
324 } else {
325 // TODO: do this more intelligently?
326 $this->_params = $params;
329 $this->_hrefCache = null;
330 return $this;
334 * Returns params to use when assembling URL
336 * @see getHref()
338 * @return array page params
340 public function getParams()
342 return $this->_params;
346 * Sets route name to use when assembling URL
348 * @see getHref()
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;
364 return $this;
368 * Returns route name to use when assembling URL
370 * @see getHref()
372 * @return string route name
374 public function getRoute()
376 return $this->_route;
380 * Sets whether params should be reset when assembling URL
382 * @see getHref()
384 * @param bool $resetParams whether params should be reset when
385 * assembling URL
386 * @return Zend_Navigation_Page_Mvc fluent interface, returns self
388 public function setResetParams($resetParams)
390 $this->_resetParams = (bool) $resetParams;
391 $this->_hrefCache = null;
392 return $this;
396 * Returns whether params should be reset when assembling URL
398 * @see getHref()
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
410 * @see getHref()
412 * @param Zend_Controller_Action_Helper_Url $uh URL helper
413 * @return void
415 public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
417 self::$_urlHelper = $uh;
420 // Public methods:
423 * Returns an array representation of the page
425 * @return array associative array containing all page properties
427 public function toArray()
429 return array_merge(
430 parent::toArray(),
431 array(
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()