‪TYPO3CMS  9.5
ReportController.php
Go to the documentation of this file.
1 <?php
2 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 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
30 use TYPO3Fluid\Fluid\View\ViewInterface;
31 
37 {
39 
43  private ‪$deprecatedPublicMethods = [
44  'indexAction' => 'Using ReportController::indexAction() is deprecated and will not be possible anymore in TYPO3 v10.0.',
45  'detailAction' => 'Using ReportController::detailAction() is deprecated and will not be possible anymore in TYPO3 v10.0.',
46  ];
47 
53  protected ‪$moduleTemplate;
54 
58  protected ‪$view;
59 
65  protected ‪$shortcutName;
66 
70  public function ‪__construct()
71  {
72  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
73  }
74 
81  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
82  {
83  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? '';
84  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
85  $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;
86 
87  if ($action !== 'index' && !$isRedirect && !$extension
88  && is_array(‪$GLOBALS['BE_USER']->uc['reports']['selection'])) {
89  $previousSelection = ‪$GLOBALS['BE_USER']->uc['reports']['selection'];
90  if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
91  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
92  return new ‪RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
93  'action' => 'detail',
94  'extension' => $previousSelection['extension'],
95  'report' => $previousSelection['report'],
96  'redirect' => 1,
97  ]), 303);
98  }
99  }
100  if (empty($action)) {
101  $action = 'index';
102  }
103 
104  $this->‪initializeView($action);
105 
106  if ($action === 'index') {
107  $this->‪indexAction();
108  } elseif ($action === 'detail') {
109  $response = $this->‪detailAction($request);
110  if ($response instanceof ResponseInterface) {
111  return $response;
112  }
113  } else {
114  throw new \RuntimeException(
115  'Reports module has only "index" and "detail" action, ' . (string)$action . ' given',
116  1536322935
117  );
118  }
119 
120  $this->‪generateMenu($request);
121  $this->‪generateButtons();
122 
123  $this->moduleTemplate->setContent($this->view->render());
124  return new HtmlResponse($this->moduleTemplate->renderContent());
125  }
126 
130  protected function ‪initializeView(string $templateName)
131  {
132  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
133  $this->view->setTemplate($templateName);
134  $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
135  $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
136  $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
137  $this->view->getRequest()->setControllerExtensionName('Reports');
138  }
139 
143  protected function ‪indexAction()
144  {
145  $this->view->assign('reports', ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
146  $this->‪saveState();
147  }
148 
155  protected function ‪detailAction(ServerRequestInterface $request)
156  {
157  $content = $error = '';
158  $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
159  $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
160 
161  $reportClass = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
162 
163  if ($reportClass === null || !class_exists($reportClass)) {
164  $this->‪resetState();
165  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
166  return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
167  'action' => 'index',
168  'redirect' => 1,
169  ]), 303);
170  }
171 
172  $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
173 
174  if ($reportInstance instanceof ReportInterface) {
175  if ($reportInstance instanceof RequestAwareReportInterface) {
176  $content = $reportInstance->getReport($request);
177  } else {
178  $content = $reportInstance->getReport();
179  }
180  $this->‪saveState($extension, $report);
181  } else {
182  $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
183  }
184 
185  $this->view->assignMultiple([
186  'content' => $content,
187  'error' => $error,
188  'report' => ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
189  ]);
190  }
191 
197  protected function ‪generateMenu(ServerRequestInterface $request)
198  {
199  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
200  $lang = $this->‪getLanguageService();
201  $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
202  $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
203  $menu->setIdentifier('WebFuncJumpMenu');
204  $menuItem = $menu
205  ->makeMenuItem()
206  ->setHref(
207  $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
208  )
209  ->setTitle($lang->getLL('reports_overview'));
210  $menu->addMenuItem($menuItem);
211  $this->shortcutName = $lang->getLL('reports_overview');
212 
213  $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
214  $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
215 
216  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
217  foreach ($reports as $reportName => $report) {
218  $menuItem = $menu
219  ->makeMenuItem()
220  ->setHref($uriBuilder->buildUriFromRoute(
221  'system_reports',
222  ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
223  ))
224  ->setTitle($this->‪getLanguageService()->sL($report['title']));
225  if ($extensionParam === $extKey && $reportParam === $reportName) {
226  $menuItem->setActive(true);
227  $this->shortcutName = $menuItem->getTitle();
228  }
229  $menu->addMenuItem($menuItem);
230  }
231  }
232  $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
233  }
234 
238  protected function ‪generateButtons()
239  {
240  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
241 
242  $shortcutButton = $buttonBar->makeShortcutButton()
243  ->setModuleName('system_reports')
244  ->setGetVariables(['action', 'extension', 'report'])
245  ->setDisplayName($this->shortcutName);
246  $buttonBar->addButton($shortcutButton);
247  }
248 
255  protected function ‪saveState(string $extension = '', string $report = '')
256  {
257  $this->‪getBackendUser()->uc['reports']['selection'] = [
258  'extension' => $extension,
259  'report' => $report,
260  ];
261  $this->‪getBackendUser()->‪writeUC();
262  }
263 
267  protected function ‪resetState(): void
268  {
269  $this->‪getBackendUser()->uc['reports']['selection'] = [];
270  $this->‪getBackendUser()->‪writeUC();
271  }
272 
276  protected function ‪getBackendUser(): ‪BackendUserAuthentication
277  {
278  return ‪$GLOBALS['BE_USER'];
279  }
280 
284  protected function ‪getLanguageService(): ‪LanguageService
285  {
286  return ‪$GLOBALS['LANG'];
287  }
288 }
‪TYPO3\CMS\Reports\ReportInterface
Definition: ReportInterface.php:21
‪TYPO3\CMS\Reports\Controller\ReportController\saveState
‪saveState(string $extension='', string $report='')
Definition: ReportController.php:250
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:1172
‪TYPO3\CMS\Reports\Controller\ReportController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReportController.php:279
‪TYPO3\CMS\Reports\Controller\ReportController\detailAction
‪ResponseInterface null detailAction(ServerRequestInterface $request)
Definition: ReportController.php:150
‪TYPO3\CMS\Reports\Controller\ReportController\__construct
‪__construct()
Definition: ReportController.php:65
‪TYPO3\CMS\Reports\Controller\ReportController\generateButtons
‪generateButtons()
Definition: ReportController.php:233
‪TYPO3\CMS\Reports\Controller\ReportController\indexAction
‪indexAction()
Definition: ReportController.php:138
‪TYPO3\CMS\Reports\Controller\ReportController\resetState
‪resetState()
Definition: ReportController.php:262
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Reports\Controller\ReportController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ReportController.php:50
‪TYPO3\CMS\Reports\RequestAwareReportInterface
Definition: RequestAwareReportInterface.php:24
‪TYPO3\CMS\Reports\Controller\ReportController\$shortcutName
‪string $shortcutName
Definition: ReportController.php:60
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Reports\Controller\ReportController\$view
‪ViewInterface $view
Definition: ReportController.php:54
‪TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait
Definition: PublicMethodDeprecationTrait.php:68
‪TYPO3\CMS\Reports\Controller\ReportController\initializeView
‪initializeView(string $templateName)
Definition: ReportController.php:125
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:27
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Reports\Controller
Definition: ReportController.php:3
‪TYPO3\CMS\Reports\Controller\ReportController\$deprecatedPublicMethods
‪array $deprecatedPublicMethods
Definition: ReportController.php:41
‪TYPO3\CMS\Reports\Controller\ReportController\generateMenu
‪generateMenu(ServerRequestInterface $request)
Definition: ReportController.php:192
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Reports\Controller\ReportController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: ReportController.php:76
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25
‪TYPO3\CMS\Reports\Controller\ReportController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ReportController.php:271
‪TYPO3\CMS\Reports\Controller\ReportController
Definition: ReportController.php:37