AbstractPage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ gqU319I5SL3rzMnPNJlid4+Yb423KEyBpX2fP/xILFU=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. namespace Cube\Navigation\Page;
  13. use Cube\Navigation\AbstractContainer,
  14. Cube\Controller\Front,
  15. Cube\Translate,
  16. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  17. Cube\Permissions;
  18. abstract class AbstractPage extends AbstractContainer
  19. {
  20. const URI_DELIMITER = '/';
  21. /**
  22. *
  23. * page label
  24. *
  25. * @var string
  26. */
  27. protected $_label;
  28. /**
  29. *
  30. * page id
  31. *
  32. * @var string
  33. */
  34. protected $_id;
  35. /**
  36. *
  37. * page order
  38. *
  39. * @var int
  40. */
  41. protected $_order = null;
  42. /**
  43. *
  44. * active page flag
  45. *
  46. * @var bool
  47. */
  48. protected $_active;
  49. /**
  50. *
  51. * ACL resource associated with the page
  52. *
  53. * @var \Cube\Permissions\ResourceInterface
  54. */
  55. protected $_resource;
  56. /**
  57. *
  58. * ACL privilege associated with the page
  59. *
  60. * @var string|null
  61. */
  62. protected $_privilege;
  63. /**
  64. *
  65. * custom page attributes (eg: icon)
  66. *
  67. * @var array
  68. */
  69. protected $_attributes = array();
  70. /**
  71. *
  72. * the parent of the page
  73. *
  74. * @var \Cube\Navigation\AbstractContainer
  75. */
  76. protected $_parent;
  77. /**
  78. *
  79. * translate adapter
  80. *
  81. * @var \Cube\Translate\Adapter\AbstractAdapter
  82. */
  83. protected $_translate;
  84. /**
  85. *
  86. * creates a new page object
  87. * to create MVC pages, at least one of the 'controller', 'action' or 'params' keys needs to be set
  88. *
  89. * @param array $options
  90. *
  91. * @return \Cube\Navigation\Page\AbstractPage
  92. * @throws \RuntimeException
  93. */
  94. public static function factory($options = null)
  95. {
  96. if (isset($options['className'])) {
  97. $className = $options['className'];
  98. unset($options['className']);
  99. if (class_exists($className)) {
  100. $page = new $className($options);
  101. if (!$page instanceof AbstractPage) {
  102. throw new \RuntimeException("The page object must be an instance of \Cube\Navigation\Page\AbstractPage.");
  103. }
  104. else {
  105. return $page;
  106. }
  107. }
  108. else {
  109. throw new \RuntimeException(
  110. printf("Could not create page object. Class '%s' does not exist.", $options['type']));
  111. }
  112. }
  113. else if (isset($options['uri'])) {
  114. return new Uri($options);
  115. }
  116. else if (isset($options['controller']) || isset($options['action']) || isset($options['params'])) {
  117. return new Mvc($options);
  118. }
  119. else {
  120. return false;
  121. //// throw new \InvalidArgumentException(
  122. //// sprintf("Could not create navigation page, the 'uri' or
  123. //// 'controller' and 'action' parameters are required to
  124. //// create a page object, provided: %s.", serialize($options)));
  125. }
  126. }
  127. /**
  128. *
  129. * class constructor
  130. *
  131. * @param array $options
  132. */
  133. public function __construct($options = null)
  134. {
  135. if (is_array($options)) {
  136. $this->setOptions($options);
  137. }
  138. }
  139. /**
  140. *
  141. * get page label
  142. *
  143. * @return string
  144. */
  145. public function getLabel()
  146. {
  147. $translate = $this->getTranslate();
  148. if (null !== $translate) {
  149. return $translate->_($this->_label);
  150. }
  151. return $this->_label;
  152. }
  153. /**
  154. *
  155. * set page label
  156. *
  157. * @param string $label
  158. *
  159. * @return $this
  160. */
  161. public function setLabel($label)
  162. {
  163. $this->_label = $label;
  164. return $this;
  165. }
  166. /**
  167. *
  168. * get page id
  169. *
  170. * @return string
  171. */
  172. public function getId()
  173. {
  174. return $this->_id;
  175. }
  176. /**
  177. *
  178. * set page id
  179. *
  180. * @param string $id
  181. *
  182. * @return \Cube\Navigation\Page\AbstractPage
  183. * @throws \InvalidArgumentException
  184. */
  185. public function setId($id = null)
  186. {
  187. if (!is_string($id) && !is_numeric($id) && $id === null) {
  188. throw new \InvalidArgumentException(
  189. sprintf("The page id must be a string, numeric or null, %s given.", gettype($id)));
  190. }
  191. $this->_id = $id;
  192. return $this;
  193. }
  194. /**
  195. *
  196. * get page order in pages array
  197. *
  198. * @return string|null
  199. */
  200. public function getOrder()
  201. {
  202. return $this->_order;
  203. }
  204. /**
  205. *
  206. * set page order
  207. *
  208. * @param int $order
  209. */
  210. public function setOrder($order)
  211. {
  212. $this->_order = $order;
  213. }
  214. /**
  215. *
  216. * get page active status
  217. * if recursive, check children as well and if a child is active then the page is active as well
  218. *
  219. * @param bool $recursive
  220. *
  221. * @return bool
  222. */
  223. public function isActive($recursive = false)
  224. {
  225. if (!$this->_active && $recursive) {
  226. /** @var \Cube\Navigation\Page\AbstractPage $page */
  227. foreach ($this->_pages as $page) {
  228. if ($page->isActive(true)) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. return $this->_active;
  235. }
  236. /**
  237. *
  238. * set active status
  239. *
  240. * @param bool $active
  241. *
  242. * @return \Cube\Navigation\Page\AbstractPage
  243. */
  244. public function setActive($active = true)
  245. {
  246. $this->_active = (bool)$active;
  247. return $this;
  248. }
  249. /**
  250. *
  251. * get ACL resource corresponding to the page
  252. *
  253. * @return \Cube\Permissions\ResourceInterface
  254. */
  255. public function getResource()
  256. {
  257. return $this->_resource;
  258. }
  259. /**
  260. *
  261. * set ACL resource
  262. *
  263. * @param \Cube\Permissions\ResourceInterface $resource
  264. *
  265. * @return \Cube\Navigation\Page\AbstractPage
  266. */
  267. public function setResource($resource)
  268. {
  269. if ($resource instanceof Permissions\ResourceInterface) {
  270. $this->_resource = $resource;
  271. }
  272. else if (is_string($resource)) {
  273. $this->_resource = new Permissions\Resource($resource);
  274. }
  275. return $this;
  276. }
  277. /**
  278. *
  279. * get ACL privilege corresponding to this page
  280. *
  281. * @return string|null
  282. */
  283. public function getPrivilege()
  284. {
  285. return $this->_privilege;
  286. }
  287. /**
  288. *
  289. * set ACL privilege
  290. *
  291. * @param string $privilege
  292. *
  293. * @return \Cube\Navigation\Page\AbstractPage
  294. */
  295. public function setPrivilege($privilege)
  296. {
  297. $this->_privilege = is_string($privilege) ? $privilege : null;
  298. return $this;
  299. }
  300. /**
  301. *
  302. * get page attributes (custom tags)
  303. *
  304. * @return array
  305. */
  306. public function getAttributes()
  307. {
  308. return $this->_attributes;
  309. }
  310. /**
  311. *
  312. * set page attributes
  313. *
  314. * @param array $attributes
  315. *
  316. * @return \Cube\Navigation\Page\AbstractPage
  317. */
  318. public function setAttributes($attributes)
  319. {
  320. $this->_attributes = $attributes;
  321. return $this;
  322. }
  323. /**
  324. *
  325. * get parent container
  326. *
  327. * @return \Cube\Navigation\AbstractContainer|null parent container or null
  328. */
  329. public function getParent()
  330. {
  331. return $this->_parent;
  332. }
  333. /**
  334. *
  335. * set parent container
  336. *
  337. * @param \Cube\Navigation\AbstractContainer $parent
  338. *
  339. * @throws \InvalidArgumentException
  340. * @return \Cube\Navigation\Page\AbstractPage
  341. */
  342. public function setParent(AbstractContainer $parent = null)
  343. {
  344. if ($parent === $this) {
  345. throw new \InvalidArgumentException('A page cannot have itself as a parent');
  346. }
  347. // return if the given parent already is parent
  348. if ($parent === $this->_parent) {
  349. return $this;
  350. }
  351. // set new parent
  352. $this->_parent = $parent;
  353. return $this;
  354. }
  355. /**
  356. *
  357. * set translate adapter
  358. *
  359. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  360. *
  361. * @return $this
  362. */
  363. public function setTranslate(TranslateAdapter $translate)
  364. {
  365. $this->_translate = $translate;
  366. return $this;
  367. }
  368. /**
  369. *
  370. * get translate adapter
  371. *
  372. * @return \Cube\Translate\Adapter\AbstractAdapter
  373. */
  374. public function getTranslate()
  375. {
  376. if (!$this->_translate instanceof TranslateAdapter) {
  377. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  378. if ($translate instanceof Translate) {
  379. $this->setTranslate(
  380. $translate->getAdapter());
  381. }
  382. }
  383. return $this->_translate;
  384. }
  385. /**
  386. *
  387. * return a unique hash code for the object
  388. *
  389. * @return string
  390. */
  391. public function hashCode()
  392. {
  393. return spl_object_hash($this);
  394. }
  395. /**
  396. *
  397. * get magic method, enables <code> echo $page->name </code>
  398. *
  399. * @param string $name
  400. *
  401. * @return mixed|null
  402. */
  403. public function get($name)
  404. {
  405. $method = 'get' . ucfirst($name);
  406. if (method_exists($this, $method)) {
  407. return $this->$method();
  408. }
  409. else if (isset($this->_attributes[$name])) {
  410. return $this->_attributes[$name];
  411. }
  412. return null;
  413. }
  414. /**
  415. *
  416. * set page attributes (magic method): enables <code>$page->name = $value</code>
  417. *
  418. * @param string $name
  419. * @param mixed $value
  420. *
  421. * @return \Cube\Navigation\Page\AbstractPage
  422. */
  423. public function set($name, $value)
  424. {
  425. $method = 'set' . ucfirst($name);
  426. if (method_exists($this, $method)) {
  427. $this->$method($value);
  428. }
  429. else {
  430. $this->_attributes[$name] = $value;
  431. }
  432. return $this;
  433. }
  434. /**
  435. *
  436. * set options array as $page->name = $value
  437. *
  438. * @param array $options
  439. *
  440. * @return \Cube\Navigation\Page\AbstractPage
  441. */
  442. public function setOptions(array $options)
  443. {
  444. foreach ($options as $key => $value) {
  445. $this->set($key, $value);
  446. }
  447. return $this;
  448. }
  449. /**
  450. *
  451. * get magic method, proxy to $this->get($name)
  452. *
  453. * @param string $name
  454. *
  455. * @return string|null
  456. */
  457. public function __get($name)
  458. {
  459. return $this->get($name);
  460. }
  461. /**
  462. *
  463. * set magic method, proxy for $this->set($name, $value) method
  464. *
  465. * @param string $name
  466. * @param string $value
  467. */
  468. public function __set($name, $value)
  469. {
  470. $this->set($name, $value);
  471. }
  472. /**
  473. *
  474. * isset magic method, enables <code>isset($this->name)</code>
  475. *
  476. * @param string $name
  477. *
  478. * @return bool
  479. */
  480. public function __isset($name)
  481. {
  482. $method = 'get' . ucfirst($name);
  483. if (method_exists($this, $method)) {
  484. return true;
  485. }
  486. return isset($this->_attributes[$name]);
  487. }
  488. /**
  489. * to string magic method, returns page label, enables <code>echo $page</code>
  490. *
  491. * @return string
  492. */
  493. public function __toString()
  494. {
  495. return $this->_label;
  496. }
  497. }