Menu.php 26 KB

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