Passed
Push — master ( ff1a76...9dbd27 )
by
unknown
13:17
created

HelpController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 75
dl 0
loc 204
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 3 1
A allAction() 0 3 1
A handleRequest() 0 29 5
A initializeView() 0 9 1
A __construct() 0 5 1
A detailAction() 0 10 1
A getBackendUser() 0 3 1
A registerDocheaderButtons() 0 23 2
A getShortcutTitle() 0 10 3
A getLanguageService() 0 3 1
A getManuals() 0 8 2
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\Backend\Controller;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use TYPO3\CMS\Backend\Domain\Repository\TableManualRepository;
23
use TYPO3\CMS\Backend\Routing\UriBuilder;
24
use TYPO3\CMS\Backend\Template\ModuleTemplate;
25
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
26
use TYPO3\CMS\Core\Http\HtmlResponse;
27
use TYPO3\CMS\Core\Http\RedirectResponse;
28
use TYPO3\CMS\Core\Imaging\Icon;
29
use TYPO3\CMS\Core\Information\Typo3Information;
30
use TYPO3\CMS\Core\Localization\LanguageService;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Fluid\View\StandaloneView;
33
use TYPO3Fluid\Fluid\View\ViewInterface;
34
35
/**
36
 * Main "CSH help" module controller
37
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
38
 */
39
class HelpController
40
{
41
    protected const ALLOWED_ACTIONS = ['index', 'all', 'detail'];
42
43
    /**
44
     * Section identifiers
45
     */
46
    const FULL = 0;
47
48
    /**
49
     * Show only Table of contents
50
     */
51
    const TOC_ONLY = 1;
52
53
    /**
54
     * @var TableManualRepository
55
     */
56
    protected $tableManualRepository;
57
58
    /** @var ModuleTemplate */
59
    protected $moduleTemplate;
60
61
    /** @var ViewInterface */
62
    protected $view;
63
64
    /**
65
     * @var Typo3Information
66
     */
67
    protected $typo3Information;
68
69
    /**
70
     * Instantiate the report controller
71
     *
72
     * @param Typo3Information $typo3Information
73
     */
74
    public function __construct(Typo3Information $typo3Information)
75
    {
76
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
77
        $this->tableManualRepository = GeneralUtility::makeInstance(TableManualRepository::class);
78
        $this->typo3Information = $typo3Information;
79
    }
80
81
    /**
82
     * Injects the request object for the current request, and renders correct action
83
     *
84
     * @param ServerRequestInterface $request the current request
85
     * @return ResponseInterface the response with the content
86
     */
87
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
88
    {
89
        $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? 'index';
90
91
        if ($action === 'detail') {
92
            $table = $request->getQueryParams()['table'] ?? $request->getParsedBody()['table'];
93
            if (!$table) {
94
                $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
95
                return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('help_cshmanual', [
96
                    'action' => 'index',
97
                ]), 303);
98
            }
99
        }
100
101
        if (!in_array($action, self::ALLOWED_ACTIONS, true)) {
102
            return new HtmlResponse('Action not allowed', 400);
103
        }
104
105
        $this->initializeView($action);
106
107
        $result = $this->{$action . 'Action'}($request);
108
        if ($result instanceof ResponseInterface) {
109
            return $result;
110
        }
111
112
        $this->registerDocheaderButtons($request);
113
114
        $this->moduleTemplate->setContent($this->view->render());
115
        return new HtmlResponse($this->moduleTemplate->renderContent());
116
    }
117
118
    /**
119
     * @param string $templateName
120
     */
121
    protected function initializeView(string $templateName)
122
    {
123
        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
124
        $this->view->setTemplate($templateName);
125
        $this->view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ContextSensitiveHelp']);
126
        $this->view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials']);
127
        $this->view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
128
        $this->view->getRequest()->setControllerExtensionName('Backend');
129
        $this->view->assign('copyright', $this->typo3Information->getCopyrightNotice());
130
    }
131
132
    /**
133
     * Show table of contents
134
     */
135
    public function indexAction()
136
    {
137
        $this->view->assign('toc', $this->tableManualRepository->getSections(self::TOC_ONLY));
138
    }
139
140
    /**
141
     * Show the table of contents and all manuals
142
     */
143
    public function allAction()
144
    {
145
        $this->view->assign('all', $this->tableManualRepository->getSections(self::FULL));
146
    }
147
148
    /**
149
     * Show a single manual
150
     *
151
     * @param ServerRequestInterface $request
152
     */
153
    public function detailAction(ServerRequestInterface $request)
154
    {
155
        $table = $request->getQueryParams()['table'] ?? $request->getParsedBody()['table'];
156
        $field = $request->getQueryParams()['field'] ?? $request->getParsedBody()['field'] ?? '*';
157
158
        $this->view->assignMultiple([
159
            'table' => $table,
160
            'key' => $table,
161
            'field' => $field,
162
            'manuals' => $this->getManuals($request)
163
        ]);
164
    }
165
166
    /**
167
     * Registers the Icons into the docheader
168
     *
169
     * @param ServerRequestInterface $request
170
     */
171
    protected function registerDocheaderButtons(ServerRequestInterface $request)
172
    {
173
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
174
175
        $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? 'index';
176
        $shortcutButton = $buttonBar->makeShortcutButton()
177
            ->setModuleName('help_cshmanual')
178
            ->setDisplayName($this->getShortcutTitle($request))
179
            ->setArguments([
180
                'route' => $request->getQueryParams()['route'],
181
                'action' => $action,
182
                'table' => $request->getQueryParams()['table'] ?? '',
183
                'field' => $request->getQueryParams()['field'] ?? ''
184
            ]);
185
        $buttonBar->addButton($shortcutButton);
186
187
        if ($action !== 'index') {
188
            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
189
            $backButton = $buttonBar->makeLinkButton()
190
                ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:back'))
191
                ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-up', Icon::SIZE_SMALL))
192
                ->setHref((string)$uriBuilder->buildUriFromRoute('help_cshmanual'));
193
            $buttonBar->addButton($backButton);
194
        }
195
    }
196
197
    protected function getManuals(ServerRequestInterface $request): array
198
    {
199
        $table = $request->getQueryParams()['table'] ?? $request->getParsedBody()['table'];
200
        $field = $request->getQueryParams()['field'] ?? $request->getParsedBody()['field'] ?? '*';
201
202
        return $field === '*'
203
            ? $this->tableManualRepository->getTableManual($table)
204
            : [$this->tableManualRepository->getSingleManual($table, $field)];
205
    }
206
207
    /**
208
     * Returns the shortcut title for the current page
209
     *
210
     * @param ServerRequestInterface $request
211
     * @return string
212
     */
213
    protected function getShortcutTitle(ServerRequestInterface $request): string
214
    {
215
        $title = $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_mod_help_cshmanual.xlf:mlang_labels_tablabel');
216
        if (($manuals = $this->getManuals($request)) !== []) {
217
            $manualTitle = array_shift($manuals)['headerLine'] ?? '';
218
            if ($manualTitle !== '') {
219
                $title .= ': ' . $manualTitle;
220
            }
221
        }
222
        return $title;
223
    }
224
225
    /**
226
     * Returns the currently logged in BE user
227
     *
228
     * @return BackendUserAuthentication
229
     */
230
    protected function getBackendUser(): BackendUserAuthentication
231
    {
232
        return $GLOBALS['BE_USER'];
233
    }
234
235
    /**
236
     * Returns the LanguageService
237
     *
238
     * @return LanguageService
239
     */
240
    protected function getLanguageService(): LanguageService
241
    {
242
        return $GLOBALS['LANG'];
243
    }
244
}
245