[BUGFIX] Streamline iframe namings for new backend
[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\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 * @var IconFactory
36 */
37 protected $iconFactory;
38
39 /**
40 * Constructor
41 */
42 public function __construct()
43 {
44 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
45 $wsService = GeneralUtility::makeInstance(WorkspaceService::class);
46 $this->availableWorkspaces = $wsService->getAvailableWorkspaces();
47
48 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
49 $pageRenderer = $this->getPageRenderer();
50 $pageRenderer->addInlineLanguageLabel('Workspaces.workspaceTitle', WorkspaceService::getWorkspaceTitle($this->getBackendUser()->workspace));
51 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Workspaces/Toolbar/WorkspacesMenu');
52 }
53
54 /**
55 * Checks whether the user has access to this toolbar item
56 *
57 * @return bool TRUE if user has access, FALSE if not
58 */
59 public function checkAccess()
60 {
61 return count($this->availableWorkspaces) > 1;
62 }
63
64 /**
65 * Render item
66 *
67 * @return string HTML
68 */
69 public function getItem()
70 {
71 if (empty($this->availableWorkspaces)) {
72 return '';
73 }
74 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace'));
75 $icon = $this->iconFactory->getIcon('apps-toolbar-menu-workspace', Icon::SIZE_SMALL)->render('inline');
76 return '
77 <span class="toolbar-item-icon" title="' . $title . '">' . $icon . '</span>
78 <span class="toolbar-item-title">' . $title . '</span>
79 ';
80 }
81
82 /**
83 * Get drop down
84 *
85 * @return string
86 */
87 public function getDropDown()
88 {
89 $backendUser = $this->getBackendUser();
90 $languageService = $this->getLanguageService();
91
92 $index = 0;
93 $activeWorkspace = (int)$backendUser->workspace;
94 $stateCheckedIcon = $this->iconFactory->getIcon('status-status-checked', Icon::SIZE_SMALL)->render();
95 $stateUncheckedIcon = '<span title="' . htmlspecialchars($languageService->getLL('bookmark_inactive')) . '">' . $this->iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
96 $workspaceSections = [
97 'top' => [],
98 'items' => [],
99 ];
100
101 foreach ($this->availableWorkspaces as $workspaceId => $label) {
102 $workspaceId = (int)$workspaceId;
103 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
104 $classValue = ($workspaceId === $activeWorkspace ? 'selected' : '');
105 $sectionName = ($index++ === 0 ? 'top' : 'items');
106 $workspaceSections[$sectionName][] = '
107 <div class="dropdown-table-row t3js-workspace-item ' . $classValue . '">
108 <div class="dropdown-table-column dropdown-table-icon">
109 ' . $iconState . '
110 </div>
111 <div class="dropdown-table-column">
112 <a href="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('main', ['changeWorkspace' => $workspaceId])) . '" data-workspaceid="' . $workspaceId . '" class="t3js-workspaces-switchlink">
113 ' . htmlspecialchars($label) . '
114 </a>
115 </div>
116 </div>
117 ';
118 }
119
120 if (!empty($workspaceSections['top'])) {
121 // Add the "Go to workspace module" link
122 // if there is at least one icon on top and if the access rights are there
123 if ($backendUser->check('modules', 'web_WorkspacesWorkspaces')) {
124 $workspaceSections['top'][] = '
125 <div class="dropdown-table-row">
126 <div class="dropdown-table-column dropdown-table-icon">
127 ' . $stateUncheckedIcon . '
128 </div>
129 <div class="dropdown-table-column">
130 <a href="#" target="list_frame" data-module="web_WorkspacesWorkspaces" class="t3js-workspaces-modulelink">
131 ' . htmlspecialchars($languageService->getLL('bookmark_workspace')) . '
132 </a>
133 </div>
134 </div>
135 ';
136 }
137 } else {
138 // no items on top (= no workspace to work in)
139 $workspaceSections['top'][] = '
140 <div class="dropdown-table-row">
141 <div class="dropdown-table-column dropdown-table-icon">
142 ' . $stateUncheckedIcon . '
143 </div>
144 <div class="dropdown-table-column">
145 ' . htmlspecialchars($languageService->getLL('bookmark_noWSfound')) . '
146 </div>
147 </div>
148 ';
149 }
150
151 $workspaceMenu = [
152 '<h3 class="dropdown-headline">' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace')) . '</h3>',
153 '<hr>',
154 '<div class="dropdown-table">' . implode(LF, $workspaceSections['top']) . '</div>',
155 (!empty($workspaceSections['items']) ? '<hr>' : ''),
156 '<div class="dropdown-table">' . implode(LF, $workspaceSections['items']) . '</div>',
157 ];
158
159 return implode(LF, $workspaceMenu);
160 }
161
162 /**
163 * This toolbar needs no additional attributes
164 *
165 * @return array
166 */
167 public function getAdditionalAttributes()
168 {
169 return [];
170 }
171
172 /**
173 * This item has a drop down
174 *
175 * @return bool
176 */
177 public function hasDropDown()
178 {
179 return !empty($this->availableWorkspaces);
180 }
181
182 /**
183 * Position relative to others
184 *
185 * @return int
186 */
187 public function getIndex()
188 {
189 return 40;
190 }
191
192 /**
193 * Returns the current BE user.
194 *
195 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
196 */
197 protected function getBackendUser()
198 {
199 return $GLOBALS['BE_USER'];
200 }
201
202 /**
203 * Returns current PageRenderer
204 *
205 * @return PageRenderer
206 */
207 protected function getPageRenderer()
208 {
209 return GeneralUtility::makeInstance(PageRenderer::class);
210 }
211
212 /**
213 * Returns LanguageService
214 *
215 * @return \TYPO3\CMS\Lang\LanguageService
216 */
217 protected function getLanguageService()
218 {
219 return $GLOBALS['LANG'];
220 }
221 }