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