HelperAbstract.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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-2015 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_Helper
  24. */
  25. require_once 'Zend/View/Helper/Navigation/Helper.php';
  26. /**
  27. * @see Zend_View_Helper_HtmlElement
  28. */
  29. require_once 'Zend/View/Helper/HtmlElement.php';
  30. /**
  31. * Base class for navigational helpers
  32. *
  33. * @category Zend
  34. * @package Zend_View
  35. * @subpackage Helper
  36. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_View_Helper_Navigation_HelperAbstract
  40. extends Zend_View_Helper_HtmlElement
  41. implements Zend_View_Helper_Navigation_Helper
  42. {
  43. /**
  44. * Container to operate on by default
  45. *
  46. * @var Zend_Navigation_Container
  47. */
  48. protected $_container;
  49. /**
  50. * The minimum depth a page must have to be included when rendering
  51. *
  52. * @var int
  53. */
  54. protected $_minDepth;
  55. /**
  56. * The maximum depth a page can have to be included when rendering
  57. *
  58. * @var int
  59. */
  60. protected $_maxDepth;
  61. /**
  62. * Indentation string
  63. *
  64. * @var string
  65. */
  66. protected $_indent = '';
  67. /**
  68. * Whether HTML/XML output should be formatted
  69. *
  70. * @var bool
  71. */
  72. protected $_formatOutput = true;
  73. /**
  74. * Prefix for IDs when they are normalized
  75. *
  76. * @var string|null
  77. */
  78. protected $_prefixForId = null;
  79. /**
  80. * Skip current prefix for IDs when they are normalized (flag)
  81. *
  82. * @var bool
  83. */
  84. protected $_skipPrefixForId = false;
  85. /**
  86. * Translator
  87. *
  88. * @var Zend_Translate_Adapter
  89. */
  90. protected $_translator;
  91. /**
  92. * ACL to use when iterating pages
  93. *
  94. * @var Zend_Acl
  95. */
  96. protected $_acl;
  97. /**
  98. * Wheter invisible items should be rendered by this helper
  99. *
  100. * @var bool
  101. */
  102. protected $_renderInvisible = false;
  103. /**
  104. * ACL role to use when iterating pages
  105. *
  106. * @var string|Zend_Acl_Role_Interface
  107. */
  108. protected $_role;
  109. /**
  110. * Whether translator should be used for page labels and titles
  111. *
  112. * @var bool
  113. */
  114. protected $_useTranslator = true;
  115. /**
  116. * Whether ACL should be used for filtering out pages
  117. *
  118. * @var bool
  119. */
  120. protected $_useAcl = true;
  121. /**
  122. * Default ACL to use when iterating pages if not explicitly set in the
  123. * instance by calling {@link setAcl()}
  124. *
  125. * @var Zend_Acl
  126. */
  127. protected static $_defaultAcl;
  128. /**
  129. * Default ACL role to use when iterating pages if not explicitly set in the
  130. * instance by calling {@link setRole()}
  131. *
  132. * @var string|Zend_Acl_Role_Interface
  133. */
  134. protected static $_defaultRole;
  135. // Accessors:
  136. /**
  137. * Sets navigation container the helper operates on by default
  138. *
  139. * Implements {@link Zend_View_Helper_Navigation_Interface::setContainer()}.
  140. *
  141. * @param Zend_Navigation_Container $container [optional] container
  142. * to operate on.
  143. * Default is null,
  144. * meaning container
  145. * will be reset.
  146. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  147. * returns self
  148. */
  149. public function setContainer(Zend_Navigation_Container $container = null)
  150. {
  151. $this->_container = $container;
  152. return $this;
  153. }
  154. /**
  155. * Returns the navigation container helper operates on by default
  156. *
  157. * Implements {@link Zend_View_Helper_Navigation_Interface::getContainer()}.
  158. *
  159. * If a helper is not explicitly set in this helper instance by calling
  160. * {@link setContainer()} or by passing it through the helper entry point,
  161. * this method will look in {@link Zend_Registry} for a container by using
  162. * the key 'Zend_Navigation'.
  163. *
  164. * If no container is set, and nothing is found in Zend_Registry, a new
  165. * container will be instantiated and stored in the helper.
  166. *
  167. * @return Zend_Navigation_Container navigation container
  168. */
  169. public function getContainer()
  170. {
  171. if (null === $this->_container) {
  172. // try to fetch from registry first
  173. require_once 'Zend/Registry.php';
  174. if (Zend_Registry::isRegistered('Zend_Navigation')) {
  175. $nav = Zend_Registry::get('Zend_Navigation');
  176. if ($nav instanceof Zend_Navigation_Container) {
  177. return $this->_container = $nav;
  178. }
  179. }
  180. // nothing found in registry, create new container
  181. require_once 'Zend/Navigation.php';
  182. $this->_container = new Zend_Navigation();
  183. }
  184. return $this->_container;
  185. }
  186. /**
  187. * Sets the minimum depth a page must have to be included when rendering
  188. *
  189. * @param int $minDepth [optional] minimum
  190. * depth. Default is
  191. * null, which sets
  192. * no minimum depth.
  193. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  194. * returns self
  195. */
  196. public function setMinDepth($minDepth = null)
  197. {
  198. if (null === $minDepth || is_int($minDepth)) {
  199. $this->_minDepth = $minDepth;
  200. } else {
  201. $this->_minDepth = (int) $minDepth;
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Returns minimum depth a page must have to be included when rendering
  207. *
  208. * @return int|null minimum depth or null
  209. */
  210. public function getMinDepth()
  211. {
  212. if (!is_int($this->_minDepth) || $this->_minDepth < 0) {
  213. return 0;
  214. }
  215. return $this->_minDepth;
  216. }
  217. /**
  218. * Sets the maximum depth a page can have to be included when rendering
  219. *
  220. * @param int $maxDepth [optional] maximum
  221. * depth. Default is
  222. * null, which sets no
  223. * maximum depth.
  224. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  225. * returns self
  226. */
  227. public function setMaxDepth($maxDepth = null)
  228. {
  229. if (null === $maxDepth || is_int($maxDepth)) {
  230. $this->_maxDepth = $maxDepth;
  231. } else {
  232. $this->_maxDepth = (int) $maxDepth;
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Returns maximum depth a page can have to be included when rendering
  238. *
  239. * @return int|null maximum depth or null
  240. */
  241. public function getMaxDepth()
  242. {
  243. return $this->_maxDepth;
  244. }
  245. /**
  246. * Set the indentation string for using in {@link render()}, optionally a
  247. * number of spaces to indent with
  248. *
  249. * @param string|int $indent indentation string or
  250. * number of spaces
  251. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  252. * returns self
  253. */
  254. public function setIndent($indent)
  255. {
  256. $this->_indent = $this->_getWhitespace($indent);
  257. return $this;
  258. }
  259. /**
  260. * Returns indentation (format output is respected)
  261. *
  262. * @return string indentation string or an empty string
  263. */
  264. public function getIndent()
  265. {
  266. if (false === $this->getFormatOutput()) {
  267. return '';
  268. }
  269. return $this->_indent;
  270. }
  271. /**
  272. * Returns the EOL character (format output is respected)
  273. *
  274. * @see self::EOL
  275. * @see getFormatOutput()
  276. *
  277. * @return string standard EOL charater or an empty string
  278. */
  279. public function getEOL()
  280. {
  281. if (false === $this->getFormatOutput()) {
  282. return '';
  283. }
  284. return self::EOL;
  285. }
  286. /**
  287. * Sets whether HTML/XML output should be formatted
  288. *
  289. * @param bool $formatOutput [optional] whether output
  290. * should be formatted. Default
  291. * is true.
  292. *
  293. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  294. * self
  295. */
  296. public function setFormatOutput($formatOutput = true)
  297. {
  298. $this->_formatOutput = (bool)$formatOutput;
  299. return $this;
  300. }
  301. /**
  302. * Returns whether HTML/XML output should be formatted
  303. *
  304. * @return bool whether HTML/XML output should be formatted
  305. */
  306. public function getFormatOutput()
  307. {
  308. return $this->_formatOutput;
  309. }
  310. /**
  311. * Sets prefix for IDs when they are normalized
  312. *
  313. * @param string $prefix Prefix for IDs
  314. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface, returns self
  315. */
  316. public function setPrefixForId($prefix)
  317. {
  318. if (is_string($prefix)) {
  319. $this->_prefixForId = trim($prefix);
  320. }
  321. return $this;
  322. }
  323. /**
  324. * Returns prefix for IDs when they are normalized
  325. *
  326. * @return string Prefix for
  327. */
  328. public function getPrefixForId()
  329. {
  330. if (null === $this->_prefixForId) {
  331. $prefix = get_class($this);
  332. $this->_prefixForId = strtolower(
  333. trim(substr($prefix, strrpos($prefix, '_')), '_')
  334. ) . '-';
  335. }
  336. return $this->_prefixForId;
  337. }
  338. /**
  339. * Skip the current prefix for IDs when they are normalized
  340. *
  341. * @param bool $flag
  342. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface, returns self
  343. */
  344. public function skipPrefixForId($flag = true)
  345. {
  346. $this->_skipPrefixForId = (bool) $flag;
  347. return $this;
  348. }
  349. /**
  350. * Sets translator to use in helper
  351. *
  352. * Implements {@link Zend_View_Helper_Navigation_Helper::setTranslator()}.
  353. *
  354. * @param mixed $translator [optional] translator.
  355. * Expects an object of
  356. * type
  357. * {@link Zend_Translate_Adapter}
  358. * or {@link Zend_Translate},
  359. * or null. Default is
  360. * null, which sets no
  361. * translator.
  362. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  363. * returns self
  364. */
  365. public function setTranslator($translator = null)
  366. {
  367. if (null == $translator ||
  368. $translator instanceof Zend_Translate_Adapter) {
  369. $this->_translator = $translator;
  370. } elseif ($translator instanceof Zend_Translate) {
  371. $this->_translator = $translator->getAdapter();
  372. }
  373. return $this;
  374. }
  375. /**
  376. * Returns translator used in helper
  377. *
  378. * Implements {@link Zend_View_Helper_Navigation_Helper::getTranslator()}.
  379. *
  380. * @return Zend_Translate_Adapter|null translator or null
  381. */
  382. public function getTranslator()
  383. {
  384. if (null === $this->_translator) {
  385. require_once 'Zend/Registry.php';
  386. if (Zend_Registry::isRegistered('Zend_Translate')) {
  387. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  388. }
  389. }
  390. return $this->_translator;
  391. }
  392. /**
  393. * Sets ACL to use when iterating pages
  394. *
  395. * Implements {@link Zend_View_Helper_Navigation_Helper::setAcl()}.
  396. *
  397. * @param Zend_Acl $acl [optional] ACL object.
  398. * Default is null.
  399. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  400. * returns self
  401. */
  402. public function setAcl(Zend_Acl $acl = null)
  403. {
  404. $this->_acl = $acl;
  405. return $this;
  406. }
  407. /**
  408. * Returns ACL or null if it isn't set using {@link setAcl()} or
  409. * {@link setDefaultAcl()}
  410. *
  411. * Implements {@link Zend_View_Helper_Navigation_Helper::getAcl()}.
  412. *
  413. * @return Zend_Acl|null ACL object or null
  414. */
  415. public function getAcl()
  416. {
  417. if ($this->_acl === null && self::$_defaultAcl !== null) {
  418. return self::$_defaultAcl;
  419. }
  420. return $this->_acl;
  421. }
  422. /**
  423. * Sets ACL role(s) to use when iterating pages
  424. *
  425. * Implements {@link Zend_View_Helper_Navigation_Helper::setRole()}.
  426. *
  427. * @param mixed $role [optional] role to
  428. * set. Expects a string,
  429. * an instance of type
  430. * {@link Zend_Acl_Role_Interface},
  431. * or null. Default is
  432. * null, which will set
  433. * no role.
  434. * @throws Zend_View_Exception if $role is invalid
  435. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  436. * returns self
  437. */
  438. public function setRole($role = null)
  439. {
  440. if (null === $role || is_string($role) ||
  441. $role instanceof Zend_Acl_Role_Interface) {
  442. $this->_role = $role;
  443. } else {
  444. require_once 'Zend/View/Exception.php';
  445. $e = new Zend_View_Exception(sprintf(
  446. '$role must be a string, null, or an instance of '
  447. . 'Zend_Acl_Role_Interface; %s given',
  448. gettype($role)
  449. ));
  450. $e->setView($this->view);
  451. throw $e;
  452. }
  453. return $this;
  454. }
  455. /**
  456. * Returns ACL role to use when iterating pages, or null if it isn't set
  457. * using {@link setRole()} or {@link setDefaultRole()}
  458. *
  459. * Implements {@link Zend_View_Helper_Navigation_Helper::getRole()}.
  460. *
  461. * @return string|Zend_Acl_Role_Interface|null role or null
  462. */
  463. public function getRole()
  464. {
  465. if ($this->_role === null && self::$_defaultRole !== null) {
  466. return self::$_defaultRole;
  467. }
  468. return $this->_role;
  469. }
  470. /**
  471. * Sets whether ACL should be used
  472. *
  473. * Implements {@link Zend_View_Helper_Navigation_Helper::setUseAcl()}.
  474. *
  475. * @param bool $useAcl [optional] whether ACL
  476. * should be used.
  477. * Default is true.
  478. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  479. * returns self
  480. */
  481. public function setUseAcl($useAcl = true)
  482. {
  483. $this->_useAcl = (bool) $useAcl;
  484. return $this;
  485. }
  486. /**
  487. * Returns whether ACL should be used
  488. *
  489. * Implements {@link Zend_View_Helper_Navigation_Helper::getUseAcl()}.
  490. *
  491. * @return bool whether ACL should be used
  492. */
  493. public function getUseAcl()
  494. {
  495. return $this->_useAcl;
  496. }
  497. /**
  498. * Return renderInvisible flag
  499. *
  500. * @return bool
  501. */
  502. public function getRenderInvisible()
  503. {
  504. return $this->_renderInvisible;
  505. }
  506. /**
  507. * Render invisible items?
  508. *
  509. * @param bool $renderInvisible [optional] boolean flag
  510. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface
  511. * returns self
  512. */
  513. public function setRenderInvisible($renderInvisible = true)
  514. {
  515. $this->_renderInvisible = (bool) $renderInvisible;
  516. return $this;
  517. }
  518. /**
  519. * Sets whether translator should be used
  520. *
  521. * Implements {@link Zend_View_Helper_Navigation_Helper::setUseTranslator()}.
  522. *
  523. * @param bool $useTranslator [optional] whether
  524. * translator should be
  525. * used. Default is true.
  526. * @return Zend_View_Helper_Navigation_HelperAbstract fluent interface,
  527. * returns self
  528. */
  529. public function setUseTranslator($useTranslator = true)
  530. {
  531. $this->_useTranslator = (bool) $useTranslator;
  532. return $this;
  533. }
  534. /**
  535. * Returns whether translator should be used
  536. *
  537. * Implements {@link Zend_View_Helper_Navigation_Helper::getUseTranslator()}.
  538. *
  539. * @return bool whether translator should be used
  540. */
  541. public function getUseTranslator()
  542. {
  543. return $this->_useTranslator;
  544. }
  545. // Magic overloads:
  546. /**
  547. * Magic overload: Proxy calls to the navigation container
  548. *
  549. * @param string $method method name in container
  550. * @param array $arguments [optional] arguments to pass
  551. * @return mixed returns what the container returns
  552. * @throws Zend_Navigation_Exception if method does not exist in container
  553. */
  554. public function __call($method, array $arguments = array())
  555. {
  556. return call_user_func_array(
  557. array($this->getContainer(), $method),
  558. $arguments);
  559. }
  560. /**
  561. * Magic overload: Proxy to {@link render()}.
  562. *
  563. * This method will trigger an E_USER_ERROR if rendering the helper causes
  564. * an exception to be thrown.
  565. *
  566. * Implements {@link Zend_View_Helper_Navigation_Helper::__toString()}.
  567. *
  568. * @return string
  569. */
  570. public function __toString()
  571. {
  572. try {
  573. return $this->render();
  574. } catch (Exception $e) {
  575. $msg = get_class($e) . ': ' . $e->getMessage();
  576. trigger_error($msg, E_USER_ERROR);
  577. return '';
  578. }
  579. }
  580. // Public methods:
  581. /**
  582. * Finds the deepest active page in the given container
  583. *
  584. * @param Zend_Navigation_Container $container container to search
  585. * @param int|null $minDepth [optional] minimum depth
  586. * required for page to be
  587. * valid. Default is to use
  588. * {@link getMinDepth()}. A
  589. * null value means no minimum
  590. * depth required.
  591. * @param int|null $minDepth [optional] maximum depth
  592. * a page can have to be
  593. * valid. Default is to use
  594. * {@link getMaxDepth()}. A
  595. * null value means no maximum
  596. * depth required.
  597. * @return array an associative array with
  598. * the values 'depth' and
  599. * 'page', or an empty array
  600. * if not found
  601. */
  602. public function findActive(Zend_Navigation_Container $container,
  603. $minDepth = null,
  604. $maxDepth = -1)
  605. {
  606. if (!is_int($minDepth)) {
  607. $minDepth = $this->getMinDepth();
  608. }
  609. if ((!is_int($maxDepth) || $maxDepth < 0) && null !== $maxDepth) {
  610. $maxDepth = $this->getMaxDepth();
  611. }
  612. $found = null;
  613. $foundDepth = -1;
  614. $iterator = new RecursiveIteratorIterator($container,
  615. RecursiveIteratorIterator::CHILD_FIRST);
  616. foreach ($iterator as $page) {
  617. $currDepth = $iterator->getDepth();
  618. if ($currDepth < $minDepth || !$this->accept($page)) {
  619. // page is not accepted
  620. continue;
  621. }
  622. if ($page->isActive(false) && $currDepth > $foundDepth) {
  623. // found an active page at a deeper level than before
  624. $found = $page;
  625. $foundDepth = $currDepth;
  626. }
  627. }
  628. if (is_int($maxDepth) && $foundDepth > $maxDepth) {
  629. while ($foundDepth > $maxDepth) {
  630. if (--$foundDepth < $minDepth) {
  631. $found = null;
  632. break;
  633. }
  634. $found = $found->getParent();
  635. if (!$found instanceof Zend_Navigation_Page) {
  636. $found = null;
  637. break;
  638. }
  639. }
  640. }
  641. if ($found) {
  642. return array('page' => $found, 'depth' => $foundDepth);
  643. } else {
  644. return array();
  645. }
  646. }
  647. /**
  648. * Checks if the helper has a container
  649. *
  650. * Implements {@link Zend_View_Helper_Navigation_Helper::hasContainer()}.
  651. *
  652. * @return bool whether the helper has a container or not
  653. */
  654. public function hasContainer()
  655. {
  656. return null !== $this->_container;
  657. }
  658. /**
  659. * Checks if the helper has an ACL instance
  660. *
  661. * Implements {@link Zend_View_Helper_Navigation_Helper::hasAcl()}.
  662. *
  663. * @return bool whether the helper has a an ACL instance or not
  664. */
  665. public function hasAcl()
  666. {
  667. return null !== $this->_acl;
  668. }
  669. /**
  670. * Checks if the helper has an ACL role
  671. *
  672. * Implements {@link Zend_View_Helper_Navigation_Helper::hasRole()}.
  673. *
  674. * @return bool whether the helper has a an ACL role or not
  675. */
  676. public function hasRole()
  677. {
  678. return null !== $this->_role;
  679. }
  680. /**
  681. * Checks if the helper has a translator
  682. *
  683. * Implements {@link Zend_View_Helper_Navigation_Helper::hasTranslator()}.
  684. *
  685. * @return bool whether the helper has a translator or not
  686. */
  687. public function hasTranslator()
  688. {
  689. return null !== $this->_translator;
  690. }
  691. /**
  692. * Returns an HTML string containing an 'a' element for the given page
  693. *
  694. * @param Zend_Navigation_Page $page page to generate HTML for
  695. * @return string HTML string for the given page
  696. */
  697. public function htmlify(Zend_Navigation_Page $page)
  698. {
  699. // get label and title for translating
  700. $label = $page->getLabel();
  701. $title = $page->getTitle();
  702. if ($this->getUseTranslator() && $t = $this->getTranslator()) {
  703. if (is_string($label) && !empty($label)) {
  704. $label = $t->translate($label);
  705. }
  706. if (is_string($title) && !empty($title)) {
  707. $title = $t->translate($title);
  708. }
  709. }
  710. // get attribs for anchor element
  711. $attribs = array_merge(
  712. array(
  713. 'id' => $page->getId(),
  714. 'title' => $title,
  715. 'class' => $page->getClass(),
  716. 'href' => $page->getHref(),
  717. 'target' => $page->getTarget()
  718. ),
  719. $page->getCustomHtmlAttribs()
  720. );
  721. return '<a' . $this->_htmlAttribs($attribs) . '>'
  722. . $this->view->escape($label)
  723. . '</a>';
  724. }
  725. // Iterator filter methods:
  726. /**
  727. * Determines whether a page should be accepted when iterating
  728. *
  729. * Rules:
  730. * - If a page is not visible it is not accepted, unless RenderInvisible has
  731. * been set to true.
  732. * - If helper has no ACL, page is accepted
  733. * - If helper has ACL, but no role, page is not accepted
  734. * - If helper has ACL and role:
  735. * - Page is accepted if it has no resource or privilege
  736. * - Page is accepted if ACL allows page's resource or privilege
  737. * - If page is accepted by the rules above and $recursive is true, the page
  738. * will not be accepted if it is the descendant of a non-accepted page.
  739. *
  740. * @param Zend_Navigation_Page $page page to check
  741. * @param bool $recursive [optional] if true, page will not
  742. * be accepted if it is the
  743. * descendant of a page that is not
  744. * accepted. Default is true.
  745. * @return bool whether page should be accepted
  746. */
  747. public function accept(Zend_Navigation_Page $page, $recursive = true)
  748. {
  749. // accept by default
  750. $accept = true;
  751. if (!$page->isVisible(false) && !$this->getRenderInvisible()) {
  752. // don't accept invisible pages
  753. $accept = false;
  754. } elseif ($this->getUseAcl() && !$this->_acceptAcl($page)) {
  755. // acl is not amused
  756. $accept = false;
  757. }
  758. if ($accept && $recursive) {
  759. $parent = $page->getParent();
  760. if ($parent instanceof Zend_Navigation_Page) {
  761. $accept = $this->accept($parent, true);
  762. }
  763. }
  764. return $accept;
  765. }
  766. /**
  767. * Determines whether a page should be accepted by ACL when iterating
  768. *
  769. * Rules:
  770. * - If helper has no ACL, page is accepted
  771. * - If page has a resource or privilege defined, page is accepted
  772. * if the ACL allows access to it using the helper's role
  773. * - If page has no resource or privilege, page is accepted
  774. *
  775. * @param Zend_Navigation_Page $page page to check
  776. * @return bool whether page is accepted by ACL
  777. */
  778. protected function _acceptAcl(Zend_Navigation_Page $page)
  779. {
  780. if (!$acl = $this->getAcl()) {
  781. // no acl registered means don't use acl
  782. return true;
  783. }
  784. $role = $this->getRole();
  785. $resource = $page->getResource();
  786. $privilege = $page->getPrivilege();
  787. if ($resource || $privilege) {
  788. // determine using helper role and page resource/privilege
  789. return $acl->isAllowed($role, $resource, $privilege);
  790. }
  791. return true;
  792. }
  793. // Util methods:
  794. /**
  795. * Retrieve whitespace representation of $indent
  796. *
  797. * @param int|string $indent
  798. * @return string
  799. */
  800. protected function _getWhitespace($indent)
  801. {
  802. if (is_int($indent)) {
  803. $indent = str_repeat(' ', $indent);
  804. }
  805. return (string) $indent;
  806. }
  807. /**
  808. * Converts an associative array to a string of tag attributes.
  809. *
  810. * Overloads {@link Zend_View_Helper_HtmlElement::_htmlAttribs()}.
  811. *
  812. * @param array $attribs an array where each key-value pair is converted
  813. * to an attribute name and value
  814. * @return string an attribute string
  815. */
  816. protected function _htmlAttribs($attribs)
  817. {
  818. // filter out null values and empty string values
  819. foreach ($attribs as $key => $value) {
  820. if ($value === null || (is_string($value) && !strlen($value))) {
  821. unset($attribs[$key]);
  822. }
  823. }
  824. return parent::_htmlAttribs($attribs);
  825. }
  826. /**
  827. * Normalize an ID
  828. *
  829. * Extends {@link Zend_View_Helper_HtmlElement::_normalizeId()}.
  830. *
  831. * @param string $value ID
  832. * @return string Normalized ID
  833. */
  834. protected function _normalizeId($value)
  835. {
  836. if (false === $this->_skipPrefixForId) {
  837. $prefix = $this->getPrefixForId();
  838. if (strlen($prefix)) {
  839. return $prefix . $value;
  840. }
  841. }
  842. return parent::_normalizeId($value);
  843. }
  844. // Static methods:
  845. /**
  846. * Sets default ACL to use if another ACL is not explicitly set
  847. *
  848. * @param Zend_Acl $acl [optional] ACL object. Default is null, which
  849. * sets no ACL object.
  850. * @return void
  851. */
  852. public static function setDefaultAcl(Zend_Acl $acl = null)
  853. {
  854. self::$_defaultAcl = $acl;
  855. }
  856. /**
  857. * Sets default ACL role(s) to use when iterating pages if not explicitly
  858. * set later with {@link setRole()}
  859. *
  860. * @param mixed $role [optional] role to set. Expects null,
  861. * string, or an instance of
  862. * {@link Zend_Acl_Role_Interface}.
  863. * Default is null, which sets no default
  864. * role.
  865. * @throws Zend_View_Exception if role is invalid
  866. * @return void
  867. */
  868. public static function setDefaultRole($role = null)
  869. {
  870. if (null === $role ||
  871. is_string($role) ||
  872. $role instanceof Zend_Acl_Role_Interface) {
  873. self::$_defaultRole = $role;
  874. } else {
  875. require_once 'Zend/View/Exception.php';
  876. throw new Zend_View_Exception(
  877. '$role must be null|string|Zend_Acl_Role_Interface'
  878. );
  879. }
  880. }
  881. }