a800bee86ae3b17495859f291d8aa921f955bac0
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Controller / ReportController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Reports\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\Http\RedirectResponse;
24 use TYPO3\CMS\Core\Localization\LanguageService;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Fluid\View\StandaloneView;
27 use TYPO3\CMS\Reports\ReportInterface;
28 use TYPO3Fluid\Fluid\View\ViewInterface;
29
30 /**
31 * Reports controller
32 */
33 class ReportController
34 {
35 /**
36 * ModuleTemplate object
37 *
38 * @var ModuleTemplate
39 */
40 protected $moduleTemplate;
41
42 /**
43 * @var ViewInterface
44 */
45 protected $view;
46
47 /**
48 * Module name for the shortcut
49 *
50 * @var string
51 */
52 protected $shortcutName;
53
54 /**
55 * Instantiate the report controller
56 */
57 public function __construct()
58 {
59 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
60 }
61
62 /**
63 * Injects the request object for the current request, and renders correct action
64 *
65 * @param ServerRequestInterface $request the current request
66 * @return ResponseInterface the response with the content
67 */
68 public function handleRequest(ServerRequestInterface $request): ResponseInterface
69 {
70 $actionDefault = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'];
71 $action = $actionDefault ?: 'index';
72 $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
73 $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;
74
75 if ($actionDefault !== 'index' && !$isRedirect && !$extension
76 && is_array($GLOBALS['BE_USER']->uc['reports']['selection'])) {
77 $previousSelection = $GLOBALS['BE_USER']->uc['reports']['selection'];
78 if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
79 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
80 return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
81 'action' => 'detail',
82 'extension' => $previousSelection['extension'],
83 'report' => $previousSelection['report'],
84 'redirect' => 1,
85 ]), 303);
86 }
87 }
88
89 $this->initializeView($action);
90
91 $result = call_user_func_array([$this, $action . 'Action'], [$request]);
92 if ($result instanceof ResponseInterface) {
93 return $result;
94 }
95
96 $this->generateMenu($request);
97 $this->generateButtons();
98
99 $this->moduleTemplate->setContent($this->view->render());
100 return new HtmlResponse($this->moduleTemplate->renderContent());
101 }
102
103 /**
104 * @param string $templateName
105 */
106 protected function initializeView(string $templateName)
107 {
108 $this->view = GeneralUtility::makeInstance(StandaloneView::class);
109 $this->view->setTemplate($templateName);
110 $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
111 $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
112 $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
113 $this->view->getRequest()->setControllerExtensionName('Reports');
114 }
115
116 /**
117 * Overview
118 */
119 public function indexAction()
120 {
121 $this->view->assign('reports', $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
122 $this->saveState();
123 }
124
125 /**
126 * Display a single report
127 *
128 * @param ServerRequestInterface $request
129 */
130 public function detailAction(ServerRequestInterface $request)
131 {
132 $content = $error = '';
133 $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
134 $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
135
136 $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
137
138 $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
139 if ($reportInstance instanceof ReportInterface) {
140 $content = $reportInstance->getReport();
141 $this->saveState($extension, $report);
142 } else {
143 $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
144 }
145 $this->view->assignMultiple([
146 'content' => $content,
147 'error' => $error,
148 'report' => $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
149 ]);
150 }
151
152 /**
153 * Generates the menu
154 *
155 * @param ServerRequestInterface $request
156 */
157 protected function generateMenu(ServerRequestInterface $request)
158 {
159 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
160 $lang = $this->getLanguageService();
161 $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
162 $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
163 $menu->setIdentifier('WebFuncJumpMenu');
164 $menuItem = $menu
165 ->makeMenuItem()
166 ->setHref(
167 $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
168 )
169 ->setTitle($lang->getLL('reports_overview'));
170 $menu->addMenuItem($menuItem);
171 $this->shortcutName = $lang->getLL('reports_overview');
172
173 $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
174 $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
175
176 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
177 foreach ($reports as $reportName => $report) {
178 $menuItem = $menu
179 ->makeMenuItem()
180 ->setHref($uriBuilder->buildUriFromRoute(
181 'system_reports',
182 ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
183 ))
184 ->setTitle($this->getLanguageService()->sL($report['title']));
185 if ($extensionParam === $extKey && $reportParam === $reportName) {
186 $menuItem->setActive(true);
187 $this->shortcutName = $menuItem->getTitle();
188 }
189 $menu->addMenuItem($menuItem);
190 }
191 }
192 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
193 }
194
195 /**
196 * Gets all buttons for the docHeader
197 */
198 protected function generateButtons()
199 {
200 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
201
202 $shortcutButton = $buttonBar->makeShortcutButton()
203 ->setModuleName('system_reports')
204 ->setGetVariables(['action', 'extension', 'report'])
205 ->setDisplayName($this->shortcutName);
206 $buttonBar->addButton($shortcutButton);
207 }
208
209 /**
210 * Save the selected report
211 *
212 * @param string $extension Extension name
213 * @param string $report Report name
214 */
215 protected function saveState(string $extension = '', string $report = '')
216 {
217 $this->getBackendUser()->uc['reports']['selection'] = [
218 'extension' => $extension,
219 'report' => $report,
220 ];
221 $this->getBackendUser()->writeUC();
222 }
223
224 /**
225 * @return BackendUserAuthentication
226 */
227 protected function getBackendUser(): BackendUserAuthentication
228 {
229 return $GLOBALS['BE_USER'];
230 }
231
232 /**
233 * @return LanguageService
234 */
235 protected function getLanguageService(): LanguageService
236 {
237 return $GLOBALS['LANG'];
238 }
239 }