[TASK] Rewrite toggle and save to ajax in AdminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Controller / MainController.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Controller;
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 TYPO3\CMS\Adminpanel\Modules\AdminPanelModuleInterface;
20 use TYPO3\CMS\Adminpanel\Service\ModuleLoader;
21 use TYPO3\CMS\Adminpanel\View\AdminPanelView;
22 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
23 use TYPO3\CMS\Core\Cache\CacheManager;
24 use TYPO3\CMS\Core\Http\ServerRequest;
25 use TYPO3\CMS\Core\Localization\LanguageService;
26 use TYPO3\CMS\Core\SingletonInterface;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
29
30 /**
31 * Main controller for the admin panel
32 *
33 * @internal
34 */
35 class MainController implements SingletonInterface
36 {
37 /**
38 * @var array<AdminPanelModuleInterface>
39 */
40 protected $modules = [];
41
42 /**
43 * Initializes settings for the admin panel.
44 *
45 * @param \TYPO3\CMS\Core\Http\ServerRequest $request
46 */
47 public function initialize(ServerRequest $request): void
48 {
49 $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
50 $this->modules = $moduleLoader->getModulesFromConfiguration();
51 $this->saveConfiguration();
52
53 foreach ($this->modules as $module) {
54 if ($module->isEnabled()) {
55 $module->initializeModule($request);
56 }
57 }
58 }
59
60 /**
61 * Renders the panel - Is currently called via RenderHook in postProcessOutput
62 *
63 * @todo Still uses the legacy AdminpanelView and should be rewritten to fluid
64 *
65 * @return string
66 */
67 public function render(): string
68 {
69 // handling via legacy functions
70 $adminPanelView = GeneralUtility::makeInstance(AdminPanelView::class);
71 $adminPanelView->setModules($this->modules);
72 return $adminPanelView->display();
73 }
74
75 /**
76 * Save admin panel configuration to backend user UC
77 */
78 protected function saveConfiguration(): void
79 {
80 $input = GeneralUtility::_GP('TSFE_ADMIN_PANEL');
81 $beUser = $this->getBackendUser();
82 if (is_array($input)) {
83 // Setting
84 $beUser->uc['TSFE_adminConfig'] = array_merge(
85 !is_array($beUser->uc['TSFE_adminConfig']) ? [] : $beUser->uc['TSFE_adminConfig'],
86 $input
87 );
88 unset($beUser->uc['TSFE_adminConfig']['action']);
89
90 foreach ($this->modules as $module) {
91 if ($module->isEnabled() && $module->isOpen()) {
92 $module->onSubmit($input);
93 }
94 }
95 // Saving
96 $beUser->writeUC();
97 // Flush fluid template cache
98 $cacheManager = new CacheManager();
99 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
100 $cacheManager->getCache('fluid_template')->flush();
101 }
102 }
103
104 /**
105 * Returns LanguageService
106 *
107 * @return LanguageService
108 */
109 protected function getLanguageService(): LanguageService
110 {
111 return $GLOBALS['LANG'];
112 }
113
114 /**
115 * Returns the current BE user.
116 *
117 * @return FrontendBackendUserAuthentication
118 */
119 protected function getBackendUser(): FrontendBackendUserAuthentication
120 {
121 return $GLOBALS['BE_USER'];
122 }
123
124 /**
125 * @return TypoScriptFrontendController
126 */
127 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
128 {
129 return $GLOBALS['TSFE'];
130 }
131 }