b22850aef03b4b2f07228031ba4e8325b1b12030
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Modules / PreviewModule.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Adminpanel\Modules;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Adminpanel\Repositories\FrontendGroupsRepository;
20 use TYPO3\CMS\Core\Type\Bitmask\Permission;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Fluid\View\StandaloneView;
23 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
24 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25
26 /**
27 * Admin Panel Preview Module
28 */
29 class PreviewModule extends AbstractModule
30 {
31 /**
32 * Force the preview panel to be opened
33 *
34 * @var bool
35 */
36 protected $forceOpen = false;
37
38 /**
39 * @inheritdoc
40 */
41 public function getAdditionalJavaScriptCode(): string
42 {
43 return 'TSFEtypo3FormFieldSet("TSFE_ADMIN_PANEL[preview_simulateDate]", "datetime", "", 0, 0);';
44 }
45
46 /**
47 * Creates the content for the "preview" section ("module") of the Admin Panel
48 *
49 * @return string HTML content for the section. Consists of a string with table-rows with four columns.
50 * @see display()
51 * @throws \InvalidArgumentException
52 */
53 public function getContent(): string
54 {
55 $view = GeneralUtility::makeInstance(StandaloneView::class);
56 $templateNameAndPath = $this->extResources . '/Templates/Modules/Preview.html';
57 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
58 $view->setPartialRootPaths([$this->extResources . '/Partials']);
59
60 $frontendGroupsRepository = GeneralUtility::makeInstance(FrontendGroupsRepository::class);
61
62 $view->assignMultiple(
63 [
64 'show' => [
65 'hiddenPages' => $this->getConfigurationOption('showHiddenPages'),
66 'hiddenRecords' => $this->getConfigurationOption('showHiddenRecords'),
67 'fluidDebug' => $this->getConfigurationOption('showFluidDebug'),
68 ],
69 'simulateDate' => $this->getConfigurationOption('simulateDate'),
70 'frontendUserGroups' => [
71 'availableGroups' => $frontendGroupsRepository->getAvailableFrontendUserGroups(),
72 'selected' => $this->getConfigurationOption('simulateUserGroup'),
73 ],
74 ]
75 );
76 return $view->render();
77 }
78
79 /**
80 * @inheritdoc
81 */
82 public function getIdentifier(): string
83 {
84 return 'preview';
85 }
86
87 /**
88 * @inheritdoc
89 */
90 public function getLabel(): string
91 {
92 $locallangFileAndPath = 'LLL:' . $this->extResources . '/Language/locallang_preview.xlf:module.label';
93 return $this->getLanguageService()->sL($locallangFileAndPath);
94 }
95
96 public function initializeModule(): void
97 {
98 $this->initializeFrontendPreview();
99 if (GeneralUtility::_GP('ADMCMD_simUser')) {
100 $this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateUserGroup'] = (int)GeneralUtility::_GP(
101 'ADMCMD_simUser'
102 );
103 $this->forceOpen = true;
104 }
105 if (GeneralUtility::_GP('ADMCMD_simTime')) {
106 $this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateDate'] = (int)GeneralUtility::_GP(
107 'ADMCMD_simTime'
108 );
109 $this->forceOpen = true;
110 }
111 }
112
113 /**
114 * Force module to be shown if either time or users/groups are simulated
115 *
116 * @return bool
117 */
118 public function isShown(): bool
119 {
120 if ($this->forceOpen) {
121 return true;
122 }
123 return parent::isShown();
124 }
125
126 /**
127 * Clear page cache if fluid debug output is enabled
128 *
129 * @param array $input
130 */
131 public function onSubmit(array $input): void
132 {
133 if ($input['preview_showFluidDebug'] ?? false) {
134 $theStartId = (int)$this->getTypoScriptFrontendController()->id;
135 $this->getTypoScriptFrontendController()
136 ->clearPageCacheContent_pidList(
137 $this->getBackendUser()->extGetTreeList(
138 $theStartId,
139 0,
140 0,
141 $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
142 ) . $theStartId
143 );
144 }
145 }
146
147 /**
148 * @inheritdoc
149 */
150 public function showFormSubmitButton(): bool
151 {
152 return true;
153 }
154
155 /**
156 * @return TypoScriptFrontendController
157 */
158 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
159 {
160 return $GLOBALS['TSFE'];
161 }
162
163 /**
164 * Initialize frontend preview functionality incl.
165 * simulation of users or time
166 */
167 protected function initializeFrontendPreview()
168 {
169 $tsfe = $this->getTypoScriptFrontendController();
170 $tsfe->clear_preview();
171 $tsfe->fePreview = 1;
172 $tsfe->showHiddenPage = (bool)$this->getConfigurationOption('showHiddenPages');
173 $tsfe->showHiddenRecords = (bool)$this->getConfigurationOption('showHiddenRecords');
174 // Simulate date
175 $simTime = $this->getConfigurationOption('simulateDate');
176 if ($simTime) {
177 $GLOBALS['SIM_EXEC_TIME'] = $simTime;
178 $GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
179 }
180 // simulate user
181 $tsfe->simUserGroup = $this->getConfigurationOption('simulateUserGroup');
182 if ($tsfe->simUserGroup) {
183 if ($tsfe->fe_user->user) {
184 $tsfe->fe_user->user[$tsfe->fe_user->usergroup_column] = $tsfe->simUserGroup;
185 } else {
186 $tsfe->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
187 $tsfe->fe_user->user = [
188 $tsfe->fe_user->usergroup_column => $tsfe->simUserGroup,
189 ];
190 }
191 }
192 if (!$tsfe->simUserGroup && !$simTime && !$tsfe->showHiddenPage && !$tsfe->showHiddenRecords) {
193 $tsfe->fePreview = 0;
194 }
195 }
196 }