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