‪TYPO3CMS  9.5
PreviewController.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
33 use TYPO3Fluid\Fluid\View\ViewInterface;
34 
40 {
44  protected ‪$stageService;
45 
49  protected ‪$workspaceService;
50 
54  protected ‪$pageId;
55 
61  protected ‪$moduleTemplate;
62 
66  protected ‪$view;
67 
71  public function ‪__construct()
72  {
73  $this->stageService = GeneralUtility::makeInstance(StagesService::class);
74  $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
75  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
76  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Workspaces/Preview');
77  $this->moduleTemplate->getDocHeaderComponent()->disable();
78  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
79  $states = $this->‪getBackendUser()->uc['moduleData']['Workspaces']['States'];
80  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'States', $states);
81  $this->moduleTemplate->getPageRenderer()->addInlineSetting('FormEngine', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_edit'));
82  $this->moduleTemplate->getPageRenderer()->addInlineSetting('RecordHistory', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_history'));
83  $this->moduleTemplate->getPageRenderer()->addJsInlineCode('workspace-inline-code', $this->‪generateJavascript());
84  $this->moduleTemplate->getPageRenderer()->addCssFile('EXT:workspaces/Resources/Public/Css/preview.css');
85  $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/wizard.xlf');
86  $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:workspaces/Resources/Private/Language/locallang.xlf');
87  }
88 
94  protected function ‪initializeView(string $templateName)
95  {
96  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
97  $this->view->setTemplate($templateName);
98  $this->view->setTemplateRootPaths(['EXT:workspaces/Resources/Private/Templates/Preview']);
99  $this->view->setPartialRootPaths(['EXT:workspaces/Resources/Private/Partials']);
100  $this->view->setLayoutRootPaths(['EXT:workspaces/Resources/Private/Layouts']);
101  }
102 
112  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
113  {
114  $this->‪initializeView('Index');
115 
116  // Get all the GET parameters to pass them on to the frames
117  $queryParameters = $request->getQueryParams();
118 
119  $previewWS = $queryParameters['previewWS'] ?? null;
120  $this->pageId = (int)$queryParameters['id'];
121 
122  // Remove the GET parameters related to the workspaces module
123  unset($queryParameters['route'], $queryParameters['token'], $queryParameters['previewWS']);
124 
125  // Assemble a query string from the retrieved parameters
126  $queryString = ‪HttpUtility::buildQueryString($queryParameters, '&');
127 
128  // fetch the next and previous stage
129  $workspaceItemsArray = $this->workspaceService->selectVersionsInWorkspace(
130  $this->stageService->getWorkspaceId(),
131  $filter = 1,
132  $stage = -99,
133  $this->pageId,
134  $recursionLevel = 0,
135  $selectionType = 'tables_modify'
136  );
137  list(, $nextStage) = $this->stageService->getNextStageForElementCollection($workspaceItemsArray);
138  list(, $previousStage) = $this->stageService->getPreviousStageForElementCollection($workspaceItemsArray);
139  $availableWorkspaces = $this->workspaceService->getAvailableWorkspaces();
140  $activeWorkspace = $this->‪getBackendUser()->workspace;
141  if ($previewWS !== null && array_key_exists($previewWS, $availableWorkspaces) && $activeWorkspace != $previewWS) {
142  $activeWorkspace = $previewWS;
143  $this->‪getBackendUser()->‪setWorkspace($activeWorkspace);
144  ‪BackendUtility::setUpdateSignal('updatePageTree');
145  }
146 
147  $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
148  try {
149  $site = $siteFinder->getSiteByPageId($this->pageId);
150  if (isset($queryParameters['L'])) {
151  $queryParameters['_language'] = $site->getLanguageById((int)$queryParameters['L']);
152  unset($queryParameters['L']);
153  }
154  if (!‪WorkspaceService::isNewPage($this->pageId)) {
155  $parameters = $queryParameters;
156  $parameters['ADMCMD_noBeUser'] = 1;
157  $parameters['ADMCMD_prev'] = 'IGNORE';
158  $liveUrl = (string)$site->getRouter()->generateUri($this->pageId, $parameters);
159  }
160 
161  $parameters = $queryParameters;
162  $parameters['ADMCMD_view'] = 1;
163  $parameters['ADMCMD_editIcons'] = 1;
164  $parameters['ADMCMD_prev'] = 'IGNORE';
165  $wsUrl = (string)$site->getRouter()->generateUri($this->pageId, $parameters);
166  } catch (SiteNotFoundException | InvalidRouteArgumentsException $e) {
167  // Base URL for frontend preview links
168  $previewBaseUrl = ‪BackendUtility::getViewDomain($this->pageId) . '/index.php?' . $queryString;
169  if (!‪WorkspaceService::isNewPage($this->pageId)) {
170  $liveUrl = $previewBaseUrl . '&ADMCMD_noBeUser=1&ADMCMD_prev=IGNORE';
171  }
172  $wsUrl = $previewBaseUrl . '&ADMCMD_prev=IGNORE&ADMCMD_view=1&ADMCMD_editIcons=1';
173  }
174 
175  // Build the "list view" link to the review controller
176  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
177  $wsSettingsUrl = $uriBuilder->buildUriFromRoute('web_WorkspacesWorkspaces', [
178  'tx_workspaces_web_workspacesworkspaces' => ['action' => 'singleIndex'],
179  'id' => $this->pageId
181 
182  // Evaluate available preview modes
183  $splitPreviewModes = GeneralUtility::trimExplode(
184  ',',
185  ‪BackendUtility::getPagesTSconfig($this->pageId)['workspaces.']['splitPreviewModes'] ?? '',
186  true
187  );
188  $allPreviewModes = ['slider', 'vbox', 'hbox'];
189  if (!array_intersect($splitPreviewModes, $allPreviewModes)) {
190  $splitPreviewModes = $allPreviewModes;
191  }
192  $this->moduleTemplate->getPageRenderer()->addJsFile('EXT:backend/Resources/Public/JavaScript/backend.js');
193  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'SplitPreviewModes', $splitPreviewModes);
194  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'id', $this->pageId);
195 
196  $this->view->assignMultiple([
197  'logoLink' => TYPO3_URL_GENERAL,
198  'liveUrl' => $liveUrl ?? false,
199  'wsUrl' => $wsUrl,
200  'wsSettingsUrl' => $wsSettingsUrl,
201  'activeWorkspace' => $availableWorkspaces[$activeWorkspace],
202  'splitPreviewModes' => $splitPreviewModes,
203  'firstPreviewMode' => current($splitPreviewModes),
204  'enablePreviousStageButton' => $this->‪isValidStage($previousStage),
205  'enableNextStageButton' => $this->‪isValidStage($nextStage),
206  'enableDiscardStageButton' => $this->‪isValidStage($nextStage) || $this->‪isValidStage($previousStage),
207  'nextStage' => $nextStage['title'],
208  'nextStageId' => $nextStage['uid'],
209  'prevStage' => $previousStage['title'],
210  'prevStageId' => $previousStage['uid'],
211  ]);
212 
213  $this->moduleTemplate->setContent($this->view->render());
214  return new HtmlResponse($this->moduleTemplate->renderContent());
215  }
216 
223  protected function ‪isValidStage($stageArray): bool
224  {
225  return is_array($stageArray) && !empty($stageArray);
226  }
227 
235  protected function ‪generateJavascript(): string
236  {
237  // Needed for FormEngine manipulation (date picker)
238  $dateFormat = (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? ['MM-DD-YYYY', 'HH:mm MM-DD-YYYY'] : ['DD-MM-YYYY', 'HH:mm DD-MM-YYYY']);
239  $this->moduleTemplate->getPageRenderer()->addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
240 
241  // If another page module was specified, replace the default Page module with the new one
242  $pageModule = \trim($this->‪getBackendUser()->getTSConfig()['options.']['overridePageModule'] ?? '');
243  $pageModule = ‪BackendUtility::isModuleSetInTBE_MODULES($pageModule) ? $pageModule : 'web_layout';
244  $pageModuleUrl = '';
245  if (!$this->‪getBackendUser()->check('modules', $pageModule)) {
246  $pageModule = '';
247  } else {
248  $pageModuleUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute($pageModule);
249  }
250  $t3Configuration = [
251  'username' => htmlspecialchars($this->‪getBackendUser()->user['username']),
252  'pageModule' => $pageModule,
253  'pageModuleUrl' => $pageModuleUrl,
254  'inWorkspace' => $this->‪getBackendUser()->workspace !== 0,
255  'showRefreshLoginPopup' => (bool)(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['showRefreshLoginPopup'] ?? false)
256  ];
257 
258  return 'TYPO3.configuration = ' . json_encode($t3Configuration) . ';';
259  }
260 
264  protected function ‪getBackendUser(): ‪BackendUserAuthentication
265  {
266  return ‪$GLOBALS['BE_USER'];
267  }
268 }
‪TYPO3\CMS\Workspaces\Controller\PreviewController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: PreviewController.php:259
‪TYPO3\CMS\Backend\Utility\BackendUtility\isModuleSetInTBE_MODULES
‪static bool isModuleSetInTBE_MODULES($modName)
Definition: BackendUtility.php:3763
‪TYPO3\CMS\Workspaces\Controller\PreviewController\isValidStage
‪bool isValidStage($stageArray)
Definition: PreviewController.php:218
‪TYPO3\CMS\Backend\Utility\BackendUtility\setUpdateSignal
‪static setUpdateSignal($set='', $params='')
Definition: BackendUtility.php:3164
‪TYPO3\CMS\Workspaces\Controller\PreviewController
Definition: PreviewController.php:40
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Workspaces\Controller\PreviewController\__construct
‪__construct()
Definition: PreviewController.php:66
‪TYPO3\CMS\Workspaces\Controller\PreviewController\generateJavascript
‪string generateJavascript()
Definition: PreviewController.php:230
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Workspaces\Controller\PreviewController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: PreviewController.php:107
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: PreviewController.php:57
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\setWorkspace
‪setWorkspace($workspaceId)
Definition: BackendUserAuthentication.php:2250
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:160
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$workspaceService
‪WorkspaceService $workspaceService
Definition: PreviewController.php:47
‪TYPO3\CMS\Backend\Utility\BackendUtility\getViewDomain
‪static string getViewDomain($pageId, $rootLine=null)
Definition: BackendUtility.php:2864
‪TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
Definition: InvalidRouteArgumentsException.php:23
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$pageId
‪int $pageId
Definition: PreviewController.php:51
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:21
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Workspaces\Service\WorkspaceService
Definition: WorkspaceService.php:34
‪TYPO3\CMS\Workspaces\Service\StagesService
Definition: StagesService.php:31
‪TYPO3\CMS\Workspaces\Service\WorkspaceService\isNewPage
‪static bool isNewPage($id, $language=0)
Definition: WorkspaceService.php:720
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
Definition: BackendUtility.php:864
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Workspaces\Controller\PreviewController\initializeView
‪initializeView(string $templateName)
Definition: PreviewController.php:89
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$stageService
‪StagesService $stageService
Definition: PreviewController.php:43
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:21
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$view
‪ViewInterface $view
Definition: PreviewController.php:61
‪TYPO3\CMS\Workspaces\Controller
Definition: AjaxController.php:2
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:39
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25