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