ConfigurationController.php 6.68 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\Lowlevel\Controller;

20
21
use Psr\Http\Message\ResponseInterface;
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
47
48
     * Main controller action determines get/post values, takes care of
     * stored backend user settings for this module, determines tree
     * and renders it.
49
50
51
     *
     * @param ServerRequestInterface $request the current request
     * @return ResponseInterface the response with the content
52
     * @throws \RuntimeException
53
     */
54
    public function mainAction(ServerRequestInterface $request): ResponseInterface
55
    {
56
57
58
59
        $backendUser = $this->getBackendUser();
        $queryParams = $request->getQueryParams();
        $postValues = $request->getParsedBody();
        $moduleState = $backendUser->uc['moduleData']['system_config'] ?? [];
60

61
        $configurationProviderIdentifier = (string)($queryParams['tree'] ?? $moduleState['tree'] ?? '');
62

63
64
65
66
67
        if ($configurationProviderIdentifier === ''
            && $this->configurationProviderRegistry->getFirstProvider() !== null
        ) {
            $configurationProviderIdentifier = $this->configurationProviderRegistry->getFirstProvider()->getIdentifier();
        }
68

69
70
71
72
73
74
        if (!$this->configurationProviderRegistry->hasProvider($configurationProviderIdentifier)) {
            throw new \InvalidArgumentException(
                'No provider found for identifier: ' . $configurationProviderIdentifier,
                1606306196
            );
        }
75

76
77
        $configurationProvider = $this->configurationProviderRegistry->getProvider($configurationProviderIdentifier);
        $moduleState['tree'] = $configurationProviderIdentifier;
78

79
        $configurationArray = $configurationProvider->getConfiguration();
80

81
82
83
        // Search string given or regex search enabled?
        $searchString = (string)($postValues['searchString'] ? trim($postValues['searchString']) : '');
        $moduleState['regexSearch'] = (bool)($postValues['regexSearch'] ?? $moduleState['regexSearch'] ?? false);
84
85

        // Prepare array renderer class, apply search and expand / collapse states
86
87
        $route = GeneralUtility::makeInstance(Router::class)->match(GeneralUtility::_GP('route'));
        $arrayBrowser = GeneralUtility::makeInstance(ArrayBrowser::class, $route);
88
89
90
        $arrayBrowser->regexMode = $moduleState['regexSearch'];
        $node = $queryParams['node'];
        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
101
102
        // Store new state
        $backendUser->uc['moduleData']['system_config'] = $moduleState;
        $backendUser->writeUC();
103

104
105
106
107
108
109
110
        // Render main body
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->getRequest()->setControllerExtensionName('lowlevel');
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
            'EXT:lowlevel/Resources/Private/Templates/Backend/Configuration.html'
        ));
        $view->assignMultiple([
111
            'treeName' => $configurationProvider->getLabel(),
112
113
            'searchString' => $searchString,
            'regexSearch' => $moduleState['regexSearch'],
114
            'tree' => $arrayBrowser->tree($configurationArray, ''),
115
        ]);
116

117
118
119
120
121
122
123
        // Prepare module setup
        $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
        $moduleTemplate->setContent($view->render());
        $moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lowlevel/ConfigurationView');

        // Shortcut in doc header
        $shortcutButton = $moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
124
125
        $shortcutButton
            ->setRouteIdentifier('system_config')
126
            ->setDisplayName($configurationProvider->getLabel())
127
            ->setArguments(['tree' => $configurationProviderIdentifier]);
128
        $moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortcutButton);
129

130
131
132
        // Main drop down in doc header
        $menu = $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
        $menu->setIdentifier('tree');
133
134

        foreach ($this->configurationProviderRegistry->getProviders() as $provider) {
135
            $menuItem = $menu->makeMenuItem();
136
            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
137
138
139
140
            $menuItem
                ->setHref((string)$uriBuilder->buildUriFromRoute('system_config', ['tree' => $provider->getIdentifier()]))
                ->setTitle($provider->getLabel());
            if ($configurationProvider === $provider) {
141
                $menuItem->setActive(true);
142
            }
143
            $menu->addMenuItem($menuItem);
144
        }
145

146
147
        $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);

148
        return new HtmlResponse($moduleTemplate->renderContent());
149
150
151
152
153
154
    }

    /**
     * Returns the Backend User
     * @return BackendUserAuthentication
     */
155
    protected function getBackendUser(): BackendUserAuthentication
156
157
    {
        return $GLOBALS['BE_USER'];
158
    }
159
}