Guest User

TNG

a guest
Aug 6th, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.46 KB | None | 0 0
  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 [email protected] so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Menu.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22.  
  23. /**
  24. * @see Zend_View_Helper_Navigation_HelperAbstract
  25. */
  26. require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
  27.  
  28. /**
  29. * Helper for rendering menus from navigation containers
  30. *
  31. * @category Zend
  32. * @package Zend_View
  33. * @subpackage Helper
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_View_Helper_Navigation_Menu
  38. extends Zend_View_Helper_Navigation_HelperAbstract
  39. {
  40. /**
  41. * CSS class to use for the ul element
  42. *
  43. * @var string
  44. */
  45. protected $_ulClass = 'navigation';
  46.  
  47. /**
  48. * TNG hacking ZF 1.x to fucking pieces...
  49. *
  50. * want a way to set aria role on menu li elements because its fucking 2015 for christs sake.
  51. *
  52. * @var string
  53. */
  54. protected $_liRole = '';
  55.  
  56. /**
  57. * Workaround so I can render the damn thing twice on the same page and not collide IDs on the <a>'s
  58. * Issue arose when adopting bootstrap and rendering full page nav and collapsed nav bar
  59. *
  60. * @var string
  61. */
  62. protected $_idAlias = '';
  63.  
  64. /**
  65. * Whether only active branch should be rendered
  66. *
  67. * @var bool
  68. */
  69. protected $_onlyActiveBranch = false;
  70.  
  71. /**
  72. * Whether parents should be rendered when only rendering active branch
  73. *
  74. * @var bool
  75. */
  76. protected $_renderParents = true;
  77.  
  78. /**
  79. * Partial view script to use for rendering menu
  80. *
  81. * @var string|array
  82. */
  83. protected $_partial = null;
  84.  
  85. /**
  86. * View helper entry point:
  87. * Retrieves helper and optionally sets container to operate on
  88. *
  89. * @param Zend_Navigation_Container $container [optional] container to
  90. * operate on
  91. * @return Zend_View_Helper_Navigation_Menu fluent interface,
  92. * returns self
  93. */
  94. public function menu(Zend_Navigation_Container $container = null)
  95. {
  96. if (null !== $container) {
  97. $this->setContainer($container);
  98. }
  99.  
  100. return $this;
  101. }
  102.  
  103. // Accessors:
  104.  
  105. /**
  106. * Sets CSS class to use for the first 'ul' element when rendering
  107. *
  108. * @param string $ulClass CSS class to set
  109. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  110. */
  111. public function setUlClass($ulClass)
  112. {
  113. if (is_string($ulClass)) {
  114. $this->_ulClass = $ulClass;
  115. }
  116.  
  117. return $this;
  118. }
  119.  
  120. /**
  121. * Returns CSS class to use for the first 'ul' element when rendering
  122. *
  123. * @return string CSS class
  124. */
  125. public function getUlClass()
  126. {
  127. return $this->_ulClass;
  128. }
  129.  
  130.  
  131.  
  132. public function setLiRole($liRole)
  133. {
  134. if (is_string($liRole)) {
  135. $this->_liRole = $liRole;
  136. }
  137. return $this;
  138. }
  139. public function getLiRole()
  140. {
  141. return $this->_liRole;
  142. }
  143.  
  144. public function setIdAlias($alias)
  145. {
  146. $this->_idAlias = $alias;
  147. return $this;
  148. }
  149. public function getIdAlias()
  150. {
  151. return $this->_idAlias;
  152. }
  153.  
  154. /**
  155. * Sets a flag indicating whether only active branch should be rendered
  156. *
  157. * @param bool $flag [optional] render only active
  158. * branch. Default is true.
  159. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  160. */
  161. public function setOnlyActiveBranch($flag = true)
  162. {
  163. $this->_onlyActiveBranch = (bool) $flag;
  164. return $this;
  165. }
  166.  
  167. /**
  168. * Returns a flag indicating whether only active branch should be rendered
  169. *
  170. * By default, this value is false, meaning the entire menu will be
  171. * be rendered.
  172. *
  173. * @return bool whether only active branch should be rendered
  174. */
  175. public function getOnlyActiveBranch()
  176. {
  177. return $this->_onlyActiveBranch;
  178. }
  179.  
  180. /**
  181. * Enables/disables rendering of parents when only rendering active branch
  182. *
  183. * See {@link setOnlyActiveBranch()} for more information.
  184. *
  185. * @param bool $flag [optional] render parents when
  186. * rendering active branch.
  187. * Default is true.
  188. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  189. */
  190. public function setRenderParents($flag = true)
  191. {
  192. $this->_renderParents = (bool) $flag;
  193. return $this;
  194. }
  195.  
  196. /**
  197. * Returns flag indicating whether parents should be rendered when rendering
  198. * only the active branch
  199. *
  200. * By default, this value is true.
  201. *
  202. * @return bool whether parents should be rendered
  203. */
  204. public function getRenderParents()
  205. {
  206. return $this->_renderParents;
  207. }
  208.  
  209. /**
  210. * Sets which partial view script to use for rendering menu
  211. *
  212. * @param string|array $partial partial view script or null. If
  213. * an array is given, it is
  214. * expected to contain two values;
  215. * the partial view script to use,
  216. * and the module where the script
  217. * can be found.
  218. * @return Zend_View_Helper_Navigation_Menu fluent interface, returns self
  219. */
  220. public function setPartial($partial)
  221. {
  222. if (null === $partial || is_string($partial) || is_array($partial)) {
  223. $this->_partial = $partial;
  224. }
  225.  
  226. return $this;
  227. }
  228.  
  229. /**
  230. * Returns partial view script to use for rendering menu
  231. *
  232. * @return string|array|null
  233. */
  234. public function getPartial()
  235. {
  236. return $this->_partial;
  237. }
  238.  
  239. // Public methods:
  240.  
  241. /**
  242. * Returns an HTML string containing an 'a' element for the given page if
  243. * the page's href is not empty, and a 'span' element if it is empty
  244. *
  245. * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}.
  246. *
  247. * @param Zend_Navigation_Page $page page to generate HTML for
  248. * @return string HTML string for the given page
  249. */
  250. public function htmlify(Zend_Navigation_Page $page)
  251. {
  252. // get label and title for translating
  253. $label = $page->getLabel();
  254. $title = $page->getTitle();
  255.  
  256. // translate label and title?
  257. if ($this->getUseTranslator() && $t = $this->getTranslator()) {
  258. if (is_string($label) && !empty($label)) {
  259. $label = $t->translate($label);
  260. }
  261. if (is_string($title) && !empty($title)) {
  262. $title = $t->translate($title);
  263. }
  264. }
  265.  
  266. // get attribs for element
  267. $attribs = array(
  268. 'id' => $this->getIdAlias() . $page->getId(),
  269. 'title' => $title,
  270. 'class' => $page->getClass()
  271. );
  272.  
  273. // does page have a href?
  274. if ($href = $page->getHref()) {
  275. $element = 'a';
  276. $attribs['href'] = $href;
  277. $attribs['target'] = $page->getTarget();
  278. } else {
  279. $element = 'span';
  280. }
  281.  
  282. return '<' . $element . $this->_htmlAttribs($attribs) . '><span class="span-nav-icon"></span><span>'
  283. . str_replace(chr(32), '&nbsp;', $this->view->escape($label))
  284. . '</span></' . $element . '>';
  285. }
  286.  
  287. /**
  288. * Normalizes given render options
  289. *
  290. * @param array $options [optional] options to normalize
  291. * @return array normalized options
  292. */
  293. protected function _normalizeOptions(array $options = array())
  294. {
  295. if (isset($options['indent'])) {
  296. $options['indent'] = $this->_getWhitespace($options['indent']);
  297. } else {
  298. $options['indent'] = $this->getIndent();
  299. }
  300.  
  301. if (isset($options['ulClass']) && $options['ulClass'] !== null) {
  302. $options['ulClass'] = (string) $options['ulClass'];
  303. } else {
  304. $options['ulClass'] = $this->getUlClass();
  305. }
  306.  
  307. if (isset($options['liRole']) && $options['liRole'] !== null) {
  308. $options['liRole'] = (string) $options['liRole'];
  309. } else {
  310. $options['liRole'] = $this->getLiRole();
  311. }
  312.  
  313. if (isset($options['idAlias']) && $options['idAlias'] !== null) {
  314. $options['idAlias'] = (string) $options['idAlias'];
  315. } else {
  316. $options['idAlias'] = '';
  317. }
  318.  
  319. if (array_key_exists('minDepth', $options)) {
  320. if (null !== $options['minDepth']) {
  321. $options['minDepth'] = (int) $options['minDepth'];
  322. }
  323. } else {
  324. $options['minDepth'] = $this->getMinDepth();
  325. }
  326.  
  327. if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
  328. $options['minDepth'] = 0;
  329. }
  330.  
  331. if (array_key_exists('maxDepth', $options)) {
  332. if (null !== $options['maxDepth']) {
  333. $options['maxDepth'] = (int) $options['maxDepth'];
  334. }
  335. } else {
  336. $options['maxDepth'] = $this->getMaxDepth();
  337. }
  338.  
  339. if (!isset($options['onlyActiveBranch'])) {
  340. $options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
  341. }
  342.  
  343. if (!isset($options['renderParents'])) {
  344. $options['renderParents'] = $this->getRenderParents();
  345. }
  346.  
  347. return $options;
  348. }
  349.  
  350. // Render methods:
  351.  
  352. /**
  353. * Renders the deepest active menu within [$minDepth, $maxDeth], (called
  354. * from {@link renderMenu()})
  355. *
  356. * @param Zend_Navigation_Container $container container to render
  357. * @param array $active active page and depth
  358. * @param string $ulClass CSS class for first UL
  359. * @param string $indent initial indentation
  360. * @param int|null $minDepth minimum depth
  361. * @param int|null $maxDepth maximum depth
  362. * @return string rendered menu
  363. */
  364. protected function _renderDeepestMenu(Zend_Navigation_Container $container,
  365. $ulClass,
  366. $indent,
  367. $minDepth,
  368. $maxDepth)
  369. {
  370. if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
  371. return '';
  372. }
  373.  
  374. // special case if active page is one below minDepth
  375. if ($active['depth'] < $minDepth) {
  376. if (!$active['page']->hasPages()) {
  377. return '';
  378. }
  379. } else if (!$active['page']->hasPages()) {
  380. // found pages has no children; render siblings
  381. $active['page'] = $active['page']->getParent();
  382. } else if (is_int($maxDepth) && $active['depth'] +1 > $maxDepth) {
  383. // children are below max depth; render siblings
  384. $active['page'] = $active['page']->getParent();
  385. }
  386.  
  387. $ulClass = $ulClass ? ' class="' . $ulClass . '"' : '';
  388. $html = $indent . '<ul' . $ulClass . '>' . self::EOL;
  389.  
  390. $liRole = (! empty($this->getLiRole())) ? "role=\"{$this->getLiRole()}\"" : "";
  391.  
  392. foreach ($active['page'] as $subPage) {
  393. if (!$this->accept($subPage)) {
  394. continue;
  395. }
  396. $liClass = $subPage->isActive(true) ? ' class="active"' : '';
  397. $html .= $indent . ' <li' . $liClass . ' ' . $liRole . '>' . self::EOL;
  398. $html .= $indent . ' ' . $this->htmlify($subPage) . self::EOL;
  399. $html .= $indent . ' </li>' . self::EOL;
  400. }
  401.  
  402. $html .= $indent . '</ul>';
  403.  
  404. return $html;
  405. }
  406.  
  407. /**
  408. * Renders a normal menu (called from {@link renderMenu()})
  409. *
  410. * @param Zend_Navigation_Container $container container to render
  411. * @param string $ulClass CSS class for first UL
  412. * @param string $indent initial indentation
  413. * @param int|null $minDepth minimum depth
  414. * @param int|null $maxDepth maximum depth
  415. * @param bool $onlyActive render only active branch?
  416. * @return string
  417. */
  418. protected function _renderMenu(Zend_Navigation_Container $container,
  419. $ulClass,
  420. $indent,
  421. $minDepth,
  422. $maxDepth,
  423. $onlyActive)
  424. {
  425. $html = '';
  426.  
  427. // find deepest active
  428. if ($found = $this->findActive($container, $minDepth, $maxDepth)) {
  429. $foundPage = $found['page'];
  430. $foundDepth = $found['depth'];
  431. } else {
  432. $foundPage = null;
  433. }
  434.  
  435. // create iterator
  436. $iterator = new RecursiveIteratorIterator($container,
  437. RecursiveIteratorIterator::SELF_FIRST);
  438. if (is_int($maxDepth)) {
  439. $iterator->setMaxDepth($maxDepth);
  440. }
  441.  
  442. // iterate container
  443. $prevDepth = -1;
  444. foreach ($iterator as $page) {
  445. $depth = $iterator->getDepth();
  446. $isActive = $page->isActive(true);
  447. if ($depth < $minDepth || !$this->accept($page)) {
  448. // page is below minDepth or not accepted by acl/visibilty
  449. continue;
  450. } else if ($onlyActive && !$isActive) {
  451. // page is not active itself, but might be in the active branch
  452. $accept = false;
  453. if ($foundPage) {
  454. if ($foundPage->hasPage($page)) {
  455. // accept if page is a direct child of the active page
  456. $accept = true;
  457. } else if ($foundPage->getParent()->hasPage($page)) {
  458. // page is a sibling of the active page...
  459. if (!$foundPage->hasPages() ||
  460. is_int($maxDepth) && $foundDepth + 1 > $maxDepth) {
  461. // accept if active page has no children, or the
  462. // children are too deep to be rendered
  463. $accept = true;
  464. }
  465. }
  466. }
  467.  
  468. if (!$accept) {
  469. continue;
  470. }
  471. }
  472.  
  473. $liRole = (! empty($this->getLiRole())) ? "role=\"{$this->getLiRole()}\"" : "";
  474.  
  475.  
  476. // make sure indentation is correct
  477. $depth -= $minDepth;
  478. $myIndent = $indent . str_repeat(' ', $depth);
  479.  
  480. if ($depth > $prevDepth) {
  481. // start new ul tag
  482. if ($ulClass && $depth == 0) {
  483. $ulClass = ' class="' . $ulClass . '"';
  484. } else {
  485. $ulClass = '';
  486. }
  487. $html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
  488. } else if ($prevDepth > $depth) {
  489. // close li/ul tags until we're at current depth
  490. for ($i = $prevDepth; $i > $depth; $i--) {
  491. $ind = $indent . str_repeat(' ', $i);
  492. $html .= $ind . ' </li>' . self::EOL;
  493. $html .= $ind . '</ul>' . self::EOL;
  494. }
  495. // close previous li tag
  496. $html .= $myIndent . ' </li>' . self::EOL;
  497. } else {
  498. // close previous li tag
  499. $html .= $myIndent . ' </li>' . self::EOL;
  500. }
  501.  
  502. // render li tag and page
  503. $liClass = $isActive ? ' class="active"' : '';
  504. $html .= $myIndent . ' <li' . $liClass . ' ' . $liRole . '>' . self::EOL
  505. . $myIndent . ' ' . $this->htmlify($page) . self::EOL;
  506.  
  507. // store as previous depth for next iteration
  508. $prevDepth = $depth;
  509. }
  510.  
  511. if ($html) {
  512. // done iterating container; close open ul/li tags
  513. for ($i = $prevDepth+1; $i > 0; $i--) {
  514. $myIndent = $indent . str_repeat(' ', $i-1);
  515. $html .= $myIndent . ' </li>' . self::EOL
  516. . $myIndent . '</ul>' . self::EOL;
  517. }
  518. $html = rtrim($html, self::EOL);
  519. }
  520.  
  521. return $html;
  522. }
  523.  
  524. /**
  525. * Renders helper
  526. *
  527. * Renders a HTML 'ul' for the given $container. If $container is not given,
  528. * the container registered in the helper will be used.
  529. *
  530. * Available $options:
  531. *
  532. *
  533. * @param Zend_Navigation_Container $container [optional] container to
  534. * create menu from. Default
  535. * is to use the container
  536. * retrieved from
  537. * {@link getContainer()}.
  538. * @param array $options [optional] options for
  539. * controlling rendering
  540. * @return string rendered menu
  541. */
  542. public function renderMenu(Zend_Navigation_Container $container = null,
  543. array $options = array())
  544. {
  545. if (null === $container) {
  546. $container = $this->getContainer();
  547. }
  548.  
  549. $options = $this->_normalizeOptions($options);
  550.  
  551. $this->setLiRole($options['liRole']);
  552. $this->setIdAlias($options['idAlias']);
  553.  
  554. if ($options['onlyActiveBranch'] && !$options['renderParents']) {
  555. $html = $this->_renderDeepestMenu($container,
  556. $options['ulClass'],
  557. $options['indent'],
  558. $options['minDepth'],
  559. $options['maxDepth']);
  560. } else {
  561. $html = $this->_renderMenu($container,
  562. $options['ulClass'],
  563. $options['indent'],
  564. $options['minDepth'],
  565. $options['maxDepth'],
  566. $options['onlyActiveBranch']);
  567. }
  568.  
  569. return $html;
  570. }
  571.  
  572. /**
  573. * Renders the inner-most sub menu for the active page in the $container
  574. *
  575. * This is a convenience method which is equivalent to the following call:
  576. * <code>
  577. * renderMenu($container, array(
  578. * 'indent' => $indent,
  579. * 'ulClass' => $ulClass,
  580. * 'minDepth' => null,
  581. * 'maxDepth' => null,
  582. * 'onlyActiveBranch' => true,
  583. * 'renderParents' => false
  584. * ));
  585. * </code>
  586. *
  587. * @param Zend_Navigation_Container $container [optional] container to
  588. * render. Default is to render
  589. * the container registered in
  590. * the helper.
  591. * @param string $ulClass [optional] CSS class to
  592. * use for UL element. Default
  593. * is to use the value from
  594. * {@link getUlClass()}.
  595. * @param string|int $indent [optional] indentation as
  596. * a string or number of
  597. * spaces. Default is to use
  598. * the value retrieved from
  599. * {@link getIndent()}.
  600. * @return string rendered content
  601. */
  602. public function renderSubMenu(Zend_Navigation_Container $container = null,
  603. $ulClass = null,
  604. $indent = null)
  605. {
  606. return $this->renderMenu($container, array(
  607. 'indent' => $indent,
  608. 'ulClass' => $ulClass,
  609. 'minDepth' => null,
  610. 'maxDepth' => null,
  611. 'onlyActiveBranch' => true,
  612. 'renderParents' => false
  613. ));
  614. }
  615.  
  616. /**
  617. * Renders the given $container by invoking the partial view helper
  618. *
  619. * The container will simply be passed on as a model to the view script
  620. * as-is, and will be available in the partial script as 'container', e.g.
  621. * <code>echo 'Number of pages: ', count($this->container);</code>.
  622. *
  623. * @param Zend_Navigation_Container $container [optional] container to
  624. * pass to view script. Default
  625. * is to use the container
  626. * registered in the helper.
  627. * @param string|array $partial [optional] partial view
  628. * script to use. Default is to
  629. * use the partial registered
  630. * in the helper. If an array
  631. * is given, it is expected to
  632. * contain two values; the
  633. * partial view script to use,
  634. * and the module where the
  635. * script can be found.
  636. * @return string helper output
  637. */
  638. public function renderPartial(Zend_Navigation_Container $container = null,
  639. $partial = null)
  640. {
  641. if (null === $container) {
  642. $container = $this->getContainer();
  643. }
  644.  
  645. if (null === $partial) {
  646. $partial = $this->getPartial();
  647. }
  648.  
  649. if (empty($partial)) {
  650. require_once 'Zend/View/Exception.php';
  651. $e = new Zend_View_Exception(
  652. 'Unable to render menu: No partial view script provided'
  653. );
  654. $e->setView($this->view);
  655. throw $e;
  656. }
  657.  
  658. $model = array(
  659. 'container' => $container
  660. );
  661.  
  662. if (is_array($partial)) {
  663. if (count($partial) != 2) {
  664. require_once 'Zend/View/Exception.php';
  665. $e = new Zend_View_Exception(
  666. 'Unable to render menu: A view partial supplied as '
  667. . 'an array must contain two values: partial view '
  668. . 'script and module where script can be found'
  669. );
  670. $e->setView($this->view);
  671. throw $e;
  672. }
  673.  
  674. return $this->view->partial($partial[0], $partial[1], $model);
  675. }
  676.  
  677. return $this->view->partial($partial, null, $model);
  678. }
  679.  
  680. // Zend_View_Helper_Navigation_Helper:
  681.  
  682. /**
  683. * Renders menu
  684. *
  685. * Implements {@link Zend_View_Helper_Navigation_Helper::render()}.
  686. *
  687. * If a partial view is registered in the helper, the menu will be rendered
  688. * using the given partial script. If no partial is registered, the menu
  689. * will be rendered as an 'ul' element by the helper's internal method.
  690. *
  691. * @see renderPartial()
  692. * @see renderMenu()
  693. *
  694. * @param Zend_Navigation_Container $container [optional] container to
  695. * render. Default is to
  696. * render the container
  697. * registered in the helper.
  698. * @return string helper output
  699. */
  700. public function render(Zend_Navigation_Container $container = null)
  701. {
  702. if ($partial = $this->getPartial()) {
  703. return $this->renderPartial($container, $partial);
  704. } else {
  705. return $this->renderMenu($container);
  706. }
  707. }
  708. }
Advertisement
Add Comment
Please, Sign In to add comment