TYPO3  7.6
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 
22 
28 {
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 = array('result' => true);
85  break;
86  default:
87  $content = array('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)) {
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  $array = &$array[$subkey];
208  }
209  return $array;
210  }
211 
219  protected function setFromDottedNotation($key, $value)
220  {
221  $subkeys = GeneralUtility::trimExplode('.', $key, true);
222  $lastKey = $subkeys[count($subkeys) - 1];
223  $array = &$this->getBackendUser()->uc;
224  foreach ($subkeys as $subkey) {
225  if ($subkey === $lastKey) {
226  $array[$subkey] = $value;
227  } else {
228  $array = &$array[$subkey];
229  }
230  }
231  }
232 
238  protected function getBackendUser()
239  {
240  return $GLOBALS['BE_USER'];
241  }
242 }