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