‪TYPO3CMS  ‪main
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;
31 
37 #[AsController]
39 {
40  public function ‪__construct(
41  protected readonly ‪UriBuilder $uriBuilder,
42  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
43  protected readonly ‪IconRegistry $iconRegistry,
44  protected readonly ‪ReportRegistry $reportRegistry
45  ) {}
46 
50  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
51  {
52  $allReports = $this->reportRegistry->getReports();
53  $queryParams = $request->getQueryParams();
54  $backendUserUc = $this->‪getBackendUser()->uc['reports']['selection'] ?? [];
55  // This can be 'index' for "overview", or 'detail' to render one specific report specified by 'report'
56  $mainView = $queryParams['action'] ?? $backendUserUc['action'] ?? 'detail';
57  if ($mainView === 'index' || count($allReports) === 0) {
58  // Render overview directly if there are no reports at all to have some info box about that,
59  // or if that view has been requested explicitly.
60  $this->‪updateBackendUserUc('index');
61  return $this->‪indexAction($request);
62  }
63 
64  // For fallbacks if backendUser->uc() pointer is invalid or called first time.
65  $firstReportIdentifier = array_keys($allReports)[0];
66  $reportIdentifier = $request->getQueryParams()['report'] ?? $backendUserUc['report'] ?? $firstReportIdentifier;
67  if (!$this->reportRegistry->hasReport($reportIdentifier)) {
68  // If a selected report has been removed meanwhile (e.g. extension deleted), fall back to first one.
69  $reportIdentifier = $firstReportIdentifier;
70  }
71  if (($backendUserUc['action'] ?? '') !== 'detail'
72  || ($backendUserUc['report'] ?? '') !== $reportIdentifier
73  ) {
74  // Update uc if view changed to render same view on next call.
75  $this->‪updateBackendUserUc('detail', $reportIdentifier);
76  }
77  return $this->‪detailAction($request, $reportIdentifier);
78  }
79 
83  protected function ‪indexAction(ServerRequestInterface $request): ResponseInterface
84  {
85  $languageService = $this->‪getLanguageService();
86 
87  $view = $this->moduleTemplateFactory->create($request);
88  $view->assignMultiple([
89  'reports' => $this->reportRegistry->getReports(),
90  ]);
91  $view->setTitle(
92  $languageService->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:mlang_tabs_tab'),
93  $languageService->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:reports_overview')
94  );
95  $this->‪addMainMenu($view);
96  $this->‪addShortcutButton(
97  $view,
98  $languageService->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:reports_overview'),
99  ['action' => 'index']
100  );
101  return $view->renderResponse('Report/Index');
102  }
103 
107  protected function ‪detailAction(ServerRequestInterface $request, string $report): ResponseInterface
108  {
109  $languageService = $this->‪getLanguageService();
110  $reportInstance = $this->reportRegistry->getReport($report);
111  $content = $reportInstance instanceof ‪RequestAwareReportInterface ? $reportInstance->‪getReport($request) : $reportInstance->getReport();
112 
113  $view = $this->moduleTemplateFactory->create($request);
114  $view->assignMultiple([
115  'content' => $content,
116  'report' => $reportInstance,
117  ]);
118  $view->setTitle(
119  $languageService->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:mlang_tabs_tab'),
120  $languageService->sL($reportInstance->getTitle())
121  );
122  $allReports = $this->reportRegistry->getReports();
123  if (count($allReports) > 1) {
124  // Add the main module drop-down only if there are more than one reports registered.
125  // This also means the "overview" view is not selectable with default core.
126  $this->‪addMainMenu($view, $report);
127  }
128  $this->‪addShortcutButton(
129  $view,
130  $languageService->sL($reportInstance->getTitle()),
131  ['action' => 'detail', 'report' => $reportInstance->getIdentifier()]
132  );
133  return $view->renderResponse('Report/Detail');
134  }
135 
136  protected function ‪addMainMenu(‪ModuleTemplate $view, string $activeReportIdentifier = ''): void
137  {
138  $languageService = $this->‪getLanguageService();
139  $menu = $view->‪getDocHeaderComponent()->getMenuRegistry()->makeMenu();
140  $menu->setIdentifier('WebFuncJumpMenu');
141  $menuItem = $menu->makeMenuItem()
142  ->setHref(
143  (string)$this->uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
144  )
145  ->setTitle($languageService->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:reports_overview'));
146  $menu->addMenuItem($menuItem);
147  foreach ($this->reportRegistry->getReports() as $report) {
148  $menuItem = $menu->makeMenuItem()
149  ->setHref((string)$this->uriBuilder->buildUriFromRoute(
150  'system_reports',
151  ['action' => 'detail', 'report' => $report->getIdentifier()]
152  ))
153  ->setTitle($this->‪getLanguageService()->sL($report->getTitle()));
154  if ($activeReportIdentifier === $report->getIdentifier()) {
155  $menuItem->setActive(true);
156  }
157  $menu->addMenuItem($menuItem);
158  }
159  $view->‪getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
160  }
161 
162  protected function ‪addShortcutButton(‪ModuleTemplate $view, string $title, array $arguments): void
163  {
164  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
165  $shortcutButton = $buttonBar->makeShortcutButton()
166  ->setRouteIdentifier('system_reports')
167  ->setDisplayName($title)
168  ->setArguments($arguments);
169  $buttonBar->addButton($shortcutButton);
170  }
171 
175  protected function ‪updateBackendUserUc(string $action, string $report = ''): void
176  {
177  $backendUser = $this->‪getBackendUser();
178  $backendUser->uc['reports']['selection'] = [
179  'action' => $action,
180  'report' => $report,
181  ];
182  $backendUser->writeUC();
183  }
184 
186  {
187  return ‪$GLOBALS['BE_USER'];
188  }
189 
191  {
192  return ‪$GLOBALS['LANG'];
193  }
194 }
‪TYPO3\CMS\Reports\Controller\ReportController\indexAction
‪indexAction(ServerRequestInterface $request)
Definition: ReportController.php:83
‪TYPO3\CMS\Reports\Controller\ReportController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: ReportController.php:50
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Reports\Controller\ReportController\updateBackendUserUc
‪updateBackendUserUc(string $action, string $report='')
Definition: ReportController.php:175
‪TYPO3\CMS\Reports\RequestAwareReportInterface\getReport
‪getReport(ServerRequestInterface $request=null)
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Reports\RequestAwareReportInterface
Definition: RequestAwareReportInterface.php:26
‪TYPO3\CMS\Reports\Controller\ReportController\addShortcutButton
‪addShortcutButton(ModuleTemplate $view, string $title, array $arguments)
Definition: ReportController.php:162
‪TYPO3\CMS\Reports\Registry\ReportRegistry
Definition: ReportRegistry.php:29
‪TYPO3\CMS\Reports\Controller\ReportController\getBackendUser
‪getBackendUser()
Definition: ReportController.php:185
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Reports\Controller\ReportController\addMainMenu
‪addMainMenu(ModuleTemplate $view, string $activeReportIdentifier='')
Definition: ReportController.php:136
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:32
‪TYPO3\CMS\Reports\Controller\ReportController\getLanguageService
‪getLanguageService()
Definition: ReportController.php:190
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getDocHeaderComponent
‪getDocHeaderComponent()
Definition: ModuleTemplate.php:181
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Reports\Controller
Definition: ReportController.php:18
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Reports\Controller\ReportController\__construct
‪__construct(protected readonly UriBuilder $uriBuilder, protected readonly ModuleTemplateFactory $moduleTemplateFactory, protected readonly IconRegistry $iconRegistry, protected readonly ReportRegistry $reportRegistry)
Definition: ReportController.php:40
‪TYPO3\CMS\Reports\Controller\ReportController
Definition: ReportController.php:39
‪TYPO3\CMS\Reports\Controller\ReportController\detailAction
‪detailAction(ServerRequestInterface $request, string $report)
Definition: ReportController.php:107