TYPO3 CMS  TYPO3_8-7
UserSettingsController.php
Go to the documentation of this file.
1 <?php
2 
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 
22 
28 {
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()['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 
45  $response->getBody()->write(json_encode($content));
46  return $response;
47  }
48 
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 = ['result' => true];
85  break;
86  default:
87  $content = ['result' => false];
88  }
89 
90  return $content;
91  }
92 
99  protected function get($key)
100  {
101  return (strpos($key, '.') !== false) ? $this->getFromDottedNotation($key) : $this->getBackendUser()->uc[$key];
102  }
103 
109  protected function getAll()
110  {
111  return $this->getBackendUser()->uc;
112  }
113 
120  protected function set($key, $value)
121  {
122  $beUser = $this->getBackendUser();
123  if (strpos($key, '.') !== false) {
124  $this->setFromDottedNotation($key, $value);
125  } else {
126  $beUser->uc[$key] = $value;
127  }
128  $beUser->writeUC($beUser->uc);
129  }
130 
138  protected function addToList($key, $value)
139  {
140  $list = $this->get($key);
141  if (!isset($list)) {
142  $list = $value;
143  } else {
144  if (!GeneralUtility::inList($list, $value)) {
145  $list .= ',' . $value;
146  }
147  }
148  $this->set($key, $list);
149  }
150 
158  protected function removeFromList($key, $value)
159  {
160  $list = $this->get($key);
161  if (GeneralUtility::inList($list, $value)) {
162  $list = GeneralUtility::trimExplode(',', $list, true);
163  $list = ArrayUtility::removeArrayEntryByValue($list, $value);
164  $this->set($key, implode(',', $list));
165  }
166  }
167 
171  protected function clear()
172  {
173  $this->getBackendUser()->resetUC();
174  }
175 
181  protected function unsetOption($key)
182  {
183  $beUser = $this->getBackendUser();
184  if (isset($beUser->uc[$key])) {
185  unset($beUser->uc[$key]);
186  $beUser->writeUC($beUser->uc);
187  }
188  }
189 
196  protected function getFromDottedNotation($key)
197  {
198  $subkeys = GeneralUtility::trimExplode('.', $key);
199  $array = $this->getBackendUser()->uc;
200  foreach ($subkeys as $subkey) {
201  if (isset($array[$subkey])) {
202  $array = &$array[$subkey];
203  } else {
204  $array = [];
205  break;
206  }
207  }
208  return $array;
209  }
210 
217  protected function setFromDottedNotation($key, $value)
218  {
219  $subkeys = GeneralUtility::trimExplode('.', $key, true);
220  $lastKey = $subkeys[count($subkeys) - 1];
221  $array = &$this->getBackendUser()->uc;
222  foreach ($subkeys as $subkey) {
223  if ($subkey === $lastKey) {
224  $array[$subkey] = $value;
225  } else {
226  $array = &$array[$subkey];
227  }
228  }
229  }
230 
236  protected function getBackendUser()
237  {
238  return $GLOBALS['BE_USER'];
239  }
240 }
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static removeArrayEntryByValue(array $array, $cmpValue)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']