OtCustomizerController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Opentalent\OtTemplating\Controller;
  3. use Opentalent\OtTemplating\Utilities\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. CONST templates = [
  16. 'Classic' => [
  17. 'name' => 'Classique',
  18. 'description' => 'Le thème classique, simple et complet.',
  19. 'picture' => 'EXT:ot_templating/Resources/Public/media/theme_classic.png'
  20. ],
  21. 'Modern' => [
  22. 'name' => 'Moderne',
  23. 'description' => '[Nouveau] Un thème moderne et intuitif.',
  24. 'picture' => 'EXT:ot_templating/Resources/Public/media/theme_modern.png'
  25. ]
  26. ];
  27. /**
  28. * Index action (default action)
  29. * Displays the customize page on the backend
  30. */
  31. public function indexAction() {
  32. // Get the current root page's uid
  33. $rootPageUid = $this->getCurrentRootPageUid();
  34. // If the current page is not a root page or a subpage of one, abort
  35. $pageSelected = ($rootPageUid !== null);
  36. $this->view->assign('pageSelected', (int)$pageSelected);
  37. if (!$pageSelected) {
  38. return;
  39. }
  40. $this->view->assign('rootPage', $rootPageUid);
  41. $this->view->assign('templates', self::templates);
  42. }
  43. /**
  44. * A template has been selected, apply the change
  45. */
  46. public function selectTemplateAction() {
  47. $templateKey = $this->request->getArgument('template_key');
  48. // Get the current root page's uid
  49. $rootPageUid = $this->getCurrentRootPageUid();
  50. // applies the change in the database
  51. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
  52. $queryBuilder->update('pages')
  53. ->where(
  54. $queryBuilder->expr()->eq(
  55. 'uid',
  56. $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
  57. )
  58. )
  59. ->set('tx_opentalent_template', $templateKey)
  60. ->execute()
  61. ;
  62. // Clear the page cache
  63. $this->cleanCache();
  64. $this->redirect('index');
  65. }
  66. /**
  67. * Return the uid of the root page of the currently selected
  68. * site, or null if no site's page is selected
  69. *
  70. * @return int | null
  71. */
  72. public function getCurrentRootPageUid() {
  73. // Get the current page uid
  74. $pageId = (int) GeneralUtility::_GP('id');
  75. // Get the root page of the site
  76. $otPageRepository = new OtPageRepository();
  77. $rootPage = $otPageRepository->getRootPageFor($pageId);
  78. if (!$rootPage['uid'] > 0) {
  79. return null;
  80. }
  81. return (int)$rootPage['uid'];
  82. }
  83. /**
  84. * Cleans the pages cache
  85. */
  86. public function cleanCache() {
  87. $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
  88. $dataHandler->start([], []);
  89. $dataHandler->clear_cacheCmd('pages');
  90. }
  91. /**
  92. * Gets the backend user
  93. *
  94. * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
  95. */
  96. protected function getBackendUser() {
  97. return $GLOBALS['BE_USER'];
  98. }
  99. }