OtPageRepository.php 4.7 KB

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