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