[BUGFIX] adminpanel: Do not use getTSConfig in a deprecated way
[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\ModuleApi\ConfigurableInterface;
21 use TYPO3\CMS\Adminpanel\ModuleApi\OnSubmitActorInterface;
22 use TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface;
23 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
24 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
25 use TYPO3\CMS\Core\SingletonInterface;
26
27 /**
28 * Admin Panel Service Class for Configuration Handling
29 *
30 * Scope: User TSConfig + Backend User UC
31 */
32 class ConfigurationService implements SingletonInterface
33 {
34 /**
35 * @var array
36 */
37 protected $mainConfiguration;
38
39 public function __construct()
40 {
41 $this->mainConfiguration = $this->getBackendUser()->getTSConfig()['admPanel.'] ?? [];
42 }
43
44 /**
45 * Get MainConfiguration (User TSConfig admPanel)
46 *
47 * @return array
48 */
49 public function getMainConfiguration(): array
50 {
51 return $this->mainConfiguration;
52 }
53
54 /**
55 * Helper method to return configuration options
56 * Checks User TSConfig overrides and current backend user session
57 *
58 * @param string $identifier
59 * @param string $option
60 * @return string
61 */
62 public function getConfigurationOption(string $identifier, string $option): string
63 {
64 if ($identifier === '' || $option === '') {
65 throw new \InvalidArgumentException('Identifier and option may not be empty', 1532861423);
66 }
67
68 if (isset($this->mainConfiguration['override.'][$identifier . '.'][$option])) {
69 $returnValue = $this->mainConfiguration['override.'][$identifier . '.'][$option];
70 } else {
71 $returnValue = $this->getBackendUser()->uc['AdminPanel'][$identifier . '_' . $option] ?? '';
72 }
73
74 return (string)$returnValue;
75 }
76
77 /**
78 * Save admin panel configuration to backend user UC
79 * triggers onSubmit method of modules to enable each module
80 * to enhance the save action
81 *
82 * @param \TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface[] $modules
83 * @param ServerRequestInterface $request
84 */
85 public function saveConfiguration(array $modules, ServerRequestInterface $request): void
86 {
87 $configurationToSave = $request->getParsedBody()['TSFE_ADMIN_PANEL'] ?? [];
88 $beUser = $this->getBackendUser();
89 $this->triggerOnSubmitActors($modules, $request, $configurationToSave);
90
91 // Settings
92 $beUser->uc['AdminPanel'] = array_merge(
93 !is_array($beUser->uc['AdminPanel']) ? [] : $beUser->uc['AdminPanel'],
94 $configurationToSave
95 );
96 unset($beUser->uc['AdminPanel']['action']);
97 // Saving
98 $beUser->writeUC();
99 }
100
101 /**
102 * Returns the current BE user.
103 *
104 * @return BackendUserAuthentication|FrontendBackendUserAuthentication
105 */
106 protected function getBackendUser(): BackendUserAuthentication
107 {
108 return $GLOBALS['BE_USER'];
109 }
110
111 /**
112 * @param array $modules
113 * @param \Psr\Http\Message\ServerRequestInterface $request
114 * @param $configurationToSave
115 */
116 protected function triggerOnSubmitActors(
117 array $modules,
118 ServerRequestInterface $request,
119 $configurationToSave
120 ): void {
121 foreach ($modules as $module) {
122 if (
123 $module instanceof OnSubmitActorInterface
124 && (
125 ($module instanceof ConfigurableInterface && $module->isEnabled())
126 || !($module instanceof ConfigurableInterface)
127 )
128 ) {
129 $module->onSubmit($configurationToSave, $request);
130 }
131 if ($module instanceof SubmoduleProviderInterface) {
132 $this->triggerOnSubmitActors($module->getSubModules(), $request, $configurationToSave);
133 }
134 }
135 }
136 }