cf7cbad9e11f011a48405aca14ca7d0f7f7d44f4
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / UserSettingsController.php
1 <?php
2
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\Core\Http\JsonResponse;
21 use TYPO3\CMS\Core\Utility\ArrayUtility;
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 */
28 class UserSettingsController
29 {
30 /**
31 * Processes all AJAX calls and returns a JSON for the data
32 *
33 * @param ServerRequestInterface $request
34 * @return ResponseInterface
35 */
36 public function processAjaxRequest(ServerRequestInterface $request): ResponseInterface
37 {
38 // do the regular / main logic, depending on the action parameter
39 $action = isset($request->getParsedBody()['action']) ? $request->getParsedBody()['action'] : $request->getQueryParams()['action'];
40 $key = isset($request->getParsedBody()['key']) ? $request->getParsedBody()['key'] : $request->getQueryParams()['key'];
41 $value = isset($request->getParsedBody()['value']) ? $request->getParsedBody()['value'] : $request->getQueryParams()['value'];
42
43 $content = $this->process($action, $key, $value);
44 return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($content);
45 }
46
47 /**
48 * Process data
49 *
50 * @param string $action
51 * @param string $key
52 * @param string $value
53 * @return mixed
54 */
55 public function process($action, $key = '', $value = '')
56 {
57 switch ($action) {
58 case 'get':
59 $content = $this->get($key);
60 break;
61 case 'getAll':
62 $content = $this->getAll();
63 break;
64 case 'set':
65 $this->set($key, $value);
66 $content = $this->getAll();
67 break;
68 case 'addToList':
69 $this->addToList($key, $value);
70 $content = $this->getAll();
71 break;
72 case 'removeFromList':
73 $this->removeFromList($key, $value);
74 $content = $this->getAll();
75 break;
76 case 'unset':
77 $this->unsetOption($key);
78 $content = $this->getAll();
79 break;
80 case 'clear':
81 $this->clear();
82 $content = ['result' => true];
83 break;
84 default:
85 $content = ['result' => false];
86 }
87
88 return $content;
89 }
90
91 /**
92 * Returns a specific user setting
93 *
94 * @param string $key Identifier, allows also dotted notation for subarrays
95 * @return mixed Value associated
96 */
97 protected function get($key)
98 {
99 return (strpos($key, '.') !== false) ? $this->getFromDottedNotation($key) : $this->getBackendUser()->uc[$key];
100 }
101
102 /**
103 * Get all user settings
104 *
105 * @return mixed all values, usually a multi-dimensional array
106 */
107 protected function getAll()
108 {
109 return $this->getBackendUser()->uc;
110 }
111
112 /**
113 * Sets user settings by key/value pair
114 *
115 * @param string $key
116 * @param mixed $value
117 */
118 protected function set($key, $value)
119 {
120 $beUser = $this->getBackendUser();
121 if (strpos($key, '.') !== false) {
122 $this->setFromDottedNotation($key, $value);
123 } else {
124 $beUser->uc[$key] = $value;
125 }
126 $beUser->writeUC($beUser->uc);
127 }
128
129 /**
130 * Adds an value to an Comma-separated list
131 * stored $key of user settings
132 *
133 * @param string $key
134 * @param mixed $value
135 */
136 protected function addToList($key, $value)
137 {
138 $list = $this->get($key);
139 if (!isset($list)) {
140 $list = $value;
141 } else {
142 if (!GeneralUtility::inList($list, $value)) {
143 $list .= ',' . $value;
144 }
145 }
146 $this->set($key, $list);
147 }
148
149 /**
150 * Removes an value from an Comma-separated list
151 * stored $key of user settings
152 *
153 * @param string $key
154 * @param mixed $value
155 */
156 protected function removeFromList($key, $value)
157 {
158 $list = $this->get($key);
159 if (GeneralUtility::inList($list, $value)) {
160 $list = GeneralUtility::trimExplode(',', $list, true);
161 $list = ArrayUtility::removeArrayEntryByValue($list, $value);
162 $this->set($key, implode(',', $list));
163 }
164 }
165
166 /**
167 * Resets the user settings to the default
168 */
169 protected function clear()
170 {
171 $this->getBackendUser()->resetUC();
172 }
173
174 /**
175 * Unsets a key in user settings
176 *
177 * @param string $key
178 */
179 protected function unsetOption($key)
180 {
181 $beUser = $this->getBackendUser();
182 if (isset($beUser->uc[$key])) {
183 unset($beUser->uc[$key]);
184 $beUser->writeUC($beUser->uc);
185 }
186 }
187
188 /**
189 * Computes the subarray from dotted notation
190 *
191 * @param $key string Dotted notation of subkeys like moduleData.module1.general.checked
192 * @return mixed value of the settings
193 */
194 protected function getFromDottedNotation($key)
195 {
196 $subkeys = GeneralUtility::trimExplode('.', $key);
197 $array = $this->getBackendUser()->uc;
198 foreach ($subkeys as $subkey) {
199 if (isset($array[$subkey])) {
200 $array = &$array[$subkey];
201 } else {
202 $array = [];
203 break;
204 }
205 }
206 return $array;
207 }
208
209 /**
210 * Sets the value of a key written in dotted notation
211 *
212 * @param string $key
213 * @param mixed $value
214 */
215 protected function setFromDottedNotation($key, $value)
216 {
217 $subkeys = GeneralUtility::trimExplode('.', $key, true);
218 $lastKey = $subkeys[count($subkeys) - 1];
219 $array = &$this->getBackendUser()->uc;
220 foreach ($subkeys as $subkey) {
221 if ($subkey === $lastKey) {
222 $array[$subkey] = $value;
223 } else {
224 $array = &$array[$subkey];
225 }
226 }
227 }
228
229 /**
230 * Returns the current BE user.
231 *
232 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
233 */
234 protected function getBackendUser()
235 {
236 return $GLOBALS['BE_USER'];
237 }
238 }