7e287684d1ca7a8b2fc70fcbfa808084a71d34ae
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / User / ExtDirect / BackendUserSettingsDataProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\User\ExtDirect;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * ExtDirect DataProvider for BE User Settings
31 *
32 * @author Steffen Kamper <steffen@typo3.org>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class BackendUserSettingsDataProvider {
37
38 /**
39 * Get user settings
40 *
41 * Returns all user settings, if $key is not specified, otherwise it retuns the value for $key
42 *
43 * @param string $key Identifier, allows also dotted notation for subarrays
44 * @return mixed Value associated
45 */
46 public function get($key = '') {
47 if (strpos($key, '.') !== FALSE) {
48 $return = $this->getFromDottedNotation($key);
49 } else {
50 $return = $key === '' ? $GLOBALS['BE_USER']->uc : $GLOBALS['BE_USER']->uc[$key];
51 }
52 return $return;
53 }
54
55 /**
56 * Sets user settings by key/value pair
57 *
58 * @param string $key
59 * @param mixed $value
60 * @return void
61 */
62 public function set($key, $value) {
63 if (strpos($key, '.') !== FALSE) {
64 $this->setFromDottedNotation($key, $value);
65 } else {
66 $GLOBALS['BE_USER']->uc[$key] = $value;
67 }
68 $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
69 }
70
71 /**
72 * Sets user settings by array and merges them with current settings
73 *
74 * @param array $array
75 * @return void
76 */
77 public function setFromArray(array $array) {
78 $GLOBALS['BE_USER']->uc = array_merge($GLOBALS['BE_USER']->uc, $array);
79 $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
80 }
81
82 /**
83 * Resets the user settings to the default
84 *
85 * @return void
86 */
87 public function reset() {
88 $GLOBALS['BE_USER']->resetUC();
89 }
90
91 /**
92 * Unsets a key in user settings
93 *
94 * @param string $key
95 * @return void
96 */
97 public function unsetKey($key) {
98 if (isset($GLOBALS['BE_USER']->uc[$key])) {
99 unset($GLOBALS['BE_USER']->uc[$key]);
100 $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
101 }
102 }
103
104 /**
105 * Adds an value to an Comma-separated list
106 * stored $key of user settings
107 *
108 * @param string $key
109 * @param mixed $value
110 * @return void
111 */
112 public function addToList($key, $value) {
113 $list = $this->get($key);
114 if (!isset($list)) {
115 $list = $value;
116 } else {
117 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::inList($list, $value)) {
118 $list .= ',' . $value;
119 }
120 }
121 $this->set($key, $list);
122 }
123
124 /**
125 * Removes an value from an Comma-separated list
126 * stored $key of user settings
127 *
128 * @param string $key
129 * @param mixed $value
130 * @return void
131 */
132 public function removeFromList($key, $value) {
133 $list = $this->get($key);
134 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($list, $value)) {
135 $list = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $list, TRUE);
136 $list = \TYPO3\CMS\Core\Utility\GeneralUtility::removeArrayEntryByValue($list, $value);
137 $this->set($key, implode(',', $list));
138 }
139 }
140
141 /**
142 * Computes the subarray from dotted notation
143 *
144 * @param $key Dotted notation of subkeys like moduleData.module1.general.checked
145 * @return mixed $array value of the settings
146 */
147 protected function getFromDottedNotation($key) {
148 $subkeys = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $key);
149 $array =& $GLOBALS['BE_USER']->uc;
150 foreach ($subkeys as $subkey) {
151 $array =& $array[$subkey];
152 }
153 return $array;
154 }
155
156 /**
157 * Sets the value of a key written in dotted notation
158 *
159 * @param string $key
160 * @param mixed $value
161 * @return void
162 */
163 protected function setFromDottedNotation($key, $value) {
164 $subkeys = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $key, TRUE);
165 $lastKey = $subkeys[count($subkeys) - 1];
166 $array =& $GLOBALS['BE_USER']->uc;
167 foreach ($subkeys as $subkey) {
168 if ($subkey === $lastKey) {
169 $array[$subkey] = $value;
170 } else {
171 $array =& $array[$subkey];
172 }
173 }
174 }
175
176 /**
177 * Gets the last part of of an Dotted Notation
178 *
179 * @param string $key
180 * @return void
181 */
182 protected function getLastKeyFromDottedNotation($key) {
183 $subkeys = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $key, TRUE);
184 return $subkeys[count($subkeys) - 1];
185 }
186
187 }
188
189
190 ?>