Completed
Push — master ( 818496...5be7b8 )
by
unknown
16:18
created

ReportController::generateButtons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
18
namespace TYPO3\CMS\Reports\Controller;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use TYPO3\CMS\Backend\Routing\UriBuilder;
23
use TYPO3\CMS\Backend\Template\ModuleTemplate;
24
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
25
use TYPO3\CMS\Core\Http\HtmlResponse;
26
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
     * ModuleTemplate object
42
     *
43
     * @var ModuleTemplate
44
     */
45
    protected $moduleTemplate;
46
47
    /**
48
     * @var ViewInterface
49
     */
50
    protected $view;
51
52
    /**
53
     * Module name for the shortcut
54
     *
55
     * @var string
56
     */
57
    protected $shortcutName;
58
59
    /**
60
     * Instantiate the report controller
61
     */
62
    public function __construct()
63
    {
64
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
65
    }
66
67
    /**
68
     * Injects the request object for the current request, and renders correct action
69
     *
70
     * @param ServerRequestInterface $request the current request
71
     * @return ResponseInterface the response with the content
72
     */
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) {
0 ignored issues
show
introduced by
$response is always a sub-type of Psr\Http\Message\ResponseInterface.
Loading history...
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
114
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
115
        $shortcutButton = $buttonBar->makeShortcutButton()
116
            ->setModuleName('system_reports')
117
            ->setDisplayName($this->shortcutName)
118
            ->setArguments([
119
                'route' => $request->getQueryParams()['route'],
120
                'action' => $action,
121
                'extension' => $extension,
122
                'report' => $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'] ?? '',
123
            ]);
124
        $buttonBar->addButton($shortcutButton);
125
126
        $this->moduleTemplate->setContent($this->view->render());
127
        return new HtmlResponse($this->moduleTemplate->renderContent());
128
    }
129
130
    /**
131
     * @param string $templateName
132
     */
133
    protected function initializeView(string $templateName)
134
    {
135
        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
136
        $this->view->setTemplate($templateName);
137
        $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
138
        $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
139
        $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
140
        $this->view->getRequest()->setControllerExtensionName('Reports');
141
    }
142
143
    /**
144
     * Overview
145
     */
146
    protected function indexAction()
147
    {
148
        $this->view->assign('reports', $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
149
        $this->saveState();
150
    }
151
152
    /**
153
     * Display a single report
154
     *
155
     * @param ServerRequestInterface $request
156
     * @return ResponseInterface|void
157
     */
158
    protected function detailAction(ServerRequestInterface $request)
159
    {
160
        $content = $error = '';
161
        $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
162
        $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
163
164
        $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
165
166
        if ($reportClass === null || !class_exists($reportClass)) {
167
            $this->resetState();
168
            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
169
            return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
170
                'action' => 'index',
171
                'redirect' => 1,
172
            ]), 303);
173
        }
174
175
        $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
176
177
        if ($reportInstance instanceof ReportInterface) {
178
            if ($reportInstance instanceof RequestAwareReportInterface) {
179
                $content = $reportInstance->getReport($request);
180
            } else {
181
                $content = $reportInstance->getReport();
182
            }
183
            $this->saveState($extension, $report);
184
        } else {
185
            $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
186
        }
187
188
        $this->view->assignMultiple([
189
            'content' => $content,
190
            'error' => $error,
191
            'report' => $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
192
        ]);
193
    }
194
195
    /**
196
     * Generates the menu
197
     *
198
     * @param ServerRequestInterface $request
199
     */
200
    protected function generateMenu(ServerRequestInterface $request)
201
    {
202
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
203
        $lang = $this->getLanguageService();
204
        $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
205
        $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
206
        $menu->setIdentifier('WebFuncJumpMenu');
207
        $menuItem = $menu
208
            ->makeMenuItem()
209
            ->setHref(
210
                $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
211
            )
212
            ->setTitle($lang->getLL('reports_overview'));
213
        $menu->addMenuItem($menuItem);
214
        $this->shortcutName = $lang->getLL('reports_overview');
215
216
        $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
217
        $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
218
219
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
220
            foreach ($reports as $reportName => $report) {
221
                $menuItem = $menu
222
                    ->makeMenuItem()
223
                    ->setHref($uriBuilder->buildUriFromRoute(
224
                        'system_reports',
225
                        ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
226
                    ))
227
                    ->setTitle($this->getLanguageService()->sL($report['title']));
228
                if ($extensionParam === $extKey && $reportParam === $reportName) {
229
                    $menuItem->setActive(true);
230
                    $this->shortcutName = $menuItem->getTitle();
231
                }
232
                $menu->addMenuItem($menuItem);
233
            }
234
        }
235
        $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
236
    }
237
238
    /**
239
     * Save the selected report
240
     *
241
     * @param string $extension Extension name
242
     * @param string $report Report name
243
     */
244
    protected function saveState(string $extension = '', string $report = '')
245
    {
246
        $this->getBackendUser()->uc['reports']['selection'] = [
247
            'extension' => $extension,
248
            'report' => $report,
249
        ];
250
        $this->getBackendUser()->writeUC();
251
    }
252
253
    /**
254
     * Reset state in user settings
255
     */
256
    protected function resetState(): void
257
    {
258
        $this->getBackendUser()->uc['reports']['selection'] = [];
259
        $this->getBackendUser()->writeUC();
260
    }
261
262
    /**
263
     * @return BackendUserAuthentication
264
     */
265
    protected function getBackendUser(): BackendUserAuthentication
266
    {
267
        return $GLOBALS['BE_USER'];
268
    }
269
270
    /**
271
     * @return LanguageService
272
     */
273
    protected function getLanguageService(): LanguageService
274
    {
275
        return $GLOBALS['LANG'];
276
    }
277
}
278