OtCustomizerController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Opentalent\OtTemplating\Controller;
  3. use Opentalent\OtTemplating\Page\OtPageRepository;
  4. use Opentalent\OtTemplating\Utility\CacheManager;
  5. use PDO;
  6. use TYPO3\CMS\Core\Database\ConnectionPool;
  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 customizer 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 = GeneralUtility::makeInstance(OtPageRepository::class);
  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. */
  40. public function selectTemplateAction() {
  41. $templateKey = $this->request->getArgument('template_key');
  42. // Get the current root page's uid
  43. $rootPageUid = $this->getCurrentRootPageUid();
  44. // applies the change in the database
  45. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  46. $queryBuilder->update('pages')
  47. ->where(
  48. $queryBuilder->expr()->eq(
  49. 'uid',
  50. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  51. )
  52. )
  53. ->set('tx_opentalent_template', $templateKey)
  54. ->execute()
  55. ;
  56. // Clear the site's cache
  57. CacheManager::clearSiteCache($rootPageUid);
  58. CacheManager::clearAssetsTempfiles();
  59. $this->forward('index');
  60. }
  61. /**
  62. * Update the site's preferences
  63. */
  64. public function updatePreferencesAction() {
  65. $args = $this->request->getArguments();
  66. $prefs = OtPageRepository::defaultPreferences;
  67. if (isset($args['themeColor'])) {
  68. $prefs['themeColor'] = $args['themeColor'];
  69. }
  70. if (isset($args['displayCarousel'])) {
  71. $prefs['displayCarousel'] = $args['displayCarousel'];
  72. }
  73. // Get the current root page's uid
  74. $rootPageUid = $this->getCurrentRootPageUid();
  75. // applies the change in the database
  76. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  77. $queryBuilder->update('pages')
  78. ->where(
  79. $queryBuilder->expr()->eq(
  80. 'uid',
  81. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  82. )
  83. )
  84. ->set('tx_opentalent_template_preferences', json_encode($prefs))
  85. ->execute()
  86. ;
  87. // Clear the site's cache
  88. CacheManager::clearSiteCache($rootPageUid);
  89. $this->forward(
  90. 'index',
  91. 'OtCustomizer',
  92. 'OtTemplating',
  93. ['preferencesUpdated'=>'1']
  94. );
  95. }
  96. /**
  97. * Return the uid of the root page of the currently selected
  98. * site, or null if no site's page is selected
  99. *
  100. * @return int | null
  101. */
  102. public function getCurrentRootPageUid() {
  103. // Get the current page uid
  104. $pageId = (int) GeneralUtility::_GP('id');
  105. // Get the root page of the site
  106. $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
  107. $rootPage = $otPageRepository->getRootPageFor($pageId);
  108. if (!$rootPage['uid'] > 0) {
  109. return null;
  110. }
  111. return (int)$rootPage['uid'];
  112. }
  113. /**
  114. * Gets the backend user
  115. *
  116. * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
  117. */
  118. protected function getBackendUser() {
  119. return $GLOBALS['BE_USER'];
  120. }
  121. }