‪TYPO3CMS  10.4
ReportController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
32 use TYPO3Fluid\Fluid\View\ViewInterface;
33 
39 {
45  protected ‪$moduleTemplate;
46 
50  protected ‪$view;
51 
57  protected ‪$shortcutName;
58 
62  public function ‪__construct()
63  {
64  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
65  }
66 
73  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
74  {
75  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? '';
76  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
77  $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;
78 
79  if ($action !== 'index' && !$isRedirect && !$extension
80  && is_array(‪$GLOBALS['BE_USER']->uc['reports']['selection'])) {
81  $previousSelection = ‪$GLOBALS['BE_USER']->uc['reports']['selection'];
82  if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
83  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
84  return new ‪RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
85  'action' => 'detail',
86  'extension' => $previousSelection['extension'],
87  'report' => $previousSelection['report'],
88  'redirect' => 1,
89  ]), 303);
90  }
91  }
92  if (empty($action)) {
93  $action = 'index';
94  }
95 
96  $this->‪initializeView($action);
97 
98  if ($action === 'index') {
99  $this->‪indexAction();
100  } elseif ($action === 'detail') {
101  $response = $this->‪detailAction($request);
102  if ($response instanceof ResponseInterface) {
103  return $response;
104  }
105  } else {
106  throw new \RuntimeException(
107  'Reports module has only "index" and "detail" action, ' . (string)$action . ' given',
108  1536322935
109  );
110  }
111 
112  $this->‪generateMenu($request);
113  $this->‪generateButtons();
114 
115  $this->moduleTemplate->setContent($this->view->render());
116  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
117  }
118 
122  protected function ‪initializeView(string $templateName)
123  {
124  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
125  $this->view->setTemplate($templateName);
126  $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
127  $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
128  $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
129  $this->view->getRequest()->setControllerExtensionName('Reports');
130  }
131 
135  protected function ‪indexAction()
136  {
137  $this->view->assign('reports', ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
138  $this->‪saveState();
139  }
140 
147  protected function ‪detailAction(ServerRequestInterface $request)
148  {
149  $content = $error = '';
150  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
151  $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
152 
153  $reportClass = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
154 
155  if ($reportClass === null || !class_exists($reportClass)) {
156  $this->‪resetState();
157  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
158  return new ‪RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
159  'action' => 'index',
160  'redirect' => 1,
161  ]), 303);
162  }
163 
164  $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
165 
166  if ($reportInstance instanceof ‪ReportInterface) {
167  if ($reportInstance instanceof ‪RequestAwareReportInterface) {
168  $content = $reportInstance->getReport($request);
169  } else {
170  $content = $reportInstance->getReport();
171  }
172  $this->‪saveState($extension, $report);
173  } else {
174  $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
175  }
176 
177  $this->view->assignMultiple([
178  'content' => $content,
179  'error' => $error,
180  'report' => ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
181  ]);
182  }
183 
189  protected function ‪generateMenu(ServerRequestInterface $request)
190  {
191  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
192  $lang = $this->‪getLanguageService();
193  $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
194  $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
195  $menu->setIdentifier('WebFuncJumpMenu');
196  $menuItem = $menu
197  ->makeMenuItem()
198  ->setHref(
199  $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
200  )
201  ->setTitle($lang->getLL('reports_overview'));
202  $menu->addMenuItem($menuItem);
203  $this->shortcutName = $lang->getLL('reports_overview');
204 
205  $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
206  $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
207 
208  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
209  foreach ($reports as $reportName => $report) {
210  $menuItem = $menu
211  ->makeMenuItem()
212  ->setHref($uriBuilder->buildUriFromRoute(
213  'system_reports',
214  ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
215  ))
216  ->setTitle($this->‪getLanguageService()->sL($report['title']));
217  if ($extensionParam === $extKey && $reportParam === $reportName) {
218  $menuItem->setActive(true);
219  $this->shortcutName = $menuItem->getTitle();
220  }
221  $menu->addMenuItem($menuItem);
222  }
223  }
224  $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
225  }
226 
230  protected function ‪generateButtons()
231  {
232  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
233 
234  $shortcutButton = $buttonBar->makeShortcutButton()
235  ->setModuleName('system_reports')
236  ->setGetVariables(['action', 'extension', 'report'])
237  ->setDisplayName($this->shortcutName);
238  $buttonBar->addButton($shortcutButton);
239  }
240 
247  protected function ‪saveState(string $extension = '', string $report = '')
248  {
249  $this->‪getBackendUser()->uc['reports']['selection'] = [
250  'extension' => $extension,
251  'report' => $report,
252  ];
253  $this->‪getBackendUser()->‪writeUC();
254  }
255 
259  protected function ‪resetState(): void
260  {
261  $this->‪getBackendUser()->uc['reports']['selection'] = [];
262  $this->‪getBackendUser()->‪writeUC();
263  }
264 
268  protected function ‪getBackendUser(): ‪BackendUserAuthentication
269  {
270  return ‪$GLOBALS['BE_USER'];
271  }
272 
276  protected function ‪getLanguageService(): ‪LanguageService
277  {
278  return ‪$GLOBALS['LANG'];
279  }
280 }
‪TYPO3\CMS\Reports\ReportInterface
Definition: ReportInterface.php:22
‪TYPO3\CMS\Reports\Controller\ReportController\saveState
‪saveState(string $extension='', string $report='')
Definition: ReportController.php:244
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:1120
‪TYPO3\CMS\Reports\Controller\ReportController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReportController.php:273
‪TYPO3\CMS\Reports\Controller\ReportController\detailAction
‪ResponseInterface void detailAction(ServerRequestInterface $request)
Definition: ReportController.php:144
‪TYPO3\CMS\Reports\Controller\ReportController\__construct
‪__construct()
Definition: ReportController.php:59
‪TYPO3\CMS\Reports\Controller\ReportController\generateButtons
‪generateButtons()
Definition: ReportController.php:227
‪TYPO3\CMS\Reports\Controller\ReportController\indexAction
‪indexAction()
Definition: ReportController.php:132
‪TYPO3\CMS\Reports\Controller\ReportController\resetState
‪resetState()
Definition: ReportController.php:256
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Reports\Controller\ReportController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ReportController.php:44
‪TYPO3\CMS\Reports\RequestAwareReportInterface
Definition: RequestAwareReportInterface.php:26
‪TYPO3\CMS\Reports\Controller\ReportController\$shortcutName
‪string $shortcutName
Definition: ReportController.php:54
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Reports\Controller\ReportController\$view
‪ViewInterface $view
Definition: ReportController.php:48
‪TYPO3\CMS\Reports\Controller\ReportController\initializeView
‪initializeView(string $templateName)
Definition: ReportController.php:119
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Reports\Controller
Definition: ReportController.php:18
‪TYPO3\CMS\Reports\Controller\ReportController\generateMenu
‪generateMenu(ServerRequestInterface $request)
Definition: ReportController.php:186
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Reports\Controller\ReportController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: ReportController.php:70
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Reports\Controller\ReportController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ReportController.php:265
‪TYPO3\CMS\Reports\Controller\ReportController
Definition: ReportController.php:39