TYPO3 API  SVNRelease
class.extdirect_dataprovider_beusersettings.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * ExtDirect DataProvider for BE User Settings
00030  */
00031 class extDirect_DataProvider_BackendUserSettings {
00032 
00033     /**
00034      * Get user settings
00035      *
00036      * Returns all user settings, if $key is not specified, otherwise it retuns the value for $key
00037      *
00038      * @param  string $key  identifier, allows also dotted notation for subarrays
00039      * @return mixed value associated
00040      */
00041     public function get($key = '') {
00042         if (strpos($key, '.') !== FALSE) {
00043             $return = $this->getFromDottedNotation($key);
00044         } else {
00045             $return = ($key === '' ? $GLOBALS['BE_USER']->uc : $GLOBALS['BE_USER']->uc[$key]);
00046         }
00047         return $return;
00048     }
00049 
00050     /**
00051      * Sets user settings by key/value pair
00052      *
00053      * @param  string $key
00054      * @param  mixed $value
00055      * @return void
00056      */
00057     public function set($key, $value) {
00058         if (strpos($key, '.') !== FALSE) {
00059             $this->setFromDottedNotation($key, $value);
00060         } else {
00061             $GLOBALS['BE_USER']->uc[$key] = $value;
00062         }
00063         $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
00064     }
00065 
00066     /**
00067      * Sets user settings by array and merges them with current settings
00068      *
00069      * @param  array $array
00070      * @return void
00071      */
00072     public function setFromArray(array $array) {
00073         $GLOBALS['BE_USER']->uc = array_merge($GLOBALS['BE_USER']->uc, $array);
00074         $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
00075     }
00076 
00077     /**
00078      * Resets the user settings to the default
00079      *
00080      * @return void
00081      */
00082     public function reset() {
00083         $GLOBALS['BE_USER']->resetUC();
00084     }
00085 
00086     /**
00087      * Unsets a key in user settings
00088      *
00089      * @param  string $key
00090      * @return void
00091      */
00092     public function unsetKey($key) {
00093         if (isset($GLOBALS['BE_USER']->uc[$key])) {
00094             unset($GLOBALS['BE_USER']->uc[$key]);
00095             $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
00096         }
00097     }
00098 
00099     /**
00100      * Adds an value to an Comma-separated list
00101      * stored $key  of user settings
00102      *
00103      * @param string $key
00104      * @param mixed $value
00105      * @return void
00106      */
00107     public function addToList($key, $value) {
00108         $list = $this->get($key);
00109         if (!isset($list)) {
00110             $list = $value;
00111         } else {
00112             if (!t3lib_div::inList($list, $value)) {
00113                 $list .= ',' . $value;
00114             }
00115         }
00116         $this->set($key, $list);
00117     }
00118 
00119     /**
00120      * Removes an value from an Comma-separated list
00121      * stored $key of user settings
00122      *
00123      * @param string $key
00124      * @param mixed $value
00125      * @return void
00126      */
00127     public function removeFromList($key, $value) {
00128         $list = $this->get($key);
00129         if (t3lib_div::inList($list, $value)) {
00130             $list = t3lib_div::trimExplode(',', $list, TRUE);
00131             $list = t3lib_div::removeArrayEntryByValue($list, $value);
00132             $this->set($key, implode(',' ,$list));
00133         }
00134     }
00135 
00136     /**
00137      * Computes the subarray from dotted notation
00138      *
00139      * @param  $key dotted notation of subkeys like moduleData.module1.general.checked
00140      * @return mixed $array value of the settings
00141      */
00142     protected function getFromDottedNotation($key) {
00143         $subkeys = t3lib_div::trimExplode('.', $key);
00144         $array =& $GLOBALS['BE_USER']->uc;
00145         foreach ($subkeys as $subkey) {
00146             $array =& $array[$subkey];
00147         }
00148         return $array;
00149     }
00150 
00151     /**
00152      * Sets the value of a key written in dotted notation
00153      *
00154      * @param  string $key
00155      * @param  mixed $value
00156      * @return void
00157      */
00158     protected function setFromDottedNotation($key, $value) {
00159         $subkeys = t3lib_div::trimExplode('.', $key, TRUE);
00160         $lastKey = $subkeys[count($subkeys) - 1];
00161         $array =& $GLOBALS['BE_USER']->uc;
00162         foreach ($subkeys as $subkey) {
00163             if ($subkey === $lastKey) {
00164                 $array[$subkey] = $value;
00165             } else {
00166                 $array =& $array[$subkey];
00167             }
00168         }
00169     }
00170 
00171     /**
00172      * Gets the last part of of an Dotted Notation
00173      *
00174      * @param string $key
00175      * @return void
00176      */
00177     protected function getLastKeyFromDottedNotation($key) {
00178         $subkeys = t3lib_div::trimExplode('.', $key, TRUE);
00179         return $subkeys[count($subkeys) - 1];
00180     }
00181 }
00182 
00183 ?>