OtPageRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Opentalent\OtTemplating\Page;
  3. use FluidTYPO3\Vhs\Service\PageService;
  4. use TYPO3\CMS\Core\Exception\SiteNotFoundException;
  5. use TYPO3\CMS\Core\Site\SiteFinder;
  6. use TYPO3\CMS\Core\Utility\GeneralUtility;
  7. use TYPO3\CMS\Extbase\Object\ObjectManager;
  8. use TYPO3\CMS\Frontend\Page\PageRepository;
  9. /**
  10. * Class OtPageRepository
  11. *
  12. * Provides some useful methods to query typo3 pages
  13. *
  14. * @package Opentalent\OtTemplating\Page
  15. */
  16. class OtPageRepository extends PageRepository
  17. {
  18. CONST templates = [
  19. 'Classic' => [
  20. 'name' => 'Classique',
  21. 'description' => "Le thème classique, simple et complet. C'est le thème par défaut.",
  22. 'picture' => 'EXT:ot_templating/Resources/Public/media/theme_classic.png'
  23. ],
  24. 'Modern' => [
  25. 'name' => 'Moderne',
  26. 'description' => '[Nouveauté 2020] Un thème moderne et intuitif.',
  27. 'picture' => 'EXT:ot_templating/Resources/Public/media/theme_modern.png'
  28. ]
  29. ];
  30. CONST defaultTemplate = 'Classic';
  31. CONST defaultPreferences = [
  32. 'themeColor' => 'lightblue',
  33. 'displayCarousel' => '1'
  34. ];
  35. /**
  36. * Returns the root page of the given page,
  37. * or the page itself if the given page is
  38. * already the rootpage of the site
  39. *
  40. * @param $pageUid
  41. *
  42. * @return array
  43. */
  44. public function getRootPageFor($pageUid) {
  45. $pageService = GeneralUtility::makeInstance(ObjectManager::class)->get(PageService::class);
  46. $rootLine = $pageService->getRootLine($pageUid);
  47. for (end($rootLine); key($rootLine)!==null; prev($rootLine)){
  48. $page = current($rootLine);
  49. if ($page['is_siteroot'] == 1) {
  50. return $page;
  51. }
  52. }
  53. return [];
  54. }
  55. /**
  56. * Recursively returns all the subpages of the given page
  57. *
  58. * @param $pageUid
  59. * @return array
  60. */
  61. public function getAllSubpagesForPage($pageUid) {
  62. $subpages = [];
  63. $stack = $this->getSubpagesForPages(
  64. [$pageUid],
  65. '*',
  66. 'sorting',
  67. '',
  68. false
  69. );
  70. foreach ($stack as $page) {
  71. $subpages[] = $page;
  72. $children = $this->getAllSubpagesForPage($page['uid']);
  73. if (!empty($children)) {
  74. $subpages = array_merge($subpages, $children);
  75. }
  76. }
  77. return $subpages;
  78. }
  79. /**
  80. * Returns the current site template's name
  81. * @return string
  82. */
  83. public function getCurrentTemplate() {
  84. $rootPageUid = $this->getCurrentSiteRootPageId();
  85. if (!($rootPageUid >= 0)) {
  86. throw new \RuntimeException('Can not find any root page');
  87. }
  88. $rootPage = $this->getPage($rootPageUid);
  89. $templateName = $rootPage['tx_opentalent_template'];
  90. if ($templateName==='') {
  91. return self::defaultTemplate;
  92. }
  93. return $templateName;
  94. }
  95. /**
  96. * Returns the current site template's name
  97. * @return array
  98. */
  99. public function getTemplatePreferences() {
  100. $rootPageUid = $this->getCurrentSiteRootPageId();
  101. if (!($rootPageUid >= 0)) {
  102. return [];
  103. }
  104. $rootPage = $this->getPage($rootPageUid);
  105. $templatePreferences = $rootPage['tx_opentalent_template_preferences'];
  106. if ($templatePreferences==='') {
  107. return self::defaultPreferences;
  108. }
  109. return json_decode($templatePreferences, true);
  110. }
  111. /**
  112. * Returns the typo3 site matching the current request
  113. *
  114. */
  115. public function getCurrentSite() {
  116. $request = $GLOBALS['TYPO3_REQUEST'];
  117. $site = $request->getAttribute('site');
  118. return GeneralUtility::makeInstance(SiteFinder::class)
  119. ->getSiteByIdentifier($site->getIdentifier());
  120. }
  121. /**
  122. *
  123. */
  124. public function getCurrentSiteRootPageId() {
  125. $site = $this->getCurrentSite();
  126. return $site->getRootPageId();
  127. }
  128. /**
  129. *
  130. */
  131. public function getCurrentSiteRootPageUri() {
  132. $site = $this->getCurrentSite();
  133. return $site->getBase();
  134. }
  135. /**
  136. *
  137. */
  138. public function getCurrentSiteRootPage() {
  139. $uid = $this->getCurrentSiteRootPageUri();
  140. return $this->getPage($uid);
  141. }
  142. }