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