[TASK] Code style cleanup EXT:backend/Classes/Backend/*.php
[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 $moduleIcon = $module->getIcon();
88 $dropdown[] ='<li'
89 . ' id="' . htmlspecialchars($module->getName()) . '"'
90 . ' class="typo3-module-menu-item submodule mod-' . htmlspecialchars($module->getName()) . '" '
91 . ' data-modulename="' . htmlspecialchars($module->getName()) . '"'
92 . ' data-navigationcomponentid="' . htmlspecialchars($module->getNavigationComponentId()) . '"'
93 . ' data-navigationframescript="' . htmlspecialchars($module->getNavigationFrameScript()) . '"'
94 . ' data-navigationframescriptparameters="' . htmlspecialchars($module->getNavigationFrameScriptParameters()) . '"'
95 . '>';
96 $dropdown[] = '<a title="' . htmlspecialchars($module->getDescription()) . '" href="' . htmlspecialchars($module->getLink()) . '" class="dropdown-list-link modlink">';
97 $dropdown[] = '<span class="submodule-icon typo3-app-icon"><span><span>' . ($moduleIcon['html'] ?: $moduleIcon['html']) . '</span></span></span>';
98 $dropdown[] = '<span class="submodule-label">' . htmlspecialchars($module->getTitle()) . '</span>';
99 $dropdown[] = '</a>';
100 $dropdown[] = '</li>';
101 }
102 $dropdown[] = '<li class="divider"></li>';
103 }
104
105 // Logout button
106 $buttonLabel = 'LLL:EXT:lang/locallang_core.xlf:' . ($backendUser->user['ses_backuserid'] ? 'buttons.exit' : 'buttons.logout');
107 $dropdown[] = '<li class="reset-dropdown">';
108 $dropdown[] = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('logout')) . '" class="btn btn-danger pull-right" target="_top"><i class="fa fa-power-off"></i> ';
109 $dropdown[] = $languageService->sL($buttonLabel, TRUE);
110 $dropdown[] = '</a>';
111 $dropdown[] = '</li>';
112
113 $dropdown[] = '</ul>';
114
115 return implode(LF, $dropdown);
116 }
117
118 /**
119 * Returns an additional class if user is in "switch user" mode
120 *
121 * @return array
122 */
123 public function getAdditionalAttributes() {
124 $result = array();
125 if ($this->getBackendUser()->user['ses_backuserid']) {
126 $result['class'] = 'su-user';
127 }
128 return $result;
129 }
130
131 /**
132 * This item has a drop down
133 *
134 * @return bool
135 */
136 public function hasDropDown() {
137 return TRUE;
138 }
139
140 /**
141 * Position relative to others
142 *
143 * @return int
144 */
145 public function getIndex() {
146 return 80;
147 }
148
149 /**
150 * Returns the current BE user.
151 *
152 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
153 */
154 protected function getBackendUser() {
155 return $GLOBALS['BE_USER'];
156 }
157
158 /**
159 * Returns LanguageService
160 *
161 * @return \TYPO3\CMS\Lang\LanguageService
162 */
163 protected function getLanguageService() {
164 return $GLOBALS['LANG'];
165 }
166
167 }