Menu.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. return '<' . $element . $this->_htmlAttribs($attribs) . '>'
  247. . $this->view->escape($label)
  248. . '</' . $element . '>';
  249. }
  250. /**
  251. * Normalizes given render options
  252. *
  253. * @param array $options [optional] options to normalize
  254. * @return array normalized options
  255. */
  256. protected function _normalizeOptions(array $options = array())
  257. {
  258. if (isset($options['indent'])) {
  259. $options['indent'] = $this->_getWhitespace($options['indent']);
  260. } else {
  261. $options['indent'] = $this->getIndent();
  262. }
  263. if (isset($options['ulClass']) && $options['ulClass'] !== null) {
  264. $options['ulClass'] = (string) $options['ulClass'];
  265. } else {
  266. $options['ulClass'] = $this->getUlClass();
  267. }
  268. if (array_key_exists('minDepth', $options)) {
  269. if (null !== $options['minDepth']) {
  270. $options['minDepth'] = (int) $options['minDepth'];
  271. }
  272. } else {
  273. $options['minDepth'] = $this->getMinDepth();
  274. }
  275. if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
  276. $options['minDepth'] = 0;
  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. if (!isset($options['onlyActiveBranch'])) {
  286. $options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
  287. }
  288. if (!isset($options['expandSiblingNodesOfActiveBranch'])) {
  289. $options['expandSiblingNodesOfActiveBranch'] = $this->getExpandSiblingNodesOfActiveBranch();
  290. }
  291. if (!isset($options['renderParents'])) {
  292. $options['renderParents'] = $this->getRenderParents();
  293. }
  294. return $options;
  295. }
  296. // Render methods:
  297. /**
  298. * Renders the deepest active menu within [$minDepth, $maxDeth], (called
  299. * from {@link renderMenu()})
  300. *
  301. * @param Zend_Navigation_Container $container container to render
  302. * @param array $active active page and depth
  303. * @param string $ulClass CSS class for first UL
  304. * @param string $indent initial indentation
  305. * @param int|null $minDepth minimum depth
  306. * @param int|null $maxDepth maximum depth
  307. * @return string rendered menu
  308. */
  309. protected function _renderDeepestMenu(Zend_Navigation_Container $container,
  310. $ulClass,
  311. $indent,
  312. $minDepth,
  313. $maxDepth)
  314. {
  315. if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
  316. return '';
  317. }
  318. // special case if active page is one below minDepth
  319. if ($active['depth'] < $minDepth) {
  320. if (!$active['page']->hasPages()) {
  321. return '';
  322. }
  323. } else if (!$active['page']->hasPages()) {
  324. // found pages has no children; render siblings
  325. $active['page'] = $active['page']->getParent();
  326. } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
  327. // children are below max depth; render siblings
  328. $active['page'] = $active['page']->getParent();
  329. }
  330. $ulClass = $ulClass ? ' class="' . $ulClass . '"' : '';
  331. $html = $indent . '<ul' . $ulClass . '>' . self::EOL;
  332. foreach ($active['page'] as $subPage) {
  333. if (!$this->accept($subPage)) {
  334. continue;
  335. }
  336. $liClass = $subPage->isActive(true) ? ' class="active"' : '';
  337. $html .= $indent . ' <li' . $liClass . '>' . self::EOL;
  338. $html .= $indent . ' ' . $this->htmlify($subPage) . self::EOL;
  339. $html .= $indent . ' </li>' . self::EOL;
  340. }
  341. $html .= $indent . '</ul>';
  342. return $html;
  343. }
  344. /**
  345. * Renders a normal menu (called from {@link renderMenu()})
  346. *
  347. * @param Zend_Navigation_Container $container container to render
  348. * @param string $ulClass CSS class for first UL
  349. * @param string $indent initial indentation
  350. * @param int|null $minDepth minimum depth
  351. * @param int|null $maxDepth maximum depth
  352. * @param bool $onlyActive render only active branch?
  353. * @param bool $expandSibs render siblings of active branch nodes?
  354. * @return string
  355. */
  356. protected function _renderMenu(Zend_Navigation_Container $container,
  357. $ulClass,
  358. $indent,
  359. $minDepth,
  360. $maxDepth,
  361. $onlyActive,
  362. $expandSibs)
  363. {
  364. $html = '';
  365. // find deepest active
  366. if ($found = $this->findActive($container, $minDepth, $maxDepth)) {
  367. $foundPage = $found['page'];
  368. $foundDepth = $found['depth'];
  369. } else {
  370. $foundPage = null;
  371. }
  372. // create iterator
  373. $iterator = new RecursiveIteratorIterator($container,
  374. RecursiveIteratorIterator::SELF_FIRST);
  375. if (is_int($maxDepth)) {
  376. $iterator->setMaxDepth($maxDepth);
  377. }
  378. // iterate container
  379. $prevDepth = -1;
  380. foreach ($iterator as $page) {
  381. $depth = $iterator->getDepth();
  382. $isActive = $page->isActive(true);
  383. if ($depth < $minDepth || !$this->accept($page)) {
  384. // page is below minDepth or not accepted by acl/visibilty
  385. continue;
  386. } else if ($expandSibs && $depth > $minDepth) {
  387. // page is not active itself, but might be in the active branch
  388. $accept = false;
  389. if ($foundPage) {
  390. if ($foundPage->hasPage($page)) {
  391. // accept if page is a direct child of the active page
  392. $accept = true;
  393. } else if ($page->getParent()->isActive(true)) {
  394. // page is a sibling of the active branch...
  395. $accept = true;
  396. }
  397. }
  398. if (!$isActive && !$accept) {
  399. continue;
  400. }
  401. } else if ($onlyActive && !$isActive) {
  402. // page is not active itself, but might be in the active branch
  403. $accept = false;
  404. if ($foundPage) {
  405. if ($foundPage->hasPage($page)) {
  406. // accept if page is a direct child of the active page
  407. $accept = true;
  408. } else if ($foundPage->getParent()->hasPage($page)) {
  409. // page is a sibling of the active page...
  410. if (!$foundPage->hasPages() ||
  411. is_int($maxDepth) && $foundDepth + 1 > $maxDepth) {
  412. // accept if active page has no children, or the
  413. // children are too deep to be rendered
  414. $accept = true;
  415. }
  416. }
  417. }
  418. if (!$accept) {
  419. continue;
  420. }
  421. }
  422. // make sure indentation is correct
  423. $depth -= $minDepth;
  424. $myIndent = $indent . str_repeat(' ', $depth);
  425. if ($depth > $prevDepth) {
  426. // start new ul tag
  427. if ($ulClass && $depth == 0) {
  428. $ulClass = ' class="' . $ulClass . '"';
  429. } else {
  430. $ulClass = '';
  431. }
  432. $html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
  433. } else if ($prevDepth > $depth) {
  434. // close li/ul tags until we're at current depth
  435. for ($i = $prevDepth; $i > $depth; $i--) {
  436. $ind = $indent . str_repeat(' ', $i);
  437. $html .= $ind . ' </li>' . self::EOL;
  438. $html .= $ind . '</ul>' . self::EOL;
  439. }
  440. // close previous li tag
  441. $html .= $myIndent . ' </li>' . self::EOL;
  442. } else {
  443. // close previous li tag
  444. $html .= $myIndent . ' </li>' . self::EOL;
  445. }
  446. // render li tag and page
  447. $liClass = $isActive ? ' class="active"' : '';
  448. $html .= $myIndent . ' <li' . $liClass . '>' . self::EOL
  449. . $myIndent . ' ' . $this->htmlify($page) . self::EOL;
  450. // store as previous depth for next iteration
  451. $prevDepth = $depth;
  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. return $html;
  463. }
  464. /**
  465. * Renders helper
  466. *
  467. * Renders a HTML 'ul' for the given $container. If $container is not given,
  468. * the container registered in the helper will be used.
  469. *
  470. * Available $options:
  471. *
  472. *
  473. * @param Zend_Navigation_Container $container [optional] container to
  474. * create menu from. Default
  475. * is to use the container
  476. * retrieved from
  477. * {@link getContainer()}.
  478. * @param array $options [optional] options for
  479. * controlling rendering
  480. * @return string rendered menu
  481. */
  482. public function renderMenu(Zend_Navigation_Container $container = null,
  483. array $options = array())
  484. {
  485. if (null === $container) {
  486. $container = $this->getContainer();
  487. }
  488. $options = $this->_normalizeOptions($options);
  489. if ($options['onlyActiveBranch'] && !$options['renderParents']) {
  490. $html = $this->_renderDeepestMenu($container,
  491. $options['ulClass'],
  492. $options['indent'],
  493. $options['minDepth'],
  494. $options['maxDepth']);
  495. } else {
  496. $html = $this->_renderMenu($container,
  497. $options['ulClass'],
  498. $options['indent'],
  499. $options['minDepth'],
  500. $options['maxDepth'],
  501. $options['onlyActiveBranch'],
  502. $options['expandSiblingNodesOfActiveBranch']);
  503. }
  504. return $html;
  505. }
  506. /**
  507. * Renders the inner-most sub menu for the active page in the $container
  508. *
  509. * This is a convenience method which is equivalent to the following call:
  510. * <code>
  511. * renderMenu($container, array(
  512. * 'indent' => $indent,
  513. * 'ulClass' => $ulClass,
  514. * 'minDepth' => null,
  515. * 'maxDepth' => null,
  516. * 'onlyActiveBranch' => true,
  517. * 'renderParents' => false
  518. * ));
  519. * </code>
  520. *
  521. * @param Zend_Navigation_Container $container [optional] container to
  522. * render. Default is to render
  523. * the container registered in
  524. * the helper.
  525. * @param string $ulClass [optional] CSS class to
  526. * use for UL element. Default
  527. * is to use the value from
  528. * {@link getUlClass()}.
  529. * @param string|int $indent [optional] indentation as
  530. * a string or number of
  531. * spaces. Default is to use
  532. * the value retrieved from
  533. * {@link getIndent()}.
  534. * @return string rendered content
  535. */
  536. public function renderSubMenu(Zend_Navigation_Container $container = null,
  537. $ulClass = null,
  538. $indent = null)
  539. {
  540. return $this->renderMenu($container, array(
  541. 'indent' => $indent,
  542. 'ulClass' => $ulClass,
  543. 'minDepth' => null,
  544. 'maxDepth' => null,
  545. 'onlyActiveBranch' => true,
  546. 'renderParents' => false
  547. ));
  548. }
  549. /**
  550. * Renders the given $container by invoking the partial view helper
  551. *
  552. * The container will simply be passed on as a model to the view script
  553. * as-is, and will be available in the partial script as 'container', e.g.
  554. * <code>echo 'Number of pages: ', count($this->container);</code>.
  555. *
  556. * @param Zend_Navigation_Container $container [optional] container to
  557. * pass to view script. Default
  558. * is to use the container
  559. * registered in the helper.
  560. * @param string|array $partial [optional] partial view
  561. * script to use. Default is to
  562. * use the partial registered
  563. * in the helper. If an array
  564. * is given, it is expected to
  565. * contain two values; the
  566. * partial view script to use,
  567. * and the module where the
  568. * script can be found.
  569. * @return string helper output
  570. */
  571. public function renderPartial(Zend_Navigation_Container $container = null,
  572. $partial = null)
  573. {
  574. if (null === $container) {
  575. $container = $this->getContainer();
  576. }
  577. if (null === $partial) {
  578. $partial = $this->getPartial();
  579. }
  580. if (empty($partial)) {
  581. require_once 'Zend/View/Exception.php';
  582. $e = new Zend_View_Exception(
  583. 'Unable to render menu: No partial view script provided'
  584. );
  585. $e->setView($this->view);
  586. throw $e;
  587. }
  588. $model = array(
  589. 'container' => $container
  590. );
  591. if (is_array($partial)) {
  592. if (count($partial) != 2) {
  593. require_once 'Zend/View/Exception.php';
  594. $e = new Zend_View_Exception(
  595. 'Unable to render menu: A view partial supplied as '
  596. . 'an array must contain two values: partial view '
  597. . 'script and module where script can be found'
  598. );
  599. $e->setView($this->view);
  600. throw $e;
  601. }
  602. return $this->view->partial($partial[0], $partial[1], $model);
  603. }
  604. return $this->view->partial($partial, null, $model);
  605. }
  606. // Zend_View_Helper_Navigation_Helper:
  607. /**
  608. * Renders menu
  609. *
  610. * Implements {@link Zend_View_Helper_Navigation_Helper::render()}.
  611. *
  612. * If a partial view is registered in the helper, the menu will be rendered
  613. * using the given partial script. If no partial is registered, the menu
  614. * will be rendered as an 'ul' element by the helper's internal method.
  615. *
  616. * @see renderPartial()
  617. * @see renderMenu()
  618. *
  619. * @param Zend_Navigation_Container $container [optional] container to
  620. * render. Default is to
  621. * render the container
  622. * registered in the helper.
  623. * @return string helper output
  624. */
  625. public function render(Zend_Navigation_Container $container = null)
  626. {
  627. if ($partial = $this->getPartial()) {
  628. return $this->renderPartial($container, $partial);
  629. } else {
  630. return $this->renderMenu($container);
  631. }
  632. }
  633. }