ReportController.php 9.7 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
9
10
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
11
 *
12
13
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
16
 * The TYPO3 project - inspiring people to share!
 */
17
18
19

namespace TYPO3\CMS\Reports\Controller;

20
21
22
23
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
24
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
25
26
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\RedirectResponse;
27
use TYPO3\CMS\Core\Localization\LanguageService;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Fluid\View\StandaloneView;
30
use TYPO3\CMS\Reports\ReportInterface;
31
use TYPO3\CMS\Reports\RequestAwareReportInterface;
32
use TYPO3Fluid\Fluid\View\ViewInterface;
33

34
35
/**
 * Reports controller
36
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
37
 */
38
class ReportController
39
{
40
    /**
41
42
43
     * ModuleTemplate object
     *
     * @var ModuleTemplate
44
     */
45
    protected $moduleTemplate;
46
47

    /**
48
     * @var ViewInterface
49
     */
50
    protected $view;
51

52
53
54
55
56
57
58
    /**
     * Module name for the shortcut
     *
     * @var string
     */
    protected $shortcutName;

59
    /**
60
     * Instantiate the report controller
61
     */
62
    public function __construct()
63
    {
64
65
66
67
68
69
70
71
72
73
74
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
    }

    /**
     * Injects the request object for the current request, and renders correct action
     *
     * @param ServerRequestInterface $request the current request
     * @return ResponseInterface the response with the content
     */
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
    {
75
        $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? '';
76
        $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'] ?? '';
77
78
        $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;

79
        if ($action !== 'index' && !$isRedirect && !$extension
80
            && is_array($GLOBALS['BE_USER']->uc['reports']['selection'])) {
81
82
            $previousSelection = $GLOBALS['BE_USER']->uc['reports']['selection'];
            if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
83
84
85
                $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
                return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
                    'action' => 'detail',
86
87
88
                    'extension' => $previousSelection['extension'],
                    'report' => $previousSelection['report'],
                    'redirect' => 1,
89
                ]), 303);
90
91
            }
        }
92
93
94
        if (empty($action)) {
            $action = 'index';
        }
95
96
97

        $this->initializeView($action);

98
99
100
        if ($action === 'index') {
            $this->indexAction();
        } elseif ($action === 'detail') {
101
102
103
104
            $response = $this->detailAction($request);
            if ($response instanceof ResponseInterface) {
                return $response;
            }
105
106
107
108
109
        } else {
            throw new \RuntimeException(
                'Reports module has only "index" and "detail" action, ' . (string)$action . ' given',
                1536322935
            );
110
111
112
        }

        $this->generateMenu($request);
113
114
115

        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
        $shortcutButton = $buttonBar->makeShortcutButton()
116
            ->setRouteIdentifier('system_reports')
117
118
119
120
121
122
123
            ->setDisplayName($this->shortcutName)
            ->setArguments([
                'action' => $action,
                'extension' => $extension,
                'report' => $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'] ?? '',
            ]);
        $buttonBar->addButton($shortcutButton);
124
125
126

        $this->moduleTemplate->setContent($this->view->render());
        return new HtmlResponse($this->moduleTemplate->renderContent());
127
    }
128

129
    /**
130
     * @param string $templateName
131
     */
132
    protected function initializeView(string $templateName)
133
    {
134
135
136
137
138
139
        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
        $this->view->setTemplate($templateName);
        $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
        $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
        $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
        $this->view->getRequest()->setControllerExtensionName('Reports');
140
141
    }

142
143
144
    /**
     * Overview
     */
145
    protected function indexAction()
146
    {
147
        $this->view->assign('reports', $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
148
149
        $this->saveState();
    }
150

151
152
153
    /**
     * Display a single report
     *
154
     * @param ServerRequestInterface $request
155
     * @return ResponseInterface|void
156
     */
157
    protected function detailAction(ServerRequestInterface $request)
158
    {
159
160
161
162
163
        $content = $error = '';
        $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
        $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];

        $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
164

165
166
167
168
169
170
171
172
173
        if ($reportClass === null || !class_exists($reportClass)) {
            $this->resetState();
            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
            return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
                'action' => 'index',
                'redirect' => 1,
            ]), 303);
        }

174
        $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
175

176
        if ($reportInstance instanceof ReportInterface) {
177
178
179
180
181
            if ($reportInstance instanceof RequestAwareReportInterface) {
                $content = $reportInstance->getReport($request);
            } else {
                $content = $reportInstance->getReport();
            }
182
183
184
185
            $this->saveState($extension, $report);
        } else {
            $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
        }
186

187
        $this->view->assignMultiple([
188
189
190
            'content' => $content,
            'error' => $error,
            'report' => $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
191
        ]);
192
    }
193

194
    /**
195
     * Generates the menu
196
197
     *
     * @param ServerRequestInterface $request
198
     */
199
    protected function generateMenu(ServerRequestInterface $request)
200
    {
201
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
202
203
        $lang = $this->getLanguageService();
        $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
204
        $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
205
206
207
208
        $menu->setIdentifier('WebFuncJumpMenu');
        $menuItem = $menu
            ->makeMenuItem()
            ->setHref(
209
                $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
210
211
212
            )
            ->setTitle($lang->getLL('reports_overview'));
        $menu->addMenuItem($menuItem);
213
        $this->shortcutName = $lang->getLL('reports_overview');
214
215
216
217

        $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
        $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];

218
219
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
            foreach ($reports as $reportName => $report) {
220
221
                $menuItem = $menu
                    ->makeMenuItem()
222
223
224
225
                    ->setHref($uriBuilder->buildUriFromRoute(
                        'system_reports',
                        ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
                    ))
226
                    ->setTitle($this->getLanguageService()->sL($report['title']));
227
228
229
                if ($extensionParam === $extKey && $reportParam === $reportName) {
                    $menuItem->setActive(true);
                    $this->shortcutName = $menuItem->getTitle();
230
231
232
233
                }
                $menu->addMenuItem($menuItem);
            }
        }
234
        $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
235
236
    }

237
238
239
240
241
242
    /**
     * Save the selected report
     *
     * @param string $extension Extension name
     * @param string $report Report name
     */
243
    protected function saveState(string $extension = '', string $report = '')
244
    {
245
        $this->getBackendUser()->uc['reports']['selection'] = [
246
247
            'extension' => $extension,
            'report' => $report,
248
        ];
249
        $this->getBackendUser()->writeUC();
250
    }
251

252
253
254
255
256
257
258
259
260
    /**
     * Reset state in user settings
     */
    protected function resetState(): void
    {
        $this->getBackendUser()->uc['reports']['selection'] = [];
        $this->getBackendUser()->writeUC();
    }

261
262
263
    /**
     * @return BackendUserAuthentication
     */
264
    protected function getBackendUser(): BackendUserAuthentication
265
266
267
268
269
270
271
    {
        return $GLOBALS['BE_USER'];
    }

    /**
     * @return LanguageService
     */
272
    protected function getLanguageService(): LanguageService
273
274
275
    {
        return $GLOBALS['LANG'];
    }
276
}