Menu.php 31 KB

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