HelperAbstract.php 27 KB

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