|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
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. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Navigation |
|
17 * @subpackage Page |
|
18 * @copyright Copyright (c) 2005-2010 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 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Navigation_Page |
|
25 */ |
|
26 require_once 'Zend/Navigation/Page.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Controller_Action_HelperBroker |
|
30 */ |
|
31 require_once 'Zend/Controller/Action/HelperBroker.php'; |
|
32 |
|
33 /** |
|
34 * Used to check if page is active |
|
35 * |
|
36 * @see Zend_Controller_Front |
|
37 */ |
|
38 require_once 'Zend/Controller/Front.php'; |
|
39 |
|
40 /** |
|
41 * Represents a page that is defined using module, controller, action, route |
|
42 * name and route params to assemble the href |
|
43 * |
|
44 * @category Zend |
|
45 * @package Zend_Navigation |
|
46 * @subpackage Page |
|
47 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
48 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
49 */ |
|
50 class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page |
|
51 { |
|
52 /** |
|
53 * Action name to use when assembling URL |
|
54 * |
|
55 * @var string |
|
56 */ |
|
57 protected $_action; |
|
58 |
|
59 /** |
|
60 * Controller name to use when assembling URL |
|
61 * |
|
62 * @var string |
|
63 */ |
|
64 protected $_controller; |
|
65 |
|
66 /** |
|
67 * Module name to use when assembling URL |
|
68 * |
|
69 * @var string |
|
70 */ |
|
71 protected $_module; |
|
72 |
|
73 /** |
|
74 * Params to use when assembling URL |
|
75 * |
|
76 * @see getHref() |
|
77 * @var array |
|
78 */ |
|
79 protected $_params = array(); |
|
80 |
|
81 /** |
|
82 * Route name to use when assembling URL |
|
83 * |
|
84 * @see getHref() |
|
85 * @var string |
|
86 */ |
|
87 protected $_route; |
|
88 |
|
89 /** |
|
90 * Whether params should be reset when assembling URL |
|
91 * |
|
92 * @see getHref() |
|
93 * @var bool |
|
94 */ |
|
95 protected $_resetParams = true; |
|
96 |
|
97 /** |
|
98 * Cached href |
|
99 * |
|
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. |
|
103 * |
|
104 * @var string |
|
105 */ |
|
106 protected $_hrefCache; |
|
107 |
|
108 /** |
|
109 * Action helper for assembling URLs |
|
110 * |
|
111 * @see getHref() |
|
112 * @var Zend_Controller_Action_Helper_Url |
|
113 */ |
|
114 protected static $_urlHelper = null; |
|
115 |
|
116 // Accessors: |
|
117 |
|
118 /** |
|
119 * Returns whether page should be considered active or not |
|
120 * |
|
121 * This method will compare the page properties against the request object |
|
122 * that is found in the front controller. |
|
123 * |
|
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 |
|
128 */ |
|
129 public function isActive($recursive = false) |
|
130 { |
|
131 if (!$this->_active) { |
|
132 $front = Zend_Controller_Front::getInstance(); |
|
133 $reqParams = $front->getRequest()->getParams(); |
|
134 |
|
135 if (!array_key_exists('module', $reqParams)) { |
|
136 $reqParams['module'] = $front->getDefaultModule(); |
|
137 } |
|
138 |
|
139 $myParams = $this->_params; |
|
140 |
|
141 if (null !== $this->_module) { |
|
142 $myParams['module'] = $this->_module; |
|
143 } else { |
|
144 $myParams['module'] = $front->getDefaultModule(); |
|
145 } |
|
146 |
|
147 if (null !== $this->_controller) { |
|
148 $myParams['controller'] = $this->_controller; |
|
149 } else { |
|
150 $myParams['controller'] = $front->getDefaultControllerName(); |
|
151 } |
|
152 |
|
153 if (null !== $this->_action) { |
|
154 $myParams['action'] = $this->_action; |
|
155 } else { |
|
156 $myParams['action'] = $front->getDefaultAction(); |
|
157 } |
|
158 |
|
159 if (count(array_intersect_assoc($reqParams, $myParams)) == |
|
160 count($myParams)) { |
|
161 $this->_active = true; |
|
162 return true; |
|
163 } |
|
164 } |
|
165 |
|
166 return parent::isActive($recursive); |
|
167 } |
|
168 |
|
169 /** |
|
170 * Returns href for this page |
|
171 * |
|
172 * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble |
|
173 * the href based on the page's properties. |
|
174 * |
|
175 * @return string page href |
|
176 */ |
|
177 public function getHref() |
|
178 { |
|
179 if ($this->_hrefCache) { |
|
180 return $this->_hrefCache; |
|
181 } |
|
182 |
|
183 if (null === self::$_urlHelper) { |
|
184 self::$_urlHelper = |
|
185 Zend_Controller_Action_HelperBroker::getStaticHelper('Url'); |
|
186 } |
|
187 |
|
188 $params = $this->getParams(); |
|
189 |
|
190 if ($param = $this->getModule()) { |
|
191 $params['module'] = $param; |
|
192 } |
|
193 |
|
194 if ($param = $this->getController()) { |
|
195 $params['controller'] = $param; |
|
196 } |
|
197 |
|
198 if ($param = $this->getAction()) { |
|
199 $params['action'] = $param; |
|
200 } |
|
201 |
|
202 $url = self::$_urlHelper->url($params, |
|
203 $this->getRoute(), |
|
204 $this->getResetParams()); |
|
205 |
|
206 return $this->_hrefCache = $url; |
|
207 } |
|
208 |
|
209 /** |
|
210 * Sets action name to use when assembling URL |
|
211 * |
|
212 * @see getHref() |
|
213 * |
|
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 |
|
217 */ |
|
218 public function setAction($action) |
|
219 { |
|
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'); |
|
224 } |
|
225 |
|
226 $this->_action = $action; |
|
227 $this->_hrefCache = null; |
|
228 return $this; |
|
229 } |
|
230 |
|
231 /** |
|
232 * Returns action name to use when assembling URL |
|
233 * |
|
234 * @see getHref() |
|
235 * |
|
236 * @return string|null action name |
|
237 */ |
|
238 public function getAction() |
|
239 { |
|
240 return $this->_action; |
|
241 } |
|
242 |
|
243 /** |
|
244 * Sets controller name to use when assembling URL |
|
245 * |
|
246 * @see getHref() |
|
247 * |
|
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 |
|
251 */ |
|
252 public function setController($controller) |
|
253 { |
|
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'); |
|
258 } |
|
259 |
|
260 $this->_controller = $controller; |
|
261 $this->_hrefCache = null; |
|
262 return $this; |
|
263 } |
|
264 |
|
265 /** |
|
266 * Returns controller name to use when assembling URL |
|
267 * |
|
268 * @see getHref() |
|
269 * |
|
270 * @return string|null controller name or null |
|
271 */ |
|
272 public function getController() |
|
273 { |
|
274 return $this->_controller; |
|
275 } |
|
276 |
|
277 /** |
|
278 * Sets module name to use when assembling URL |
|
279 * |
|
280 * @see getHref() |
|
281 * |
|
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 |
|
285 */ |
|
286 public function setModule($module) |
|
287 { |
|
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'); |
|
292 } |
|
293 |
|
294 $this->_module = $module; |
|
295 $this->_hrefCache = null; |
|
296 return $this; |
|
297 } |
|
298 |
|
299 /** |
|
300 * Returns module name to use when assembling URL |
|
301 * |
|
302 * @see getHref() |
|
303 * |
|
304 * @return string|null module name or null |
|
305 */ |
|
306 public function getModule() |
|
307 { |
|
308 return $this->_module; |
|
309 } |
|
310 |
|
311 /** |
|
312 * Sets params to use when assembling URL |
|
313 * |
|
314 * @see getHref() |
|
315 * |
|
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 |
|
319 */ |
|
320 public function setParams(array $params = null) |
|
321 { |
|
322 if (null === $params) { |
|
323 $this->_params = array(); |
|
324 } else { |
|
325 // TODO: do this more intelligently? |
|
326 $this->_params = $params; |
|
327 } |
|
328 |
|
329 $this->_hrefCache = null; |
|
330 return $this; |
|
331 } |
|
332 |
|
333 /** |
|
334 * Returns params to use when assembling URL |
|
335 * |
|
336 * @see getHref() |
|
337 * |
|
338 * @return array page params |
|
339 */ |
|
340 public function getParams() |
|
341 { |
|
342 return $this->_params; |
|
343 } |
|
344 |
|
345 /** |
|
346 * Sets route name to use when assembling URL |
|
347 * |
|
348 * @see getHref() |
|
349 * |
|
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 |
|
353 */ |
|
354 public function setRoute($route) |
|
355 { |
|
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'); |
|
360 } |
|
361 |
|
362 $this->_route = $route; |
|
363 $this->_hrefCache = null; |
|
364 return $this; |
|
365 } |
|
366 |
|
367 /** |
|
368 * Returns route name to use when assembling URL |
|
369 * |
|
370 * @see getHref() |
|
371 * |
|
372 * @return string route name |
|
373 */ |
|
374 public function getRoute() |
|
375 { |
|
376 return $this->_route; |
|
377 } |
|
378 |
|
379 /** |
|
380 * Sets whether params should be reset when assembling URL |
|
381 * |
|
382 * @see getHref() |
|
383 * |
|
384 * @param bool $resetParams whether params should be reset when |
|
385 * assembling URL |
|
386 * @return Zend_Navigation_Page_Mvc fluent interface, returns self |
|
387 */ |
|
388 public function setResetParams($resetParams) |
|
389 { |
|
390 $this->_resetParams = (bool) $resetParams; |
|
391 $this->_hrefCache = null; |
|
392 return $this; |
|
393 } |
|
394 |
|
395 /** |
|
396 * Returns whether params should be reset when assembling URL |
|
397 * |
|
398 * @see getHref() |
|
399 * |
|
400 * @return bool whether params should be reset when assembling URL |
|
401 */ |
|
402 public function getResetParams() |
|
403 { |
|
404 return $this->_resetParams; |
|
405 } |
|
406 |
|
407 /** |
|
408 * Sets action helper for assembling URLs |
|
409 * |
|
410 * @see getHref() |
|
411 * |
|
412 * @param Zend_Controller_Action_Helper_Url $uh URL helper |
|
413 * @return void |
|
414 */ |
|
415 public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh) |
|
416 { |
|
417 self::$_urlHelper = $uh; |
|
418 } |
|
419 |
|
420 // Public methods: |
|
421 |
|
422 /** |
|
423 * Returns an array representation of the page |
|
424 * |
|
425 * @return array associative array containing all page properties |
|
426 */ |
|
427 public function toArray() |
|
428 { |
|
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() |
|
438 )); |
|
439 } |
|
440 } |