63e0b27507355ae9bff4041f28163af3b456e7bb
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / UserSettingsController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Configuration\BackendUserConfiguration;
21 use TYPO3\CMS\Core\Http\JsonResponse;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * A wrapper class to call BE_USER->uc
26 * used for AJAX and TYPO3.Storage JS object
27 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
28 */
29 class UserSettingsController
30 {
31 /**
32 * Processes all AJAX calls and returns a JSON for the data
33 *
34 * @param ServerRequestInterface $request
35 * @return ResponseInterface
36 */
37 public function processAjaxRequest(ServerRequestInterface $request): ResponseInterface
38 {
39 // do the regular / main logic, depending on the action parameter
40 $action = $request->getParsedBody()['action'] ?? $request->getQueryParams()['action'] ?? '';
41 $key = $request->getParsedBody()['key'] ?? $request->getQueryParams()['key'] ?? '';
42 $value = $request->getParsedBody()['value'] ?? $request->getQueryParams()['value'] ?? '';
43 $backendUserConfiguration = GeneralUtility::makeInstance(BackendUserConfiguration::class);
44 switch ($action) {
45 case 'get':
46 $content = $backendUserConfiguration->get($key);
47 break;
48 case 'getAll':
49 $content = $backendUserConfiguration->getAll();
50 break;
51 case 'set':
52 $backendUserConfiguration->set($key, $value);
53 $content = $backendUserConfiguration->getAll();
54 break;
55 case 'addToList':
56 $backendUserConfiguration->addToList($key, $value);
57 $content = $backendUserConfiguration->getAll();
58 break;
59 case 'removeFromList':
60 $backendUserConfiguration->removeFromList($key, $value);
61 $content = $backendUserConfiguration->getAll();
62 break;
63 case 'unset':
64 $backendUserConfiguration->unsetOption($key);
65 $content = $backendUserConfiguration->getAll();
66 break;
67 case 'clear':
68 $backendUserConfiguration->clear();
69 $content = ['result' => true];
70 break;
71 default:
72 $content = ['result' => false];
73 }
74 return (new JsonResponse())->setPayload($content);
75 }
76 }