[TASK] Fluidification of UserToolbarItem
[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\Backend\Backend\Avatar\Avatar;
18 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
19 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Imaging\IconFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Fluid\View\StandaloneView;
25
26 /**
27 * User toolbar item
28 */
29 class UserToolbarItem implements ToolbarItemInterface
30 {
31 /**
32 * @var IconFactory
33 */
34 protected $iconFactory;
35
36 /**
37 * Constructor
38 */
39 public function __construct()
40 {
41 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
42 }
43
44 /**
45 * Item is always enabled
46 *
47 * @return bool TRUE
48 */
49 public function checkAccess()
50 {
51 return true;
52 }
53
54 /**
55 * Render username
56 *
57 * @return string HTML
58 */
59 public function getItem()
60 {
61 $backendUser = $this->getBackendUser();
62
63 /** @var Avatar $avatar */
64 $avatar = GeneralUtility::makeInstance(Avatar::class);
65 $icon = $avatar->render();
66
67 $realName = $backendUser->user['realName'];
68 $username = $backendUser->user['username'];
69
70 $view = $this->getFluidTemplateObject('UserToolbarItem.html');
71 $view->assignMultiple([
72 'username' => $username,
73 'realName' => $realName,
74 'switchUserMode' => $backendUser->user['ses_backuserid'],
75 'icon' => $icon
76 ]
77 );
78
79 return $view->render();
80 }
81
82 /**
83 * Render drop down
84 *
85 * @return string HTML
86 */
87 public function getDropDown()
88 {
89 $backendUser = $this->getBackendUser();
90
91 /** @var BackendModuleRepository $backendModuleRepository */
92 $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
93 /** @var \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule $userModuleMenu */
94 $userModuleMenu = $backendModuleRepository->findByModuleName('user');
95
96 $icon = $this->iconFactory->getIcon('actions-logout', Icon::SIZE_SMALL)->render('inline');
97
98 $view = $this->getFluidTemplateObject('UserToolbarItemDropDown.html');
99 $view->assignMultiple([
100 'modules' => $userModuleMenu->getChildren(),
101 'logoutUrl' => BackendUtility::getModuleUrl('logout'),
102 'switchUserMode' => $backendUser->user['ses_backuserid'],
103 'icon' => $icon
104 ]
105 );
106
107 return $view->render();
108 }
109
110 /**
111 * Returns an additional class if user is in "switch user" mode
112 *
113 * @return array
114 */
115 public function getAdditionalAttributes()
116 {
117 $result = [];
118 $result['class'] = 'toolbar-item-user';
119 if ($this->getBackendUser()->user['ses_backuserid']) {
120 $result['class'] .= ' su-user';
121 }
122 return $result;
123 }
124
125 /**
126 * This item has a drop down
127 *
128 * @return bool
129 */
130 public function hasDropDown()
131 {
132 return true;
133 }
134
135 /**
136 * Position relative to others
137 *
138 * @return int
139 */
140 public function getIndex()
141 {
142 return 80;
143 }
144
145 /**
146 * Returns the current BE user.
147 *
148 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
149 */
150 protected function getBackendUser()
151 {
152 return $GLOBALS['BE_USER'];
153 }
154
155 /**
156 * Returns LanguageService
157 *
158 * @return \TYPO3\CMS\Lang\LanguageService
159 */
160 protected function getLanguageService()
161 {
162 return $GLOBALS['LANG'];
163 }
164
165 /**
166 * Returns a new standalone view, shorthand function
167 *
168 * @param string $filename Which templateFile should be used.
169 *
170 * @return StandaloneView
171 */
172 protected function getFluidTemplateObject(string $filename):StandaloneView
173 {
174 /** @var StandaloneView $view */
175 $view = GeneralUtility::makeInstance(StandaloneView::class);
176 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
177 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ToolbarItems')]);
178 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems')]);
179
180 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems/' . $filename));
181
182 $view->getRequest()->setControllerExtensionName('Backend');
183 return $view;
184 }
185 }