‪TYPO3CMS  10.4
PreviewModule.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
34 
39 {
45  protected ‪$config;
46 
50  public function ‪getIconIdentifier(): string
51  {
52  return 'actions-preview';
53  }
54 
58  public function ‪getIdentifier(): string
59  {
60  return 'preview';
61  }
62 
66  public function ‪getLabel(): string
67  {
68  return $this->‪getLanguageService()->‪sL(
69  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:module.label'
70  );
71  }
72 
76  public function ‪enrich(ServerRequestInterface $request): ServerRequestInterface
77  {
78  // Backend preview params (ADMCMD_) take precedence over configured admin panel values
79  $simulateGroupByRequest = (int)($request->getQueryParams()['ADMCMD_simUser'] ?? 0);
80  $simulateTimeByRequest = (int)($request->getQueryParams()['ADMCMD_simTime'] ?? 0);
81  $this->config = [
82  'showHiddenPages' => (bool)$this->‪getConfigOptionForModule('showHiddenPages'),
83  'simulateDate' => $simulateTimeByRequest ?: (int)$this->‪getConfigOptionForModule('simulateDate'),
84  'showHiddenRecords' => (bool)$this->‪getConfigOptionForModule('showHiddenRecords'),
85  'simulateUserGroup' => $simulateGroupByRequest ?: (int)$this->‪getConfigOptionForModule('simulateUserGroup'),
86  'showFluidDebug' => (bool)$this->‪getConfigOptionForModule('showFluidDebug'),
87  ];
88  if ($this->config['showFluidDebug']) {
89  // forcibly unset fluid caching as it does not care about the tsfe based caching settings
90  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend']);
91  $request = $request->withAttribute('noCache', true);
92  }
94  $this->config['showHiddenPages'],
95  $this->config['showHiddenRecords'],
96  $this->config['simulateDate'],
97  $this->config['simulateUserGroup'],
98  $request
99  );
100 
101  return $request;
102  }
103 
107  public function ‪getPageSettings(): string
108  {
109  $view = GeneralUtility::makeInstance(StandaloneView::class);
110  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Settings/Preview.html';
111  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
112  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
113 
114  $frontendGroupsRepository = GeneralUtility::makeInstance(FrontendGroupsRepository::class);
115 
116  $pageId = 0;
117  $pageArguments = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('routing');
118  if ($pageArguments instanceof ‪PageArguments) {
119  $pageId = $pageArguments->getPageId();
120  }
121 
122  $view->assignMultiple(
123  [
124  'show' => [
125  'pageId' => $pageId,
126  'hiddenPages' => $this->config['showHiddenPages'],
127  'hiddenRecords' => $this->config['showHiddenRecords'],
128  'fluidDebug' => $this->config['showFluidDebug'],
129  ],
130  'simulateDate' => $this->config['simulateDate'],
131  'frontendUserGroups' => [
132  'availableGroups' => $frontendGroupsRepository->getAvailableFrontendUserGroups(),
133  'selected' => (int)$this->config['simulateUserGroup'],
134  ],
135  'languageKey' => $this->getBackendUser()->user['lang'],
136  ]
137  );
138  return $view->render();
139  }
140 
145  protected function ‪getConfigOptionForModule(string $option): string
146  {
147  return $this->configurationService->getConfigurationOption(
148  'preview',
149  $option
150  );
151  }
152 
164  protected function ‪initializeFrontendPreview(
165  bool $showHiddenPages,
166  bool $showHiddenRecords,
167  int $simulateDate,
168  int $simulateUserGroup,
169  ServerRequestInterface $request
170  ): void {
171  $context = GeneralUtility::makeInstance(Context::class);
172  $this->‪clearPreviewSettings($context);
173 
174  // Set preview flag
175  $context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, true));
176 
177  // Modify visibility settings (hidden pages + hidden content)
178  $context->setAspect(
179  'visibility',
180  GeneralUtility::makeInstance(VisibilityAspect::class, $showHiddenPages, $showHiddenRecords)
181  );
182 
183  // Simulate date
184  $simTime = null;
185  if ($simulateDate) {
186  $simTime = $this->‪parseDate($simulateDate);
187  if ($simTime) {
188  ‪$GLOBALS['SIM_EXEC_TIME'] = $simTime;
189  ‪$GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
190  $context->setAspect(
191  'date',
192  GeneralUtility::makeInstance(
193  DateTimeAspect::class,
194  new \DateTimeImmutable('@' . ‪$GLOBALS['SIM_EXEC_TIME'])
195  )
196  );
197  }
198  }
199  // simulate usergroup
200  if ($simulateUserGroup) {
201  $frontendUser = $request->getAttribute('frontend.user');
202  $frontendUser->user[$frontendUser->usergroup_column] = $simulateUserGroup;
203  // let's fake having a user with that group, too
204  // This can be removed once #90989 is fixed
205  $frontendUser->user['uid'] = PHP_INT_MAX;
206  $context->setAspect(
207  'frontend.user',
208  GeneralUtility::makeInstance(
209  UserAspect::class,
210  $frontendUser,
211  [$simulateUserGroup]
212  )
213  );
214  }
215  if (!$simulateUserGroup && !$simTime && !$showHiddenPages && !$showHiddenRecords) {
216  $context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class));
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 
248  protected function ‪clearPreviewSettings(‪Context $context): void
249  {
250  ‪$GLOBALS['SIM_EXEC_TIME'] = ‪$GLOBALS['EXEC_TIME'];
251  ‪$GLOBALS['SIM_ACCESS_TIME'] = ‪$GLOBALS['ACCESS_TIME'];
252  $context->‪setAspect('date', GeneralUtility::makeInstance(DateTimeAspect::class, new \DateTimeImmutable('@' . ‪$GLOBALS['SIM_EXEC_TIME'])));
253  $context->‪setAspect('visibility', GeneralUtility::makeInstance(VisibilityAspect::class));
254  }
255 
261  public function ‪getCssFiles(): array
262  {
263  return [];
264  }
265 }
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\$config
‪array $config
Definition: PreviewModule.php:44
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getJavaScriptFiles
‪array getJavaScriptFiles()
Definition: PreviewModule.php:222
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\parseDate
‪int parseDate(int $simulateDate)
Definition: PreviewModule.php:236
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\initializeFrontendPreview
‪initializeFrontendPreview(bool $showHiddenPages, bool $showHiddenRecords, int $simulateDate, int $simulateUserGroup, ServerRequestInterface $request)
Definition: PreviewModule.php:163
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractModule
Definition: AbstractModule.php:31
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getPageSettings
‪getPageSettings()
Definition: PreviewModule.php:106
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getIdentifier
‪getIdentifier()
Definition: PreviewModule.php:57
‪TYPO3\CMS\Adminpanel\Repositories\FrontendGroupsRepository
Definition: FrontendGroupsRepository.php:32
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getCssFiles
‪array getCssFiles()
Definition: PreviewModule.php:260
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Adminpanel\ModuleApi\ResourceProviderInterface
Definition: ResourceProviderInterface.php:27
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Context\Context\setAspect
‪setAspect(string $name, AspectInterface $aspect)
Definition: Context.php:162
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\enrich
‪enrich(ServerRequestInterface $request)
Definition: PreviewModule.php:75
‪TYPO3\CMS\Adminpanel\Modules
Definition: CacheModule.php:18
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractModule\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractModule.php:121
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getConfigOptionForModule
‪string getConfigOptionForModule(string $option)
Definition: PreviewModule.php:144
‪TYPO3\CMS\Adminpanel\ModuleApi\PageSettingsProviderInterface
Definition: PageSettingsProviderInterface.php:32
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getIconIdentifier
‪getIconIdentifier()
Definition: PreviewModule.php:49
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\clearPreviewSettings
‪clearPreviewSettings(Context $context)
Definition: PreviewModule.php:247
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule
Definition: PreviewModule.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Aspect\PreviewAspect
Definition: PreviewAspect.php:30
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule\getLabel
‪getLabel()
Definition: PreviewModule.php:65
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Adminpanel\ModuleApi\RequestEnricherInterface
Definition: RequestEnricherInterface.php:37