2
0

HelperAbstract.php 27 KB

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