[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / ReviewController.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\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
22 use TYPO3\CMS\Workspaces\Service\WorkspaceService;
23
24 /**
25 * Review controller.
26 */
27 class ReviewController extends AbstractController
28 {
29 /**
30 * Set up the doc header properly here
31 *
32 * @param ViewInterface $view
33 */
34 protected function initializeView(ViewInterface $view)
35 {
36 parent::initializeView($view);
37 $this->registerButtons();
38 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
39 }
40
41 /**
42 * Registers the DocHeader buttons
43 */
44 protected function registerButtons()
45 {
46 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
47 $currentRequest = $this->request;
48 $moduleName = $currentRequest->getPluginName();
49 $getVars = $this->request->getArguments();
50 $extensionName = $currentRequest->getControllerExtensionName();
51 if (count($getVars) === 0) {
52 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
53 $getVars = ['id', 'route', $modulePrefix];
54 }
55 $shortcutButton = $buttonBar->makeShortcutButton()
56 ->setModuleName($moduleName)
57 ->setGetVariables($getVars);
58 $buttonBar->addButton($shortcutButton);
59 }
60
61 /**
62 * Renders the review module user dependent with all workspaces.
63 * The module will show all records of one workspace.
64 */
65 public function indexAction()
66 {
67 $backendUser = $this->getBackendUser();
68 $moduleTemplate = $this->view->getModuleTemplate();
69
70 /** @var WorkspaceService $wsService */
71 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
72 if (GeneralUtility::_GP('id')) {
73 $pageRecord = BackendUtility::getRecord('pages', GeneralUtility::_GP('id'));
74 if ($pageRecord) {
75 $moduleTemplate->getDocHeaderComponent()->setMetaInformation($pageRecord);
76 $this->view->assign('pageTitle', BackendUtility::getRecordTitle('pages', $pageRecord));
77 }
78 }
79 $wsList = $wsService->getAvailableWorkspaces();
80 $activeWorkspace = $backendUser->workspace;
81 $performWorkspaceSwitch = false;
82 // Only admins see multiple tabs, we decided to use it this
83 // way for usability reasons. Regular users might be confused
84 // by switching workspaces with the tabs in a module.
85 if (!$backendUser->isAdmin()) {
86 $wsCur = [$activeWorkspace => true];
87 $wsList = array_intersect_key($wsList, $wsCur);
88 } else {
89 if ((string)GeneralUtility::_GP('workspace') !== '') {
90 $switchWs = (int)GeneralUtility::_GP('workspace');
91 if (in_array($switchWs, array_keys($wsList)) && $activeWorkspace != $switchWs) {
92 $activeWorkspace = $switchWs;
93 $backendUser->setWorkspace($activeWorkspace);
94 $performWorkspaceSwitch = true;
95 BackendUtility::setUpdateSignal('updatePageTree');
96 } elseif ($switchWs == WorkspaceService::SELECT_ALL_WORKSPACES) {
97 $this->redirect('fullIndex');
98 }
99 }
100 }
101 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
102 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
103 $this->pageRenderer->addInlineSetting('Workspaces', 'isLiveWorkspace', (int)$backendUser->workspace === 0);
104 $this->pageRenderer->addInlineSetting('Workspaces', 'workspaceTabs', $this->prepareWorkspaceTabs($wsList, $activeWorkspace));
105 $this->pageRenderer->addInlineSetting('Workspaces', 'activeWorkspaceId', $activeWorkspace);
106 $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_edit'));
107 $workspaceIsAccessible = !($backendUser->workspace === 0 && !$backendUser->isAdmin());
108 $this->view->assignMultiple([
109 'showGrid' => $workspaceIsAccessible,
110 'showLegend' => $workspaceIsAccessible,
111 'pageUid' => (int)GeneralUtility::_GP('id'),
112 'performWorkspaceSwitch' => $performWorkspaceSwitch,
113 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace),
114 'activeWorkspaceUid' => $activeWorkspace,
115 'activeWorkspaceTitle' => WorkspaceService::getWorkspaceTitle($activeWorkspace),
116 'showPreviewLink' => $wsService->canCreatePreviewLink(GeneralUtility::_GP('id'), $activeWorkspace)
117 ]);
118
119 if ($wsService->canCreatePreviewLink(GeneralUtility::_GP('id'), $activeWorkspace)) {
120 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
121 $iconFactory = $moduleTemplate->getIconFactory();
122 $showButton = $buttonBar->makeLinkButton()
123 ->setHref('#')
124 ->setClasses('t3js-preview-link')
125 ->setTitle($this->getLanguageService()->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf:tooltip.generatePagePreview'))
126 ->setIcon($iconFactory->getIcon('module-workspaces-action-preview-link', Icon::SIZE_SMALL));
127 $buttonBar->addButton($showButton);
128 }
129 $backendUser->setAndSaveSessionData('tx_workspace_activeWorkspace', $activeWorkspace);
130 }
131
132 /**
133 * Renders the review module user dependent.
134 * The module will show all records of all workspaces.
135 */
136 public function fullIndexAction()
137 {
138 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
139 $wsList = $wsService->getAvailableWorkspaces();
140
141 $activeWorkspace = $this->getBackendUser()->workspace;
142 if (!$this->getBackendUser()->isAdmin()) {
143 $wsCur = [$activeWorkspace => true];
144 $wsList = array_intersect_key($wsList, $wsCur);
145 }
146
147 $this->pageRenderer->addInlineSetting('Workspaces', 'workspaceTabs', $this->prepareWorkspaceTabs($wsList, WorkspaceService::SELECT_ALL_WORKSPACES));
148 $this->pageRenderer->addInlineSetting('Workspaces', 'activeWorkspaceId', WorkspaceService::SELECT_ALL_WORKSPACES);
149 $this->view->assignMultiple([
150 'pageUid' => (int)GeneralUtility::_GP('id'),
151 'showGrid' => true,
152 'showLegend' => true,
153 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace),
154 'activeWorkspaceUid' => WorkspaceService::SELECT_ALL_WORKSPACES,
155 'showPreviewLink', false
156 ]);
157 $this->getBackendUser()->setAndSaveSessionData('tx_workspace_activeWorkspace', WorkspaceService::SELECT_ALL_WORKSPACES);
158 // set flag for javascript
159 $this->pageRenderer->addInlineSetting('Workspaces', 'allView', '1');
160 }
161
162 /**
163 * Renders the review module for a single page. This is used within the
164 * workspace-preview frame.
165 */
166 public function singleIndexAction()
167 {
168 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
169 $wsList = $wsService->getAvailableWorkspaces();
170 $activeWorkspace = $this->getBackendUser()->workspace;
171 $wsCur = [$activeWorkspace => true];
172 $wsList = array_intersect_key($wsList, $wsCur);
173 $backendDomain = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
174 $this->view->assignMultiple([
175 'pageUid' => (int)GeneralUtility::_GP('id'),
176 'showGrid' => true,
177 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace, false),
178 'activeWorkspaceUid' => $activeWorkspace,
179 'backendDomain' => $backendDomain
180 ]);
181 // Setting the document.domain early before JavScript
182 // libraries are loaded, try to access top frame reference
183 // and possibly run into some CORS issue
184 $this->pageRenderer->setMetaCharsetTag(
185 $this->pageRenderer->getMetaCharsetTag() . LF
186 . GeneralUtility::wrapJS('document.domain = ' . GeneralUtility::quoteJSvalue($backendDomain) . ';')
187 );
188 $this->pageRenderer->addInlineSetting('Workspaces', 'singleView', '1');
189 }
190
191 /**
192 * Initializes the controller before invoking an action method.
193 */
194 protected function initializeAction()
195 {
196 parent::initializeAction();
197 $states = $this->getBackendUser()->uc['moduleData']['Workspaces']['States'];
198 $this->pageRenderer->addInlineSetting('Workspaces', 'States', $states);
199
200 foreach ($this->getAdditionalResourceService()->getLocalizationResources() as $localizationResource) {
201 $this->pageRenderer->addInlineLanguageLabelFile($localizationResource);
202 }
203 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
204 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
205
206 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Backend');
207 $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_edit'));
208 $this->pageRenderer->addInlineSetting('RecordHistory', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_history'));
209 $this->pageRenderer->addInlineSetting('Workspaces', 'id', (int)GeneralUtility::_GP('id'));
210 }
211
212 /**
213 * Prepares available workspace tabs.
214 *
215 * @param array $workspaceList
216 * @param int $activeWorkspace
217 * @param bool $showAllWorkspaceTab
218 * @return array
219 */
220 protected function prepareWorkspaceTabs(array $workspaceList, $activeWorkspace, $showAllWorkspaceTab = true)
221 {
222 $tabs = [];
223
224 if ($activeWorkspace !== WorkspaceService::SELECT_ALL_WORKSPACES) {
225 $tabs[] = [
226 'title' => $workspaceList[$activeWorkspace],
227 'itemId' => 'workspace-' . $activeWorkspace,
228 'workspaceId' => $activeWorkspace,
229 'triggerUrl' => $this->getModuleUri($activeWorkspace),
230 ];
231 }
232
233 if ($showAllWorkspaceTab) {
234 $tabs[] = [
235 'title' => 'All workspaces',
236 'itemId' => 'workspace-' . WorkspaceService::SELECT_ALL_WORKSPACES,
237 'workspaceId' => WorkspaceService::SELECT_ALL_WORKSPACES,
238 'triggerUrl' => $this->getModuleUri(WorkspaceService::SELECT_ALL_WORKSPACES),
239 ];
240 }
241
242 foreach ($workspaceList as $workspaceId => $workspaceTitle) {
243 if ($workspaceId === $activeWorkspace) {
244 continue;
245 }
246 $tabs[] = [
247 'title' => $workspaceTitle,
248 'itemId' => 'workspace-' . $workspaceId,
249 'workspaceId' => $workspaceId,
250 'triggerUrl' => $this->getModuleUri($workspaceId),
251 ];
252 }
253
254 return $tabs;
255 }
256
257 /**
258 * Gets the module URI.
259 *
260 * @param int $workspaceId
261 * @return string
262 */
263 protected function getModuleUri($workspaceId)
264 {
265 $parameters = [
266 'id' => (int)$this->pageId,
267 'workspace' => (int)$workspaceId,
268 ];
269 // The "all workspaces" tab is handled in fullIndexAction
270 // which is required as additional GET parameter in the URI then
271 if ($workspaceId === WorkspaceService::SELECT_ALL_WORKSPACES) {
272 $this->uriBuilder->reset()->uriFor('fullIndex');
273 $parameters = array_merge($parameters, $this->uriBuilder->getArguments());
274 }
275 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
276 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
277 return (string)$uriBuilder->buildUriFromRoute('web_WorkspacesWorkspaces', $parameters);
278 }
279
280 /**
281 * @return \TYPO3\CMS\Core\Localization\LanguageService
282 */
283 protected function getLanguageService()
284 {
285 return $GLOBALS['LANG'];
286 }
287
288 /**
289 * @return BackendUserAuthentication
290 */
291 protected function getBackendUser()
292 {
293 return $GLOBALS['BE_USER'];
294 }
295 }