23db0026e83c644a673f585163776261cf232647
[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\ServerRequestInterface;
20 use TYPO3\CMS\Adminpanel\Service\ConfigurationService;
21 use TYPO3\CMS\Adminpanel\Service\ModuleLoader;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Http\JsonResponse;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Admin Panel Ajax Controller - Route endpoint for ajax actions
28 */
29 class AjaxController
30 {
31 /**
32 * @var array
33 */
34 protected $adminPanelModuleConfiguration;
35
36 /**
37 * @var \TYPO3\CMS\Adminpanel\Service\ModuleLoader
38 */
39 protected $moduleLoader;
40
41 /**
42 * @var \TYPO3\CMS\Adminpanel\Service\ConfigurationService
43 */
44 private $configurationService;
45
46 /**
47 * @param ConfigurationService $configurationService
48 * @param ModuleLoader $moduleLoader
49 */
50 public function __construct(ConfigurationService $configurationService = null, ModuleLoader $moduleLoader = null)
51 {
52 $this->configurationService = $configurationService
53 ??
54 GeneralUtility::makeInstance(ConfigurationService::class);
55 $this->adminPanelModuleConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules'] ?? [];
56 $this->moduleLoader = $moduleLoader ?? GeneralUtility::makeInstance(ModuleLoader::class);
57 }
58
59 /**
60 * Save adminPanel data
61 *
62 * @param ServerRequestInterface $request
63 * @return JsonResponse
64 */
65 public function saveDataAction(ServerRequestInterface $request): JsonResponse
66 {
67 $this->configurationService->saveConfiguration(
68 $this->moduleLoader->validateSortAndInitializeModules($this->adminPanelModuleConfiguration),
69 $request
70 );
71 return new JsonResponse(['success' => true]);
72 }
73
74 /**
75 * Toggle admin panel active state via UC
76 *
77 * @return \TYPO3\CMS\Core\Http\JsonResponse
78 */
79 public function toggleActiveState(): JsonResponse
80 {
81 $backendUser = $this->getBackendUser();
82 if ($backendUser->uc['TSFE_adminConfig']['display_top'] ?? false) {
83 $backendUser->uc['TSFE_adminConfig']['display_top'] = false;
84 } else {
85 $backendUser->uc['TSFE_adminConfig']['display_top'] = true;
86 }
87 $backendUser->writeUC();
88 return new JsonResponse(['success' => true]);
89 }
90
91 /**
92 * Returns the current BE user.
93 *
94 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
95 */
96 protected function getBackendUser(): BackendUserAuthentication
97 {
98 return $GLOBALS['BE_USER'];
99 }
100 }