TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
UserSettingsController.php
Go to the documentation of this file.
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;
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()['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 
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 
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 
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 
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 
176  protected function clear()
177  {
178  $this->getBackendUser()->resetUC();
179  }
180 
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 
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 
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 
243  protected function getBackendUser()
244  {
245  return $GLOBALS['BE_USER'];
246  }
247 }
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static removeArrayEntryByValue(array $array, $cmpValue)