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