[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Backend / ToolbarItems / WorkspaceSelectorToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Backend\ToolbarItems;
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\Toolbar\ToolbarItemInterface;
18 use TYPO3\CMS\Core\Page\PageRenderer;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Fluid\View\StandaloneView;
21 use TYPO3\CMS\Workspaces\Service\WorkspaceService;
22
23 /**
24 * Class to render the workspace selector
25 */
26 class WorkspaceSelectorToolbarItem implements ToolbarItemInterface
27 {
28 /**
29 * @var array
30 */
31 protected $availableWorkspaces;
32
33 /**
34 * Constructor
35 */
36 public function __construct()
37 {
38 $this->availableWorkspaces = GeneralUtility::makeInstance(WorkspaceService::class)
39 ->getAvailableWorkspaces();
40
41 $pageRenderer = $this->getPageRenderer();
42 $pageRenderer->addInlineLanguageLabel('Workspaces.workspaceTitle', WorkspaceService::getWorkspaceTitle($this->getBackendUser()->workspace));
43 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Toolbar/WorkspacesMenu');
44 }
45
46 /**
47 * Checks whether the user has access to this toolbar item
48 *
49 * @return bool TRUE if user has access, FALSE if not
50 */
51 public function checkAccess()
52 {
53 return count($this->availableWorkspaces) > 1;
54 }
55
56 /**
57 * Render item
58 *
59 * @return string HTML
60 */
61 public function getItem()
62 {
63 if (empty($this->availableWorkspaces)) {
64 return '';
65 }
66 return $this->getFluidTemplateObject('ToolbarItem.html')->render();
67 }
68
69 /**
70 * Get drop down
71 *
72 * @return string
73 */
74 public function getDropDown()
75 {
76 $topItem = null;
77 $additionalItems = [];
78 $backendUser = $this->getBackendUser();
79 $view = $this->getFluidTemplateObject('DropDown.html');
80 $activeWorkspace = (int)$backendUser->workspace;
81 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
82 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
83 foreach ($this->availableWorkspaces as $workspaceId => $label) {
84 $workspaceId = (int)$workspaceId;
85 $item = [
86 'isActive' => $workspaceId === $activeWorkspace,
87 'label' => $label,
88 'link' => (string)$uriBuilder->buildUriFromRoute('main', ['changeWorkspace' => $workspaceId]),
89 'workspaceId' => $workspaceId
90 ];
91 if ($topItem === null) {
92 $topItem = $item;
93 } else {
94 $additionalItems[] = $item;
95 }
96 }
97
98 // Add the "Go to workspace module" link
99 // if there is at least one icon on top and if the access rights are there
100 if ($topItem !== null && $backendUser->check('modules', 'web_WorkspacesWorkspaces')) {
101 $view->assign('showLinkToModule', true);
102 }
103 $view->assign('topItem', $topItem);
104 $view->assign('additionalItems', $additionalItems);
105 return $view->render();
106 }
107
108 /**
109 * This toolbar needs no additional attributes
110 *
111 * @return array
112 */
113 public function getAdditionalAttributes()
114 {
115 return [];
116 }
117
118 /**
119 * This item has a drop down
120 *
121 * @return bool
122 */
123 public function hasDropDown()
124 {
125 return !empty($this->availableWorkspaces);
126 }
127
128 /**
129 * Position relative to others
130 *
131 * @return int
132 */
133 public function getIndex()
134 {
135 return 40;
136 }
137
138 /**
139 * Returns the current BE user.
140 *
141 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
142 */
143 protected function getBackendUser()
144 {
145 return $GLOBALS['BE_USER'];
146 }
147
148 /**
149 * Returns current PageRenderer
150 *
151 * @return PageRenderer
152 */
153 protected function getPageRenderer()
154 {
155 return GeneralUtility::makeInstance(PageRenderer::class);
156 }
157
158 /**
159 * Returns a new standalone view, shorthand function
160 *
161 * @param string $filename Which templateFile should be used.
162 * @return StandaloneView
163 */
164 protected function getFluidTemplateObject(string $filename): StandaloneView
165 {
166 $view = GeneralUtility::makeInstance(StandaloneView::class);
167 $view->setLayoutRootPaths(['EXT:workspaces/Resources/Private/Layouts']);
168 $view->setPartialRootPaths([
169 'EXT:backend/Resources/Private/Partials/ToolbarItems',
170 'EXT:workspaces/Resources/Private/Partials/ToolbarItems'
171 ]);
172 $view->setTemplateRootPaths(['EXT:workspaces/Resources/Private/Templates/ToolbarItems']);
173
174 $view->setTemplate($filename);
175
176 $view->getRequest()->setControllerExtensionName('Workspaces');
177 return $view;
178 }
179 }