[TASK] Cleanup inline JavaScript in Backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / PreviewController.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\View\BackendTemplateView;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Core\Messaging\FlashMessageService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
24 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
25 use TYPO3\CMS\Workspaces\Service\StagesService;
26 use TYPO3\CMS\Workspaces\Service\WorkspaceService;
27
28 /**
29 * Implements the preview controller of the workspace module.
30 */
31 class PreviewController extends AbstractController
32 {
33 /**
34 * @var StagesService
35 */
36 protected $stageService;
37
38 /**
39 * @var WorkspaceService
40 */
41 protected $workspaceService;
42
43 /**
44 * Set up the doc header properly here
45 *
46 * @param ViewInterface $view
47 */
48 protected function initializeView(ViewInterface $view)
49 {
50 if ($view instanceof BackendTemplateView) {
51 /** @var BackendTemplateView $view */
52 parent::initializeView($view);
53 $view->getModuleTemplate()->getDocHeaderComponent()->disable();
54 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
55 }
56 }
57
58 /**
59 * Initializes the controller before invoking an action method.
60 */
61 protected function initializeAction()
62 {
63 parent::initializeAction();
64 $this->stageService = GeneralUtility::makeInstance(StagesService::class);
65 $this->workspaceService = GeneralUtility::makeInstance(WorkspaceService::class);
66 $states = $this->getBackendUser()->uc['moduleData']['Workspaces']['States'];
67 $this->pageRenderer->addInlineSetting('Workspaces', 'States', $states);
68 $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', BackendUtility::getModuleUrl('record_edit'));
69 $this->pageRenderer->addInlineSetting('RecordHistory', 'moduleUrl', BackendUtility::getModuleUrl('record_history'));
70 // @todo this part should be done with inlineLocallanglabels
71 $this->pageRenderer->addJsInlineCode('workspace-inline-code', $this->generateJavascript());
72 }
73
74 /**
75 * Basically makes sure that the workspace preview is rendered.
76 * The preview itself consists of three frames, so there are
77 * only the frames-urls we've to generate here
78 *
79 * @param int $previewWS
80 */
81 public function indexAction($previewWS = null)
82 {
83 $backendUser = $this->getBackendUser();
84
85 // Get all the GET parameters to pass them on to the frames
86 $queryParameters = GeneralUtility::_GET();
87
88 // Remove the GET parameters related to the workspaces module and the page id
89 unset($queryParameters['tx_workspaces_web_workspacesworkspaces']);
90 unset($queryParameters['route']);
91 unset($queryParameters['id']);
92
93 // Assemble a query string from the retrieved parameters
94 $queryString = GeneralUtility::implodeArrayForUrl('', $queryParameters);
95
96 // fetch the next and previous stage
97 $workspaceItemsArray = $this->workspaceService->selectVersionsInWorkspace($this->stageService->getWorkspaceId(), ($filter = 1), ($stage = -99), $this->pageId, ($recursionLevel = 0), ($selectionType = 'tables_modify'));
98 list(, $nextStage) = $this->stageService->getNextStageForElementCollection($workspaceItemsArray);
99 list(, $previousStage) = $this->stageService->getPreviousStageForElementCollection($workspaceItemsArray);
100 /** @var $wsService WorkspaceService */
101 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
102 $wsList = $wsService->getAvailableWorkspaces();
103 $activeWorkspace = $backendUser->workspace;
104 if (!is_null($previewWS)) {
105 if (in_array($previewWS, array_keys($wsList)) && $activeWorkspace != $previewWS) {
106 $activeWorkspace = $previewWS;
107 $backendUser->setWorkspace($activeWorkspace);
108 BackendUtility::setUpdateSignal('updatePageTree');
109 }
110 }
111 /** @var $uriBuilder UriBuilder */
112 $uriBuilder = $this->objectManager->get(UriBuilder::class);
113 $wsSettingsPath = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
114 $wsSettingsUri = $uriBuilder->uriFor('singleIndex', [], ReviewController::class, 'workspaces', 'web_workspacesworkspaces');
115 $wsSettingsParams = '&tx_workspaces_web_workspacesworkspaces[controller]=Review';
116 $wsSettingsUrl = $wsSettingsPath . $wsSettingsUri . $wsSettingsParams;
117 $viewDomain = BackendUtility::getViewDomain($this->pageId);
118 $wsBaseUrl = $viewDomain . '/index.php?id=' . $this->pageId . $queryString;
119 // @todo - handle new pages here
120 // branchpoints are not handled anymore because this feature is not supposed anymore
121 if (WorkspaceService::isNewPage($this->pageId)) {
122 $wsNewPageUri = $uriBuilder->uriFor('newPage', [], self::class, 'workspaces', 'web_workspacesworkspaces');
123 $wsNewPageParams = '&tx_workspaces_web_workspacesworkspaces[controller]=Preview';
124 $liveUrl = $wsSettingsPath . $wsNewPageUri . $wsNewPageParams . '&ADMCMD_prev=IGNORE';
125 } else {
126 $liveUrl = $wsBaseUrl . '&ADMCMD_noBeUser=1&ADMCMD_prev=IGNORE';
127 }
128 $wsUrl = $wsBaseUrl . '&ADMCMD_prev=IGNORE&ADMCMD_view=1&ADMCMD_editIcons=1&ADMCMD_previewWS=' . $backendUser->workspace;
129 $backendDomain = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
130 $splitPreviewTsConfig = BackendUtility::getModTSconfig($this->pageId, 'workspaces.splitPreviewModes');
131 $splitPreviewModes = GeneralUtility::trimExplode(',', $splitPreviewTsConfig['value']);
132 $allPreviewModes = ['slider', 'vbox', 'hbox'];
133 if (!array_intersect($splitPreviewModes, $allPreviewModes)) {
134 $splitPreviewModes = $allPreviewModes;
135 }
136
137 $wsList = $wsService->getAvailableWorkspaces();
138 $activeWorkspace = $backendUser->workspace;
139
140 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Preview');
141 $this->pageRenderer->addInlineSetting('Workspaces', 'SplitPreviewModes', $splitPreviewModes);
142
143 $cssFile = 'EXT:workspaces/Resources/Public/Css/preview.css';
144 $cssFile = GeneralUtility::getFileAbsFileName($cssFile);
145 $this->pageRenderer->addCssFile(PathUtility::getAbsoluteWebPath($cssFile));
146
147 $backendUser->setAndSaveSessionData('workspaces.backend_domain', GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'));
148
149 $logoPath = GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Public/Images/typo3_logo_orange.svg');
150 $logoWidth = 22;
151 $logoHeight = 22;
152
153 $this->view->assignMultiple([
154 'logoUrl' => PathUtility::getAbsoluteWebPath($logoPath),
155 'logoLink' => TYPO3_URL_GENERAL,
156 'logoWidth' => $logoWidth,
157 'logoHeight' => $logoHeight,
158 'liveUrl' => $liveUrl,
159 'wsUrl' => $wsUrl,
160 'wsSettingsUrl' => $wsSettingsUrl,
161 'backendDomain' => $backendDomain,
162 'activeWorkspace' => $wsList[$activeWorkspace],
163 'splitPreviewModes' => $splitPreviewModes,
164 'firstPreviewMode' => current($splitPreviewModes),
165 'enablePreviousStageButton' => !$this->isInvalidStage($previousStage),
166 'enableNextStageButton' => !$this->isInvalidStage($nextStage),
167 'enableDiscardStageButton' => !$this->isInvalidStage($nextStage) || !$this->isInvalidStage($previousStage),
168 'nextStage' => $nextStage['title'],
169 'nextStageId' => $nextStage['uid'],
170 'prevStage' => $previousStage['title'],
171 'prevStageId' => $previousStage['uid'],
172 ]);
173 foreach ($this->getAdditionalResourceService()->getLocalizationResources() as $localizationResource) {
174 $this->pageRenderer->addInlineLanguageLabelFile($localizationResource);
175 }
176 }
177
178 /**
179 * Evaluate the activate state based on given $stageArray.
180 *
181 * @param array $stageArray
182 * @return bool
183 */
184 protected function isInvalidStage($stageArray)
185 {
186 return !(is_array($stageArray) && !empty($stageArray));
187 }
188
189 /**
190 */
191 public function newPageAction()
192 {
193 /** @var FlashMessage $flashMessage */
194 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf:info.newpage.detail'), $this->getLanguageService()->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf:info.newpage'), FlashMessage::INFO);
195 /** @var $flashMessageService FlashMessageService */
196 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
197 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
198 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
199 $defaultFlashMessageQueue->enqueue($flashMessage);
200 }
201
202 /**
203 * Generates the JavaScript code for the backend,
204 * and since we're loading a backend module outside of the actual backend
205 * this copies parts of the index.php?M=main module
206 *
207 * @return string
208 */
209 protected function generateJavascript()
210 {
211 $backendUser = $this->getBackendUser();
212 // If another page module was specified, replace the default Page module with the new one
213 $newPageModule = trim($backendUser->getTSConfigVal('options.overridePageModule'));
214 $pageModule = BackendUtility::isModuleSetInTBE_MODULES($newPageModule) ? $newPageModule : 'web_layout';
215 if (!$backendUser->check('modules', $pageModule)) {
216 $pageModule = '';
217 }
218 $t3Configuration = [
219 'username' => htmlspecialchars($backendUser->user['username']),
220 'pageModule' => $pageModule,
221 'inWorkspace' => $backendUser->workspace !== 0,
222 'showRefreshLoginPopup' => isset($GLOBALS['TYPO3_CONF_VARS']['BE']['showRefreshLoginPopup']) ? (int)$GLOBALS['TYPO3_CONF_VARS']['BE']['showRefreshLoginPopup'] : false
223 ];
224
225 return 'TYPO3.configuration = ' . json_encode($t3Configuration) . ';';
226 }
227
228 /**
229 * @return \TYPO3\CMS\Core\Localization\LanguageService
230 */
231 protected function getLanguageService()
232 {
233 return $GLOBALS['LANG'];
234 }
235
236 /**
237 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
238 */
239 protected function getBackendUser()
240 {
241 return $GLOBALS['BE_USER'];
242 }
243 }