Passed
Push — master ( 337e8f...9738f3 )
by
unknown
16:20
created

ReviewController::getAdditionalResourceService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Workspaces\Controller;
17
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Backend\Routing\UriBuilder;
21
use TYPO3\CMS\Backend\Template\ModuleTemplate;
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Backend\View\BackendTemplateView;
24
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
25
use TYPO3\CMS\Core\Http\HtmlResponse;
26
use TYPO3\CMS\Core\Imaging\Icon;
27
use TYPO3\CMS\Core\Imaging\IconFactory;
28
use TYPO3\CMS\Core\Localization\LanguageService;
29
use TYPO3\CMS\Core\Page\PageRenderer;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Core\Versioning\VersionState;
32
use TYPO3\CMS\Fluid\View\StandaloneView;
33
use TYPO3\CMS\Workspaces\Service\WorkspaceService;
34
35
/**
36
 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
37
 */
38
class ReviewController
39
{
40
    /**
41
     * @var ModuleTemplate
42
     */
43
    protected $moduleTemplate;
44
45
    /**
46
     * @var string
47
     */
48
    protected $defaultViewObjectName = BackendTemplateView::class;
49
50
    /**
51
     * @var BackendTemplateView
52
     */
53
    protected $view;
54
55
    /**
56
     * @var PageRenderer
57
     */
58
    protected $pageRenderer;
59
60
    /**
61
     * @var int
62
     */
63
    protected $pageId;
64
65
    public function __construct()
66
    {
67
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
68
    }
69
70
    /**
71
     * Initializes the controller before invoking an action method.
72
     */
73
    protected function initializeAction()
74
    {
75
        $this->pageRenderer = $this->getPageRenderer();
76
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
77
        $lang = $this->getLanguageService();
78
        $icons = [
79
            'language' => $iconFactory->getIcon('flags-multiple', Icon::SIZE_SMALL)->render(),
80
            'integrity' => $iconFactory->getIcon('status-dialog-information', Icon::SIZE_SMALL)->render(),
81
            'success' => $iconFactory->getIcon('status-dialog-ok', Icon::SIZE_SMALL)->render(),
82
            'info' => $iconFactory->getIcon('status-dialog-information', Icon::SIZE_SMALL)->render(),
83
            'warning' => $iconFactory->getIcon('status-dialog-warning', Icon::SIZE_SMALL)->render(),
84
            'error' => $iconFactory->getIcon('status-dialog-error', Icon::SIZE_SMALL)->render()
85
        ];
86
        $this->pageRenderer->addInlineSetting('Workspaces', 'icons', $icons);
87
        $this->pageRenderer->addInlineSetting('Workspaces', 'id', $this->pageId);
88
        $this->pageRenderer->addInlineSetting('Workspaces', 'depth', $this->pageId === 0 ? 999 : 1);
89
        $this->pageRenderer->addInlineSetting('Workspaces', 'language', $this->getLanguageSelection());
90
        $this->pageRenderer->addInlineLanguageLabelArray([
91
            'title' => $lang->getLL('title'),
92
            'path' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.path'),
93
            'table' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.table'),
94
            'depth' => $lang->sL('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:Depth'),
95
            'depth_0' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
96
            'depth_1' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
97
            'depth_2' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
98
            'depth_3' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
99
            'depth_4' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
100
            'depth_infi' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi')
101
        ]);
102
        $this->pageRenderer->addInlineLanguageLabelFile('EXT:workspaces/Resources/Private/Language/locallang.xlf');
103
        $states = $this->getBackendUser()->uc['moduleData']['Workspaces']['States'];
104
        $this->pageRenderer->addInlineSetting('Workspaces', 'States', $states);
105
106
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
107
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Backend');
108
        $this->pageRenderer->addInlineSetting('FormEngine', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_edit'));
109
        $this->pageRenderer->addInlineSetting('RecordHistory', 'moduleUrl', (string)$uriBuilder->buildUriFromRoute('record_history'));
110
        $this->pageRenderer->addInlineSetting('Workspaces', 'id', $this->pageId);
111
    }
112
113
    /**
114
     * Renders the review module user dependent with all workspaces.
115
     * The module will show all records of one workspace.
116
     *
117
     * @param ServerRequestInterface $request
118
     * @return ResponseInterface
119
     */
120
    public function indexAction(ServerRequestInterface $request): ResponseInterface
121
    {
122
        $queryParams = $request->getQueryParams();
123
        $this->pageId = (int)($queryParams['id'] ?? 0);
124
125
        $this->initializeAction();
126
127
        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
128
        $this->view->setTemplate('Index');
129
        // This is only needed for translate VH to resolve 'label only' to default locallang.xlf files
130
        $this->view->getRequest()->setControllerExtensionName('Workspaces');
131
        $this->view->setTemplateRootPaths(['EXT:workspaces/Resources/Private/Templates/Review']);
132
        $this->view->setPartialRootPaths(['EXT:workspaces/Resources/Private/Partials']);
133
        $this->view->setLayoutRootPaths(['EXT:workspaces/Resources/Private/Layouts']);
134
135
        $backendUser = $this->getBackendUser();
136
        $moduleTemplate = $this->moduleTemplate;
137
138
        if ($this->pageId) {
139
            $pageRecord = BackendUtility::getRecord('pages', $this->pageId);
140
            if ($pageRecord) {
141
                $moduleTemplate->getDocHeaderComponent()->setMetaInformation($pageRecord);
142
                $this->view->assign('pageTitle', BackendUtility::getRecordTitle('pages', $pageRecord));
143
            }
144
        }
145
        $wsList = GeneralUtility::makeInstance(WorkspaceService::class)->getAvailableWorkspaces();
146
        $customWorkspaceExists = $this->customWorkspaceExists($wsList);
147
        $activeWorkspace = (int)$backendUser->workspace;
148
        $performWorkspaceSwitch = false;
149
        if ((int)($queryParams['workspace'] ?? 0) > 0) {
150
            $switchWs = (int)$queryParams['workspace'];
151
            if (array_key_exists($switchWs, $wsList) && $activeWorkspace !== $switchWs) {
152
                $activeWorkspace = $switchWs;
153
                $backendUser->setWorkspace($activeWorkspace);
154
                $performWorkspaceSwitch = true;
155
                BackendUtility::setUpdateSignal('updatePageTree');
156
            }
157
        }
158
        $this->pageRenderer->addInlineSetting('Workspaces', 'isLiveWorkspace', (int)$backendUser->workspace === 0);
159
        $this->pageRenderer->addInlineSetting('Workspaces', 'workspaceTabs', $this->prepareWorkspaceTabs($wsList, $activeWorkspace));
160
        $this->pageRenderer->addInlineSetting('Workspaces', 'activeWorkspaceId', $activeWorkspace);
161
        $workspaceIsAccessible = $backendUser->workspace !== WorkspaceService::LIVE_WORKSPACE_ID;
162
        $this->view->assignMultiple([
163
            'isAdmin' => $backendUser->isAdmin(),
164
            'customWorkspaceExists' => $customWorkspaceExists,
165
            'showGrid' => $workspaceIsAccessible,
166
            'showLegend' => $workspaceIsAccessible,
167
            'pageUid' => $this->pageId,
168
            'performWorkspaceSwitch' => $performWorkspaceSwitch,
169
            'workspaceList' => $this->prepareWorkspaceTabs($wsList, $activeWorkspace),
170
            'activeWorkspaceUid' => $activeWorkspace,
171
            'activeWorkspaceTitle' => WorkspaceService::getWorkspaceTitle($activeWorkspace),
172
        ]);
173
174
        $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
175
        if ($this->canCreatePreviewLink($this->pageId, $activeWorkspace)) {
176
            $iconFactory = $moduleTemplate->getIconFactory();
177
            $showButton = $buttonBar->makeLinkButton()
178
                ->setHref('#')
179
                ->setClasses('t3js-preview-link')
180
                ->setShowLabelText(true)
181
                ->setTitle($this->getLanguageService()->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf:tooltip.generatePagePreview'))
182
                ->setIcon($iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL));
183
            $buttonBar->addButton($showButton);
184
        }
185
        $shortcutButton = $buttonBar->makeShortcutButton()
186
            ->setModuleName('web_WorkspacesWorkspaces')
187
            ->setArguments([
188
                'route' => (string)GeneralUtility::_GP('route'),
189
                'id' => (int)$this->pageId,
190
            ]);
191
        $buttonBar->addButton($shortcutButton);
192
193
        $this->moduleTemplate->setContent($this->view->render());
194
        return new HtmlResponse($this->moduleTemplate->renderContent());
195
    }
196
197
    /**
198
     * Prepares available workspace tabs.
199
     *
200
     * @param array $workspaceList
201
     * @param int $activeWorkspace
202
     * @return array
203
     */
204
    protected function prepareWorkspaceTabs(array $workspaceList, int $activeWorkspace)
205
    {
206
        $tabs = [];
207
208
        if ($activeWorkspace !== WorkspaceService::LIVE_WORKSPACE_ID) {
209
            $tabs[] = [
210
                'title' => $workspaceList[$activeWorkspace],
211
                'itemId' => 'workspace-' . $activeWorkspace,
212
                'workspaceId' => $activeWorkspace,
213
                'triggerUrl' => $this->getModuleUri($activeWorkspace),
214
            ];
215
        }
216
217
        foreach ($workspaceList as $workspaceId => $workspaceTitle) {
218
            if ($workspaceId === $activeWorkspace
219
                || $workspaceId === WorkspaceService::LIVE_WORKSPACE_ID
220
            ) {
221
                continue;
222
            }
223
            $tabs[] = [
224
                'title' => $workspaceTitle,
225
                'itemId' => 'workspace-' . $workspaceId,
226
                'workspaceId' => $workspaceId,
227
                'triggerUrl' => $this->getModuleUri($workspaceId),
228
            ];
229
        }
230
231
        return $tabs;
232
    }
233
234
    /**
235
     * Gets the module URI.
236
     *
237
     * @param int $workspaceId
238
     * @return string
239
     */
240
    protected function getModuleUri(int $workspaceId): string
241
    {
242
        $parameters = [
243
            'id' => $this->pageId,
244
            'workspace' => $workspaceId,
245
        ];
246
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
247
        return (string)$uriBuilder->buildUriFromRoute('web_WorkspacesWorkspaces', $parameters);
248
    }
249
250
    /**
251
     * Determine whether this page for the current
252
     *
253
     * @param int $pageUid
254
     * @param int $workspaceUid
255
     * @return bool
256
     */
257
    protected function canCreatePreviewLink(int $pageUid, int $workspaceUid): bool
258
    {
259
        if ($pageUid > 0 && $workspaceUid > 0) {
260
            $pageRecord = BackendUtility::getRecord('pages', $pageUid);
261
            BackendUtility::workspaceOL('pages', $pageRecord, $workspaceUid);
262
            if (VersionState::cast($pageRecord['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
263
                return false;
264
            }
265
            return true;
266
        }
267
        return false;
268
    }
269
270
    /**
271
     * Gets the selected language.
272
     *
273
     * @return string
274
     */
275
    protected function getLanguageSelection(): string
276
    {
277
        $language = 'all';
278
        $backendUser = $this->getBackendUser();
279
        if (isset($backendUser->uc['moduleData']['Workspaces'][$backendUser->workspace]['language'])) {
280
            $language = $backendUser->uc['moduleData']['Workspaces'][$backendUser->workspace]['language'];
281
        }
282
        return $language;
283
    }
284
285
    /**
286
     * Returns true if at least one custom workspace next to live workspace exists.
287
     *
288
     * @param array $workspaceList
289
     * @return bool
290
     */
291
    protected function customWorkspaceExists(array $workspaceList): bool
292
    {
293
        foreach (array_keys($workspaceList) as $workspaceId) {
294
            if ($workspaceId > 0) {
295
                return true;
296
            }
297
        }
298
        return false;
299
    }
300
301
    /**
302
     * @return PageRenderer
303
     */
304
    protected function getPageRenderer(): PageRenderer
305
    {
306
        return GeneralUtility::makeInstance(PageRenderer::class);
307
    }
308
309
    /**
310
     * @return LanguageService
311
     */
312
    protected function getLanguageService(): LanguageService
313
    {
314
        return $GLOBALS['LANG'];
315
    }
316
317
    /**
318
     * @return BackendUserAuthentication
319
     */
320
    protected function getBackendUser(): BackendUserAuthentication
321
    {
322
        return $GLOBALS['BE_USER'];
323
    }
324
}
325