|
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_View |
|
17 * @subpackage Helper |
|
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: Menu.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_View_Helper_Navigation_HelperAbstract |
|
25 */ |
|
26 require_once 'Zend/View/Helper/Navigation/HelperAbstract.php'; |
|
27 |
|
28 /** |
|
29 * Helper for rendering menus from navigation containers |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_View |
|
33 * @subpackage Helper |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 class Zend_View_Helper_Navigation_Menu |
|
38 extends Zend_View_Helper_Navigation_HelperAbstract |
|
39 { |
|
40 /** |
|
41 * CSS class to use for the ul element |
|
42 * |
|
43 * @var string |
|
44 */ |
|
45 protected $_ulClass = 'navigation'; |
|
46 |
|
47 /** |
|
48 * Whether only active branch should be rendered |
|
49 * |
|
50 * @var bool |
|
51 */ |
|
52 protected $_onlyActiveBranch = false; |
|
53 |
|
54 /** |
|
55 * Whether parents should be rendered when only rendering active branch |
|
56 * |
|
57 * @var bool |
|
58 */ |
|
59 protected $_renderParents = true; |
|
60 |
|
61 /** |
|
62 * Partial view script to use for rendering menu |
|
63 * |
|
64 * @var string|array |
|
65 */ |
|
66 protected $_partial = null; |
|
67 |
|
68 /** |
|
69 * View helper entry point: |
|
70 * Retrieves helper and optionally sets container to operate on |
|
71 * |
|
72 * @param Zend_Navigation_Container $container [optional] container to |
|
73 * operate on |
|
74 * @return Zend_View_Helper_Navigation_Menu fluent interface, |
|
75 * returns self |
|
76 */ |
|
77 public function menu(Zend_Navigation_Container $container = null) |
|
78 { |
|
79 if (null !== $container) { |
|
80 $this->setContainer($container); |
|
81 } |
|
82 |
|
83 return $this; |
|
84 } |
|
85 |
|
86 // Accessors: |
|
87 |
|
88 /** |
|
89 * Sets CSS class to use for the first 'ul' element when rendering |
|
90 * |
|
91 * @param string $ulClass CSS class to set |
|
92 * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self |
|
93 */ |
|
94 public function setUlClass($ulClass) |
|
95 { |
|
96 if (is_string($ulClass)) { |
|
97 $this->_ulClass = $ulClass; |
|
98 } |
|
99 |
|
100 return $this; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Returns CSS class to use for the first 'ul' element when rendering |
|
105 * |
|
106 * @return string CSS class |
|
107 */ |
|
108 public function getUlClass() |
|
109 { |
|
110 return $this->_ulClass; |
|
111 } |
|
112 |
|
113 /** |
|
114 * Sets a flag indicating whether only active branch should be rendered |
|
115 * |
|
116 * @param bool $flag [optional] render only active |
|
117 * branch. Default is true. |
|
118 * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self |
|
119 */ |
|
120 public function setOnlyActiveBranch($flag = true) |
|
121 { |
|
122 $this->_onlyActiveBranch = (bool) $flag; |
|
123 return $this; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Returns a flag indicating whether only active branch should be rendered |
|
128 * |
|
129 * By default, this value is false, meaning the entire menu will be |
|
130 * be rendered. |
|
131 * |
|
132 * @return bool whether only active branch should be rendered |
|
133 */ |
|
134 public function getOnlyActiveBranch() |
|
135 { |
|
136 return $this->_onlyActiveBranch; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Enables/disables rendering of parents when only rendering active branch |
|
141 * |
|
142 * See {@link setOnlyActiveBranch()} for more information. |
|
143 * |
|
144 * @param bool $flag [optional] render parents when |
|
145 * rendering active branch. |
|
146 * Default is true. |
|
147 * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self |
|
148 */ |
|
149 public function setRenderParents($flag = true) |
|
150 { |
|
151 $this->_renderParents = (bool) $flag; |
|
152 return $this; |
|
153 } |
|
154 |
|
155 /** |
|
156 * Returns flag indicating whether parents should be rendered when rendering |
|
157 * only the active branch |
|
158 * |
|
159 * By default, this value is true. |
|
160 * |
|
161 * @return bool whether parents should be rendered |
|
162 */ |
|
163 public function getRenderParents() |
|
164 { |
|
165 return $this->_renderParents; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Sets which partial view script to use for rendering menu |
|
170 * |
|
171 * @param string|array $partial partial view script or null. If |
|
172 * an array is given, it is |
|
173 * expected to contain two values; |
|
174 * the partial view script to use, |
|
175 * and the module where the script |
|
176 * can be found. |
|
177 * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self |
|
178 */ |
|
179 public function setPartial($partial) |
|
180 { |
|
181 if (null === $partial || is_string($partial) || is_array($partial)) { |
|
182 $this->_partial = $partial; |
|
183 } |
|
184 |
|
185 return $this; |
|
186 } |
|
187 |
|
188 /** |
|
189 * Returns partial view script to use for rendering menu |
|
190 * |
|
191 * @return string|array|null |
|
192 */ |
|
193 public function getPartial() |
|
194 { |
|
195 return $this->_partial; |
|
196 } |
|
197 |
|
198 // Public methods: |
|
199 |
|
200 /** |
|
201 * Returns an HTML string containing an 'a' element for the given page if |
|
202 * the page's href is not empty, and a 'span' element if it is empty |
|
203 * |
|
204 * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}. |
|
205 * |
|
206 * @param Zend_Navigation_Page $page page to generate HTML for |
|
207 * @return string HTML string for the given page |
|
208 */ |
|
209 public function htmlify(Zend_Navigation_Page $page) |
|
210 { |
|
211 // get label and title for translating |
|
212 $label = $page->getLabel(); |
|
213 $title = $page->getTitle(); |
|
214 |
|
215 // translate label and title? |
|
216 if ($this->getUseTranslator() && $t = $this->getTranslator()) { |
|
217 if (is_string($label) && !empty($label)) { |
|
218 $label = $t->translate($label); |
|
219 } |
|
220 if (is_string($title) && !empty($title)) { |
|
221 $title = $t->translate($title); |
|
222 } |
|
223 } |
|
224 |
|
225 // get attribs for element |
|
226 $attribs = array( |
|
227 'id' => $page->getId(), |
|
228 'title' => $title, |
|
229 'class' => $page->getClass() |
|
230 ); |
|
231 |
|
232 // does page have a href? |
|
233 if ($href = $page->getHref()) { |
|
234 $element = 'a'; |
|
235 $attribs['href'] = $href; |
|
236 $attribs['target'] = $page->getTarget(); |
|
237 } else { |
|
238 $element = 'span'; |
|
239 } |
|
240 |
|
241 return '<' . $element . $this->_htmlAttribs($attribs) . '>' |
|
242 . $this->view->escape($label) |
|
243 . '</' . $element . '>'; |
|
244 } |
|
245 |
|
246 /** |
|
247 * Normalizes given render options |
|
248 * |
|
249 * @param array $options [optional] options to normalize |
|
250 * @return array normalized options |
|
251 */ |
|
252 protected function _normalizeOptions(array $options = array()) |
|
253 { |
|
254 if (isset($options['indent'])) { |
|
255 $options['indent'] = $this->_getWhitespace($options['indent']); |
|
256 } else { |
|
257 $options['indent'] = $this->getIndent(); |
|
258 } |
|
259 |
|
260 if (isset($options['ulClass']) && $options['ulClass'] !== null) { |
|
261 $options['ulClass'] = (string) $options['ulClass']; |
|
262 } else { |
|
263 $options['ulClass'] = $this->getUlClass(); |
|
264 } |
|
265 |
|
266 if (array_key_exists('minDepth', $options)) { |
|
267 if (null !== $options['minDepth']) { |
|
268 $options['minDepth'] = (int) $options['minDepth']; |
|
269 } |
|
270 } else { |
|
271 $options['minDepth'] = $this->getMinDepth(); |
|
272 } |
|
273 |
|
274 if ($options['minDepth'] < 0 || $options['minDepth'] === null) { |
|
275 $options['minDepth'] = 0; |
|
276 } |
|
277 |
|
278 if (array_key_exists('maxDepth', $options)) { |
|
279 if (null !== $options['maxDepth']) { |
|
280 $options['maxDepth'] = (int) $options['maxDepth']; |
|
281 } |
|
282 } else { |
|
283 $options['maxDepth'] = $this->getMaxDepth(); |
|
284 } |
|
285 |
|
286 if (!isset($options['onlyActiveBranch'])) { |
|
287 $options['onlyActiveBranch'] = $this->getOnlyActiveBranch(); |
|
288 } |
|
289 |
|
290 if (!isset($options['renderParents'])) { |
|
291 $options['renderParents'] = $this->getRenderParents(); |
|
292 } |
|
293 |
|
294 return $options; |
|
295 } |
|
296 |
|
297 // Render methods: |
|
298 |
|
299 /** |
|
300 * Renders the deepest active menu within [$minDepth, $maxDeth], (called |
|
301 * from {@link renderMenu()}) |
|
302 * |
|
303 * @param Zend_Navigation_Container $container container to render |
|
304 * @param array $active active page and depth |
|
305 * @param string $ulClass CSS class for first UL |
|
306 * @param string $indent initial indentation |
|
307 * @param int|null $minDepth minimum depth |
|
308 * @param int|null $maxDepth maximum depth |
|
309 * @return string rendered menu |
|
310 */ |
|
311 protected function _renderDeepestMenu(Zend_Navigation_Container $container, |
|
312 $ulClass, |
|
313 $indent, |
|
314 $minDepth, |
|
315 $maxDepth) |
|
316 { |
|
317 if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) { |
|
318 return ''; |
|
319 } |
|
320 |
|
321 // special case if active page is one below minDepth |
|
322 if ($active['depth'] < $minDepth) { |
|
323 if (!$active['page']->hasPages()) { |
|
324 return ''; |
|
325 } |
|
326 } else if (!$active['page']->hasPages()) { |
|
327 // found pages has no children; render siblings |
|
328 $active['page'] = $active['page']->getParent(); |
|
329 } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) { |
|
330 // children are below max depth; render siblings |
|
331 $active['page'] = $active['page']->getParent(); |
|
332 } |
|
333 |
|
334 $ulClass = $ulClass ? ' class="' . $ulClass . '"' : ''; |
|
335 $html = $indent . '<ul' . $ulClass . '>' . self::EOL; |
|
336 |
|
337 foreach ($active['page'] as $subPage) { |
|
338 if (!$this->accept($subPage)) { |
|
339 continue; |
|
340 } |
|
341 $liClass = $subPage->isActive(true) ? ' class="active"' : ''; |
|
342 $html .= $indent . ' <li' . $liClass . '>' . self::EOL; |
|
343 $html .= $indent . ' ' . $this->htmlify($subPage) . self::EOL; |
|
344 $html .= $indent . ' </li>' . self::EOL; |
|
345 } |
|
346 |
|
347 $html .= $indent . '</ul>'; |
|
348 |
|
349 return $html; |
|
350 } |
|
351 |
|
352 /** |
|
353 * Renders a normal menu (called from {@link renderMenu()}) |
|
354 * |
|
355 * @param Zend_Navigation_Container $container container to render |
|
356 * @param string $ulClass CSS class for first UL |
|
357 * @param string $indent initial indentation |
|
358 * @param int|null $minDepth minimum depth |
|
359 * @param int|null $maxDepth maximum depth |
|
360 * @param bool $onlyActive render only active branch? |
|
361 * @return string |
|
362 */ |
|
363 protected function _renderMenu(Zend_Navigation_Container $container, |
|
364 $ulClass, |
|
365 $indent, |
|
366 $minDepth, |
|
367 $maxDepth, |
|
368 $onlyActive) |
|
369 { |
|
370 $html = ''; |
|
371 |
|
372 // find deepest active |
|
373 if ($found = $this->findActive($container, $minDepth, $maxDepth)) { |
|
374 $foundPage = $found['page']; |
|
375 $foundDepth = $found['depth']; |
|
376 } else { |
|
377 $foundPage = null; |
|
378 } |
|
379 |
|
380 // create iterator |
|
381 $iterator = new RecursiveIteratorIterator($container, |
|
382 RecursiveIteratorIterator::SELF_FIRST); |
|
383 if (is_int($maxDepth)) { |
|
384 $iterator->setMaxDepth($maxDepth); |
|
385 } |
|
386 |
|
387 // iterate container |
|
388 $prevDepth = -1; |
|
389 foreach ($iterator as $page) { |
|
390 $depth = $iterator->getDepth(); |
|
391 $isActive = $page->isActive(true); |
|
392 if ($depth < $minDepth || !$this->accept($page)) { |
|
393 // page is below minDepth or not accepted by acl/visibilty |
|
394 continue; |
|
395 } else if ($onlyActive && !$isActive) { |
|
396 // page is not active itself, but might be in the active branch |
|
397 $accept = false; |
|
398 if ($foundPage) { |
|
399 if ($foundPage->hasPage($page)) { |
|
400 // accept if page is a direct child of the active page |
|
401 $accept = true; |
|
402 } else if ($foundPage->getParent()->hasPage($page)) { |
|
403 // page is a sibling of the active page... |
|
404 if (!$foundPage->hasPages() || |
|
405 is_int($maxDepth) && $foundDepth + 1 > $maxDepth) { |
|
406 // accept if active page has no children, or the |
|
407 // children are too deep to be rendered |
|
408 $accept = true; |
|
409 } |
|
410 } |
|
411 } |
|
412 |
|
413 if (!$accept) { |
|
414 continue; |
|
415 } |
|
416 } |
|
417 |
|
418 // make sure indentation is correct |
|
419 $depth -= $minDepth; |
|
420 $myIndent = $indent . str_repeat(' ', $depth); |
|
421 |
|
422 if ($depth > $prevDepth) { |
|
423 // start new ul tag |
|
424 if ($ulClass && $depth == 0) { |
|
425 $ulClass = ' class="' . $ulClass . '"'; |
|
426 } else { |
|
427 $ulClass = ''; |
|
428 } |
|
429 $html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL; |
|
430 } else if ($prevDepth > $depth) { |
|
431 // close li/ul tags until we're at current depth |
|
432 for ($i = $prevDepth; $i > $depth; $i--) { |
|
433 $ind = $indent . str_repeat(' ', $i); |
|
434 $html .= $ind . ' </li>' . self::EOL; |
|
435 $html .= $ind . '</ul>' . self::EOL; |
|
436 } |
|
437 // close previous li tag |
|
438 $html .= $myIndent . ' </li>' . self::EOL; |
|
439 } else { |
|
440 // close previous li tag |
|
441 $html .= $myIndent . ' </li>' . self::EOL; |
|
442 } |
|
443 |
|
444 // render li tag and page |
|
445 $liClass = $isActive ? ' class="active"' : ''; |
|
446 $html .= $myIndent . ' <li' . $liClass . '>' . self::EOL |
|
447 . $myIndent . ' ' . $this->htmlify($page) . self::EOL; |
|
448 |
|
449 // store as previous depth for next iteration |
|
450 $prevDepth = $depth; |
|
451 } |
|
452 |
|
453 if ($html) { |
|
454 // done iterating container; close open ul/li tags |
|
455 for ($i = $prevDepth+1; $i > 0; $i--) { |
|
456 $myIndent = $indent . str_repeat(' ', $i-1); |
|
457 $html .= $myIndent . ' </li>' . self::EOL |
|
458 . $myIndent . '</ul>' . self::EOL; |
|
459 } |
|
460 $html = rtrim($html, self::EOL); |
|
461 } |
|
462 |
|
463 return $html; |
|
464 } |
|
465 |
|
466 /** |
|
467 * Renders helper |
|
468 * |
|
469 * Renders a HTML 'ul' for the given $container. If $container is not given, |
|
470 * the container registered in the helper will be used. |
|
471 * |
|
472 * Available $options: |
|
473 * |
|
474 * |
|
475 * @param Zend_Navigation_Container $container [optional] container to |
|
476 * create menu from. Default |
|
477 * is to use the container |
|
478 * retrieved from |
|
479 * {@link getContainer()}. |
|
480 * @param array $options [optional] options for |
|
481 * controlling rendering |
|
482 * @return string rendered menu |
|
483 */ |
|
484 public function renderMenu(Zend_Navigation_Container $container = null, |
|
485 array $options = array()) |
|
486 { |
|
487 if (null === $container) { |
|
488 $container = $this->getContainer(); |
|
489 } |
|
490 |
|
491 $options = $this->_normalizeOptions($options); |
|
492 |
|
493 if ($options['onlyActiveBranch'] && !$options['renderParents']) { |
|
494 $html = $this->_renderDeepestMenu($container, |
|
495 $options['ulClass'], |
|
496 $options['indent'], |
|
497 $options['minDepth'], |
|
498 $options['maxDepth']); |
|
499 } else { |
|
500 $html = $this->_renderMenu($container, |
|
501 $options['ulClass'], |
|
502 $options['indent'], |
|
503 $options['minDepth'], |
|
504 $options['maxDepth'], |
|
505 $options['onlyActiveBranch']); |
|
506 } |
|
507 |
|
508 return $html; |
|
509 } |
|
510 |
|
511 /** |
|
512 * Renders the inner-most sub menu for the active page in the $container |
|
513 * |
|
514 * This is a convenience method which is equivalent to the following call: |
|
515 * <code> |
|
516 * renderMenu($container, array( |
|
517 * 'indent' => $indent, |
|
518 * 'ulClass' => $ulClass, |
|
519 * 'minDepth' => null, |
|
520 * 'maxDepth' => null, |
|
521 * 'onlyActiveBranch' => true, |
|
522 * 'renderParents' => false |
|
523 * )); |
|
524 * </code> |
|
525 * |
|
526 * @param Zend_Navigation_Container $container [optional] container to |
|
527 * render. Default is to render |
|
528 * the container registered in |
|
529 * the helper. |
|
530 * @param string $ulClass [optional] CSS class to |
|
531 * use for UL element. Default |
|
532 * is to use the value from |
|
533 * {@link getUlClass()}. |
|
534 * @param string|int $indent [optional] indentation as |
|
535 * a string or number of |
|
536 * spaces. Default is to use |
|
537 * the value retrieved from |
|
538 * {@link getIndent()}. |
|
539 * @return string rendered content |
|
540 */ |
|
541 public function renderSubMenu(Zend_Navigation_Container $container = null, |
|
542 $ulClass = null, |
|
543 $indent = null) |
|
544 { |
|
545 return $this->renderMenu($container, array( |
|
546 'indent' => $indent, |
|
547 'ulClass' => $ulClass, |
|
548 'minDepth' => null, |
|
549 'maxDepth' => null, |
|
550 'onlyActiveBranch' => true, |
|
551 'renderParents' => false |
|
552 )); |
|
553 } |
|
554 |
|
555 /** |
|
556 * Renders the given $container by invoking the partial view helper |
|
557 * |
|
558 * The container will simply be passed on as a model to the view script |
|
559 * as-is, and will be available in the partial script as 'container', e.g. |
|
560 * <code>echo 'Number of pages: ', count($this->container);</code>. |
|
561 * |
|
562 * @param Zend_Navigation_Container $container [optional] container to |
|
563 * pass to view script. Default |
|
564 * is to use the container |
|
565 * registered in the helper. |
|
566 * @param string|array $partial [optional] partial view |
|
567 * script to use. Default is to |
|
568 * use the partial registered |
|
569 * in the helper. If an array |
|
570 * is given, it is expected to |
|
571 * contain two values; the |
|
572 * partial view script to use, |
|
573 * and the module where the |
|
574 * script can be found. |
|
575 * @return string helper output |
|
576 */ |
|
577 public function renderPartial(Zend_Navigation_Container $container = null, |
|
578 $partial = null) |
|
579 { |
|
580 if (null === $container) { |
|
581 $container = $this->getContainer(); |
|
582 } |
|
583 |
|
584 if (null === $partial) { |
|
585 $partial = $this->getPartial(); |
|
586 } |
|
587 |
|
588 if (empty($partial)) { |
|
589 require_once 'Zend/View/Exception.php'; |
|
590 $e = new Zend_View_Exception( |
|
591 'Unable to render menu: No partial view script provided' |
|
592 ); |
|
593 $e->setView($this->view); |
|
594 throw $e; |
|
595 } |
|
596 |
|
597 $model = array( |
|
598 'container' => $container |
|
599 ); |
|
600 |
|
601 if (is_array($partial)) { |
|
602 if (count($partial) != 2) { |
|
603 require_once 'Zend/View/Exception.php'; |
|
604 $e = new Zend_View_Exception( |
|
605 'Unable to render menu: A view partial supplied as ' |
|
606 . 'an array must contain two values: partial view ' |
|
607 . 'script and module where script can be found' |
|
608 ); |
|
609 $e->setView($this->view); |
|
610 throw $e; |
|
611 } |
|
612 |
|
613 return $this->view->partial($partial[0], $partial[1], $model); |
|
614 } |
|
615 |
|
616 return $this->view->partial($partial, null, $model); |
|
617 } |
|
618 |
|
619 // Zend_View_Helper_Navigation_Helper: |
|
620 |
|
621 /** |
|
622 * Renders menu |
|
623 * |
|
624 * Implements {@link Zend_View_Helper_Navigation_Helper::render()}. |
|
625 * |
|
626 * If a partial view is registered in the helper, the menu will be rendered |
|
627 * using the given partial script. If no partial is registered, the menu |
|
628 * will be rendered as an 'ul' element by the helper's internal method. |
|
629 * |
|
630 * @see renderPartial() |
|
631 * @see renderMenu() |
|
632 * |
|
633 * @param Zend_Navigation_Container $container [optional] container to |
|
634 * render. Default is to |
|
635 * render the container |
|
636 * registered in the helper. |
|
637 * @return string helper output |
|
638 */ |
|
639 public function render(Zend_Navigation_Container $container = null) |
|
640 { |
|
641 if ($partial = $this->getPartial()) { |
|
642 return $this->renderPartial($container, $partial); |
|
643 } else { |
|
644 return $this->renderMenu($container); |
|
645 } |
|
646 } |
|
647 } |