‪TYPO3CMS  11.5
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;
35 use TYPO3Fluid\Fluid\View\ViewInterface;
36 
42 {
48  protected ‪$moduleTemplate;
49 
53  protected ‪$view;
54 
60  protected ‪$shortcutName;
61 
62  protected ‪UriBuilder ‪$uriBuilder;
65 
66  public function ‪__construct(
70  ) {
71  $this->uriBuilder = ‪$uriBuilder;
72  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
73  $this->iconRegistry = ‪$iconRegistry;
74  }
75 
82  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
83  {
84  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
85  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? '';
86  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'] ?? '';
87  $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;
88 
89  if ($action !== 'index' && !$isRedirect && !$extension
90  && is_array(‪$GLOBALS['BE_USER']->uc['reports']['selection'] ?? null)) {
91  $previousSelection = ‪$GLOBALS['BE_USER']->uc['reports']['selection'];
92  if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
93  return new ‪RedirectResponse((string)$this->uriBuilder->buildUriFromRoute('system_reports', [
94  'action' => 'detail',
95  'extension' => $previousSelection['extension'],
96  'report' => $previousSelection['report'],
97  'redirect' => 1,
98  ]), 303);
99  }
100  }
101  if (empty($action)) {
102  $action = 'index';
103  }
104 
105  $this->‪initializeView($action);
106 
107  if ($action === 'index') {
108  $this->‪indexAction();
109  } elseif ($action === 'detail') {
110  $response = $this->‪detailAction($request);
111  if ($response instanceof ResponseInterface) {
112  return $response;
113  }
114  } else {
115  throw new \RuntimeException(
116  'Reports module has only "index" and "detail" action, ' . (string)$action . ' given',
117  1536322935
118  );
119  }
120 
121  $this->‪generateMenu($request);
122 
123  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
124  $shortcutButton = $buttonBar->makeShortcutButton()
125  ->setRouteIdentifier('system_reports')
126  ->setDisplayName($this->shortcutName)
127  ->setArguments([
128  'action' => $action,
129  'extension' => $extension,
130  'report' => $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'] ?? '',
131  ]);
132  $buttonBar->addButton($shortcutButton);
133 
134  $this->moduleTemplate->setContent($this->view->render());
135  return new HtmlResponse($this->moduleTemplate->renderContent());
136  }
137 
141  protected function ‪initializeView(string $templateName)
142  {
143  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
144  $this->view->setTemplate($templateName);
145  $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
146  $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
147  $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
148  $this->view->getRequest()->setControllerExtensionName('Reports');
149  }
150 
154  protected function ‪indexAction()
155  {
156  $this->moduleTemplate->setTitle(
157  $this->‪getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:mlang_tabs_tab'),
158  $this->‪getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:reports_overview')
159  );
160 
161  $reports = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'];
162 
163  foreach ($reports as $extension => $reportModules) {
164  foreach ($reportModules as $module => $configuration) {
165  $icon = $configuration['icon'] ?? 'EXT:reports/Resources/Public/Icons/Extension.png';
166  $isRegisteredIcon = $reports[$extension][$module]['isIconIdentifier'] = $this->iconRegistry->isRegistered($icon);
167  if (!$isRegisteredIcon) {
168  // TODO: deprecate icons from non extension resources
169  $reports[$extension][$module]['icon'] = ‪PathUtility::isExtensionPath($icon) ? ‪PathUtility::getPublicResourceWebPath($icon) : PathUtility::getAbsoluteWebPath($icon);
170  }
171  }
172  }
173 
174  $this->view->assign('reports', $reports);
175  $this->‪saveState();
176  }
177 
184  protected function ‪detailAction(ServerRequestInterface $request)
185  {
186  $content = $error = '';
187  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
188  $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
189 
190  $reportClass = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
191 
192  if ($reportClass === null || !class_exists($reportClass)) {
193  $this->‪resetState();
194  return new RedirectResponse((string)$this->uriBuilder->buildUriFromRoute('system_reports', [
195  'action' => 'index',
196  'redirect' => 1,
197  ]), 303);
198  }
199 
200  $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
201 
202  if ($reportInstance instanceof ReportInterface) {
203  if ($reportInstance instanceof RequestAwareReportInterface) {
204  $content = $reportInstance->getReport($request);
205  } else {
206  $content = $reportInstance->getReport();
207  }
208  $this->‪saveState($extension, $report);
209  } else {
210  $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
211  }
212 
213  $this->moduleTemplate->setTitle(
214  $this->‪getLanguageService()->sL('LLL:EXT:reports/Resources/Private/Language/locallang.xlf:mlang_tabs_tab'),
215  $this->‪getLanguageService()->sL(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['title'])
216  );
217 
218  $this->view->assignMultiple([
219  'content' => $content,
220  'error' => $error,
221  'report' => ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
222  ]);
223  }
224 
230  protected function ‪generateMenu(ServerRequestInterface $request)
231  {
232  $lang = $this->‪getLanguageService();
233  $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
234  $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
235  $menu->setIdentifier('WebFuncJumpMenu');
236  $menuItem = $menu
237  ->makeMenuItem()
238  ->setHref((string)$this->uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index']))
239  ->setTitle($lang->getLL('reports_overview'));
240  $menu->addMenuItem($menuItem);
241  $this->shortcutName = $lang->getLL('reports_overview');
242 
243  $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'] ?? '';
244  $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'] ?? '';
245 
246  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
247  foreach ($reports as $reportName => $report) {
248  $menuItem = $menu
249  ->makeMenuItem()
250  ->setHref((string)$this->uriBuilder->buildUriFromRoute(
251  'system_reports',
252  ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
253  ))
254  ->setTitle($this->‪getLanguageService()->sL($report['title']));
255  if ($extensionParam === $extKey && $reportParam === $reportName) {
256  $menuItem->setActive(true);
257  $this->shortcutName = $menuItem->getTitle();
258  }
259  $menu->addMenuItem($menuItem);
260  }
261  }
262  $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
263  }
264 
271  protected function ‪saveState(string $extension = '', string $report = '')
272  {
273  $this->‪getBackendUser()->uc['reports']['selection'] = [
274  'extension' => $extension,
275  'report' => $report,
276  ];
277  $this->‪getBackendUser()->‪writeUC();
278  }
279 
283  protected function ‪resetState(): void
284  {
285  $this->‪getBackendUser()->uc['reports']['selection'] = [];
286  $this->‪getBackendUser()->‪writeUC();
287  }
288 
292  protected function ‪getBackendUser(): BackendUserAuthentication
293  {
294  return ‪$GLOBALS['BE_USER'];
295  }
296 
300  protected function ‪getLanguageService(): LanguageService
301  {
302  return ‪$GLOBALS['LANG'];
303  }
304 }
‪TYPO3\CMS\Reports\ReportInterface
Definition: ReportInterface.php:22
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Reports\Controller\ReportController\saveState
‪saveState(string $extension='', string $report='')
Definition: ReportController.php:268
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:984
‪TYPO3\CMS\Reports\Controller\ReportController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReportController.php:297
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static bool isExtensionPath(string $path)
Definition: PathUtility.php:121
‪TYPO3\CMS\Reports\Controller\ReportController\detailAction
‪ResponseInterface void detailAction(ServerRequestInterface $request)
Definition: ReportController.php:181
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static string getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Reports\Controller\ReportController\indexAction
‪indexAction()
Definition: ReportController.php:151
‪TYPO3\CMS\Reports\Controller\ReportController\resetState
‪resetState()
Definition: ReportController.php:280
‪TYPO3\CMS\Reports\Controller\ReportController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: ReportController.php:60
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Reports\Controller\ReportController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ReportController.php:47
‪TYPO3\CMS\Reports\Controller\ReportController\__construct
‪__construct(UriBuilder $uriBuilder, ModuleTemplateFactory $moduleTemplateFactory, IconRegistry $iconRegistry)
Definition: ReportController.php:63
‪TYPO3\CMS\Reports\RequestAwareReportInterface
Definition: RequestAwareReportInterface.php:26
‪TYPO3\CMS\Reports\Controller\ReportController\$shortcutName
‪string $shortcutName
Definition: ReportController.php:57
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Reports\Controller\ReportController\$view
‪ViewInterface $view
Definition: ReportController.php:51
‪TYPO3\CMS\Reports\Controller\ReportController\initializeView
‪initializeView(string $templateName)
Definition: ReportController.php:138
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Reports\Controller\ReportController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: ReportController.php:59
‪TYPO3\CMS\Reports\Controller
Definition: ReportController.php:18
‪TYPO3\CMS\Reports\Controller\ReportController\generateMenu
‪generateMenu(ServerRequestInterface $request)
Definition: ReportController.php:227
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Reports\Controller\ReportController\$iconRegistry
‪IconRegistry $iconRegistry
Definition: ReportController.php:61
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Reports\Controller\ReportController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: ReportController.php:79
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Reports\Controller\ReportController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ReportController.php:289
‪TYPO3\CMS\Reports\Controller\ReportController
Definition: ReportController.php:42