OtCustomizerController.php 4.4 KB

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