|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com) 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 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings. 00029 * Provides a presets box for BE modules. 00030 * 00031 * inspired by t3lib_fullsearch 00032 * 00033 * $Id: class.t3lib_modsettings.php 10121 2011-01-18 20:15:30Z ohader $ 00034 * 00035 * @author René Fritz <r.fritz@colorcube.de> 00036 */ 00037 /** 00038 * [CLASS/FUNCTION INDEX of SCRIPT] 00039 * 00040 * 00041 * 00042 * 125: class t3lib_modSettings 00043 * 00044 * SECTION: Init / setup 00045 * 181: function init($prefix='', $storeList='') 00046 * 197: function setSessionType($type='ses') 00047 * 00048 * SECTION: Store list - which values should be stored 00049 * 218: function setStoreList($storeList) 00050 * 231: function addToStoreList($storeList) 00051 * 245: function addToStoreListFromPrefix($prefix='') 00052 * 00053 * SECTION: Process storage array 00054 * 279: function initStorage() 00055 * 294: function cleanupStorageArray($storedSettings) 00056 * 316: function compileEntry($data) 00057 * 343: function getStoredData($storeIndex, $writeArray=array()) 00058 * 360: function processStoreControl($mconfName='') 00059 * 442: function writeStoredSetting($writeArray=array(), $mconfName='') 00060 * 00061 * SECTION: GUI 00062 * 474: function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE) 00063 * 00064 * SECTION: Misc 00065 * 576: function processEntry($storageArr) 00066 * 00067 * TOTAL FUNCTIONS: 13 00068 * (This index is automatically created/updated by the extension "extdeveval") 00069 * 00070 */ 00071 00072 00073 /** 00074 * usage inside of scbase class 00075 * 00076 * .... 00077 * 00078 * $this->MOD_MENU = array( 00079 * 'function' => array( 00080 * 'xxx ... 00081 * ), 00082 * 'tx_dam_select_storedSettings' => '', 00083 * 00084 * .... 00085 * 00086 * function main() { 00087 * // reStore settings 00088 * $store = t3lib_div::makeInstance('t3lib_modSettings'); 00089 * $store->init('tx_dam_select'); 00090 * $store->setStoreList('tx_dam_select'); 00091 * $store->processStoreControl(); 00092 * 00093 * // show control panel 00094 * $this->content.= $this->doc->section('Settings',$store->getStoreControl(),0,1); 00095 * 00096 * 00097 * 00098 * Format of saved settings 00099 * 00100 * $SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings'] = serialize( 00101 * array ( 00102 * 'any id' => array ( 00103 * 'title' => 'title for saved settings', 00104 * 'desc' => 'descritpion text, not mandatory', 00105 * 'data' => array(), // data from MOD_SETTINGS 00106 * 'user' => NULL, // can be used for extra data used by the application to identify this entry 00107 * 'tstamp' => 12345, // $GLOBALS['EXEC_TIME'] 00108 * ), 00109 * 'another id' => ... 00110 * 00111 * ) ); 00112 * 00113 */ 00114 00115 /** 00116 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings. 00117 * Provides a presets box for BE modules. 00118 * 00119 * @author René Fritz <r.fritz@colorcube.de> 00120 * @package TYPO3 00121 * @subpackage t3lib 00122 */ 00123 class t3lib_modSettings { 00124 00125 /** 00126 * If type is set 'ses' then the module data will be stored into the session and will be lost with logout. 00127 * Type 'perm' will store the data permanently. 00128 */ 00129 var $type = 'perm'; 00130 00131 /** 00132 * prefix of MOD_SETTING array keys that should be stored 00133 */ 00134 var $prefix = ''; 00135 00136 /** 00137 * Names of keys of the MOD_SETTING array which should be stored 00138 */ 00139 var $storeList = array(); 00140 00141 /** 00142 * The stored settings array 00143 */ 00144 var $storedSettings = array(); 00145 00146 /** 00147 * Message from the last storage command 00148 */ 00149 var $msg = ''; 00150 00151 00152 /** 00153 * Name of the form. Needed for JS 00154 */ 00155 var $formName = 'storeControl'; 00156 00157 00158 var $writeDevLog = 0; // write messages into the devlog? 00159 00160 00161 /******************************** 00162 * 00163 * Init / setup 00164 * 00165 ********************************/ 00166 00167 00168 /** 00169 * Initializes the object 00170 * 00171 * @param string Prefix of MOD_SETTING array keys that should be stored 00172 * @param array additional names of keys of the MOD_SETTING array which should be stored (array or comma list) 00173 * @return void 00174 */ 00175 function init($prefix = '', $storeList = '') { 00176 $this->prefix = $prefix; 00177 $this->setStoreList($storeList); 00178 $this->type = 'perm'; 00179 00180 // enable dev logging if set 00181 if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) { 00182 $this->writeDevLog = TRUE; 00183 } 00184 if (TYPO3_DLOG) { 00185 $this->writeDevLog = TRUE; 00186 } 00187 } 00188 00189 /** 00190 * Set session type to 'ses' which will store the settings data not permanently. 00191 * 00192 * @param string Default is 'ses' 00193 * @return void 00194 */ 00195 function setSessionType($type = 'ses') { 00196 $this->type = $type; 00197 } 00198 00199 00200 /******************************** 00201 * 00202 * Store list - which values should be stored 00203 * 00204 ********************************/ 00205 00206 00207 /** 00208 * Set MOD_SETTINGS keys which should be stored 00209 * 00210 * @param mixed array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored 00211 * @return void 00212 */ 00213 function setStoreList($storeList) { 00214 $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',', $storeList, 1); 00215 00216 if ($this->writeDevLog) { 00217 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0); 00218 } 00219 } 00220 00221 00222 /** 00223 * Add MOD_SETTINGS keys to the current list 00224 * 00225 * @param mixed array or string (,) - add names of keys of the MOD_SETTING array which should be stored 00226 * @return void 00227 */ 00228 function addToStoreList($storeList) { 00229 $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',', $storeList, 1); 00230 $this->storeList = array_merge($this->storeList, $storeList); 00231 00232 if ($this->writeDevLog) { 00233 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0); 00234 } 00235 } 00236 00237 00238 /** 00239 * Add names of keys of the MOD_SETTING array by a prefix 00240 * 00241 * @param string prefix of MOD_SETTING array keys that should be stored 00242 * @return void 00243 */ 00244 function addToStoreListFromPrefix($prefix = '') { 00245 global $SOBE; 00246 00247 $prefix = $prefix ? $prefix : $this->prefix; 00248 00249 foreach ($SOBE->MOD_SETTINGS as $key => $value) { 00250 if (preg_match('/^' . $prefix . '/', $key)) { 00251 $this->storeList[$key] = $key; 00252 } 00253 } 00254 00255 unset($this->storeList[$this->prefix . '_storedSettings']); 00256 00257 if ($this->writeDevLog) { 00258 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0); 00259 } 00260 } 00261 00262 00263 /******************************** 00264 * 00265 * Process storage array 00266 * 00267 ********************************/ 00268 00269 00270 /** 00271 * Get the stored settings from MOD_SETTINGS and set them in $this->storedSettings 00272 * 00273 * @return void 00274 */ 00275 function initStorage() { 00276 global $SOBE; 00277 00278 $storedSettings = unserialize($SOBE->MOD_SETTINGS[$this->prefix . '_storedSettings']); 00279 $this->storedSettings = $this->cleanupStorageArray($storedSettings); 00280 } 00281 00282 00283 /** 00284 * Remove corrupted data entries from the stored settings array 00285 * 00286 * @param array $storedSettings 00287 * @return array $storedSettings 00288 */ 00289 function cleanupStorageArray($storedSettings) { 00290 00291 $storedSettings = is_array($storedSettings) ? $storedSettings : array(); 00292 00293 // clean up the array 00294 foreach ($storedSettings as $id => $sdArr) { 00295 if (!is_array($sdArr)) { 00296 unset($storedSettings[$id]); 00297 } 00298 if (!is_array($sdArr['data'])) { 00299 unset($storedSettings[$id]); 00300 } 00301 if (!trim($sdArr['title'])) { 00302 $storedSettings[$id]['title'] = '[no title]'; 00303 } 00304 } 00305 00306 return $storedSettings; 00307 } 00308 00309 00310 /** 00311 * Creates an entry for the stored settings array 00312 * Collects data from MOD_SETTINGS selected by the storeList 00313 * 00314 * @param array Should work with data from _GP('storeControl'). This is ['title']: Title for the entry. ['desc']: A description text. Currently not used by this class 00315 * @return array $storageArr: entry for the stored settings array 00316 */ 00317 function compileEntry($data) { 00318 global $SOBE; 00319 00320 $storageData = array(); 00321 foreach ($this->storeList as $MS_key) { 00322 $storageData[$MS_key] = $SOBE->MOD_SETTINGS[$MS_key]; 00323 } 00324 $storageArr = array( 00325 'title' => $data['title'], 00326 'desc' => (string) $data['desc'], 00327 'data' => $storageData, 00328 'user' => NULL, 00329 'tstamp' => $GLOBALS['EXEC_TIME'], 00330 ); 00331 $storageArr = $this->processEntry($storageArr); 00332 00333 return $storageArr; 00334 } 00335 00336 00337 /** 00338 * Copies the stored data from entry $index to $writeArray which can be used to set MOD_SETTINGS 00339 * 00340 * @param mixed The entry key 00341 * @param array Preset data array. Will be overwritten by copied values. 00342 * @return array Data array 00343 */ 00344 function getStoredData($storeIndex, $writeArray = array()) { 00345 if ($this->storedSettings[$storeIndex]) { 00346 foreach ($this->storeList as $k) { 00347 $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k]; 00348 } 00349 } 00350 return $writeArray; 00351 } 00352 00353 00354 /** 00355 * Processing of the storage command LOAD, SAVE, REMOVE 00356 * 00357 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module) 00358 * @return string Storage message. Also set in $this->msg 00359 */ 00360 function processStoreControl($mconfName = '') { 00361 00362 $this->initStorage(); 00363 00364 $storeControl = t3lib_div::_GP('storeControl'); 00365 $storeIndex = $storeControl['STORE']; 00366 00367 $msg = ''; 00368 $saveSettings = FALSE; 00369 $writeArray = array(); 00370 00371 if (is_array($storeControl)) { 00372 if ($this->writeDevLog) { 00373 t3lib_div::devLog('Store command: ' . t3lib_div::arrayToLogString($storeControl), 't3lib_modSettings', 0); 00374 } 00375 00376 // 00377 // processing LOAD 00378 // 00379 00380 if ($storeControl['LOAD'] AND $storeIndex) { 00381 $writeArray = $this->getStoredData($storeIndex, $writeArray); 00382 $saveSettings = TRUE; 00383 $msg = "'" . $this->storedSettings[$storeIndex]['title'] . "' preset loaded!"; 00384 00385 // 00386 // processing SAVE 00387 // 00388 00389 } elseif ($storeControl['SAVE']) { 00390 if (trim($storeControl['title'])) { 00391 00392 // get the data to store 00393 $newEntry = $this->compileEntry($storeControl); 00394 00395 // create an index for the storage array 00396 if (!$storeIndex) { 00397 $storeIndex = t3lib_div::shortMD5($newEntry['title']); 00398 } 00399 00400 // add data to the storage array 00401 $this->storedSettings[$storeIndex] = $newEntry; 00402 00403 $saveSettings = TRUE; 00404 $msg = "'" . $newEntry['title'] . "' preset saved!"; 00405 00406 } else { 00407 $msg = 'Please enter a name for the preset!'; 00408 } 00409 00410 // 00411 // processing REMOVE 00412 // 00413 00414 } elseif ($storeControl['REMOVE'] AND $storeIndex) { 00415 // Removing entry 00416 $msg = "'" . $this->storedSettings[$storeIndex]['title'] . "' preset entry removed!"; 00417 unset($this->storedSettings[$storeIndex]); 00418 00419 $saveSettings = TRUE; 00420 } 00421 00422 00423 $this->msg = $msg; 00424 00425 if ($saveSettings) { 00426 $this->writeStoredSetting($writeArray, $mconfName); 00427 } 00428 00429 } 00430 return $this->msg; 00431 } 00432 00433 00434 /** 00435 * Write the current storage array and update MOD_SETTINGS 00436 * 00437 * @param array Array of settings which should be overwrite current MOD_SETTINGS 00438 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module) 00439 * @return void 00440 */ 00441 function writeStoredSetting($writeArray = array(), $mconfName = '') { 00442 global $SOBE; 00443 00444 // for debugging: just removes all module data from user settings 00445 // $GLOBALS['BE_USER']->pushModuleData($SOBE->MCONF['name'],array()); 00446 00447 unset($this->storedSettings[0]); // making sure, index 0 is not set! 00448 $this->storedSettings = $this->cleanupStorageArray($this->storedSettings); 00449 $writeArray[$this->prefix . '_storedSettings'] = serialize($this->storedSettings); 00450 00451 $SOBE->MOD_SETTINGS = t3lib_BEfunc::getModuleData($SOBE->MOD_MENU, $writeArray, ($mconfName ? $mconfName : $SOBE->MCONF['name']), $this->type); 00452 00453 if ($this->writeDevLog) { 00454 t3lib_div::devLog('Settings stored:' . $this->msg, 't3lib_modSettings', 0); 00455 } 00456 } 00457 00458 00459 /******************************** 00460 * 00461 * GUI 00462 * 00463 ********************************/ 00464 00465 00466 /** 00467 * Returns the storage control box 00468 * 00469 * @param string List of elemetns which should be shown: load,remove,save 00470 * @param boolean If set the box is wrapped with own form tag 00471 * @return string HTML code 00472 */ 00473 function getStoreControl($showElements = 'load,remove,save', $useOwnForm = TRUE) { 00474 global $TYPO3_CONF_VARS; 00475 00476 $showElements = t3lib_div::trimExplode(',', $showElements, 1); 00477 00478 $this->initStorage(); 00479 00480 // Preset selector 00481 $opt = array(); 00482 $opt[] = '<option value="0"> </option>'; 00483 foreach ($this->storedSettings as $id => $v) { 00484 $opt[] = '<option value="' . $id . '">' . htmlspecialchars($v['title']) . '</option>'; 00485 } 00486 $storedEntries = count($opt) > 1; 00487 00488 00489 $codeTD = array(); 00490 00491 00492 // LOAD, REMOVE, but also show selector so you can overwrite an entry with SAVE 00493 if ($storedEntries AND (count($showElements))) { 00494 00495 // selector box 00496 $onChange = 'document.forms[\'' . $this->formName . '\'][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';'; 00497 $code = ' 00498 <select name="storeControl[STORE]" onChange="' . htmlspecialchars($onChange) . '"> 00499 ' . implode(' 00500 ', $opt) . ' 00501 </select>'; 00502 00503 // load button 00504 if (in_array('load', $showElements)) { 00505 $code .= ' 00506 <input type="submit" name="storeControl[LOAD]" value="Load" /> '; 00507 } 00508 00509 // remove button 00510 if (in_array('remove', $showElements)) { 00511 $code .= ' 00512 <input type="submit" name="storeControl[REMOVE]" value="Remove" /> '; 00513 } 00514 $codeTD[] = '<td width="1%">Preset:</td>'; 00515 $codeTD[] = '<td nowrap="nowrap">' . $code . ' </td>'; 00516 } 00517 00518 00519 // SAVE 00520 if (in_array('save', $showElements)) { 00521 $onClick = (!$storedEntries) ? '' : 'if (document.forms[\'' . $this->formName . '\'][\'storeControl[STORE]\'].options[document.forms[\'' . $this->formName . '\'][\'storeControl[STORE]\'].selectedIndex].value<0) return confirm(\'Are you sure you want to overwrite the existing entry?\');'; 00522 $code = '<input name="storeControl[title]" value="" type="text" max="80" width="25"> '; 00523 $code .= '<input type="submit" name="storeControl[SAVE]" value="Save" onClick="' . htmlspecialchars($onClick) . '" />'; 00524 $codeTD[] = '<td nowrap="nowrap">' . $code . '</td>'; 00525 } 00526 00527 00528 $codeTD = implode(' 00529 ', $codeTD); 00530 00531 if (trim($code)) { 00532 $code = ' 00533 <!-- 00534 Store control 00535 --> 00536 <table border="0" cellpadding="3" cellspacing="0" width="100%"> 00537 <tr class="bgColor4"> 00538 ' . $codeTD . ' 00539 </tr> 00540 </table> 00541 '; 00542 } 00543 00544 if ($this->msg) { 00545 $code .= ' 00546 <div><strong>' . htmlspecialchars($this->msg) . '</strong></div>'; 00547 } 00548 #TODO need to add parameters 00549 if ($useOwnForm AND trim($code)) { 00550 $code = ' 00551 <form action="' . t3lib_div::getIndpEnv('SCRIPT_NAME') . '" method="post" name="' . $this->formName . '" enctype="' . $TYPO3_CONF_VARS['SYS']['form_enctype'] . '">' . $code . '</form>'; 00552 } 00553 00554 return $code; 00555 } 00556 00557 00558 /******************************** 00559 * 00560 * Misc 00561 * 00562 ********************************/ 00563 00564 00565 /** 00566 * Processing entry for the stored settings array 00567 * Can be overwritten by extended class 00568 * 00569 * @param array $storageData: entry for the stored settings array 00570 * @return array $storageData: entry for the stored settings array 00571 */ 00572 function processEntry($storageArr) { 00573 return $storageArr; 00574 } 00575 } 00576 00577 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php'])) { 00578 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']); 00579 } 00580 00581 ?>
1.8.0