Revert "[TASK] Port toolbar dropdowns to Fluid"
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / UserToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\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\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
21 use TYPO3\CMS\Backend\Domain\Model\Module\BackendModule;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23
24 /**
25 * User toolbar item
26 */
27 class UserToolbarItem implements ToolbarItemInterface {
28
29 /**
30 * Item is always enabled
31 *
32 * @return bool TRUE
33 */
34 public function checkAccess() {
35 return TRUE;
36 }
37
38 /**
39 * Render username
40 *
41 * @return string HTML
42 */
43 public function getItem() {
44 $backendUser = $this->getBackendUser();
45 $languageService = $this->getLanguageService();
46 $icon = IconUtility::getSpriteIcon('status-user-' . ($backendUser->isAdmin() ? 'admin' : 'backend'));
47
48 $realName = $backendUser->user['realName'];
49 $username = $backendUser->user['username'];
50 $label = $realName ?: $username;
51 $title = $username;
52
53 // Switch user mode
54 if ($backendUser->user['ses_backuserid']) {
55 $title = $languageService->getLL('switchtouser') . ': ' . $username;
56 $label = $languageService->getLL('switchtousershort') . ' ' . ($realName ? $realName . ' (' . $username . ')' : $username);
57 }
58
59 $html = array();
60 $html[] = $icon;
61 $html[] = '<span title="' . htmlspecialchars($title) . '">';
62 $html[] = htmlspecialchars($label);
63 $html[] = '<span class="caret"></span></span>';
64
65 return implode(LF, $html);
66 }
67
68 /**
69 * Render drop down
70 *
71 * @return string HTML
72 */
73 public function getDropDown() {
74 $backendUser = $this->getBackendUser();
75 $languageService = $this->getLanguageService();
76
77 $dropdown = array();
78 $dropdown[] = '<ul class="dropdown-list">';
79
80 /** @var BackendModuleRepository $backendModuleRepository */
81 $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
82 /** @var \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule $userModuleMenu */
83 $userModuleMenu = $backendModuleRepository->findByModuleName('user');
84 if ($userModuleMenu != FALSE && $userModuleMenu->getChildren()->count() > 0) {
85 foreach ($userModuleMenu->getChildren() as $module) {
86 /** @var BackendModule $module */
87 $dropdown[] ='<li'
88 . ' id="' . htmlspecialchars($module->getName()) . '"'
89 . ' class="typo3-module-menu-item submodule mod-' . htmlspecialchars($module->getName()) . '" '
90 . ' data-modulename="' . htmlspecialchars($module->getName()) . '"'
91 . ' data-navigationcomponentid="' . htmlspecialchars($module->getNavigationComponentId()) . '"'
92 . ' data-navigationframescript="' . htmlspecialchars($module->getNavigationFrameScript()) . '"'
93 . ' data-navigationframescriptparameters="' . htmlspecialchars($module->getNavigationFrameScriptParameters()) . '"'
94 . '>';
95 $dropdown[] = '<a title="' . htmlspecialchars($module->getDescription()) . '" href="' . htmlspecialchars($module->getLink()) . '" class="dropdown-list-link modlink">';
96 $dropdown[] = '<span class="submodule-icon typo3-app-icon"><span><span>' . $module->getIcon() . '</span></span></span>';
97 $dropdown[] = '<span class="submodule-label">' . htmlspecialchars($module->getTitle()) . '</span>';
98 $dropdown[] = '</a>';
99 $dropdown[] = '</li>';
100 }
101 $dropdown[] = '<li class="divider"></li>';
102 }
103
104 // Logout button
105 $buttonLabel = 'LLL:EXT:lang/locallang_core.xlf:' . ($backendUser->user['ses_backuserid'] ? 'buttons.exit' : 'buttons.logout');
106 $dropdown[] = '<li class="reset-dropdown">';
107 $dropdown[] = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('logout')) . '" class="btn btn-danger pull-right" target="_top"><i class="fa fa-power-off"></i> ';
108 $dropdown[] = $languageService->sL($buttonLabel, TRUE);
109 $dropdown[] = '</a>';
110 $dropdown[] = '</li>';
111
112 $dropdown[] = '</ul>';
113
114 return implode(LF, $dropdown);
115 }
116
117 /**
118 * Returns an additional class if user is in "switch user" mode
119 *
120 * @return array
121 */
122 public function getAdditionalAttributes() {
123 $result = array();
124 if ($this->getBackendUser()->user['ses_backuserid']) {
125 $result['class'] = 'su-user';
126 }
127 return $result;
128 }
129
130 /**
131 * This item has a drop down
132 *
133 * @return bool
134 */
135 public function hasDropDown() {
136 return TRUE;
137 }
138
139 /**
140 * Position relative to others
141 *
142 * @return int
143 */
144 public function getIndex() {
145 return 80;
146 }
147
148 /**
149 * Returns the current BE user.
150 *
151 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
152 */
153 protected function getBackendUser() {
154 return $GLOBALS['BE_USER'];
155 }
156
157 /**
158 * Returns LanguageService
159 *
160 * @return \TYPO3\CMS\Lang\LanguageService
161 */
162 protected function getLanguageService() {
163 return $GLOBALS['LANG'];
164 }
165
166 }