OtCustomizerController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Opentalent\OtTemplating\Controller;
  3. use Opentalent\OtTemplating\Page\OtPageRepository;
  4. use PDO;
  5. use TYPO3\CMS\Core\Database\ConnectionPool;
  6. use TYPO3\CMS\Core\DataHandling\DataHandler;
  7. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  8. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. /**
  10. * Controller for the OtCustomizer backend submodule
  11. *
  12. * @author olivier.massot
  13. */
  14. class OtCustomizerController extends ActionController {
  15. /**
  16. * Index action (default action)
  17. * Displays the customize page on the backend
  18. */
  19. public function indexAction() {
  20. // Get the current root page's uid
  21. $rootPageUid = $this->getCurrentRootPageUid();
  22. // If the current page is not a root page or a subpage of one, abort
  23. $pageSelected = ($rootPageUid !== null);
  24. $this->view->assign('pageSelected', (int)$pageSelected);
  25. if (!$pageSelected) {
  26. return;
  27. }
  28. $this->view->assign('rootPage', $rootPageUid);
  29. $this->view->assign('templates', OtPageRepository::templates);
  30. $pageRepository = new OtPageRepository();
  31. $this->view->assign('currentTemplate', $pageRepository->getCurrentTemplate($rootPageUid));
  32. $this->view->assign('preferences', $pageRepository->getTemplatePreferences($rootPageUid));
  33. $args = $this->request->getArguments();
  34. $this->view->assign('preferencesUpdated', $args['preferencesUpdated']);
  35. }
  36. /**
  37. * A template has been selected, apply the change
  38. */
  39. public function selectTemplateAction() {
  40. $templateKey = $this->request->getArgument('template_key');
  41. // Get the current root page's uid
  42. $rootPageUid = $this->getCurrentRootPageUid();
  43. // applies the change in the database
  44. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  45. $queryBuilder->update('pages')
  46. ->where(
  47. $queryBuilder->expr()->eq(
  48. 'uid',
  49. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  50. )
  51. )
  52. ->set('tx_opentalent_template', $templateKey)
  53. ->execute()
  54. ;
  55. // Clear the page cache
  56. $this->cleanCache();
  57. $this->forward('index');
  58. }
  59. /**
  60. * Update the site's preferences
  61. */
  62. public function updatePreferencesAction() {
  63. $args = $this->request->getArguments();
  64. $prefs = OtPageRepository::defaultPreferences;
  65. if (isset($args['themeColor'])) {
  66. $prefs['themeColor'] = $args['themeColor'];
  67. }
  68. if (isset($args['displayCarousel'])) {
  69. $prefs['displayCarousel'] = $args['displayCarousel'];
  70. }
  71. // Get the current root page's uid
  72. $rootPageUid = $this->getCurrentRootPageUid();
  73. // applies the change in the database
  74. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  75. $queryBuilder->update('pages')
  76. ->where(
  77. $queryBuilder->expr()->eq(
  78. 'uid',
  79. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  80. )
  81. )
  82. ->set('tx_opentalent_template_preferences', json_encode($prefs))
  83. ->execute()
  84. ;
  85. // Clear the page cache
  86. $this->cleanCache();
  87. $this->forward(
  88. 'index',
  89. 'OtCustomizer',
  90. 'OtTemplating',
  91. ['preferencesUpdated'=>'1']
  92. );
  93. }
  94. /**
  95. * Return the uid of the root page of the currently selected
  96. * site, or null if no site's page is selected
  97. *
  98. * @return int | null
  99. */
  100. public function getCurrentRootPageUid() {
  101. // Get the current page uid
  102. $pageId = (int) GeneralUtility::_GP('id');
  103. // Get the root page of the site
  104. $otPageRepository = new OtPageRepository();
  105. $rootPage = $otPageRepository->getRootPageFor($pageId);
  106. if (!$rootPage['uid'] > 0) {
  107. return null;
  108. }
  109. return (int)$rootPage['uid'];
  110. }
  111. /**
  112. * Cleans the pages cache
  113. */
  114. public function cleanCache() {
  115. $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
  116. $dataHandler->start([], []);
  117. $dataHandler->clear_cacheCmd('pages');
  118. }
  119. /**
  120. * Gets the backend user
  121. *
  122. * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
  123. */
  124. protected function getBackendUser() {
  125. return $GLOBALS['BE_USER'];
  126. }
  127. }