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