Revert "[TASK] Port toolbar dropdowns to Fluid"
[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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
19 use TYPO3\CMS\Workspaces\Service\WorkspaceService;
20 use TYPO3\CMS\Backend\Utility\IconUtility;
21
22 /**
23 * Class to render the workspace selector
24 *
25 * @author Ingo Renner <ingo@typo3.org>
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 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
39 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
40 $this->availableWorkspaces = $wsService->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 return count($this->availableWorkspaces) > 1;
54 }
55
56 /**
57 * Render item
58 *
59 * @return string HTML
60 */
61 public function getItem() {
62 if (empty($this->availableWorkspaces)) {
63 return '';
64 }
65
66 return IconUtility::getSpriteIcon(
67 'apps-toolbar-menu-workspace',
68 array(
69 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace', TRUE),
70 )
71 );
72 }
73
74 /**
75 * Get drop down
76 *
77 * @return string
78 */
79 public function getDropDown() {
80 $backendUser = $this->getBackendUser();
81 $languageService = $this->getLanguageService();
82
83 $index = 0;
84 $activeWorkspace = (int)$backendUser->workspace;
85 $stateCheckedIcon = IconUtility::getSpriteIcon('status-status-checked');
86 $stateUncheckedIcon = IconUtility::getSpriteIcon('empty-empty', array(
87 'title' => $languageService->getLL('bookmark_inactive')
88 ));
89
90 $workspaceSections = array(
91 'top' => array(),
92 'items' => array(),
93 );
94
95 foreach ($this->availableWorkspaces as $workspaceId => $label) {
96 $workspaceId = (int)$workspaceId;
97 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
98 $classValue = ($workspaceId === $activeWorkspace ? ' class="selected"' : '');
99 $sectionName = ($index++ === 0 ? 'top' : 'items');
100 $workspaceSections[$sectionName][] = '<li' . $classValue . '>'
101 . '<a href="backend.php?changeWorkspace=' . $workspaceId . '" data-workspaceid="' . $workspaceId . '" class="dropdown-list-link tx-workspaces-switchlink">'
102 . $iconState . ' ' . htmlspecialchars($label)
103 . '</a></li>';
104 }
105
106 if (!empty($workspaceSections['top'])) {
107 // Add the "Go to workspace module" link
108 // if there is at least one icon on top and if the access rights are there
109 if ($backendUser->check('modules', 'web_WorkspacesWorkspaces')) {
110 $workspaceSections['top'][] = '<li><a target="content" data-module="web_WorkspacesWorkspaces" class="dropdown-list-link tx-workspaces-modulelink">'
111 . $stateUncheckedIcon . ' ' . $languageService->getLL('bookmark_workspace', TRUE)
112 . '</a></li>';
113 }
114 } else {
115 // no items on top (= no workspace to work in)
116 $workspaceSections['top'][] = '<li>' . $stateUncheckedIcon . ' ' . $languageService->getLL('bookmark_noWSfound', TRUE) . '</li>';
117 }
118
119 $workspaceMenu = array(
120 '<ul class="dropdown-list">' ,
121 implode(LF, $workspaceSections['top']),
122 (!empty($workspaceSections['items']) ? '<li class="divider"></li>' : ''),
123 implode(LF, $workspaceSections['items']),
124 '</ul>'
125 );
126
127 return implode(LF, $workspaceMenu);
128 }
129
130 /**
131 * This toolbar needs no additional attributes
132 *
133 * @return array
134 */
135 public function getAdditionalAttributes() {
136 return array();
137 }
138
139 /**
140 * This item has a drop down
141 *
142 * @return bool
143 */
144 public function hasDropDown() {
145 return !empty($this->availableWorkspaces);
146 }
147
148 /**
149 * Position relative to others
150 *
151 * @return int
152 */
153 public function getIndex() {
154 return 40;
155 }
156
157 /**
158 * Returns the current BE user.
159 *
160 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
161 */
162 protected function getBackendUser() {
163 return $GLOBALS['BE_USER'];
164 }
165
166 /**
167 * Returns current PageRenderer
168 *
169 * @return \TYPO3\CMS\Core\Page\PageRenderer
170 */
171 protected function getPageRenderer() {
172 /** @var \TYPO3\CMS\Backend\Template\DocumentTemplate $documentTemplate */
173 $documentTemplate = $GLOBALS['TBE_TEMPLATE'];
174 return $documentTemplate->getPageRenderer();
175 }
176
177 /**
178 * Returns LanguageService
179 *
180 * @return \TYPO3\CMS\Lang\LanguageService
181 */
182 protected function getLanguageService() {
183 return $GLOBALS['LANG'];
184 }
185
186 }