[TASK] Rewrite toggle and save to ajax in AdminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Controller / AjaxController.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 Psr\Http\Message\RequestInterface;
20 use TYPO3\CMS\Adminpanel\Service\ModuleLoader;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Cache\CacheManager;
23 use TYPO3\CMS\Core\Http\JsonResponse;
24 use TYPO3\CMS\Core\Http\ServerRequest;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Admin Panel Ajax Controller - Route endpoint for ajax actions
29 */
30 class AjaxController
31 {
32
33 /**
34 * Save adminPanel data
35 *
36 * @param ServerRequest $request
37 * @return JsonResponse
38 */
39 public function saveDataAction(ServerRequest $request): JsonResponse
40 {
41 $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
42
43 $modules = $moduleLoader->getModulesFromConfiguration();
44
45 $input = $request->getParsedBody()['TSFE_ADMIN_PANEL'] ?? null;
46 $beUser = $this->getBackendUser();
47 if (is_array($input)) {
48 // Setting
49 $beUser->uc['TSFE_adminConfig'] = array_merge(
50 !is_array($beUser->uc['TSFE_adminConfig']) ? [] : $beUser->uc['TSFE_adminConfig'],
51 $input
52 );
53 unset($beUser->uc['TSFE_adminConfig']['action']);
54
55 /** @var \TYPO3\CMS\Adminpanel\Modules\AdminPanelModuleInterface $module */
56 foreach ($modules as $module) {
57 if ($module->isEnabled()) {
58 $module->onSubmit($input);
59 }
60 }
61 // Saving
62 $beUser->writeUC();
63 // Flush fluid template cache
64 $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
65 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
66 $cacheManager->getCache('fluid_template')->flush();
67 }
68 return new JsonResponse(['success' => true]);
69 }
70
71 /**
72 * Toggle admin panel active state via UC
73 *
74 * @param \Psr\Http\Message\RequestInterface $request
75 * @return \TYPO3\CMS\Core\Http\JsonResponse
76 */
77 public function toggleActiveState(RequestInterface $request): JsonResponse
78 {
79 $backendUser = $this->getBackendUser();
80 if ($backendUser->uc['TSFE_adminConfig']['display_top'] ?? false) {
81 $backendUser->uc['TSFE_adminConfig']['display_top'] = false;
82 } else {
83 $backendUser->uc['TSFE_adminConfig']['display_top'] = true;
84 }
85 $backendUser->writeUC();
86 return new JsonResponse(['success' => true]);
87 }
88
89 /**
90 * Returns the current BE user.
91 *
92 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
93 */
94 protected function getBackendUser(): BackendUserAuthentication
95 {
96 return $GLOBALS['BE_USER'];
97 }
98 }