2
0

Menu.php 23 KB

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