Menu.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_View_Helper_Navigation_HelperAbstract
  24. */
  25. require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
  26. /**
  27. * Helper for rendering menus from navigation containers
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_View_Helper_Navigation_Menu
  36. extends Zend_View_Helper_Navigation_HelperAbstract
  37. {
  38. /**
  39. * CSS class to use for the ul element
  40. *
  41. * @var string
  42. */
  43. protected $_ulClass = 'navigation';
  44. /**
  45. * Unique identifier (id) for the ul element
  46. *
  47. * @var string
  48. */
  49. protected $_ulId = null;
  50. /**
  51. * CSS class to use for the active elements
  52. *
  53. * @var string
  54. */
  55. protected $_activeClass = 'active';
  56. /**
  57. * Whether only active branch should be rendered
  58. *
  59. * @var bool
  60. */
  61. protected $_onlyActiveBranch = false;
  62. /**
  63. * Whether parents should be rendered when only rendering active branch
  64. *
  65. * @var bool
  66. */
  67. protected $_renderParents = true;
  68. /**
  69. * Partial view script to use for rendering menu
  70. *
  71. * @var string|array
  72. */
  73. protected $_partial = null;
  74. /**
  75. * Expand all sibling nodes of active branch nodes
  76. *
  77. * @var bool
  78. */
  79. protected $_expandSiblingNodesOfActiveBranch = false;
  80. /**
  81. * Adds CSS class from page to li element
  82. *
  83. * @var bool
  84. */
  85. protected $_addPageClassToLi = false;
  86. /**
  87. * View helper entry point:
  88. * Retrieves helper and optionally sets container to operate on
  89. *
  90. * @param Zend_Navigation_Container $container [optional] container to
  91. * operate on
  92. * @return Zend_View_Helper_Navigation_Menu fluent interface,
  93. * returns self
  94. */
  95. public function menu(Zend_Navigation_Container $container = null)
  96. {
  97. if (null !== $container) {
  98. $this->setContainer($container);
  99. }
  100. return $this;
  101. }
  102. // Accessors:
  103. /**
  104. * Sets CSS class to use for the first 'ul' element when rendering
  105. *
  106. * @param string $ulClass CSS class to set
  107. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  108. */
  109. public function setUlClass($ulClass)
  110. {
  111. if (is_string($ulClass)) {
  112. $this->_ulClass = $ulClass;
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Returns CSS class to use for the first 'ul' element when rendering
  118. *
  119. * @return string CSS class
  120. */
  121. public function getUlClass()
  122. {
  123. return $this->_ulClass;
  124. }
  125. /**
  126. * Sets unique identifier (id) to use for the first 'ul' element when
  127. * rendering
  128. *
  129. * @param string|null $ulId Unique identifier (id) to set
  130. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  131. */
  132. public function setUlId($ulId)
  133. {
  134. if (is_string($ulId)) {
  135. $this->_ulId = $ulId;
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Returns unique identifier (id) to use for the first 'ul' element when
  141. * rendering
  142. *
  143. * @return string|null Unique identifier (id); Default is 'null'
  144. */
  145. public function getUlId()
  146. {
  147. return $this->_ulId;
  148. }
  149. /**
  150. * Sets CSS class to use for the active elements when rendering
  151. *
  152. * @param string $activeClass CSS class to set
  153. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  154. */
  155. public function setActiveClass($activeClass)
  156. {
  157. if (is_string($activeClass)) {
  158. $this->_activeClass = $activeClass;
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Returns CSS class to use for the active elements when rendering
  164. *
  165. * @return string CSS class
  166. */
  167. public function getActiveClass()
  168. {
  169. return $this->_activeClass;
  170. }
  171. /**
  172. * Sets a flag indicating whether only active branch should be rendered
  173. *
  174. * @param bool $flag [optional] render only active
  175. * branch. Default is true.
  176. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  177. */
  178. public function setOnlyActiveBranch($flag = true)
  179. {
  180. $this->_onlyActiveBranch = (bool) $flag;
  181. return $this;
  182. }
  183. /**
  184. * Returns a flag indicating whether only active branch should be rendered
  185. *
  186. * By default, this value is false, meaning the entire menu will be
  187. * be rendered.
  188. *
  189. * @return bool whether only active branch should be rendered
  190. */
  191. public function getOnlyActiveBranch()
  192. {
  193. return $this->_onlyActiveBranch;
  194. }
  195. /**
  196. * Sets a flag indicating whether to expand all sibling nodes of the active branch
  197. *
  198. * @param bool $flag [optional] expand all siblings of
  199. * nodes in the active branch. Default is true.
  200. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  201. */
  202. public function setExpandSiblingNodesOfActiveBranch($flag = true)
  203. {
  204. $this->_expandSiblingNodesOfActiveBranch = (bool) $flag;
  205. return $this;
  206. }
  207. /**
  208. * Returns a flag indicating whether to expand all sibling nodes of the active branch
  209. *
  210. * By default, this value is false, meaning the entire menu will be
  211. * be rendered.
  212. *
  213. * @return bool whether siblings of nodes in the active branch should be expanded
  214. */
  215. public function getExpandSiblingNodesOfActiveBranch()
  216. {
  217. return $this->_expandSiblingNodesOfActiveBranch;
  218. }
  219. /**
  220. * Enables/disables rendering of parents when only rendering active branch
  221. *
  222. * See {@link setOnlyActiveBranch()} for more information.
  223. *
  224. * @param bool $flag [optional] render parents when
  225. * rendering active branch.
  226. * Default is true.
  227. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  228. */
  229. public function setRenderParents($flag = true)
  230. {
  231. $this->_renderParents = (bool) $flag;
  232. return $this;
  233. }
  234. /**
  235. * Returns flag indicating whether parents should be rendered when rendering
  236. * only the active branch
  237. *
  238. * By default, this value is true.
  239. *
  240. * @return bool whether parents should be rendered
  241. */
  242. public function getRenderParents()
  243. {
  244. return $this->_renderParents;
  245. }
  246. /**
  247. * Sets which partial view script to use for rendering menu
  248. *
  249. * @param string|array $partial partial view script or null. If
  250. * an array is given, it is
  251. * expected to contain two values;
  252. * the partial view script to use,
  253. * and the module where the script
  254. * can be found.
  255. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  256. */
  257. public function setPartial($partial)
  258. {
  259. if (null === $partial || is_string($partial) || is_array($partial)) {
  260. $this->_partial = $partial;
  261. }
  262. return $this;
  263. }
  264. /**
  265. * Returns partial view script to use for rendering menu
  266. *
  267. * @return string|array|null
  268. */
  269. public function getPartial()
  270. {
  271. return $this->_partial;
  272. }
  273. /**
  274. * Adds CSS class from page to li element
  275. *
  276. * Before:
  277. * <code>
  278. * <li>
  279. * <a href="#" class="foo">Bar</a>
  280. * </li>
  281. * </code>
  282. *
  283. * After:
  284. * <code>
  285. * <li class="foo">
  286. * <a href="#">Bar</a>
  287. * </li>
  288. * </code>
  289. *
  290. * @param bool $flag [optional] adds CSS class from
  291. * page to li element
  292. *
  293. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  294. */
  295. public function addPageClassToLi($flag = true)
  296. {
  297. $this->_addPageClassToLi = (bool) $flag;
  298. return $this;
  299. }
  300. /**
  301. * Returns a flag indicating whether the CSS class from page to be added to
  302. * li element
  303. *
  304. * @return bool
  305. */
  306. public function getAddPageClassToLi()
  307. {
  308. return $this->_addPageClassToLi;
  309. }
  310. // Public methods:
  311. /**
  312. * Returns an HTML string containing an 'a' element for the given page if
  313. * the page's href is not empty, and a 'span' element if it is empty
  314. *
  315. * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}.
  316. *
  317. * @param Zend_Navigation_Page $page page to generate HTML for
  318. * @return string HTML string for the given page
  319. */
  320. public function htmlify(Zend_Navigation_Page $page)
  321. {
  322. // get label and title for translating
  323. $label = $page->getLabel();
  324. $title = $page->getTitle();
  325. // translate label and title?
  326. if ($this->getUseTranslator() && $t = $this->getTranslator()) {
  327. if (is_string($label) && !empty($label)) {
  328. $label = $t->translate($label);
  329. }
  330. if (is_string($title) && !empty($title)) {
  331. $title = $t->translate($title);
  332. }
  333. }
  334. // get attribs for element
  335. $attribs = array(
  336. 'id' => $page->getId(),
  337. 'title' => $title,
  338. );
  339. if (false === $this->getAddPageClassToLi()) {
  340. $attribs['class'] = $page->getClass();
  341. }
  342. // does page have a href?
  343. if ($href = $page->getHref()) {
  344. $element = 'a';
  345. $attribs['href'] = $href;
  346. $attribs['target'] = $page->getTarget();
  347. $attribs['accesskey'] = $page->getAccessKey();
  348. } else {
  349. $element = 'span';
  350. }
  351. // Add custom HTML attributes
  352. $attribs = array_merge($attribs, $page->getCustomHtmlAttribs());
  353. return '<' . $element . $this->_htmlAttribs($attribs) . '>'
  354. . $this->view->escape($label)
  355. . '</' . $element . '>';
  356. }
  357. /**
  358. * Normalizes given render options
  359. *
  360. * @param array $options [optional] options to normalize
  361. * @return array normalized options
  362. */
  363. protected function _normalizeOptions(array $options = array())
  364. {
  365. // Ident
  366. if (isset($options['indent'])) {
  367. $options['indent'] = $this->_getWhitespace($options['indent']);
  368. } else {
  369. $options['indent'] = $this->getIndent();
  370. }
  371. // UL class
  372. if (isset($options['ulClass']) && $options['ulClass'] !== null) {
  373. $options['ulClass'] = (string) $options['ulClass'];
  374. } else {
  375. $options['ulClass'] = $this->getUlClass();
  376. }
  377. // UL id
  378. if (isset($options['ulId']) && $options['ulId'] !== null) {
  379. $options['ulId'] = (string) $options['ulId'];
  380. } else {
  381. $options['ulId'] = $this->getUlId();
  382. }
  383. // Active class
  384. if (isset($options['activeClass']) && $options['activeClass'] !== null
  385. ) {
  386. $options['activeClass'] = (string) $options['activeClass'];
  387. } else {
  388. $options['activeClass'] = $this->getActiveClass();
  389. }
  390. // Minimum depth
  391. if (array_key_exists('minDepth', $options)) {
  392. if (null !== $options['minDepth']) {
  393. $options['minDepth'] = (int) $options['minDepth'];
  394. }
  395. } else {
  396. $options['minDepth'] = $this->getMinDepth();
  397. }
  398. if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
  399. $options['minDepth'] = 0;
  400. }
  401. // Maximum depth
  402. if (array_key_exists('maxDepth', $options)) {
  403. if (null !== $options['maxDepth']) {
  404. $options['maxDepth'] = (int) $options['maxDepth'];
  405. }
  406. } else {
  407. $options['maxDepth'] = $this->getMaxDepth();
  408. }
  409. // Only active branch
  410. if (!isset($options['onlyActiveBranch'])) {
  411. $options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
  412. }
  413. // Expand sibling nodes of active branch
  414. if (!isset($options['expandSiblingNodesOfActiveBranch'])) {
  415. $options['expandSiblingNodesOfActiveBranch'] = $this->getExpandSiblingNodesOfActiveBranch();
  416. }
  417. // Render parents
  418. if (!isset($options['renderParents'])) {
  419. $options['renderParents'] = $this->getRenderParents();
  420. }
  421. // Add page CSS class to LI element
  422. if (!isset($options['addPageClassToLi'])) {
  423. $options['addPageClassToLi'] = $this->getAddPageClassToLi();
  424. }
  425. return $options;
  426. }
  427. // Render methods:
  428. /**
  429. * Renders the deepest active menu within [$minDepth, $maxDeth], (called
  430. * from {@link renderMenu()})
  431. *
  432. * @param Zend_Navigation_Container $container container to render
  433. * @param string $ulClass CSS class for first UL
  434. * @param string $indent initial indentation
  435. * @param int|null $minDepth minimum depth
  436. * @param int|null $maxDepth maximum depth
  437. * @param string|null $ulId unique identifier (id) for
  438. * first UL
  439. * @param bool $addPageClassToLi adds CSS class from
  440. * page to li element
  441. * @param string|null $activeClass CSS class for active
  442. * element
  443. * @return string rendered menu (HTML)
  444. */
  445. protected function _renderDeepestMenu(Zend_Navigation_Container $container,
  446. $ulClass,
  447. $indent,
  448. $minDepth,
  449. $maxDepth,
  450. $ulId,
  451. $addPageClassToLi,
  452. $activeClass)
  453. {
  454. if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
  455. return '';
  456. }
  457. // special case if active page is one below minDepth
  458. if ($active['depth'] < $minDepth) {
  459. if (!$active['page']->hasPages()) {
  460. return '';
  461. }
  462. } else if (!$active['page']->hasPages()) {
  463. // found pages has no children; render siblings
  464. $active['page'] = $active['page']->getParent();
  465. } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
  466. // children are below max depth; render siblings
  467. $active['page'] = $active['page']->getParent();
  468. }
  469. $attribs = array(
  470. 'class' => $ulClass,
  471. 'id' => $ulId,
  472. );
  473. // We don't need a prefix for the menu ID (backup)
  474. $skipValue = $this->_skipPrefixForId;
  475. $this->skipPrefixForId();
  476. $html = $indent . '<ul'
  477. . $this->_htmlAttribs($attribs)
  478. . '>'
  479. . self::EOL;
  480. // Reset prefix for IDs
  481. $this->_skipPrefixForId = $skipValue;
  482. foreach ($active['page'] as $subPage) {
  483. if (!$this->accept($subPage)) {
  484. continue;
  485. }
  486. $liClass = '';
  487. if ($subPage->isActive(true) && $addPageClassToLi) {
  488. $liClass = $this->_htmlAttribs(
  489. array('class' => $activeClass . ' ' . $subPage->getClass())
  490. );
  491. } else if ($subPage->isActive(true)) {
  492. $liClass = $this->_htmlAttribs(array('class' => $activeClass));
  493. } else if ($addPageClassToLi) {
  494. $liClass = $this->_htmlAttribs(
  495. array('class' => $subPage->getClass())
  496. );
  497. }
  498. $html .= $indent . ' <li' . $liClass . '>' . self::EOL;
  499. $html .= $indent . ' ' . $this->htmlify($subPage) . self::EOL;
  500. $html .= $indent . ' </li>' . self::EOL;
  501. }
  502. $html .= $indent . '</ul>';
  503. return $html;
  504. }
  505. /**
  506. * Renders a normal menu (called from {@link renderMenu()})
  507. *
  508. * @param Zend_Navigation_Container $container container to render
  509. * @param string $ulClass CSS class for first UL
  510. * @param string $indent initial indentation
  511. * @param int|null $minDepth minimum depth
  512. * @param int|null $maxDepth maximum depth
  513. * @param bool $onlyActive render only active branch?
  514. * @param bool $expandSibs render siblings of active
  515. * branch nodes?
  516. * @param string|null $ulId unique identifier (id) for
  517. * first UL
  518. * @param bool $addPageClassToLi adds CSS class from
  519. * page to li element
  520. * @param string|null $activeClass CSS class for active
  521. * element
  522. * @return string rendered menu (HTML)
  523. */
  524. protected function _renderMenu(Zend_Navigation_Container $container,
  525. $ulClass,
  526. $indent,
  527. $minDepth,
  528. $maxDepth,
  529. $onlyActive,
  530. $expandSibs,
  531. $ulId,
  532. $addPageClassToLi,
  533. $activeClass)
  534. {
  535. $html = '';
  536. // find deepest active
  537. if ($found = $this->findActive($container, $minDepth, $maxDepth)) {
  538. $foundPage = $found['page'];
  539. $foundDepth = $found['depth'];
  540. } else {
  541. $foundPage = null;
  542. }
  543. // create iterator
  544. $iterator = new RecursiveIteratorIterator($container,
  545. RecursiveIteratorIterator::SELF_FIRST);
  546. if (is_int($maxDepth)) {
  547. $iterator->setMaxDepth($maxDepth);
  548. }
  549. // iterate container
  550. $prevDepth = -1;
  551. foreach ($iterator as $page) {
  552. $depth = $iterator->getDepth();
  553. $isActive = $page->isActive(true);
  554. if ($depth < $minDepth || !$this->accept($page)) {
  555. // page is below minDepth or not accepted by acl/visibilty
  556. continue;
  557. } else if ($expandSibs && $depth > $minDepth) {
  558. // page is not active itself, but might be in the active branch
  559. $accept = false;
  560. if ($foundPage) {
  561. if ($foundPage->hasPage($page)) {
  562. // accept if page is a direct child of the active page
  563. $accept = true;
  564. } else if ($page->getParent()->isActive(true)) {
  565. // page is a sibling of the active branch...
  566. $accept = true;
  567. }
  568. }
  569. if (!$isActive && !$accept) {
  570. continue;
  571. }
  572. } else if ($onlyActive && !$isActive) {
  573. // page is not active itself, but might be in the active branch
  574. $accept = false;
  575. if ($foundPage) {
  576. if ($foundPage->hasPage($page)) {
  577. // accept if page is a direct child of the active page
  578. $accept = true;
  579. } else if ($foundPage->getParent()->hasPage($page)) {
  580. // page is a sibling of the active page...
  581. if (!$foundPage->hasPages() ||
  582. is_int($maxDepth) && $foundDepth + 1 > $maxDepth) {
  583. // accept if active page has no children, or the
  584. // children are too deep to be rendered
  585. $accept = true;
  586. }
  587. }
  588. }
  589. if (!$accept) {
  590. continue;
  591. }
  592. }
  593. // make sure indentation is correct
  594. $depth -= $minDepth;
  595. $myIndent = $indent . str_repeat(' ', $depth);
  596. if ($depth > $prevDepth) {
  597. $attribs = array();
  598. // start new ul tag
  599. if (0 == $depth) {
  600. $attribs = array(
  601. 'class' => $ulClass,
  602. 'id' => $ulId,
  603. );
  604. }
  605. // We don't need a prefix for the menu ID (backup)
  606. $skipValue = $this->_skipPrefixForId;
  607. $this->skipPrefixForId();
  608. $html .= $myIndent . '<ul'
  609. . $this->_htmlAttribs($attribs)
  610. . '>'
  611. . self::EOL;
  612. // Reset prefix for IDs
  613. $this->_skipPrefixForId = $skipValue;
  614. } else if ($prevDepth > $depth) {
  615. // close li/ul tags until we're at current depth
  616. for ($i = $prevDepth; $i > $depth; $i--) {
  617. $ind = $indent . str_repeat(' ', $i);
  618. $html .= $ind . ' </li>' . self::EOL;
  619. $html .= $ind . '</ul>' . self::EOL;
  620. }
  621. // close previous li tag
  622. $html .= $myIndent . ' </li>' . self::EOL;
  623. } else {
  624. // close previous li tag
  625. $html .= $myIndent . ' </li>' . self::EOL;
  626. }
  627. // render li tag and page
  628. $liClass = '';
  629. if ($isActive && $addPageClassToLi) {
  630. $liClass = $this->_htmlAttribs(
  631. array('class' => $activeClass . ' ' . $page->getClass())
  632. );
  633. } else if ($isActive) {
  634. $liClass = $this->_htmlAttribs(array('class' => $activeClass));
  635. } else if ($addPageClassToLi) {
  636. $liClass = $this->_htmlAttribs(
  637. array('class' => $page->getClass())
  638. );
  639. }
  640. $html .= $myIndent . ' <li' . $liClass . '>' . self::EOL
  641. . $myIndent . ' ' . $this->htmlify($page) . self::EOL;
  642. // store as previous depth for next iteration
  643. $prevDepth = $depth;
  644. }
  645. if ($html) {
  646. // done iterating container; close open ul/li tags
  647. for ($i = $prevDepth+1; $i > 0; $i--) {
  648. $myIndent = $indent . str_repeat(' ', $i-1);
  649. $html .= $myIndent . ' </li>' . self::EOL
  650. . $myIndent . '</ul>' . self::EOL;
  651. }
  652. $html = rtrim($html, self::EOL);
  653. }
  654. return $html;
  655. }
  656. /**
  657. * Renders helper
  658. *
  659. * Renders a HTML 'ul' for the given $container. If $container is not given,
  660. * the container registered in the helper will be used.
  661. *
  662. * Available $options:
  663. *
  664. *
  665. * @param Zend_Navigation_Container $container [optional] container to
  666. * create menu from. Default
  667. * is to use the container
  668. * retrieved from
  669. * {@link getContainer()}.
  670. * @param array $options [optional] options for
  671. * controlling rendering
  672. * @return string rendered menu
  673. */
  674. public function renderMenu(Zend_Navigation_Container $container = null,
  675. array $options = array())
  676. {
  677. if (null === $container) {
  678. $container = $this->getContainer();
  679. }
  680. $options = $this->_normalizeOptions($options);
  681. if ($options['onlyActiveBranch'] && !$options['renderParents']) {
  682. $html = $this->_renderDeepestMenu(
  683. $container,
  684. $options['ulClass'],
  685. $options['indent'],
  686. $options['minDepth'],
  687. $options['maxDepth'],
  688. $options['ulId'],
  689. $options['addPageClassToLi'],
  690. $options['activeClass']
  691. );
  692. } else {
  693. $html = $this->_renderMenu(
  694. $container,
  695. $options['ulClass'],
  696. $options['indent'],
  697. $options['minDepth'],
  698. $options['maxDepth'],
  699. $options['onlyActiveBranch'],
  700. $options['expandSiblingNodesOfActiveBranch'],
  701. $options['ulId'],
  702. $options['addPageClassToLi'],
  703. $options['activeClass']
  704. );
  705. }
  706. return $html;
  707. }
  708. /**
  709. * Renders the inner-most sub menu for the active page in the $container
  710. *
  711. * This is a convenience method which is equivalent to the following call:
  712. * <code>
  713. * renderMenu($container, array(
  714. * 'indent' => $indent,
  715. * 'ulClass' => $ulClass,
  716. * 'minDepth' => null,
  717. * 'maxDepth' => null,
  718. * 'onlyActiveBranch' => true,
  719. * 'renderParents' => false
  720. * ));
  721. * </code>
  722. *
  723. * @param Zend_Navigation_Container $container [optional] container to
  724. * render. Default is to render
  725. * the container registered in
  726. * the helper.
  727. * @param string|null $ulClass [optional] CSS class to
  728. * use for UL element. Default
  729. * is to use the value from
  730. * {@link getUlClass()}.
  731. * @param string|int $indent [optional] indentation as
  732. * a string or number of
  733. * spaces. Default is to use
  734. * the value retrieved from
  735. * {@link getIndent()}.
  736. * @param string|null $ulId [optional] Unique identifier
  737. * (id) use for UL element
  738. * @param bool $addPageClassToLi adds CSS class from
  739. * page to li element
  740. * @return string rendered content
  741. */
  742. public function renderSubMenu(Zend_Navigation_Container $container = null,
  743. $ulClass = null,
  744. $indent = null,
  745. $ulId = null,
  746. $addPageClassToLi = false)
  747. {
  748. return $this->renderMenu($container, array(
  749. 'indent' => $indent,
  750. 'ulClass' => $ulClass,
  751. 'minDepth' => null,
  752. 'maxDepth' => null,
  753. 'onlyActiveBranch' => true,
  754. 'renderParents' => false,
  755. 'ulId' => $ulId,
  756. 'addPageClassToLi' => $addPageClassToLi,
  757. ));
  758. }
  759. /**
  760. * Renders the given $container by invoking the partial view helper
  761. *
  762. * The container will simply be passed on as a model to the view script
  763. * as-is, and will be available in the partial script as 'container', e.g.
  764. * <code>echo 'Number of pages: ', count($this->container);</code>.
  765. *
  766. * @param Zend_Navigation_Container $container [optional] container to
  767. * pass to view script. Default
  768. * is to use the container
  769. * registered in the helper.
  770. * @param string|array $partial [optional] partial view
  771. * script to use. Default is to
  772. * use the partial registered
  773. * in the helper. If an array
  774. * is given, it is expected to
  775. * contain two values; the
  776. * partial view script to use,
  777. * and the module where the
  778. * script can be found.
  779. * @return string helper output
  780. *
  781. * @throws Zend_View_Exception When no partial script is set
  782. */
  783. public function renderPartial(Zend_Navigation_Container $container = null,
  784. $partial = null)
  785. {
  786. if (null === $container) {
  787. $container = $this->getContainer();
  788. }
  789. if (null === $partial) {
  790. $partial = $this->getPartial();
  791. }
  792. if (empty($partial)) {
  793. require_once 'Zend/View/Exception.php';
  794. $e = new Zend_View_Exception(
  795. 'Unable to render menu: No partial view script provided'
  796. );
  797. $e->setView($this->view);
  798. throw $e;
  799. }
  800. $model = array(
  801. 'container' => $container
  802. );
  803. if (is_array($partial)) {
  804. if (count($partial) != 2) {
  805. require_once 'Zend/View/Exception.php';
  806. $e = new Zend_View_Exception(
  807. 'Unable to render menu: A view partial supplied as '
  808. . 'an array must contain two values: partial view '
  809. . 'script and module where script can be found'
  810. );
  811. $e->setView($this->view);
  812. throw $e;
  813. }
  814. return $this->view->partial($partial[0], $partial[1], $model);
  815. }
  816. return $this->view->partial($partial, null, $model);
  817. }
  818. // Zend_View_Helper_Navigation_Helper:
  819. /**
  820. * Renders menu
  821. *
  822. * Implements {@link Zend_View_Helper_Navigation_Helper::render()}.
  823. *
  824. * If a partial view is registered in the helper, the menu will be rendered
  825. * using the given partial script. If no partial is registered, the menu
  826. * will be rendered as an 'ul' element by the helper's internal method.
  827. *
  828. * @see renderPartial()
  829. * @see renderMenu()
  830. *
  831. * @param Zend_Navigation_Container $container [optional] container to
  832. * render. Default is to
  833. * render the container
  834. * registered in the helper.
  835. * @return string helper output
  836. */
  837. public function render(Zend_Navigation_Container $container = null)
  838. {
  839. if ($partial = $this->getPartial()) {
  840. return $this->renderPartial($container, $partial);
  841. } else {
  842. return $this->renderMenu($container);
  843. }
  844. }
  845. }