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

/lib/Zend/Navigation/Page.php

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