6c21f5a0dd2aabe34dec17b0101cac42d7b285ce
[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 $this->pageRenderer->addInlineSetting('Workspaces', 'isLiveWorkspace', (int)$backendUser->workspace === 0);
102 $this->pageRenderer->addInlineSetting('Workspaces', 'workspaceTabs', $this->prepareWorkspaceTabs($wsList, $activeWorkspace));
103 $this->pageRenderer->addInlineSetting('Workspaces', 'activeWorkspaceId', $activeWorkspace);
104 $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', BackendUtility::getModuleUrl('record_edit'));
105 $workspaceIsAccessible = !($backendUser->workspace === 0 && !$backendUser->isAdmin());
106 $this->view->assignMultiple([
107 'showGrid' => $workspaceIsAccessible,
108 'showLegend' => $workspaceIsAccessible,
109 'pageUid' => (int)GeneralUtility::_GP('id'),
110 'performWorkspaceSwitch' => $performWorkspaceSwitch,
111 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace),
112 'activeWorkspaceUid' => $activeWorkspace,
113 'activeWorkspaceTitle' => WorkspaceService::getWorkspaceTitle($activeWorkspace),
114 'showPreviewLink' => $wsService->canCreatePreviewLink(GeneralUtility::_GP('id'), $activeWorkspace)
115 ]);
116
117 if ($wsService->canCreatePreviewLink(GeneralUtility::_GP('id'), $activeWorkspace)) {
118 $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
119 $iconFactory = $moduleTemplate->getIconFactory();
120 $showButton = $buttonBar->makeLinkButton()
121 ->setHref('#')
122 ->setClasses('t3js-preview-link')
123 ->setTitle($this->getLanguageService()->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf:tooltip.generatePagePreview'))
124 ->setIcon($iconFactory->getIcon('module-workspaces-action-preview-link', Icon::SIZE_SMALL));
125 $buttonBar->addButton($showButton);
126 }
127 $backendUser->setAndSaveSessionData('tx_workspace_activeWorkspace', $activeWorkspace);
128 }
129
130 /**
131 * Renders the review module user dependent.
132 * The module will show all records of all workspaces.
133 */
134 public function fullIndexAction()
135 {
136 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
137 $wsList = $wsService->getAvailableWorkspaces();
138
139 $activeWorkspace = $this->getBackendUser()->workspace;
140 if (!$this->getBackendUser()->isAdmin()) {
141 $wsCur = [$activeWorkspace => true];
142 $wsList = array_intersect_key($wsList, $wsCur);
143 }
144
145 $this->pageRenderer->addInlineSetting('Workspaces', 'workspaceTabs', $this->prepareWorkspaceTabs($wsList, WorkspaceService::SELECT_ALL_WORKSPACES));
146 $this->pageRenderer->addInlineSetting('Workspaces', 'activeWorkspaceId', WorkspaceService::SELECT_ALL_WORKSPACES);
147 $this->view->assignMultiple([
148 'pageUid' => (int)GeneralUtility::_GP('id'),
149 'showGrid' => true,
150 'showLegend' => true,
151 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace),
152 'activeWorkspaceUid' => WorkspaceService::SELECT_ALL_WORKSPACES,
153 'showPreviewLink', false
154 ]);
155 $this->getBackendUser()->setAndSaveSessionData('tx_workspace_activeWorkspace', WorkspaceService::SELECT_ALL_WORKSPACES);
156 // set flag for javascript
157 $this->pageRenderer->addInlineSetting('Workspaces', 'allView', '1');
158 }
159
160 /**
161 * Renders the review module for a single page. This is used within the
162 * workspace-preview frame.
163 */
164 public function singleIndexAction()
165 {
166 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
167 $wsList = $wsService->getAvailableWorkspaces();
168 $activeWorkspace = $this->getBackendUser()->workspace;
169 $wsCur = [$activeWorkspace => true];
170 $wsList = array_intersect_key($wsList, $wsCur);
171 $backendDomain = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
172 $this->view->assignMultiple([
173 'pageUid' => (int)GeneralUtility::_GP('id'),
174 'showGrid' => true,
175 'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace, false),
176 'activeWorkspaceUid' => $activeWorkspace,
177 'backendDomain' => $backendDomain
178 ]);
179 // Setting the document.domain early before JavScript
180 // libraries are loaded, try to access top frame reference
181 // and possibly run into some CORS issue
182 $this->pageRenderer->setMetaCharsetTag(
183 $this->pageRenderer->getMetaCharsetTag() . LF
184 . GeneralUtility::wrapJS('document.domain = ' . GeneralUtility::quoteJSvalue($backendDomain) . ';')
185 );
186 $this->pageRenderer->addInlineSetting('Workspaces', 'singleView', '1');
187 }
188
189 /**
190 * Initializes the controller before invoking an action method.
191 */
192 protected function initializeAction()
193 {
194 parent::initializeAction();
195 $states = $this->getBackendUser()->uc['moduleData']['Workspaces']['States'];
196 $this->pageRenderer->addInlineSetting('Workspaces', 'States', $states);
197
198 foreach ($this->getAdditionalResourceService()->getLocalizationResources() as $localizationResource) {
199 $this->pageRenderer->addInlineLanguageLabelFile($localizationResource);
200 }
201 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Backend');
202 $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', BackendUtility::getModuleUrl('record_edit'));
203 $this->pageRenderer->addInlineSetting('RecordHistory', 'moduleUrl', BackendUtility::getModuleUrl('record_history'));
204 $this->pageRenderer->addInlineSetting('Workspaces', 'id', (int)GeneralUtility::_GP('id'));
205 }
206
207 /**
208 * Prepares available workspace tabs.
209 *
210 * @param array $workspaceList
211 * @param int $activeWorkspace
212 * @param bool $showAllWorkspaceTab
213 * @return array
214 */
215 protected function prepareWorkspaceTabs(array $workspaceList, $activeWorkspace, $showAllWorkspaceTab = true)
216 {
217 $tabs = [];
218
219 if ($activeWorkspace !== WorkspaceService::SELECT_ALL_WORKSPACES) {
220 $tabs[] = [
221 'title' => $workspaceList[$activeWorkspace],
222 'itemId' => 'workspace-' . $activeWorkspace,
223 'workspaceId' => $activeWorkspace,
224 'triggerUrl' => $this->getModuleUri($activeWorkspace),
225 ];
226 }
227
228 if ($showAllWorkspaceTab) {
229 $tabs[] = [
230 'title' => 'All workspaces',
231 'itemId' => 'workspace-' . WorkspaceService::SELECT_ALL_WORKSPACES,
232 'workspaceId' => WorkspaceService::SELECT_ALL_WORKSPACES,
233 'triggerUrl' => $this->getModuleUri(WorkspaceService::SELECT_ALL_WORKSPACES),
234 ];
235 }
236
237 foreach ($workspaceList as $workspaceId => $workspaceTitle) {
238 if ($workspaceId === $activeWorkspace) {
239 continue;
240 }
241 $tabs[] = [
242 'title' => $workspaceTitle,
243 'itemId' => 'workspace-' . $workspaceId,
244 'workspaceId' => $workspaceId,
245 'triggerUrl' => $this->getModuleUri($workspaceId),
246 ];
247 }
248
249 return $tabs;
250 }
251
252 /**
253 * Gets the module URI.
254 *
255 * @param int $workspaceId
256 * @return string
257 */
258 protected function getModuleUri($workspaceId)
259 {
260 $parameters = [
261 'id' => (int)$this->pageId,
262 'workspace' => (int)$workspaceId,
263 ];
264 // The "all workspaces" tab is handled in fullIndexAction
265 // which is required as additional GET parameter in the URI then
266 if ($workspaceId === WorkspaceService::SELECT_ALL_WORKSPACES) {
267 $this->uriBuilder->reset()->uriFor('fullIndex');
268 $parameters = array_merge($parameters, $this->uriBuilder->getArguments());
269 }
270 return BackendUtility::getModuleUrl('web_WorkspacesWorkspaces', $parameters);
271 }
272
273 /**
274 * @return \TYPO3\CMS\Core\Localization\LanguageService
275 */
276 protected function getLanguageService()
277 {
278 return $GLOBALS['LANG'];
279 }
280
281 /**
282 * @return BackendUserAuthentication
283 */
284 protected function getBackendUser()
285 {
286 return $GLOBALS['BE_USER'];
287 }
288 }