OtCustomizerController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Opentalent\OtTemplating\Controller;
  3. use Opentalent\OtCore\Page\OtPageRepository;
  4. use Opentalent\OtCore\Cache\OtCacheManager;
  5. use Opentalent\OtTemplating\Templating\TemplateRepository;
  6. use PDO;
  7. use TYPO3\CMS\Core\Database\ConnectionPool;
  8. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  9. use TYPO3\CMS\Core\Utility\GeneralUtility;
  10. /**
  11. * Controller for the OtCustomizer backend submodule
  12. *
  13. * @author olivier.massot
  14. */
  15. class OtCustomizerController extends ActionController {
  16. /**
  17. * Index action (default action)
  18. * Displays the customizer page on the backend
  19. */
  20. public function indexAction() {
  21. // Get the current root page's uid
  22. $rootPageUid = $this->getCurrentRootPageUid();
  23. // If the current page is not a root page or a subpage of one, abort
  24. $pageSelected = ($rootPageUid !== null);
  25. $this->view->assign('pageSelected', (int)$pageSelected);
  26. if (!$pageSelected) {
  27. return;
  28. }
  29. $this->view->assign('rootPage', $rootPageUid);
  30. $this->view->assign('templates', TemplateRepository::templates);
  31. $templateRepository = GeneralUtility::makeInstance(TemplateRepository::class);
  32. $this->view->assign('currentTemplate', $templateRepository->getCurrentTemplate());
  33. $this->view->assign('preferences', $templateRepository->getTemplatePreferences());
  34. $args = $this->request->getArguments();
  35. $this->view->assign('preferencesUpdated', $args['preferencesUpdated']);
  36. }
  37. /**
  38. * A template has been selected, apply the change
  39. *
  40. */
  41. public function selectTemplateAction() {
  42. $templateKey = $this->request->getArgument('template_key');
  43. // Get the current root page's uid
  44. $rootPageUid = $this->getCurrentRootPageUid();
  45. // applies the change in the database
  46. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  47. $queryBuilder->update('pages')
  48. ->where(
  49. $queryBuilder->expr()->eq(
  50. 'uid',
  51. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  52. )
  53. )
  54. ->set('tx_opentalent_template', $templateKey)
  55. ->execute()
  56. ;
  57. // Clear the site's cache
  58. OtCacheManager::clearSiteCache($rootPageUid);
  59. $this->forward('index');
  60. }
  61. /**
  62. * Update the site's preferences
  63. */
  64. public function updatePreferencesAction() {
  65. $args = $this->request->getArguments();
  66. $prefs = TemplateRepository::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. OtCacheManager::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. protected 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. }