[CLEANUP] Streamline backend toolbar items
[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\Domain\Repository\Module\BackendModuleRepository;
18 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Fluid\View\StandaloneView;
22
23 /**
24 * User toolbar item
25 */
26 class UserToolbarItem implements ToolbarItemInterface
27 {
28 /**
29 * Item is always enabled
30 *
31 * @return bool TRUE
32 */
33 public function checkAccess()
34 {
35 return true;
36 }
37
38 /**
39 * Render username and an icon
40 *
41 * @return string HTML
42 */
43 public function getItem()
44 {
45 $backendUser = $this->getBackendUser();
46 $view = $this->getFluidTemplateObject('UserToolbarItem.html');
47 $view->assignMultiple([
48 'currentUser' => $backendUser->user,
49 'switchUserMode' => $backendUser->user['ses_backuserid'],
50 ]);
51 return $view->render();
52 }
53
54 /**
55 * Render drop down
56 *
57 * @return string HTML
58 */
59 public function getDropDown()
60 {
61 /** @var BackendModuleRepository $backendModuleRepository */
62 $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
63 $view = $this->getFluidTemplateObject('UserToolbarItemDropDown.html');
64 $view->assignMultiple([
65 'modules' => $backendModuleRepository->findByModuleName('user')->getChildren(),
66 'logoutUrl' => BackendUtility::getModuleUrl('logout'),
67 'switchUserMode' => $this->getBackendUser()->user['ses_backuserid'],
68 ]);
69 return $view->render();
70 }
71
72 /**
73 * Returns an additional class if user is in "switch user" mode
74 *
75 * @return array
76 */
77 public function getAdditionalAttributes()
78 {
79 $result = [
80 'class' => 'toolbar-item-user'
81 ];
82 if ($this->getBackendUser()->user['ses_backuserid']) {
83 $result['class'] .= ' su-user';
84 }
85 return $result;
86 }
87
88 /**
89 * This item has a drop down
90 *
91 * @return bool
92 */
93 public function hasDropDown()
94 {
95 return true;
96 }
97
98 /**
99 * Position relative to others
100 *
101 * @return int
102 */
103 public function getIndex()
104 {
105 return 80;
106 }
107
108 /**
109 * Returns the current BE user.
110 *
111 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
112 */
113 protected function getBackendUser()
114 {
115 return $GLOBALS['BE_USER'];
116 }
117
118 /**
119 * Returns a new standalone view, shorthand function
120 *
121 * @param string $filename Which templateFile should be used.
122 *
123 * @return StandaloneView
124 */
125 protected function getFluidTemplateObject(string $filename):StandaloneView
126 {
127 $view = GeneralUtility::makeInstance(StandaloneView::class);
128 $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
129 $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
130 $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
131
132 $view->setTemplate($filename);
133
134 $view->getRequest()->setControllerExtensionName('Backend');
135 return $view;
136 }
137 }