‪TYPO3CMS  9.5
PreviewModule.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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 Psr\Http\Message\ServerRequestInterface;
33 
38 {
44  protected ‪$config;
45 
49  public function ‪getIconIdentifier(): string
50  {
51  return 'actions-preview';
52  }
53 
57  public function ‪getIdentifier(): string
58  {
59  return 'preview';
60  }
61 
65  public function ‪getLabel(): string
66  {
67  return $this->‪getLanguageService()->‪sL(
68  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:module.label'
69  );
70  }
71 
75  public function ‪initializeModule(ServerRequestInterface $request): void
76  {
77  // Backend preview params (ADMCMD_) take precedence over configured admin panel values
78  $simulateGroupByRequest = (int)($request->getQueryParams()['ADMCMD_simUser'] ?? 0);
79  $simulateTimeByRequest = (int)($request->getQueryParams()['ADMCMD_simTime'] ?? 0);
80  $this->config = [
81  'showHiddenPages' => (bool)$this->‪getConfigOptionForModule('showHiddenPages'),
82  'simulateDate' => $simulateTimeByRequest ?: (int)$this->‪getConfigOptionForModule('simulateDate'),
83  'showHiddenRecords' => (bool)$this->‪getConfigOptionForModule('showHiddenRecords'),
84  'simulateUserGroup' => $simulateGroupByRequest ?: (int)$this->‪getConfigOptionForModule('simulateUserGroup'),
85  'showFluidDebug' => (bool)$this->‪getConfigOptionForModule('showFluidDebug'),
86  ];
87  if ($this->config['showFluidDebug']) {
88  // forcibly unset fluid caching as it does not care about the tsfe based caching settings
89  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend']);
90  ‪$GLOBALS['TSFE']->set_no_cache('Cache is disabled if fluid debugging is enabled', true);
91  }
93  $this->config['showHiddenPages'],
94  $this->config['showHiddenRecords'],
95  $this->config['simulateDate'],
96  $this->config['simulateUserGroup']
97  );
98  }
99 
103  public function ‪getPageSettings(): string
104  {
105  $view = GeneralUtility::makeInstance(StandaloneView::class);
106  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Settings/Preview.html';
107  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
108  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
109 
110  $frontendGroupsRepository = GeneralUtility::makeInstance(FrontendGroupsRepository::class);
111 
112  $view->assignMultiple(
113  [
114  'show' => [
115  'pageId' => (int)$this->‪getTypoScriptFrontendController()->id,
116  'hiddenPages' => $this->config['showHiddenPages'],
117  'hiddenRecords' => $this->config['showHiddenRecords'],
118  'fluidDebug' => $this->config['showFluidDebug'],
119  ],
120  'simulateDate' => $this->config['simulateDate'],
121  'frontendUserGroups' => [
122  'availableGroups' => $frontendGroupsRepository->getAvailableFrontendUserGroups(),
123  'selected' => (int)$this->config['simulateUserGroup'],
124  ],
125  ]
126  );
127  return $view->render();
128  }
129 
134  protected function ‪getConfigOptionForModule(string $option): string
135  {
136  return $this->configurationService->getConfigurationOption(
137  'preview',
138  $option
139  );
140  }
141 
146  {
147  return ‪$GLOBALS['TSFE'];
148  }
149 
160  protected function ‪initializeFrontendPreview(
161  bool $showHiddenPages,
162  bool $showHiddenRecords,
163  int $simulateDate,
164  int $simulateUserGroup
165  ): void {
166  $context = GeneralUtility::makeInstance(Context::class);
167  $typoScriptFrontendController = $this->‪getTypoScriptFrontendController();
168  $typoScriptFrontendController->clear_preview();
169  $typoScriptFrontendController->fePreview = 1;
170 
171  // Modify visibility settings (hidden pages + hidden content)
172  $context->setAspect(
173  'visibility',
174  GeneralUtility::makeInstance(VisibilityAspect::class, $showHiddenPages, $showHiddenRecords)
175  );
176 
177  // Simulate date
178  $simTime = null;
179  if ($simulateDate) {
180  $simTime = $this->‪parseDate($simulateDate);
181  if ($simTime) {
182  ‪$GLOBALS['SIM_EXEC_TIME'] = $simTime;
183  ‪$GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
184  $context->setAspect(
185  'date',
186  GeneralUtility::makeInstance(
187  DateTimeAspect::class,
188  new \DateTimeImmutable('@' . ‪$GLOBALS['SIM_EXEC_TIME'])
189  )
190  );
191  }
192  }
193  // simulate usergroup
194  if ($simulateUserGroup) {
195  $typoScriptFrontendController->simUserGroup = $simulateUserGroup;
196  if (!$typoScriptFrontendController->fe_user instanceof ‪FrontendUserAuthentication) {
197  $typoScriptFrontendController->fe_user = GeneralUtility::makeInstance(
198  FrontendUserAuthentication::class
199  );
200  }
201  if (!is_array($typoScriptFrontendController->fe_user->user)) {
202  // This can be removed once #90989 is fixed
203  $typoScriptFrontendController->fe_user->user = ['uid' => PHP_INT_MAX];
204  }
205  $typoScriptFrontendController->fe_user->user[$typoScriptFrontendController->fe_user->usergroup_column] = $simulateUserGroup;
206  $context->setAspect(
207  'frontend.user',
208  GeneralUtility::makeInstance(
209  UserAspect::class,
210  $typoScriptFrontendController->fe_user ?: null,
211  [$simulateUserGroup]
212  )
213  );
214  }
215  if (!$simulateUserGroup && !$simTime && !$showHiddenPages && !$showHiddenRecords) {
216  $typoScriptFrontendController->fePreview = 0;
217  }
218  }
219 
223  public function ‪getJavaScriptFiles(): array
224  {
225  return ['EXT:adminpanel/Resources/Public/JavaScript/Modules/Preview.js'];
226  }
227 
237  protected function ‪parseDate(int $simulateDate): ?int
238  {
239  try {
240  $simTime = (new \DateTime('@' . $simulateDate))->getTimestamp();
241  $simTime = max($simTime, 60);
242  } catch (\Exception $e) {
243  $simTime = null;
244  }
245  return $simTime;
246  }
247 
253  public function ‪getCssFiles(): array
254  {
255  return [];
256  }
257 }
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\$config
‪array $config
Definition: PreviewModule.php:43
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getJavaScriptFiles
‪array getJavaScriptFiles()
Definition: PreviewModule.php:222
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:29
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\parseDate
‪int parseDate(int $simulateDate)
Definition: PreviewModule.php:236
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractModule
Definition: AbstractModule.php:30
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getPageSettings
‪getPageSettings()
Definition: PreviewModule.php:102
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getIdentifier
‪getIdentifier()
Definition: PreviewModule.php:56
‪TYPO3\CMS\Adminpanel\ModuleApi\InitializableInterface
Definition: InitializableInterface.php:34
‪TYPO3\CMS\Adminpanel\Repositories\FrontendGroupsRepository
Definition: FrontendGroupsRepository.php:31
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getCssFiles
‪array getCssFiles()
Definition: PreviewModule.php:252
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Adminpanel\ModuleApi\ResourceProviderInterface
Definition: ResourceProviderInterface.php:26
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Adminpanel\Modules
Definition: CacheModule.php:4
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: PreviewModule.php:144
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\initializeModule
‪initializeModule(ServerRequestInterface $request)
Definition: PreviewModule.php:74
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\initializeFrontendPreview
‪initializeFrontendPreview(bool $showHiddenPages, bool $showHiddenRecords, int $simulateDate, int $simulateUserGroup)
Definition: PreviewModule.php:159
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractModule\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractModule.php:120
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getConfigOptionForModule
‪string getConfigOptionForModule(string $option)
Definition: PreviewModule.php:133
‪TYPO3\CMS\Adminpanel\ModuleApi\PageSettingsProviderInterface
Definition: PageSettingsProviderInterface.php:31
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getIconIdentifier
‪getIconIdentifier()
Definition: PreviewModule.php:48
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule
Definition: PreviewModule.php:38
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:33
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getLabel
‪getLabel()
Definition: PreviewModule.php:64
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36