[TASK] Streamline icons for "new" actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Controller / BackendUserActionController.php
1 <?php
2 namespace TYPO3\CMS\Beuser\Controller;
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\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\View\BackendTemplateView;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Localization\LanguageService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
25 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
26 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
27
28 /**
29 * Backend module user/group action controller
30 */
31 class BackendUserActionController extends ActionController
32 {
33 /**
34 * Backend Template Container
35 *
36 * @var string
37 */
38 protected $defaultViewObjectName = BackendTemplateView::class;
39
40 /**
41 * BackendTemplateContainer
42 *
43 * @var BackendTemplateView
44 */
45 protected $view;
46
47 /**
48 * Set up the doc header properly here
49 *
50 * @param ViewInterface $view
51 */
52 protected function initializeView(ViewInterface $view)
53 {
54 /** @var BackendTemplateView $view */
55 parent::initializeView($view);
56 if ($this->actionMethodName === 'indexAction'
57 || $this->actionMethodName === 'onlineAction'
58 || $this->actionMethodName === 'compareAction') {
59 $this->generateMenu();
60 $this->registerDocheaderButtons();
61 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
62 }
63 if ($view instanceof BackendTemplateView) {
64 $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
65 }
66 }
67
68 /**
69 * Generates the action menu
70 */
71 protected function generateMenu()
72 {
73 $menuItems = [
74 'index' => [
75 'controller' => 'BackendUser',
76 'action' => 'index',
77 'label' => $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:backendUsers')
78 ],
79 'pages' => [
80 'controller' => 'BackendUserGroup',
81 'action' => 'index',
82 'label' => $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:backendUserGroupsMenu')
83 ],
84 'online' => [
85 'controller' => 'BackendUser',
86 'action' => 'online',
87 'label' => $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:onlineUsers')
88 ]
89 ];
90 $uriBuilder = $this->objectManager->get(UriBuilder::class);
91 $uriBuilder->setRequest($this->request);
92
93 $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
94 $menu->setIdentifier('BackendUserModuleMenu');
95
96 foreach ($menuItems as $menuItemConfig) {
97 if ($this->request->getControllerName() === $menuItemConfig['controller']) {
98 $isActive = $this->request->getControllerActionName() === $menuItemConfig['action'] ? true : false;
99 } else {
100 $isActive = false;
101 }
102 $menuItem = $menu->makeMenuItem()
103 ->setTitle($menuItemConfig['label'])
104 ->setHref($this->getHref($menuItemConfig['controller'], $menuItemConfig['action']))
105 ->setActive($isActive);
106 $menu->addMenuItem($menuItem);
107 }
108
109 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
110 }
111
112 /**
113 * Registers the Icons into the docheader
114 *
115 * @throws \InvalidArgumentException
116 */
117 protected function registerDocheaderButtons()
118 {
119 /** @var ButtonBar $buttonBar */
120 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
121 $currentRequest = $this->request;
122 $moduleName = $currentRequest->getPluginName();
123 $getVars = $this->request->getArguments();
124
125 $extensionName = $currentRequest->getControllerExtensionName();
126 if (count($getVars) === 0) {
127 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
128 $getVars = ['id', 'M', $modulePrefix];
129 }
130 $shortcutName = $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:backendUsers');
131 if ($this->request->getControllerName() === 'BackendUser') {
132 if ($this->request->getControllerActionName() === 'index') {
133 $returnUrl = rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser'));
134 $parameters = GeneralUtility::explodeUrl2Array('edit[be_users][0]=new&returnUrl=' . $returnUrl);
135 $addUserLink = BackendUtility::getModuleUrl('record_edit', $parameters);
136 $title = $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newRecordGeneral');
137 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-add', Icon::SIZE_SMALL);
138 $addUserButton = $buttonBar->makeLinkButton()
139 ->setHref($addUserLink)
140 ->setTitle($title)
141 ->setIcon($icon);
142 $buttonBar->addButton($addUserButton, ButtonBar::BUTTON_POSITION_LEFT);
143 }
144 if ($this->request->getControllerActionName() === 'compare') {
145 $addUserLink = BackendUtility::getModuleUrl('system_BeuserTxBeuser');
146 $title = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack');
147 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL);
148 $addUserButton = $buttonBar->makeLinkButton()
149 ->setHref($addUserLink)
150 ->setTitle($title)
151 ->setIcon($icon);
152 $buttonBar->addButton($addUserButton, ButtonBar::BUTTON_POSITION_LEFT);
153 }
154 if ($this->request->getControllerActionName() === 'online') {
155 $shortcutName = $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:onlineUsers');
156 }
157 }
158 if ($this->request->getControllerName() === 'BackendUserGroup') {
159 $shortcutName = $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xml:backendUserGroupsMenu');
160 $returnUrl = rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser', [
161 'tx_beuser_system_beusertxbeuser' => [
162 'action' => 'index',
163 'controller' => 'BackendUserGroup'
164 ]
165 ]));
166 $parameters = GeneralUtility::explodeUrl2Array('edit[be_groups][0]=new&returnUrl=' . $returnUrl);
167 $addUserLink = BackendUtility::getModuleUrl('record_edit', $parameters);
168 $title = $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newRecordGeneral');
169 $icon = $this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-add', Icon::SIZE_SMALL);
170 $addUserGroupButton = $buttonBar->makeLinkButton()
171 ->setHref($addUserLink)
172 ->setTitle($title)
173 ->setIcon($icon);
174 $buttonBar->addButton($addUserGroupButton, ButtonBar::BUTTON_POSITION_LEFT);
175 }
176 $shortcutButton = $buttonBar->makeShortcutButton()
177 ->setModuleName($moduleName)
178 ->setDisplayName($shortcutName)
179 ->setGetVariables($getVars);
180 $buttonBar->addButton($shortcutButton);
181 }
182
183 /**
184 * Creates te URI for a backend action
185 *
186 * @param string $controller
187 * @param string $action
188 * @param array $parameters
189 * @return string
190 */
191 protected function getHref($controller, $action, $parameters = [])
192 {
193 $uriBuilder = $this->objectManager->get(UriBuilder::class);
194 $uriBuilder->setRequest($this->request);
195 return $uriBuilder->reset()->uriFor($action, $parameters, $controller);
196 }
197
198 /**
199 * @return BackendUserAuthentication
200 */
201 protected function getBackendUserAuthentication()
202 {
203 return $GLOBALS['BE_USER'];
204 }
205
206 /**
207 * @return LanguageService
208 */
209 protected function getLanguageService()
210 {
211 return $GLOBALS['LANG'];
212 }
213 }