[TASK] Decouple adminPanel from frontend
[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\View\AdminPanelView;
21 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
22 use TYPO3\CMS\Core\Cache\CacheManager;
23 use TYPO3\CMS\Core\Http\ServerRequest;
24 use TYPO3\CMS\Core\Localization\LanguageService;
25 use TYPO3\CMS\Core\Service\DependencyOrderingService;
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 $this->validateSortAndInitializeModules();
50 $this->saveConfiguration();
51
52 foreach ($this->modules as $module) {
53 if ($module->isEnabled()) {
54 $module->initializeModule($request);
55 }
56 }
57 }
58
59 /**
60 * Renders the panel - Is currently called via RenderHook in postProcessOutput
61 *
62 * @todo Still uses the legacy AdminpanelView and should be rewritten to fluid
63 *
64 * @return string
65 */
66 public function render(): string
67 {
68 // handling via legacy functions
69 $adminPanelView = GeneralUtility::makeInstance(AdminPanelView::class);
70 $adminPanelView->setModules($this->modules);
71 return $adminPanelView->display();
72 }
73
74 /**
75 * Save admin panel configuration to backend user UC
76 */
77 protected function saveConfiguration(): void
78 {
79 $input = GeneralUtility::_GP('TSFE_ADMIN_PANEL');
80 $beUser = $this->getBackendUser();
81 if (is_array($input)) {
82 // Setting
83 $beUser->uc['TSFE_adminConfig'] = array_merge(
84 !is_array($beUser->uc['TSFE_adminConfig']) ? [] : $beUser->uc['TSFE_adminConfig'],
85 $input
86 );
87 unset($beUser->uc['TSFE_adminConfig']['action']);
88
89 foreach ($this->modules as $module) {
90 if ($module->isEnabled() && $module->isOpen()) {
91 $module->onSubmit($input);
92 }
93 }
94 // Saving
95 $beUser->writeUC();
96 // Flush fluid template cache
97 $cacheManager = new CacheManager();
98 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
99 $cacheManager->getCache('fluid_template')->flush();
100 }
101 }
102
103 /**
104 * Validates, sorts and initiates the registered modules
105 *
106 * @throws \RuntimeException
107 */
108 protected function validateSortAndInitializeModules(): void
109 {
110 $modules = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules'] ?? [];
111 if (empty($modules)) {
112 return;
113 }
114 foreach ($modules as $identifier => $configuration) {
115 if (empty($configuration) || !is_array($configuration)) {
116 throw new \RuntimeException(
117 'Missing configuration for module "' . $identifier . '".',
118 1519490105
119 );
120 }
121 if (!is_string($configuration['module']) ||
122 empty($configuration['module']) ||
123 !class_exists($configuration['module']) ||
124 !is_subclass_of(
125 $configuration['module'],
126 AdminPanelModuleInterface::class
127 )
128 ) {
129 throw new \RuntimeException(
130 'The module "' .
131 $identifier .
132 '" defines an invalid module class. Ensure the class exists and implements the "' .
133 AdminPanelModuleInterface::class .
134 '".',
135 1519490112
136 );
137 }
138 }
139
140 $orderedModules = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
141 $modules
142 );
143
144 foreach ($orderedModules as $module) {
145 $this->modules[] = GeneralUtility::makeInstance($module['module']);
146 }
147 }
148
149 /**
150 * Returns LanguageService
151 *
152 * @return LanguageService
153 */
154 protected function getLanguageService(): LanguageService
155 {
156 return $GLOBALS['LANG'];
157 }
158
159 /**
160 * Returns the current BE user.
161 *
162 * @return FrontendBackendUserAuthentication
163 */
164 protected function getBackendUser(): FrontendBackendUserAuthentication
165 {
166 return $GLOBALS['BE_USER'];
167 }
168
169 /**
170 * @return TypoScriptFrontendController
171 */
172 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
173 {
174 return $GLOBALS['TSFE'];
175 }
176 }