/Model/SnapshotPageProxy.php

https://github.com/youbs/SonataPageBundle · PHP · 632 lines · 275 code · 82 blank · 275 comment · 11 complexity · eb8a7e838475ab2ea4ae929f73a701a2 MD5 · raw file

  1. <?php
  2. namespace Sonata\PageBundle\Model;
  3. class SnapshotPageProxy implements PageInterface
  4. {
  5. /**
  6. * @var \Sonata\PageBundle\Model\SnapshotManagerInterface
  7. */
  8. private $manager;
  9. /**
  10. * @var \Sonata\PageBundle\Model\SnapshotInterface
  11. */
  12. private $snapshot;
  13. /**
  14. * @var \Sonata\PageBundle\Model\PageInterface
  15. */
  16. private $page;
  17. private $target;
  18. private $parents;
  19. /**
  20. * @param SnapshotManagerInterface $manager
  21. * @param SnapshotInterface $snapshot
  22. */
  23. public function __construct(SnapshotManagerInterface $manager, SnapshotInterface $snapshot)
  24. {
  25. $this->manager = $manager;
  26. $this->snapshot = $snapshot;
  27. }
  28. /**
  29. * Get the page
  30. * @return \Sonata\PageBundle\Model\PageInterface
  31. */
  32. public function getPage()
  33. {
  34. $this->load();
  35. return $this->page;
  36. }
  37. /**
  38. * load the page
  39. */
  40. private function load()
  41. {
  42. if (!$this->page) {
  43. $this->page = $this->manager->load($this->snapshot);
  44. }
  45. }
  46. /**
  47. * @param $method
  48. * @param $arguments
  49. * @return mixed
  50. */
  51. public function __call($method, $arguments)
  52. {
  53. return call_user_func_array(array($this->getPage(), $method), $arguments);
  54. }
  55. /**
  56. * Add children
  57. *
  58. * @param \Sonata\PageBundle\Model\PageInterface $children
  59. */
  60. function addChildren(PageInterface $children)
  61. {
  62. $this->getPage()->addChildren($children);
  63. }
  64. /**
  65. * @param array $headers
  66. * @return void
  67. */
  68. function setHeaders(array $headers = array())
  69. {
  70. $this->getPage()->setHeaders($headers);
  71. }
  72. /**
  73. * @param $name
  74. * @param $value
  75. * @return void
  76. */
  77. function addHeader($name, $value)
  78. {
  79. $this->getPage()->addHeader($name, $value);
  80. }
  81. /**
  82. * @return array
  83. */
  84. function getHeaders()
  85. {
  86. return $this->getPage()->getHeaders();
  87. }
  88. /**
  89. * Get children
  90. *
  91. * @return array
  92. */
  93. function getChildren()
  94. {
  95. if (!$this->getPage()->getChildren()->count()) {
  96. $this->getPage()->setChildren(new SnapshotChildrenCollection($this->manager, $this->getPage()));
  97. }
  98. return $this->getPage()->getChildren();
  99. }
  100. /**
  101. * Add blocs
  102. *
  103. * @param \Sonata\PageBundle\Model\BlockInterface $block
  104. */
  105. function addBlocks(BlockInterface $block)
  106. {
  107. $this->getPage()->addBlocks($block);
  108. }
  109. /**
  110. * Get blocs
  111. *
  112. * @return array $blocks
  113. */
  114. public function getBlocks()
  115. {
  116. if (!count($this->getPage()->getBlocks())) {
  117. $content = json_decode($this->snapshot->getContent(), true);
  118. foreach ($content['blocks'] as $block) {
  119. $this->addBlocks($this->manager->loadBlock($block, $this->getPage()));
  120. }
  121. }
  122. return $this->getPage()->getBlocks();
  123. }
  124. /**
  125. * Set target
  126. *
  127. * @param \Sonata\PageBundle\Model\PageInterface $target
  128. */
  129. function setTarget(PageInterface $target)
  130. {
  131. $this->target = $target;
  132. }
  133. /**
  134. * @return \Sonata\PageBundle\Model\PageInterface|null
  135. */
  136. public function getTarget()
  137. {
  138. if ($this->target === null) {
  139. $content = json_decode($this->snapshot->getContent(), true);
  140. if (isset($content['target_id'])) {
  141. $target = $this->manager->getPageById($content['target_id']);
  142. if ($target) {
  143. $this->setTarget($target);
  144. } else {
  145. $this->target = false;
  146. }
  147. }
  148. }
  149. return $this->target ?: null;
  150. }
  151. /**
  152. * Get parent
  153. *
  154. * @param int $level
  155. * @return null|\Sonata\PageBundle\Model\PageInterface $parent
  156. */
  157. public function getParent($level = -1)
  158. {
  159. $parents = $this->getParents();
  160. if ($level < 0) {
  161. $level = count($parents) + $level;
  162. }
  163. return isset($parents[$level]) ? $parents[$level] : null;
  164. }
  165. /**
  166. * Set parent
  167. *
  168. * @params array $parents
  169. */
  170. public function setParents(array $parents)
  171. {
  172. $this->parents = $parents;
  173. }
  174. /**
  175. * get the tree of the page
  176. *
  177. * @return array of Application\Sonata\PageBundle\Entity\Page
  178. */
  179. public function getParents()
  180. {
  181. if (!$this->parents) {
  182. $parents = array();
  183. $snapshot = $this->snapshot;
  184. while ($snapshot) {
  185. $content = json_decode($snapshot->getContent(), true);
  186. $parentId = $content['parent_id'];
  187. $snapshot = $parentId ? $this->manager->getSnapshotByPageId($parentId) : null;
  188. if ($snapshot) {
  189. $parents[] = new SnapshotPageProxy($this->manager, $snapshot);
  190. }
  191. }
  192. $this->setParents(array_reverse($parents));
  193. }
  194. return $this->parents;
  195. }
  196. /**
  197. * Set parent
  198. *
  199. * @param PageInterface $parent
  200. */
  201. function setParent(PageInterface $parent)
  202. {
  203. $this->getPage()->setParent($parent);
  204. }
  205. /**
  206. * Set template
  207. *
  208. * @param string $templateCode
  209. */
  210. function setTemplateCode($templateCode)
  211. {
  212. $this->getPage()->setTemplateCode($templateCode);
  213. }
  214. /**
  215. * Get template
  216. *
  217. * @return string $templateCode
  218. */
  219. function getTemplateCode()
  220. {
  221. return $this->getPage()->getTemplateCode();
  222. }
  223. /**
  224. * @param boolean $decorate
  225. * @return void
  226. */
  227. function setDecorate($decorate)
  228. {
  229. $this->getPage()->setDecorate($decorate);
  230. }
  231. /**
  232. * get decorate
  233. *
  234. * @return boolean $decorate
  235. */
  236. function getDecorate()
  237. {
  238. return $this->getPage()->getDecorate();
  239. }
  240. /**
  241. * @return bool
  242. */
  243. function isHybrid()
  244. {
  245. return $this->getPage()->isHybrid();
  246. }
  247. /**
  248. * @param $position
  249. * @return void
  250. */
  251. function setPosition($position)
  252. {
  253. $this->getPage()->setPosition($position);
  254. }
  255. /**
  256. * get position
  257. *
  258. * @return integer
  259. */
  260. function getPosition()
  261. {
  262. return $this->getPage()->getPosition();
  263. }
  264. /**
  265. * @param string $method
  266. * @return void
  267. */
  268. function setRequestMethod($method)
  269. {
  270. $this->getPage()->setRequestMethod($method);
  271. }
  272. /**
  273. * get request method
  274. *
  275. * @return string
  276. */
  277. function getRequestMethod()
  278. {
  279. return $this->getPage()->getRequestMethod();
  280. }
  281. /**
  282. * Get id of the page
  283. *
  284. * @return integer
  285. */
  286. function getId()
  287. {
  288. return $this->getPage()->getId();
  289. }
  290. /**
  291. * set id of the page
  292. *
  293. * @param integer $id
  294. */
  295. function setId($id)
  296. {
  297. $this->getPage()->setId($id);
  298. }
  299. /**
  300. * Get routeName
  301. *
  302. * @return string $routeName
  303. */
  304. function getRouteName()
  305. {
  306. return $this->getPage()->getRouteName();
  307. }
  308. /**
  309. * Set routeName
  310. *
  311. * @param string $routeName
  312. */
  313. function setRouteName($routeName)
  314. {
  315. $this->getPage()->setRouteName($routeName);
  316. }
  317. /**
  318. * Set enabled
  319. *
  320. * @param boolean $enabled
  321. */
  322. function setEnabled($enabled)
  323. {
  324. $this->getPage()->setEnabled($enabled);
  325. }
  326. /**
  327. * Get enabled
  328. *
  329. * @return boolean $enabled
  330. */
  331. function getEnabled()
  332. {
  333. return $this->getPage()->getEnabled();
  334. }
  335. /**
  336. * Set name
  337. *
  338. * @param string $name
  339. */
  340. function setName($name)
  341. {
  342. $this->getPage()->setName($name);
  343. }
  344. /**
  345. * Get name
  346. *
  347. * @return string $name
  348. */
  349. function getName()
  350. {
  351. return $this->getPage()->getName();
  352. }
  353. /**
  354. * Set slug
  355. *
  356. * @param string $slug
  357. */
  358. function setSlug($slug)
  359. {
  360. $this->getPage()->setSlug($slug);
  361. }
  362. /**
  363. * Get slug
  364. *
  365. * @return string
  366. */
  367. function getSlug()
  368. {
  369. return $this->getPage()->getSlug();
  370. }
  371. /**
  372. * Set Url
  373. *
  374. * @param string $url
  375. * @return void
  376. */
  377. function setUrl($url)
  378. {
  379. $this->getPage()->setUrl($url);
  380. }
  381. /**
  382. * Get url
  383. *
  384. * @return string
  385. */
  386. function getUrl()
  387. {
  388. return $this->getPage()->getUrl();
  389. }
  390. /**
  391. * Set customUrl
  392. *
  393. * @param string $customUrl
  394. */
  395. function setCustomUrl($customUrl)
  396. {
  397. $this->getPage()->setCustomUrl($customUrl);
  398. }
  399. /**
  400. * Get customUrl
  401. *
  402. * @return integer $customUrl
  403. */
  404. function getCustomUrl()
  405. {
  406. return $this->getPage()->getCustomUrl();
  407. }
  408. /**
  409. * Set metaKeyword
  410. *
  411. * @param string $metaKeyword
  412. */
  413. function setMetaKeyword($metaKeyword)
  414. {
  415. $this->getPage()->setMetaKeyword($metaKeyword);
  416. }
  417. /**
  418. * Get metaKeyword
  419. *
  420. * @return string $metaKeyword
  421. */
  422. function getMetaKeyword()
  423. {
  424. return $this->getPage()->getMetaKeyword();
  425. }
  426. /**
  427. * Set metaDescription
  428. *
  429. * @param string $metaDescription
  430. */
  431. function setMetaDescription($metaDescription)
  432. {
  433. $this->getPage()->setMetaDescription($metaDescription);
  434. }
  435. /**
  436. * Get metaDescription
  437. *
  438. * @return string $metaDescription
  439. */
  440. function getMetaDescription()
  441. {
  442. return $this->getPage()->getMetaDescription();
  443. }
  444. /**
  445. * Set javascript
  446. *
  447. * @param string $javascript
  448. */
  449. function setJavascript($javascript)
  450. {
  451. $this->getPage()->setJavascript($javascript);
  452. }
  453. /**
  454. * Get javascript
  455. *
  456. * @return string $javascript
  457. */
  458. function getJavascript()
  459. {
  460. return $this->getPage()->getJavascript();
  461. }
  462. /**
  463. * Set stylesheet
  464. *
  465. * @param string $stylesheet
  466. */
  467. function setStylesheet($stylesheet)
  468. {
  469. $this->getPage()->setStylesheet($stylesheet);
  470. }
  471. /**
  472. * Get stylesheet
  473. *
  474. * @return string $stylesheet
  475. */
  476. function getStylesheet()
  477. {
  478. return $this->getPage()->getStylesheet();
  479. }
  480. /**
  481. * Set createdAt
  482. *
  483. * @param \DateTime $createdAt
  484. */
  485. function setCreatedAt(\DateTime $createdAt = null)
  486. {
  487. $this->getPage()->setCreatedAt($createdAt);
  488. }
  489. /**
  490. * Get createdAt
  491. *
  492. * @return datetime $createdAt
  493. */
  494. function getCreatedAt()
  495. {
  496. return $this->getPage()->getCreatedAt();
  497. }
  498. /**
  499. * Set updatedAt
  500. *
  501. * @param \DateTime $updatedAt
  502. */
  503. function setUpdatedAt(\DateTime $updatedAt = null)
  504. {
  505. $this->getPage()->setUpdatedAt($updatedAt);
  506. }
  507. /**
  508. * Get updatedAt
  509. *
  510. * @return \DateTime $updatedAt
  511. */
  512. function getUpdatedAt()
  513. {
  514. return $this->getPage()->getUpdatedAt();
  515. }
  516. /**
  517. * @return boolean
  518. */
  519. function isDynamic()
  520. {
  521. return $this->getPage()->isDynamic();
  522. }
  523. /**
  524. * Return the TTL value in second
  525. *
  526. * @return integer
  527. */
  528. public function getTtl() {
  529. return $this->getPage()->getTtl();
  530. }
  531. /**
  532. * @param string $method
  533. * @return bool
  534. */
  535. public function hasRequestMethod($method) {
  536. return $this->getPage()->hasRequestMethod($method);
  537. }
  538. /**
  539. * @return bool
  540. */
  541. public function isCms() {
  542. return $this->getPage()->isCms();
  543. }
  544. /**
  545. * @return bool
  546. */
  547. public function isInternal() {
  548. return $this->getPage()->isInternal();
  549. }
  550. }