OtPageRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return self::defaultTemplate;
  88. }
  89. $rootPage = $this->getPage($rootPageUid);
  90. $templateName = $rootPage['tx_opentalent_template'];
  91. if ($templateName==='') {
  92. return self::defaultTemplate;
  93. }
  94. return $templateName;
  95. }
  96. /**
  97. * Returns the current site template's name
  98. * @return array
  99. */
  100. public function getTemplatePreferences() {
  101. $rootPageUid = $this->getCurrentSiteRootPageId();
  102. if (!($rootPageUid >= 0)) {
  103. return [];
  104. }
  105. $rootPage = $this->getPage($rootPageUid);
  106. $templatePreferences = $rootPage['tx_opentalent_template_preferences'];
  107. if ($templatePreferences==='') {
  108. return self::defaultPreferences;
  109. }
  110. return json_decode($templatePreferences, true);
  111. }
  112. /**
  113. * Returns the typo3 site matching the current request
  114. *
  115. */
  116. public function getCurrentSite() {
  117. $request = $GLOBALS['TYPO3_REQUEST'];
  118. $site = $request->getAttribute('site');
  119. return GeneralUtility::makeInstance(SiteFinder::class)
  120. ->getSiteByIdentifier($site->getIdentifier());
  121. }
  122. /**
  123. *
  124. */
  125. public function getCurrentSiteRootPageId() {
  126. $site = $this->getCurrentSite();
  127. return $site->getRootPageId();
  128. }
  129. /**
  130. *
  131. */
  132. public function getCurrentSiteRootPageUri() {
  133. $site = $this->getCurrentSite();
  134. return $site->getBase();
  135. }
  136. /**
  137. *
  138. */
  139. public function getCurrentSiteRootPage() {
  140. $uid = $this->getCurrentSiteRootPageUri();
  141. return $this->getPage($uid);
  142. }
  143. }