Passed
Push — master ( 6c7aff...ce6d49 )
by
unknown
46:49 queued 20:16
created

ConfigurationController::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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