Passed
Push — master ( df3b79...2cba0b )
by
unknown
15:07
created

ConfigurationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\Lowlevel\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\ModuleTemplateFactory;
24
use TYPO3\CMS\Backend\View\ArrayBrowser;
25
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
26
use TYPO3\CMS\Core\Http\HtmlResponse;
27
use TYPO3\CMS\Core\Localization\LanguageService;
28
use TYPO3\CMS\Core\Page\PageRenderer;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Fluid\View\StandaloneView;
31
use TYPO3\CMS\Lowlevel\ConfigurationModuleProvider\ProviderRegistry;
32
33
/**
34
 * View configuration arrays in the backend
35
 * @internal This class is a specific Backend controller implementation and is not part of the TYPO3's Core API.
36
 */
37
class ConfigurationController
38
{
39
    protected ProviderRegistry $configurationProviderRegistry;
40
    protected PageRenderer $pageRenderer;
41
    protected UriBuilder $uriBuilder;
42
    protected ModuleTemplateFactory $moduleTemplateFactory;
43
44
    public function __construct(
45
        ProviderRegistry $configurationProviderRegistry,
46
        PageRenderer $pageRenderer,
47
        UriBuilder $uriBuilder,
48
        ModuleTemplateFactory $moduleTemplateFactory
49
    ) {
50
        $this->configurationProviderRegistry = $configurationProviderRegistry;
51
        $this->pageRenderer = $pageRenderer;
52
        $this->uriBuilder = $uriBuilder;
53
        $this->moduleTemplateFactory = $moduleTemplateFactory;
54
    }
55
56
    /**
57
     * Main controller action determines get/post values, takes care of
58
     * stored backend user settings for this module, determines tree
59
     * and renders it.
60
     *
61
     * @param ServerRequestInterface $request the current request
62
     * @return ResponseInterface the response with the content
63
     * @throws \RuntimeException
64
     */
65
    public function mainAction(ServerRequestInterface $request): ResponseInterface
66
    {
67
        $moduleTemplate = $this->moduleTemplateFactory->create($request);
68
        $backendUser = $this->getBackendUser();
69
        $queryParams = $request->getQueryParams();
70
        $postValues = $request->getParsedBody();
71
        $moduleState = $backendUser->uc['moduleData']['system_config'] ?? [];
72
73
        $configurationProviderIdentifier = (string)($queryParams['tree'] ?? $moduleState['tree'] ?? '');
74
75
        if ($configurationProviderIdentifier === ''
76
            && $this->configurationProviderRegistry->getFirstProvider() !== null
77
        ) {
78
            $configurationProviderIdentifier = $this->configurationProviderRegistry->getFirstProvider()->getIdentifier();
79
        }
80
81
        if (!$this->configurationProviderRegistry->hasProvider($configurationProviderIdentifier)) {
82
            throw new \InvalidArgumentException(
83
                'No provider found for identifier: ' . $configurationProviderIdentifier,
84
                1606306196
85
            );
86
        }
87
88
        $configurationProvider = $this->configurationProviderRegistry->getProvider($configurationProviderIdentifier);
89
        $moduleState['tree'] = $configurationProviderIdentifier;
90
91
        $configurationArray = $configurationProvider->getConfiguration();
92
93
        // Search string given or regex search enabled?
94
        $searchString = trim((string)($postValues['searchString'] ?? ''));
95
        $moduleState['regexSearch'] = (bool)($postValues['regexSearch'] ?? $moduleState['regexSearch'] ?? false);
96
97
        // Prepare array renderer class, apply search and expand / collapse states
98
        $arrayBrowser = GeneralUtility::makeInstance(ArrayBrowser::class, $request->getAttribute('route'));
99
        $arrayBrowser->regexMode = $moduleState['regexSearch'];
100
        $node = $queryParams['node'] ?? null;
101
        if ($searchString) {
102
            $arrayBrowser->depthKeys = $arrayBrowser->getSearchKeys($configurationArray, '', $searchString, []);
103
        } elseif (is_array($node)) {
104
            $newExpandCollapse = $arrayBrowser->depthKeys($node, $moduleState['node_' . $configurationProviderIdentifier]);
105
            $arrayBrowser->depthKeys = $newExpandCollapse;
106
            $moduleState['node_' . $configurationProviderIdentifier] = $newExpandCollapse;
107
        } else {
108
            $arrayBrowser->depthKeys = $moduleState['node_' . $configurationProviderIdentifier] ?? [];
109
        }
110
111
        // Store new state
112
        $backendUser->uc['moduleData']['system_config'] = $moduleState;
113
        $backendUser->writeUC();
114
115
        // Render main body
116
        $view = GeneralUtility::makeInstance(StandaloneView::class);
117
        $view->getRequest()->setControllerExtensionName('lowlevel');
118
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
119
            'EXT:lowlevel/Resources/Private/Templates/Backend/Configuration.html'
120
        ));
121
        $view->assignMultiple([
122
            'treeName' => $configurationProvider->getLabel(),
123
            'searchString' => $searchString,
124
            'regexSearch' => $moduleState['regexSearch'],
125
            'tree' => $arrayBrowser->tree($configurationArray, ''),
126
        ]);
127
128
        // Prepare module setup
129
        $moduleTemplate->setContent($view->render());
130
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Lowlevel/ConfigurationView');
131
132
        // Shortcut in doc header
133
        $shortcutButton = $moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
134
        $shortcutButton
135
            ->setRouteIdentifier('system_config')
136
            ->setDisplayName($configurationProvider->getLabel())
137
            ->setArguments(['tree' => $configurationProviderIdentifier]);
138
        $moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortcutButton);
139
140
        // Main drop down in doc header
141
        $menu = $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
142
        $menu->setIdentifier('tree');
143
144
        $context = '';
145
        foreach ($this->configurationProviderRegistry->getProviders() as $provider) {
146
            $menuItem = $menu->makeMenuItem();
147
            $menuItem
148
                ->setHref((string)$this->uriBuilder->buildUriFromRoute('system_config', ['tree' => $provider->getIdentifier()]))
149
                ->setTitle($provider->getLabel());
150
            if ($configurationProvider === $provider) {
151
                $menuItem->setActive(true);
152
                $context = $menuItem->getTitle();
153
            }
154
            $menu->addMenuItem($menuItem);
155
        }
156
157
        $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
158
        $moduleTemplate->setTitle(
159
            $this->getLanguageService()->sL('LLL:EXT:lowlevel/Resources/Private/Language/locallang_mod_configuration.xlf:mlang_tabs_tab'),
160
            $context
161
        );
162
163
        return new HtmlResponse($moduleTemplate->renderContent());
164
    }
165
166
    /**
167
     * Returns the Backend User
168
     * @return BackendUserAuthentication
169
     */
170
    protected function getBackendUser(): BackendUserAuthentication
171
    {
172
        return $GLOBALS['BE_USER'];
173
    }
174
175
    /**
176
     * Returns the Language Service
177
     * @return LanguageService
178
     */
179
    protected function getLanguageService()
180
    {
181
        return $GLOBALS['LANG'];
182
    }
183
}
184