PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/vendors/Zend/Navigation/Page.php

https://bitbucket.org/negge/tlklan2
PHP | 1366 lines | 592 code | 147 blank | 627 comment | 96 complexity | 12f2edf4e39cd9bf4a2b2dda133e9cfb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.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 license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Navigation
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Page.php 24879 2012-06-06 13:09:21Z adamlundrigan $
  20. */
  21. /**
  22. * @see Zend_Navigation_Container
  23. */
  24. require_once 'Zend/Navigation/Container.php';
  25. /**
  26. * Base class for Zend_Navigation_Page pages
  27. *
  28. * @category Zend
  29. * @package Zend_Navigation
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Navigation_Page extends Zend_Navigation_Container
  34. {
  35. /**
  36. * Page label
  37. *
  38. * @var string|null
  39. */
  40. protected $_label;
  41. /**
  42. * Fragment identifier (anchor identifier)
  43. *
  44. * The fragment identifier (anchor identifier) pointing to an anchor within
  45. * a resource that is subordinate to another, primary resource.
  46. * The fragment identifier introduced by a hash mark "#".
  47. * Example: http://www.example.org/foo.html#bar ("bar" is the fragment identifier)
  48. *
  49. * @link http://www.w3.org/TR/html401/intro/intro.html#fragment-uri
  50. *
  51. * @var string|null
  52. */
  53. protected $_fragment;
  54. /**
  55. * Page id
  56. *
  57. * @var string|null
  58. */
  59. protected $_id;
  60. /**
  61. * Style class for this page (CSS)
  62. *
  63. * @var string|null
  64. */
  65. protected $_class;
  66. /**
  67. * A more descriptive title for this page
  68. *
  69. * @var string|null
  70. */
  71. protected $_title;
  72. /**
  73. * This page's target
  74. *
  75. * @var string|null
  76. */
  77. protected $_target;
  78. /**
  79. * Accessibility key character
  80. *
  81. * This attribute assigns an access key to an element. An access key is a
  82. * single character from the document character set.
  83. *
  84. * @link http://www.w3.org/TR/html401/interact/forms.html#access-keys
  85. *
  86. * @var string|null
  87. */
  88. protected $_accesskey;
  89. /**
  90. * Forward links to other pages
  91. *
  92. * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
  93. *
  94. * @var array
  95. */
  96. protected $_rel = array();
  97. /**
  98. * Reverse links to other pages
  99. *
  100. * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
  101. *
  102. * @var array
  103. */
  104. protected $_rev = array();
  105. /**
  106. * Page order used by parent container
  107. *
  108. * @var int|null
  109. */
  110. protected $_order;
  111. /**
  112. * ACL resource associated with this page
  113. *
  114. * @var string|Zend_Acl_Resource_Interface|null
  115. */
  116. protected $_resource;
  117. /**
  118. * ACL privilege associated with this page
  119. *
  120. * @var string|null
  121. */
  122. protected $_privilege;
  123. /**
  124. * Whether this page should be considered active
  125. *
  126. * @var bool
  127. */
  128. protected $_active = false;
  129. /**
  130. * Whether this page should be considered visible
  131. *
  132. * @var bool
  133. */
  134. protected $_visible = true;
  135. /**
  136. * Parent container
  137. *
  138. * @var Zend_Navigation_Container|null
  139. */
  140. protected $_parent;
  141. /**
  142. * Custom page properties, used by __set(), __get() and __isset()
  143. *
  144. * @var array
  145. */
  146. protected $_properties = array();
  147. /**
  148. * Custom HTML attributes
  149. *
  150. * @var array
  151. */
  152. protected $_customHtmlAttribs = array();
  153. /**
  154. * The type of page to use when it wasn't set
  155. *
  156. * @var string
  157. */
  158. protected static $_defaultPageType;
  159. // Initialization:
  160. /**
  161. * Factory for Zend_Navigation_Page classes
  162. *
  163. * A specific type to construct can be specified by specifying the key
  164. * 'type' in $options. If type is 'uri' or 'mvc', the type will be resolved
  165. * to Zend_Navigation_Page_Uri or Zend_Navigation_Page_Mvc. Any other value
  166. * for 'type' will be considered the full name of the class to construct.
  167. * A valid custom page class must extend Zend_Navigation_Page.
  168. *
  169. * If 'type' is not given, the type of page to construct will be determined
  170. * by the following rules:
  171. * - If $options contains either of the keys 'action', 'controller',
  172. * 'module', or 'route', a Zend_Navigation_Page_Mvc page will be created.
  173. * - If $options contains the key 'uri', a Zend_Navigation_Page_Uri page
  174. * will be created.
  175. *
  176. * @param array|Zend_Config $options options used for creating page
  177. * @return Zend_Navigation_Page a page instance
  178. * @throws Zend_Navigation_Exception if $options is not array/Zend_Config
  179. * @throws Zend_Exception if 'type' is specified and
  180. * Zend_Loader is unable to load the
  181. * class
  182. * @throws Zend_Navigation_Exception if something goes wrong during
  183. * instantiation of the page
  184. * @throws Zend_Navigation_Exception if 'type' is given, and the specified
  185. * type does not extend this class
  186. * @throws Zend_Navigation_Exception if unable to determine which class
  187. * to instantiate
  188. */
  189. public static function factory($options)
  190. {
  191. if ($options instanceof Zend_Config) {
  192. $options = $options->toArray();
  193. }
  194. if (!is_array($options)) {
  195. require_once 'Zend/Navigation/Exception.php';
  196. throw new Zend_Navigation_Exception(
  197. 'Invalid argument: $options must be an array or Zend_Config');
  198. }
  199. if (isset($options['type'])) {
  200. $type = $options['type'];
  201. } elseif(self::getDefaultPageType()!= null) {
  202. $type = self::getDefaultPageType();
  203. }
  204. if(isset($type)) {
  205. if (is_string($type) && !empty($type)) {
  206. switch (strtolower($type)) {
  207. case 'mvc':
  208. $type = 'Zend_Navigation_Page_Mvc';
  209. break;
  210. case 'uri':
  211. $type = 'Zend_Navigation_Page_Uri';
  212. break;
  213. }
  214. if (!class_exists($type)) {
  215. require_once 'Zend/Loader.php';
  216. @Zend_Loader::loadClass($type);
  217. }
  218. $page = new $type($options);
  219. if (!$page instanceof Zend_Navigation_Page) {
  220. require_once 'Zend/Navigation/Exception.php';
  221. throw new Zend_Navigation_Exception(sprintf(
  222. 'Invalid argument: Detected type "%s", which ' .
  223. 'is not an instance of Zend_Navigation_Page',
  224. $type));
  225. }
  226. return $page;
  227. }
  228. }
  229. $hasUri = isset($options['uri']);
  230. $hasMvc = isset($options['action']) || isset($options['controller']) ||
  231. isset($options['module']) || isset($options['route']);
  232. if ($hasMvc) {
  233. require_once 'Zend/Navigation/Page/Mvc.php';
  234. return new Zend_Navigation_Page_Mvc($options);
  235. } elseif ($hasUri) {
  236. require_once 'Zend/Navigation/Page/Uri.php';
  237. return new Zend_Navigation_Page_Uri($options);
  238. } else {
  239. require_once 'Zend/Navigation/Exception.php';
  240. $message = 'Invalid argument: Unable to determine class to instantiate';
  241. if (isset($options['label'])) {
  242. $message .= ' (Page label: ' . $options['label'] . ')';
  243. }
  244. throw new Zend_Navigation_Exception($message);
  245. }
  246. }
  247. /**
  248. * Page constructor
  249. *
  250. * @param array|Zend_Config $options [optional] page options. Default is
  251. * null, which should set defaults.
  252. * @throws Zend_Navigation_Exception if invalid options are given
  253. */
  254. public function __construct($options = null)
  255. {
  256. if (is_array($options)) {
  257. $this->setOptions($options);
  258. } elseif ($options instanceof Zend_Config) {
  259. $this->setConfig($options);
  260. }
  261. // do custom initialization
  262. $this->_init();
  263. }
  264. /**
  265. * Initializes page (used by subclasses)
  266. *
  267. * @return void
  268. */
  269. protected function _init()
  270. {
  271. }
  272. /**
  273. * Sets page properties using a Zend_Config object
  274. *
  275. * @param Zend_Config $config config object to get properties from
  276. * @return Zend_Navigation_Page fluent interface, returns self
  277. * @throws Zend_Navigation_Exception if invalid options are given
  278. */
  279. public function setConfig(Zend_Config $config)
  280. {
  281. return $this->setOptions($config->toArray());
  282. }
  283. /**
  284. * Sets page properties using options from an associative array
  285. *
  286. * Each key in the array corresponds to the according set*() method, and
  287. * each word is separated by underscores, e.g. the option 'target'
  288. * corresponds to setTarget(), and the option 'reset_params' corresponds to
  289. * the method setResetParams().
  290. *
  291. * @param array $options associative array of options to set
  292. * @return Zend_Navigation_Page fluent interface, returns self
  293. * @throws Zend_Navigation_Exception if invalid options are given
  294. */
  295. public function setOptions(array $options)
  296. {
  297. foreach ($options as $key => $value) {
  298. $this->set($key, $value);
  299. }
  300. return $this;
  301. }
  302. // Accessors:
  303. /**
  304. * Sets page label
  305. *
  306. * @param string $label new page label
  307. * @return Zend_Navigation_Page fluent interface, returns self
  308. * @throws Zend_Navigation_Exception if empty/no string is given
  309. */
  310. public function setLabel($label)
  311. {
  312. if (null !== $label && !is_string($label)) {
  313. require_once 'Zend/Navigation/Exception.php';
  314. throw new Zend_Navigation_Exception(
  315. 'Invalid argument: $label must be a string or null');
  316. }
  317. $this->_label = $label;
  318. return $this;
  319. }
  320. /**
  321. * Returns page label
  322. *
  323. * @return string page label or null
  324. */
  325. public function getLabel()
  326. {
  327. return $this->_label;
  328. }
  329. /**
  330. * Sets a fragment identifier
  331. *
  332. * @param string $fragment new fragment identifier
  333. * @return Zend_Navigation_Page fluent interface, returns self
  334. * @throws Zend_Navigation_Exception if empty/no string is given
  335. */
  336. public function setFragment($fragment)
  337. {
  338. if (null !== $fragment && !is_string($fragment)) {
  339. require_once 'Zend/Navigation/Exception.php';
  340. throw new Zend_Navigation_Exception(
  341. 'Invalid argument: $fragment must be a string or null');
  342. }
  343. $this->_fragment = $fragment;
  344. return $this;
  345. }
  346. /**
  347. * Returns fragment identifier
  348. *
  349. * @return string|null fragment identifier
  350. */
  351. public function getFragment()
  352. {
  353. return $this->_fragment;
  354. }
  355. /**
  356. * Sets page id
  357. *
  358. * @param string|null $id [optional] id to set. Default is null,
  359. * which sets no id.
  360. * @return Zend_Navigation_Page fluent interface, returns self
  361. * @throws Zend_Navigation_Exception if not given string or null
  362. */
  363. public function setId($id = null)
  364. {
  365. if (null !== $id && !is_string($id) && !is_numeric($id)) {
  366. require_once 'Zend/Navigation/Exception.php';
  367. throw new Zend_Navigation_Exception(
  368. 'Invalid argument: $id must be a string, number or null');
  369. }
  370. $this->_id = null === $id ? $id : (string) $id;
  371. return $this;
  372. }
  373. /**
  374. * Returns page id
  375. *
  376. * @return string|null page id or null
  377. */
  378. public function getId()
  379. {
  380. return $this->_id;
  381. }
  382. /**
  383. * Sets page CSS class
  384. *
  385. * @param string|null $class [optional] CSS class to set. Default
  386. * is null, which sets no CSS class.
  387. * @return Zend_Navigation_Page fluent interface, returns self
  388. * @throws Zend_Navigation_Exception if not given string or null
  389. */
  390. public function setClass($class = null)
  391. {
  392. if (null !== $class && !is_string($class)) {
  393. require_once 'Zend/Navigation/Exception.php';
  394. throw new Zend_Navigation_Exception(
  395. 'Invalid argument: $class must be a string or null');
  396. }
  397. $this->_class = $class;
  398. return $this;
  399. }
  400. /**
  401. * Returns page class (CSS)
  402. *
  403. * @return string|null page's CSS class or null
  404. */
  405. public function getClass()
  406. {
  407. return $this->_class;
  408. }
  409. /**
  410. * Sets page title
  411. *
  412. * @param string $title [optional] page title. Default is
  413. * null, which sets no title.
  414. * @return Zend_Navigation_Page fluent interface, returns self
  415. * @throws Zend_Navigation_Exception if not given string or null
  416. */
  417. public function setTitle($title = null)
  418. {
  419. if (null !== $title && !is_string($title)) {
  420. require_once 'Zend/Navigation/Exception.php';
  421. throw new Zend_Navigation_Exception(
  422. 'Invalid argument: $title must be a non-empty string');
  423. }
  424. $this->_title = $title;
  425. return $this;
  426. }
  427. /**
  428. * Returns page title
  429. *
  430. * @return string|null page title or null
  431. */
  432. public function getTitle()
  433. {
  434. return $this->_title;
  435. }
  436. /**
  437. * Sets page target
  438. *
  439. * @param string|null $target [optional] target to set. Default is
  440. * null, which sets no target.
  441. * @return Zend_Navigation_Page fluent interface, returns self
  442. * @throws Zend_Navigation_Exception if target is not string or null
  443. */
  444. public function setTarget($target = null)
  445. {
  446. if (null !== $target && !is_string($target)) {
  447. require_once 'Zend/Navigation/Exception.php';
  448. throw new Zend_Navigation_Exception(
  449. 'Invalid argument: $target must be a string or null');
  450. }
  451. $this->_target = $target;
  452. return $this;
  453. }
  454. /**
  455. * Returns page target
  456. *
  457. * @return string|null page target or null
  458. */
  459. public function getTarget()
  460. {
  461. return $this->_target;
  462. }
  463. /**
  464. * Sets access key for this page
  465. *
  466. * @param string|null $character [optional] access key to set. Default
  467. * is null, which sets no access key.
  468. * @return Zend_Navigation_Page fluent interface, returns self
  469. * @throws Zend_Navigation_Exception if access key is not string or null or
  470. * if the string length not equal to one
  471. */
  472. public function setAccesskey($character = null)
  473. {
  474. if (null !== $character
  475. && (!is_string($character) || 1 != strlen($character)))
  476. {
  477. require_once 'Zend/Navigation/Exception.php';
  478. throw new Zend_Navigation_Exception(
  479. 'Invalid argument: $character must be a single character or null'
  480. );
  481. }
  482. $this->_accesskey = $character;
  483. return $this;
  484. }
  485. /**
  486. * Returns page access key
  487. *
  488. * @return string|null page access key or null
  489. */
  490. public function getAccesskey()
  491. {
  492. return $this->_accesskey;
  493. }
  494. /**
  495. * Sets the page's forward links to other pages
  496. *
  497. * This method expects an associative array of forward links to other pages,
  498. * where each element's key is the name of the relation (e.g. alternate,
  499. * prev, next, help, etc), and the value is a mixed value that could somehow
  500. * be considered a page.
  501. *
  502. * @param array|Zend_Config $relations [optional] an associative array of
  503. * forward links to other pages
  504. * @return Zend_Navigation_Page fluent interface, returns self
  505. */
  506. public function setRel($relations = null)
  507. {
  508. $this->_rel = array();
  509. if (null !== $relations) {
  510. if ($relations instanceof Zend_Config) {
  511. $relations = $relations->toArray();
  512. }
  513. if (!is_array($relations)) {
  514. require_once 'Zend/Navigation/Exception.php';
  515. throw new Zend_Navigation_Exception(
  516. 'Invalid argument: $relations must be an ' .
  517. 'array or an instance of Zend_Config');
  518. }
  519. foreach ($relations as $name => $relation) {
  520. if (is_string($name)) {
  521. $this->_rel[$name] = $relation;
  522. }
  523. }
  524. }
  525. return $this;
  526. }
  527. /**
  528. * Returns the page's forward links to other pages
  529. *
  530. * This method returns an associative array of forward links to other pages,
  531. * where each element's key is the name of the relation (e.g. alternate,
  532. * prev, next, help, etc), and the value is a mixed value that could somehow
  533. * be considered a page.
  534. *
  535. * @param string $relation [optional] name of relation to return. If not
  536. * given, all relations will be returned.
  537. * @return array an array of relations. If $relation is not
  538. * specified, all relations will be returned in
  539. * an associative array.
  540. */
  541. public function getRel($relation = null)
  542. {
  543. if (null !== $relation) {
  544. return isset($this->_rel[$relation]) ?
  545. $this->_rel[$relation] :
  546. null;
  547. }
  548. return $this->_rel;
  549. }
  550. /**
  551. * Sets the page's reverse links to other pages
  552. *
  553. * This method expects an associative array of reverse links to other pages,
  554. * where each element's key is the name of the relation (e.g. alternate,
  555. * prev, next, help, etc), and the value is a mixed value that could somehow
  556. * be considered a page.
  557. *
  558. * @param array|Zend_Config $relations [optional] an associative array of
  559. * reverse links to other pages
  560. * @return Zend_Navigation_Page fluent interface, returns self
  561. */
  562. public function setRev($relations = null)
  563. {
  564. $this->_rev = array();
  565. if (null !== $relations) {
  566. if ($relations instanceof Zend_Config) {
  567. $relations = $relations->toArray();
  568. }
  569. if (!is_array($relations)) {
  570. require_once 'Zend/Navigation/Exception.php';
  571. throw new Zend_Navigation_Exception(
  572. 'Invalid argument: $relations must be an ' .
  573. 'array or an instance of Zend_Config');
  574. }
  575. foreach ($relations as $name => $relation) {
  576. if (is_string($name)) {
  577. $this->_rev[$name] = $relation;
  578. }
  579. }
  580. }
  581. return $this;
  582. }
  583. /**
  584. * Returns the page's reverse links to other pages
  585. *
  586. * This method returns an associative array of forward links to other pages,
  587. * where each element's key is the name of the relation (e.g. alternate,
  588. * prev, next, help, etc), and the value is a mixed value that could somehow
  589. * be considered a page.
  590. *
  591. * @param string $relation [optional] name of relation to return. If not
  592. * given, all relations will be returned.
  593. * @return array an array of relations. If $relation is not
  594. * specified, all relations will be returned in
  595. * an associative array.
  596. */
  597. public function getRev($relation = null)
  598. {
  599. if (null !== $relation) {
  600. return isset($this->_rev[$relation]) ?
  601. $this->_rev[$relation] :
  602. null;
  603. }
  604. return $this->_rev;
  605. }
  606. /**
  607. * Sets a single custom HTML attribute
  608. *
  609. * @param string $name name of the HTML attribute
  610. * @param string|null $value value for the HTML attribute
  611. * @return Zend_Navigation_Page fluent interface, returns self
  612. * @throws Zend_Navigation_Exception if name is not string or value is
  613. * not null or a string
  614. */
  615. public function setCustomHtmlAttrib($name, $value)
  616. {
  617. if (!is_string($name)) {
  618. require_once 'Zend/Navigation/Exception.php';
  619. throw new Zend_Navigation_Exception(
  620. 'Invalid argument: $name must be a string'
  621. );
  622. }
  623. if (null !== $value && !is_string($value)) {
  624. require_once 'Zend/Navigation/Exception.php';
  625. throw new Zend_Navigation_Exception(
  626. 'Invalid argument: $value must be a string or null'
  627. );
  628. }
  629. if (null === $value && isset($this->_customHtmlAttribs[$name])) {
  630. unset($this->_customHtmlAttribs[$name]);
  631. } else {
  632. $this->_customHtmlAttribs[$name] = $value;
  633. }
  634. return $this;
  635. }
  636. /**
  637. * Returns a single custom HTML attributes by name
  638. *
  639. * @param string $name name of the HTML attribute
  640. * @return string|null value for the HTML attribute or null
  641. * @throws Zend_Navigation_Exception if name is not string
  642. */
  643. public function getCustomHtmlAttrib($name)
  644. {
  645. if (!is_string($name)) {
  646. require_once 'Zend/Navigation/Exception.php';
  647. throw new Zend_Navigation_Exception(
  648. 'Invalid argument: $name must be a string'
  649. );
  650. }
  651. if (isset($this->_customHtmlAttribs[$name])) {
  652. return $this->_customHtmlAttribs[$name];
  653. }
  654. return null;
  655. }
  656. /**
  657. * Sets multiple custom HTML attributes at once
  658. *
  659. * @param array $attribs an associative array of html attributes
  660. * @return Zend_Navigation_Page fluent interface, returns self
  661. */
  662. public function setCustomHtmlAttribs(array $attribs)
  663. {
  664. foreach ($attribs as $key => $value) {
  665. $this->setCustomHtmlAttrib($key, $value);
  666. }
  667. return $this;
  668. }
  669. /**
  670. * Returns all custom HTML attributes as an array
  671. *
  672. * @return array an array containing custom HTML attributes
  673. */
  674. public function getCustomHtmlAttribs()
  675. {
  676. return $this->_customHtmlAttribs;
  677. }
  678. /**
  679. * Removes a custom HTML attribute from the page
  680. *
  681. * @param string $name name of the custom HTML attribute
  682. * @return Zend_Navigation_Page fluent interface, returns self
  683. */
  684. public function removeCustomHtmlAttrib($name)
  685. {
  686. if (!is_string($name)) {
  687. require_once 'Zend/Navigation/Exception.php';
  688. throw new Zend_Navigation_Exception(
  689. 'Invalid argument: $name must be a string'
  690. );
  691. }
  692. if (isset($this->_customHtmlAttribs[$name])) {
  693. unset($this->_customHtmlAttribs[$name]);
  694. }
  695. }
  696. /**
  697. * Clear all custom HTML attributes
  698. *
  699. * @return Zend_Navigation_Page fluent interface, returns self
  700. */
  701. public function clearCustomHtmlAttribs()
  702. {
  703. $this->_customHtmlAttribs = array();
  704. return $this;
  705. }
  706. /**
  707. * Sets page order to use in parent container
  708. *
  709. * @param int $order [optional] page order in container.
  710. * Default is null, which sets no
  711. * specific order.
  712. * @return Zend_Navigation_Page fluent interface, returns self
  713. * @throws Zend_Navigation_Exception if order is not integer or null
  714. */
  715. public function setOrder($order = null)
  716. {
  717. if (is_string($order)) {
  718. $temp = (int) $order;
  719. if ($temp < 0 || $temp > 0 || $order == '0') {
  720. $order = $temp;
  721. }
  722. }
  723. if (null !== $order && !is_int($order)) {
  724. require_once 'Zend/Navigation/Exception.php';
  725. throw new Zend_Navigation_Exception(
  726. 'Invalid argument: $order must be an integer or null, ' .
  727. 'or a string that casts to an integer');
  728. }
  729. $this->_order = $order;
  730. // notify parent, if any
  731. if (isset($this->_parent)) {
  732. $this->_parent->notifyOrderUpdated();
  733. }
  734. return $this;
  735. }
  736. /**
  737. * Returns page order used in parent container
  738. *
  739. * @return int|null page order or null
  740. */
  741. public function getOrder()
  742. {
  743. return $this->_order;
  744. }
  745. /**
  746. * Sets ACL resource assoicated with this page
  747. *
  748. * @param string|Zend_Acl_Resource_Interface $resource [optional] resource
  749. * to associate with
  750. * page. Default is
  751. * null, which sets no
  752. * resource.
  753. * @throws Zend_Navigation_Exception if $resource if
  754. * invalid
  755. * @return Zend_Navigation_Page fluent interface,
  756. * returns self
  757. */
  758. public function setResource($resource = null)
  759. {
  760. if (null === $resource || is_string($resource) ||
  761. $resource instanceof Zend_Acl_Resource_Interface) {
  762. $this->_resource = $resource;
  763. } else {
  764. require_once 'Zend/Navigation/Exception.php';
  765. throw new Zend_Navigation_Exception(
  766. 'Invalid argument: $resource must be null, a string, ' .
  767. ' or an instance of Zend_Acl_Resource_Interface');
  768. }
  769. return $this;
  770. }
  771. /**
  772. * Returns ACL resource assoicated with this page
  773. *
  774. * @return string|Zend_Acl_Resource_Interface|null ACL resource or null
  775. */
  776. public function getResource()
  777. {
  778. return $this->_resource;
  779. }
  780. /**
  781. * Sets ACL privilege associated with this page
  782. *
  783. * @param string|null $privilege [optional] ACL privilege to associate
  784. * with this page. Default is null, which
  785. * sets no privilege.
  786. * @return Zend_Navigation_Page fluent interface, returns self
  787. */
  788. public function setPrivilege($privilege = null)
  789. {
  790. $this->_privilege = is_string($privilege) ? $privilege : null;
  791. return $this;
  792. }
  793. /**
  794. * Returns ACL privilege associated with this page
  795. *
  796. * @return string|null ACL privilege or null
  797. */
  798. public function getPrivilege()
  799. {
  800. return $this->_privilege;
  801. }
  802. /**
  803. * Sets whether page should be considered active or not
  804. *
  805. * @param bool $active [optional] whether page should be
  806. * considered active or not. Default is true.
  807. * @return Zend_Navigation_Page fluent interface, returns self
  808. */
  809. public function setActive($active = true)
  810. {
  811. $this->_active = (bool) $active;
  812. return $this;
  813. }
  814. /**
  815. * Returns whether page should be considered active or not
  816. *
  817. * @param bool $recursive [optional] whether page should be considered
  818. * active if any child pages are active. Default is
  819. * false.
  820. * @return bool whether page should be considered active
  821. */
  822. public function isActive($recursive = false)
  823. {
  824. if (!$this->_active && $recursive) {
  825. foreach ($this->_pages as $page) {
  826. if ($page->isActive(true)) {
  827. return true;
  828. }
  829. }
  830. return false;
  831. }
  832. return $this->_active;
  833. }
  834. /**
  835. * Proxy to isActive()
  836. *
  837. * @param bool $recursive [optional] whether page should be considered
  838. * active if any child pages are active. Default
  839. * is false.
  840. * @return bool whether page should be considered active
  841. */
  842. public function getActive($recursive = false)
  843. {
  844. return $this->isActive($recursive);
  845. }
  846. /**
  847. * Sets whether the page should be visible or not
  848. *
  849. * @param bool $visible [optional] whether page should be
  850. * considered visible or not. Default is true.
  851. * @return Zend_Navigation_Page fluent interface, returns self
  852. */
  853. public function setVisible($visible = true)
  854. {
  855. if (is_string($visible) && 'false' == strtolower($visible)) {
  856. $visible = false;
  857. }
  858. $this->_visible = (bool) $visible;
  859. return $this;
  860. }
  861. /**
  862. * Returns a boolean value indicating whether the page is visible
  863. *
  864. * @param bool $recursive [optional] whether page should be considered
  865. * invisible if parent is invisible. Default is
  866. * false.
  867. * @return bool whether page should be considered visible
  868. */
  869. public function isVisible($recursive = false)
  870. {
  871. if ($recursive && isset($this->_parent) &&
  872. $this->_parent instanceof Zend_Navigation_Page) {
  873. if (!$this->_parent->isVisible(true)) {
  874. return false;
  875. }
  876. }
  877. return $this->_visible;
  878. }
  879. /**
  880. * Proxy to isVisible()
  881. *
  882. * Returns a boolean value indicating whether the page is visible
  883. *
  884. * @param bool $recursive [optional] whether page should be considered
  885. * invisible if parent is invisible. Default is
  886. * false.
  887. * @return bool whether page should be considered visible
  888. */
  889. public function getVisible($recursive = false)
  890. {
  891. return $this->isVisible($recursive);
  892. }
  893. /**
  894. * Sets parent container
  895. *
  896. * @param Zend_Navigation_Container $parent [optional] new parent to set.
  897. * Default is null which will set
  898. * no parent.
  899. * @return Zend_Navigation_Page fluent interface, returns self
  900. */
  901. public function setParent(Zend_Navigation_Container $parent = null)
  902. {
  903. if ($parent === $this) {
  904. require_once 'Zend/Navigation/Exception.php';
  905. throw new Zend_Navigation_Exception(
  906. 'A page cannot have itself as a parent');
  907. }
  908. // return if the given parent already is parent
  909. if ($parent === $this->_parent) {
  910. return $this;
  911. }
  912. // remove from old parent
  913. if (null !== $this->_parent) {
  914. $this->_parent->removePage($this);
  915. }
  916. // set new parent
  917. $this->_parent = $parent;
  918. // add to parent if page and not already a child
  919. if (null !== $this->_parent && !$this->_parent->hasPage($this, false)) {
  920. $this->_parent->addPage($this);
  921. }
  922. return $this;
  923. }
  924. /**
  925. * Returns parent container
  926. *
  927. * @return Zend_Navigation_Container|null parent container or null
  928. */
  929. public function getParent()
  930. {
  931. return $this->_parent;
  932. }
  933. /**
  934. * Sets the given property
  935. *
  936. * If the given property is native (id, class, title, etc), the matching
  937. * set method will be used. Otherwise, it will be set as a custom property.
  938. *
  939. * @param string $property property name
  940. * @param mixed $value value to set
  941. * @return Zend_Navigation_Page fluent interface, returns self
  942. * @throws Zend_Navigation_Exception if property name is invalid
  943. */
  944. public function set($property, $value)
  945. {
  946. if (!is_string($property) || empty($property)) {
  947. require_once 'Zend/Navigation/Exception.php';
  948. throw new Zend_Navigation_Exception(
  949. 'Invalid argument: $property must be a non-empty string');
  950. }
  951. $method = 'set' . self::_normalizePropertyName($property);
  952. if ($method != 'setOptions' && $method != 'setConfig' &&
  953. method_exists($this, $method)) {
  954. $this->$method($value);
  955. } else {
  956. $this->_properties[$property] = $value;
  957. }
  958. return $this;
  959. }
  960. /**
  961. * Returns the value of the given property
  962. *
  963. * If the given property is native (id, class, title, etc), the matching
  964. * get method will be used. Otherwise, it will return the matching custom
  965. * property, or null if not found.
  966. *
  967. * @param string $property property name
  968. * @return mixed the property's value or null
  969. * @throws Zend_Navigation_Exception if property name is invalid
  970. */
  971. public function get($property)
  972. {
  973. if (!is_string($property) || empty($property)) {
  974. require_once 'Zend/Navigation/Exception.php';
  975. throw new Zend_Navigation_Exception(
  976. 'Invalid argument: $property must be a non-empty string');
  977. }
  978. $method = 'get' . self::_normalizePropertyName($property);
  979. if (method_exists($this, $method)) {
  980. return $this->$method();
  981. } elseif (isset($this->_properties[$property])) {
  982. return $this->_properties[$property];
  983. }
  984. return null;
  985. }
  986. // Magic overloads:
  987. /**
  988. * Sets a custom property
  989. *
  990. * Magic overload for enabling <code>$page->propname = $value</code>.
  991. *
  992. * @param string $name property name
  993. * @param mixed $value value to set
  994. * @return void
  995. * @throws Zend_Navigation_Exception if property name is invalid
  996. */
  997. public function __set($name, $value)
  998. {
  999. $this->set($name, $value);
  1000. }
  1001. /**
  1002. * Returns a property, or null if it doesn't exist
  1003. *
  1004. * Magic overload for enabling <code>$page->propname</code>.
  1005. *
  1006. * @param string $name property name
  1007. * @return mixed property value or null
  1008. * @throws Zend_Navigation_Exception if property name is invalid
  1009. */
  1010. public function __get($name)
  1011. {
  1012. return $this->get($name);
  1013. }
  1014. /**
  1015. * Checks if a property is set
  1016. *
  1017. * Magic overload for enabling <code>isset($page->propname)</code>.
  1018. *
  1019. * Returns true if the property is native (id, class, title, etc), and
  1020. * true or false if it's a custom property (depending on whether the
  1021. * property actually is set).
  1022. *
  1023. * @param string $name property name
  1024. * @return bool whether the given property exists
  1025. */
  1026. public function __isset($name)
  1027. {
  1028. $method = 'get' . self::_normalizePropertyName($name);
  1029. if (method_exists($this, $method)) {
  1030. return true;
  1031. }
  1032. return isset($this->_properties[$name]);
  1033. }
  1034. /**
  1035. * Unsets the given custom property
  1036. *
  1037. * Magic overload for enabling <code>unset($page->propname)</code>.
  1038. *
  1039. * @param string $name property name
  1040. * @return void
  1041. * @throws Zend_Navigation_Exception if the property is native
  1042. */
  1043. public function __unset($name)
  1044. {
  1045. $method = 'set' . self::_normalizePropertyName($name);
  1046. if (method_exists($this, $method)) {
  1047. require_once 'Zend/Navigation/Exception.php';
  1048. throw new Zend_Navigation_Exception(sprintf(
  1049. 'Unsetting native property "%s" is not allowed',
  1050. $name));
  1051. }
  1052. if (isset($this->_properties[$name])) {
  1053. unset($this->_properties[$name]);
  1054. }
  1055. }
  1056. /**
  1057. * Returns page label
  1058. *
  1059. * Magic overload for enabling <code>echo $page</code>.
  1060. *
  1061. * @return string page label
  1062. */
  1063. public function __toString()
  1064. {
  1065. return $this->_label;
  1066. }
  1067. // Public methods:
  1068. /**
  1069. * Adds a forward relation to the page
  1070. *
  1071. * @param string $relation relation name (e.g. alternate, glossary,
  1072. * canonical, etc)
  1073. * @param mixed $value value to set for relation
  1074. * @return Zend_Navigation_Page fluent interface, returns self
  1075. */
  1076. public function addRel($relation, $value)
  1077. {
  1078. if (is_string($relation)) {
  1079. $this->_rel[$relation] = $value;
  1080. }
  1081. return $this;
  1082. }
  1083. /**
  1084. * Adds a reverse relation to the page
  1085. *
  1086. * @param string $relation relation name (e.g. alternate, glossary,
  1087. * canonical, etc)
  1088. * @param mixed $value value to set for relation
  1089. * @return Zend_Navigation_Page fluent interface, returns self
  1090. */
  1091. public function addRev($relation, $value)
  1092. {
  1093. if (is_string($relation)) {
  1094. $this->_rev[$relation] = $value;
  1095. }
  1096. return $this;
  1097. }
  1098. /**
  1099. * Removes a forward relation from the page
  1100. *
  1101. * @param string $relation name of relation to remove
  1102. * @return Zend_Navigation_Page fluent interface, returns self
  1103. */
  1104. public function removeRel($relation)
  1105. {
  1106. if (isset($this->_rel[$relation])) {
  1107. unset($this->_rel[$relation]);
  1108. }
  1109. return $this;
  1110. }
  1111. /**
  1112. * Removes a reverse relation from the page
  1113. *
  1114. * @param string $relation name of relation to remove
  1115. * @return Zend_Navigation_Page fluent interface, returns self
  1116. */
  1117. public function removeRev($relation)
  1118. {
  1119. if (isset($this->_rev[$relation])) {
  1120. unset($this->_rev[$relation]);
  1121. }
  1122. return $this;
  1123. }
  1124. /**
  1125. * Returns an array containing the defined forward relations
  1126. *
  1127. * @return array defined forward relations
  1128. */
  1129. public function getDefinedRel()
  1130. {
  1131. return array_keys($this->_rel);
  1132. }
  1133. /**
  1134. * Returns an array containing the defined reverse relations
  1135. *
  1136. * @return array defined reverse relations
  1137. */
  1138. public function getDefinedRev()
  1139. {
  1140. return array_keys($this->_rev);
  1141. }
  1142. /**
  1143. * Returns custom properties as an array
  1144. *
  1145. * @return array an array containing custom properties
  1146. */
  1147. public function getCustomProperties()
  1148. {
  1149. return $this->_properties;
  1150. }
  1151. /**
  1152. * Returns a hash code value for the page
  1153. *
  1154. * @return string a hash code value for this page
  1155. */
  1156. public final function hashCode()
  1157. {
  1158. return spl_object_hash($this);
  1159. }
  1160. /**
  1161. * Returns an array representation of the page
  1162. *
  1163. * @return array associative array containing all page properties
  1164. */
  1165. public function toArray()
  1166. {
  1167. return array_merge(
  1168. $this->getCustomProperties(),
  1169. array(
  1170. 'label' => $this->getlabel(),
  1171. 'fragment' => $this->getFragment(),
  1172. 'id' => $this->getId(),
  1173. 'class' => $this->getClass(),
  1174. 'title' => $this->getTitle(),
  1175. 'target' => $this->getTarget(),
  1176. 'accesskey' => $this->getAccesskey(),
  1177. 'rel' => $this->getRel(),
  1178. 'rev' => $this->getRev(),
  1179. 'customHtmlAttribs' => $this->getCustomHtmlAttribs(),
  1180. 'order' => $this->getOrder(),
  1181. 'resource' => $this->getResource(),
  1182. 'privilege' => $this->getPrivilege(),
  1183. 'active' => $this->isActive(),
  1184. 'visible' => $this->isVisible(),
  1185. 'type' => get_class($this),
  1186. 'pages' => parent::toArray()
  1187. )
  1188. );
  1189. }
  1190. // Internal methods:
  1191. /**
  1192. * Normalizes a property name
  1193. *
  1194. * @param string $property property name to normalize
  1195. * @return string normalized property name
  1196. */
  1197. protected static function _normalizePropertyName($property)
  1198. {
  1199. return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
  1200. }
  1201. public static function setDefaultPageType($type = null) {
  1202. if($type !== null && !is_string($type)) {
  1203. throw new Zend_Navigation_Exception(
  1204. 'Cannot set default page type: type is no string but should be'
  1205. );
  1206. }
  1207. self::$_defaultPageType = $type;
  1208. }
  1209. public static function getDefaultPageType() {
  1210. return self::$_defaultPageType;
  1211. }
  1212. // Abstract methods:
  1213. /**
  1214. * Returns href for this page
  1215. *
  1216. * @return string the page's href
  1217. */
  1218. abstract public function getHref();
  1219. }