‪TYPO3CMS  10.4
PreviewController.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\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
36 use TYPO3Fluid\Fluid\View\ViewInterface;
37 
43 {
47  protected ‪$stageService;
48 
52  protected ‪$workspaceService;
53 
57  protected ‪$pageId;
58 
64  protected ‪$moduleTemplate;
65 
69  protected ‪$view;
70 
74  public function ‪__construct()
75  {
76  $this->stageService = GeneralUtility::makeInstance(StagesService::class);
77  $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
78  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
79  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Workspaces/Preview');
80  $this->moduleTemplate->getDocHeaderComponent()->disable();
81  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
82  $states = $this->‪getBackendUser()->uc['moduleData']['Workspaces']['States'];
83  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'States', $states);
84  $this->moduleTemplate->getPageRenderer()->addInlineSetting('FormEngine', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_edit'));
85  $this->moduleTemplate->getPageRenderer()->addInlineSetting('RecordHistory', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_history'));
86  $this->moduleTemplate->getPageRenderer()->addJsInlineCode('workspace-inline-code', $this->‪generateJavascript());
87  $this->moduleTemplate->getPageRenderer()->addCssFile('EXT:workspaces/Resources/Public/Css/preview.css');
88  $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/wizard.xlf');
89  $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelFile('EXT:workspaces/Resources/Private/Language/locallang.xlf');
90  }
91 
97  protected function ‪initializeView(string $templateName)
98  {
99  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
100  $this->view->setTemplate($templateName);
101  $this->view->setTemplateRootPaths(['EXT:workspaces/Resources/Private/Templates/Preview']);
102  $this->view->setPartialRootPaths(['EXT:workspaces/Resources/Private/Partials']);
103  $this->view->setLayoutRootPaths(['EXT:workspaces/Resources/Private/Layouts']);
104  }
105 
115  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
116  {
117  $liveUrl = false;
118  $this->‪initializeView('Index');
119 
120  // Get all the GET parameters to pass them on to the frames
121  $queryParameters = $request->getQueryParams();
122 
123  $previewWS = $queryParameters['previewWS'] ?? null;
124  $this->pageId = (int)$queryParameters['id'];
125 
126  // Remove the GET parameters related to the workspaces module
127  unset($queryParameters['route'], $queryParameters['token'], $queryParameters['previewWS']);
128 
129  // fetch the next and previous stage
130  $workspaceItemsArray = $this->workspaceService->selectVersionsInWorkspace(
131  $this->stageService->getWorkspaceId(),
132  $filter = 1,
133  $stage = -99,
134  $this->pageId,
135  $recursionLevel = 0,
136  $selectionType = 'tables_modify'
137  );
138  [, $nextStage] = $this->stageService->getNextStageForElementCollection($workspaceItemsArray);
139  [, $previousStage] = $this->stageService->getPreviousStageForElementCollection($workspaceItemsArray);
140  $availableWorkspaces = $this->workspaceService->getAvailableWorkspaces();
141  $activeWorkspace = $this->‪getBackendUser()->workspace;
142  if ($previewWS !== null && array_key_exists($previewWS, $availableWorkspaces) && $activeWorkspace != $previewWS) {
143  $activeWorkspace = $previewWS;
144  $this->‪getBackendUser()->‪setWorkspace($activeWorkspace);
145  ‪BackendUtility::setUpdateSignal('updatePageTree');
146  }
147 
148  $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
149  try {
150  $site = $siteFinder->getSiteByPageId($this->pageId);
151  if (isset($queryParameters['L'])) {
152  $queryParameters['_language'] = $site->getLanguageById((int)$queryParameters['L']);
153  unset($queryParameters['L']);
154  }
155  $parameters = $queryParameters;
156  if (!‪WorkspaceService::isNewPage($this->pageId)) {
157  $parameters['ADMCMD_prev'] = 'LIVE';
158  $liveUrl = (string)$site->getRouter()->generateUri($this->pageId, $parameters);
159  }
160 
161  $parameters = $queryParameters;
162  $parameters['ADMCMD_prev'] = 'IGNORE';
163  $wsUrl = (string)$site->getRouter()->generateUri($this->pageId, $parameters);
164  } catch (SiteNotFoundException | InvalidRouteArgumentsException $e) {
165  throw new UnableToLinkToPageException('The page ' . $this->pageId . ' had no proper connection to a site, no link could be built.', 1559794913);
166  }
167 
168  // Evaluate available preview modes
169  $splitPreviewModes = ‪GeneralUtility::trimExplode(
170  ',',
171  ‪BackendUtility::getPagesTSconfig($this->pageId)['workspaces.']['splitPreviewModes'] ?? '',
172  true
173  );
174  $allPreviewModes = ['slider', 'vbox', 'hbox'];
175  if (!array_intersect($splitPreviewModes, $allPreviewModes)) {
176  $splitPreviewModes = $allPreviewModes;
177  }
178  $this->moduleTemplate->getPageRenderer()->addJsFile('EXT:backend/Resources/Public/JavaScript/backend.js');
179  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'SplitPreviewModes', $splitPreviewModes);
180  $this->moduleTemplate->getPageRenderer()->addInlineSetting('Workspaces', 'id', $this->pageId);
181 
182  $this->view->assignMultiple([
184  'liveUrl' => $liveUrl ?? false,
185  'wsUrl' => $wsUrl,
186  'activeWorkspace' => $availableWorkspaces[$activeWorkspace],
187  'splitPreviewModes' => $splitPreviewModes,
188  'firstPreviewMode' => current($splitPreviewModes),
189  'enablePreviousStageButton' => $this->‪isValidStage($previousStage),
190  'enableNextStageButton' => $this->‪isValidStage($nextStage),
191  'enableDiscardStageButton' => $this->‪isValidStage($nextStage) || $this->‪isValidStage($previousStage),
192  'nextStage' => $nextStage['title'],
193  'nextStageId' => $nextStage['uid'],
194  'prevStage' => $previousStage['title'],
195  'prevStageId' => $previousStage['uid'],
196  ]);
197 
198  $this->moduleTemplate->setContent($this->view->render());
199  return new HtmlResponse($this->moduleTemplate->renderContent());
200  }
201 
208  protected function ‪isValidStage($stageArray): bool
209  {
210  return is_array($stageArray) && !empty($stageArray);
211  }
212 
220  protected function ‪generateJavascript(): string
221  {
222  // Needed for FormEngine manipulation (date picker)
223  $dateFormat = (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? ['MM-DD-YYYY', 'HH:mm MM-DD-YYYY'] : ['DD-MM-YYYY', 'HH:mm DD-MM-YYYY']);
224  $this->moduleTemplate->getPageRenderer()->addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
225 
226  // If another page module was specified, replace the default Page module with the new one
227  $pageModule = \trim($this->‪getBackendUser()->getTSConfig()['options.']['overridePageModule'] ?? '');
228  $pageModule = ‪BackendUtility::isModuleSetInTBE_MODULES($pageModule) ? $pageModule : 'web_layout';
229  $pageModuleUrl = '';
230  if (!$this->‪getBackendUser()->check('modules', $pageModule)) {
231  $pageModule = '';
232  } else {
233  $pageModuleUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute($pageModule);
234  }
235  $t3Configuration = [
236  'username' => htmlspecialchars($this->‪getBackendUser()->user['username']),
237  'pageModule' => $pageModule,
238  'pageModuleUrl' => $pageModuleUrl,
239  'inWorkspace' => $this->‪getBackendUser()->workspace !== 0,
240  'showRefreshLoginPopup' => (bool)(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['showRefreshLoginPopup'] ?? false)
241  ];
242 
243  return 'TYPO3.configuration = ' . json_encode($t3Configuration) . ';';
244  }
245 
249  protected function ‪getBackendUser(): ‪BackendUserAuthentication
250  {
251  return ‪$GLOBALS['BE_USER'];
252  }
253 }
‪TYPO3\CMS\Workspaces\Controller\PreviewController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: PreviewController.php:244
‪TYPO3\CMS\Backend\Utility\BackendUtility\isModuleSetInTBE_MODULES
‪static bool isModuleSetInTBE_MODULES($modName)
Definition: BackendUtility.php:3287
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:26
‪TYPO3\CMS\Workspaces\Controller\PreviewController\isValidStage
‪bool isValidStage($stageArray)
Definition: PreviewController.php:203
‪TYPO3\CMS\Backend\Utility\BackendUtility\setUpdateSignal
‪static setUpdateSignal($set='', $params='')
Definition: BackendUtility.php:2798
‪TYPO3\CMS\Workspaces\Controller\PreviewController
Definition: PreviewController.php:43
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:26
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Workspaces\Controller\PreviewController\__construct
‪__construct()
Definition: PreviewController.php:69
‪TYPO3\CMS\Workspaces\Controller\PreviewController\generateJavascript
‪string generateJavascript()
Definition: PreviewController.php:215
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Workspaces\Controller\PreviewController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: PreviewController.php:110
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: PreviewController.php:60
‪TYPO3\CMS\Core\Information\Typo3Information\URL_COMMUNITY
‪const URL_COMMUNITY
Definition: Typo3Information.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\setWorkspace
‪setWorkspace($workspaceId)
Definition: BackendUserAuthentication.php:2186
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id)
Definition: BackendUtility.php:698
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$workspaceService
‪WorkspaceService $workspaceService
Definition: PreviewController.php:50
‪TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
Definition: InvalidRouteArgumentsException.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$pageId
‪int $pageId
Definition: PreviewController.php:54
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Workspaces\Service\WorkspaceService
Definition: WorkspaceService.php:36
‪TYPO3\CMS\Workspaces\Service\StagesService
Definition: StagesService.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Workspaces\Service\WorkspaceService\isNewPage
‪static bool isNewPage($id, $language=0)
Definition: WorkspaceService.php:712
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$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:92
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$stageService
‪StagesService $stageService
Definition: PreviewController.php:46
‪TYPO3\CMS\Workspaces\Controller\PreviewController\$view
‪ViewInterface $view
Definition: PreviewController.php:64
‪TYPO3\CMS\Workspaces\Controller
Definition: AjaxController.php:16
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26