add0623c69b5e9cde32c0f1a00c02f6fbc220628
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Service / ConfigurationService.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Service;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Adminpanel\Modules\AdminPanelModuleInterface;
21 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Cache\CacheManager;
24 use TYPO3\CMS\Core\SingletonInterface;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 class ConfigurationService implements SingletonInterface
28 {
29 /**
30 * @var array
31 */
32 protected $mainConfiguration;
33
34 public function __construct()
35 {
36 $this->mainConfiguration = $this->getBackendUser()->getTSConfig('admPanel')['properties'];
37 }
38
39 /**
40 * Get MainConfiguration (User TSConfig admPanel)
41 *
42 * @return array
43 */
44 public function getMainConfiguration(): array
45 {
46 return $this->mainConfiguration;
47 }
48
49 /**
50 * Helper method to return configuration options
51 * Checks User TSConfig overrides and current backend user session
52 *
53 * @param string $identifier
54 * @param string $option
55 * @return string
56 */
57 public function getConfigurationOption(string $identifier, string $option): string
58 {
59 $beUser = $this->getBackendUser();
60
61 if ($option && isset($this->mainConfiguration['override.'][$identifier . '.'][$option])) {
62 $returnValue = $this->mainConfiguration['override.'][$identifier . '.'][$option];
63 } else {
64 $returnValue = $beUser->uc['TSFE_adminConfig'][$identifier . '_' . $option] ?? '';
65 }
66
67 return (string)$returnValue;
68 }
69
70 /**
71 * Save admin panel configuration to backend user UC
72 * triggers onSubmit method of modules to enable each module
73 * to enhance the save action
74 *
75 * @param AdminPanelModuleInterface[] $modules
76 * @param ServerRequestInterface $request
77 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
78 */
79 public function saveConfiguration(array $modules, ServerRequestInterface $request): void
80 {
81 $configurationToSave = $request->getParsedBody()['TSFE_ADMIN_PANEL'] ?? [];
82 $beUser = $this->getBackendUser();
83
84 // Trigger onSubmit
85 foreach ($modules as $module) {
86 if ($module->isEnabled()) {
87 $module->onSubmit($configurationToSave, $request);
88 foreach ($module->getSubModules() as $subModule) {
89 $subModule->onSubmit($configurationToSave, $request);
90 }
91 }
92 }
93 // Settings
94 $beUser->uc['TSFE_adminConfig'] = array_merge(
95 !is_array($beUser->uc['TSFE_adminConfig']) ? [] : $beUser->uc['TSFE_adminConfig'],
96 $configurationToSave
97 );
98 unset($beUser->uc['TSFE_adminConfig']['action']);
99 // Saving
100 $beUser->writeUC();
101 // Flush fluid template cache
102 $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
103 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
104 $cacheManager->getCache('fluid_template')->flush();
105 }
106
107 /**
108 * Returns the current BE user.
109 *
110 * @return BackendUserAuthentication|FrontendBackendUserAuthentication
111 */
112 protected function getBackendUser(): BackendUserAuthentication
113 {
114 return $GLOBALS['BE_USER'];
115 }
116 }