6438e947a2704856659610ba6f3f6ece86ce103c
[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 case 'removeFromList':
69 $this->removeFromList($key, $value);
70 $content = $this->getAll();
71 case 'unset':
72 $this->unsetOption($key);
73 $content = $this->getAll();
74 break;
75 case 'clear':
76 $this->clear();
77 $content = array('result' => TRUE);
78 break;
79 default:
80 $content = array('result' => FALSE);
81 }
82
83 return $content;
84 }
85
86 /**
87 * Returns a specific user setting
88 *
89 * @param string $key Identifier, allows also dotted notation for subarrays
90 * @return mixed Value associated
91 */
92 protected function get($key) {
93 return (strpos($key, '.') !== FALSE) ? $this->getFromDottedNotation($key) : $this->getBackendUser()->uc[$key];
94 }
95
96 /**
97 * Get all user settings
98 *
99 * @return mixed all values, usually a multi-dimensional array
100 */
101 protected function getAll() {
102 return $this->getBackendUser()->uc;
103 }
104
105 /**
106 * Sets user settings by key/value pair
107 *
108 * @param string $key
109 * @param mixed $value
110 * @return void
111 */
112 protected function set($key, $value) {
113 $beUser = $this->getBackendUser();
114 if (strpos($key, '.') !== FALSE) {
115 $this->setFromDottedNotation($key, $value);
116 } else {
117 $beUser->uc[$key] = $value;
118 }
119 $beUser->writeUC($beUser->uc);
120 }
121
122 /**
123 * Adds an value to an Comma-separated list
124 * stored $key of user settings
125 *
126 * @param string $key
127 * @param mixed $value
128 * @return void
129 */
130 protected function addToList($key, $value) {
131 $list = $this->get($key);
132 if (!isset($list)) {
133 $list = $value;
134 } else {
135 if (!GeneralUtility::inList($list, $value)) {
136 $list .= ',' . $value;
137 }
138 }
139 $this->set($key, $list);
140 }
141
142 /**
143 * Removes an value from an Comma-separated list
144 * stored $key of user settings
145 *
146 * @param string $key
147 * @param mixed $value
148 * @return void
149 */
150 protected function removeFromList($key, $value) {
151 $list = $this->get($key);
152 if (GeneralUtility::inList($list, $value)) {
153 $list = GeneralUtility::trimExplode(',', $list, TRUE);
154 $list = ArrayUtility::removeArrayEntryByValue($list, $value);
155 $this->set($key, implode(',', $list));
156 }
157 }
158
159 /**
160 * Resets the user settings to the default
161 *
162 * @return void
163 */
164 protected function clear() {
165 $this->getBackendUser()->resetUC();
166 }
167
168 /**
169 * Unsets a key in user settings
170 *
171 * @param string $key
172 * @return void
173 */
174 protected function unsetOption($key) {
175 $beUser = $this->getBackendUser();
176 if (isset($beUser->uc[$key])) {
177 unset($beUser->uc[$key]);
178 $beUser->writeUC($beUser->uc);
179 }
180 }
181
182 /**
183 * Computes the subarray from dotted notation
184 *
185 * @param $key string Dotted notation of subkeys like moduleData.module1.general.checked
186 * @return mixed value of the settings
187 */
188 protected function getFromDottedNotation($key) {
189 $subkeys = GeneralUtility::trimExplode('.', $key);
190 $array = &$this->getBackendUser()->uc;
191 foreach ($subkeys as $subkey) {
192 $array = &$array[$subkey];
193 }
194 return $array;
195 }
196
197 /**
198 * Sets the value of a key written in dotted notation
199 *
200 * @param string $key
201 * @param mixed $value
202 * @return void
203 */
204 protected function setFromDottedNotation($key, $value) {
205 $subkeys = GeneralUtility::trimExplode('.', $key, TRUE);
206 $lastKey = $subkeys[count($subkeys) - 1];
207 $array = &$this->getBackendUser()->uc;
208 foreach ($subkeys as $subkey) {
209 if ($subkey === $lastKey) {
210 $array[$subkey] = $value;
211 } else {
212 $array = &$array[$subkey];
213 }
214 }
215 }
216
217 /**
218 * Returns the current BE user.
219 *
220 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
221 */
222 protected function getBackendUser() {
223 return $GLOBALS['BE_USER'];
224 }
225 }