Fixed issue #13670: Performance optimization: change while(list() to foreach() (thank...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_modsettings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
29 * Provides a presets box for BE modules.
30 *
31 * inspired by t3lib_fullsearch
32 *
33 * $Id$
34 *
35 * @author René Fritz <r.fritz@colorcube.de>
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 125: class t3lib_modSettings
43 *
44 * SECTION: Init / setup
45 * 181: function init($prefix='', $storeList='')
46 * 197: function setSessionType($type='ses')
47 *
48 * SECTION: Store list - which values should be stored
49 * 218: function setStoreList($storeList)
50 * 231: function addToStoreList($storeList)
51 * 245: function addToStoreListFromPrefix($prefix='')
52 *
53 * SECTION: Process storage array
54 * 279: function initStorage()
55 * 294: function cleanupStorageArray($storedSettings)
56 * 316: function compileEntry($data)
57 * 343: function getStoredData($storeIndex, $writeArray=array())
58 * 360: function processStoreControl($mconfName='')
59 * 442: function writeStoredSetting($writeArray=array(), $mconfName='')
60 *
61 * SECTION: GUI
62 * 474: function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE)
63 *
64 * SECTION: Misc
65 * 576: function processEntry($storageArr)
66 *
67 * TOTAL FUNCTIONS: 13
68 * (This index is automatically created/updated by the extension "extdeveval")
69 *
70 */
71
72
73
74
75 /**
76 * usage inside of scbase class
77 *
78 * ....
79 *
80 * $this->MOD_MENU = array(
81 * 'function' => array(
82 * 'xxx ...
83 * ),
84 * 'tx_dam_select_storedSettings' => '',
85 *
86 * ....
87 *
88 * function main() {
89 * // reStore settings
90 * $store = t3lib_div::makeInstance('t3lib_modSettings');
91 * $store->init('tx_dam_select');
92 * $store->setStoreList('tx_dam_select');
93 * $store->processStoreControl();
94 *
95 * // show control panel
96 * $this->content.= $this->doc->section('Settings',$store->getStoreControl(),0,1);
97 *
98 *
99 *
100 * Format of saved settings
101 *
102 * $SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings'] = serialize(
103 * array (
104 * 'any id' => array (
105 * 'title' => 'title for saved settings',
106 * 'desc' => 'descritpion text, not mandatory',
107 * 'data' => array(), // data from MOD_SETTINGS
108 * 'user' => NULL, // can be used for extra data used by the application to identify this entry
109 * 'tstamp' => 12345, // $GLOBALS['EXEC_TIME']
110 * ),
111 * 'another id' => ...
112 *
113 * ) );
114 *
115 */
116
117 /**
118 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
119 * Provides a presets box for BE modules.
120 *
121 * @author René Fritz <r.fritz@colorcube.de>
122 * @package TYPO3
123 * @subpackage t3lib
124 */
125 class t3lib_modSettings {
126
127 /**
128 * If type is set 'ses' then the module data will be stored into the session and will be lost with logout.
129 * Type 'perm' will store the data permanently.
130 */
131 var $type = 'perm';
132
133 /**
134 * prefix of MOD_SETTING array keys that should be stored
135 */
136 var $prefix = '';
137
138 /**
139 * Names of keys of the MOD_SETTING array which should be stored
140 */
141 var $storeList = array();
142
143 /**
144 * The stored settings array
145 */
146 var $storedSettings = array();
147
148 /**
149 * Message from the last storage command
150 */
151 var $msg = '';
152
153
154 /**
155 * Name of the form. Needed for JS
156 */
157 var $formName = 'storeControl';
158
159
160
161 var $writeDevLog = 0; // write messages into the devlog?
162
163
164
165
166 /********************************
167 *
168 * Init / setup
169 *
170 ********************************/
171
172
173
174 /**
175 * Initializes the object
176 *
177 * @param string Prefix of MOD_SETTING array keys that should be stored
178 * @param array additional names of keys of the MOD_SETTING array which should be stored (array or comma list)
179 * @return void
180 */
181 function init($prefix='', $storeList='') {
182 $this->prefix = $prefix;
183 $this->setStoreList($storeList);
184 $this->type = 'perm';
185
186 // enable dev logging if set
187 if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) $this->writeDevLog = TRUE;
188 if (TYPO3_DLOG) $this->writeDevLog = TRUE;
189 }
190
191 /**
192 * Set session type to 'ses' which will store the settings data not permanently.
193 *
194 * @param string Default is 'ses'
195 * @return void
196 */
197 function setSessionType($type='ses') {
198 $this->type = $type;
199 }
200
201
202
203
204 /********************************
205 *
206 * Store list - which values should be stored
207 *
208 ********************************/
209
210
211
212 /**
213 * Set MOD_SETTINGS keys which should be stored
214 *
215 * @param mixed array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored
216 * @return void
217 */
218 function setStoreList($storeList) {
219 $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
220
221 if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
222 }
223
224
225 /**
226 * Add MOD_SETTINGS keys to the current list
227 *
228 * @param mixed array or string (,) - add names of keys of the MOD_SETTING array which should be stored
229 * @return void
230 */
231 function addToStoreList($storeList) {
232 $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
233 $this->storeList = array_merge($this->storeList, $storeList);
234
235 if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
236 }
237
238
239 /**
240 * Add names of keys of the MOD_SETTING array by a prefix
241 *
242 * @param string prefix of MOD_SETTING array keys that should be stored
243 * @return void
244 */
245 function addToStoreListFromPrefix($prefix='') {
246 global $SOBE;
247
248 $prefix = $prefix ? $prefix : $this->prefix;
249
250 foreach ($SOBE->MOD_SETTINGS as $key => $value) {
251 if (preg_match('/^'.$prefix.'/',$key)) {
252 $this->storeList[$key]=$key;
253 }
254 }
255
256 unset($this->storeList[$this->prefix.'_storedSettings']);
257
258 if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
259 }
260
261
262
263
264
265 /********************************
266 *
267 * Process storage array
268 *
269 ********************************/
270
271
272
273 /**
274 * Get the stored settings from MOD_SETTINGS and set them in $this->storedSettings
275 *
276 * @return void
277 */
278 function initStorage() {
279 global $SOBE;
280
281 $storedSettings = unserialize($SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings']);
282 $this->storedSettings = $this->cleanupStorageArray($storedSettings);
283 }
284
285
286
287 /**
288 * Remove corrupted data entries from the stored settings array
289 *
290 * @param array $storedSettings
291 * @return array $storedSettings
292 */
293 function cleanupStorageArray($storedSettings) {
294
295 $storedSettings = is_array($storedSettings) ? $storedSettings : array();
296
297 // clean up the array
298 foreach($storedSettings as $id => $sdArr) {
299 if (!is_array($sdArr)) unset($storedSettings[$id]);
300 if (!is_array($sdArr['data'])) unset($storedSettings[$id]);
301 if (!trim($sdArr['title'])) $storedSettings[$id]['title'] = '[no title]';
302 }
303
304 return $storedSettings;
305 }
306
307
308 /**
309 * Creates an entry for the stored settings array
310 * Collects data from MOD_SETTINGS selected by the storeList
311 *
312 * @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
313 * @return array $storageArr: entry for the stored settings array
314 */
315 function compileEntry($data) {
316 global $SOBE;
317
318 $storageData = array();
319 foreach($this->storeList as $MS_key) {
320 $storageData[$MS_key] = $SOBE->MOD_SETTINGS[$MS_key];
321 }
322 $storageArr = array (
323 'title' => $data['title'],
324 'desc' => (string)$data['desc'],
325 'data' => $storageData,
326 'user' => NULL,
327 'tstamp' => $GLOBALS['EXEC_TIME'],
328 );
329 $storageArr = $this->processEntry($storageArr);
330
331 return $storageArr;
332 }
333
334
335 /**
336 * Copies the stored data from entry $index to $writeArray which can be used to set MOD_SETTINGS
337 *
338 * @param mixed The entry key
339 * @param array Preset data array. Will be overwritten by copied values.
340 * @return array Data array
341 */
342 function getStoredData($storeIndex, $writeArray=array()) {
343 if ($this->storedSettings[$storeIndex]) {
344 foreach($this->storeList as $k) {
345 $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k];
346 }
347 }
348 return $writeArray;
349 }
350
351
352
353 /**
354 * Processing of the storage command LOAD, SAVE, REMOVE
355 *
356 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
357 * @return string Storage message. Also set in $this->msg
358 */
359 function processStoreControl($mconfName='') {
360
361 $this->initStorage();
362
363 $storeControl = t3lib_div::_GP('storeControl');
364 $storeIndex = $storeControl['STORE'];
365
366 $msg = '';
367 $saveSettings = FALSE;
368 $writeArray = array();
369
370 if (is_array($storeControl)) {
371 if ($this->writeDevLog) {
372 t3lib_div::devLog('Store command: '.t3lib_div::arrayToLogString($storeControl), 't3lib_modSettings', 0);
373 }
374
375 //
376 // processing LOAD
377 //
378
379 if ($storeControl['LOAD'] AND $storeIndex) {
380 $writeArray = $this->getStoredData($storeIndex, $writeArray);
381 $saveSettings = TRUE;
382 $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset loaded!";
383
384 //
385 // processing SAVE
386 //
387
388 } elseif ($storeControl['SAVE']) {
389 if (trim($storeControl['title'])) {
390
391 // get the data to store
392 $newEntry = $this->compileEntry($storeControl);
393
394 // create an index for the storage array
395 if (!$storeIndex) {
396 $storeIndex = t3lib_div::shortMD5($newEntry['title']);
397 }
398
399 // add data to the storage array
400 $this->storedSettings[$storeIndex] = $newEntry;
401
402 $saveSettings = TRUE;
403 $msg = "'".$newEntry['title']."' preset saved!";
404
405 } else {
406 $msg = 'Please enter a name for the preset!';
407 }
408
409 //
410 // processing REMOVE
411 //
412
413 } elseif ($storeControl['REMOVE'] AND $storeIndex) {
414 // Removing entry
415 $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset entry removed!";
416 unset($this->storedSettings[$storeIndex]);
417
418 $saveSettings = TRUE;
419 }
420
421
422 $this->msg = $msg;
423
424 if ($saveSettings) {
425 $this->writeStoredSetting($writeArray, $mconfName);
426 }
427
428 }
429 return $this->msg;
430 }
431
432
433 /**
434 * Write the current storage array and update MOD_SETTINGS
435 *
436 * @param array Array of settings which should be overwrite current MOD_SETTINGS
437 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
438 * @return void
439 */
440 function writeStoredSetting($writeArray=array(), $mconfName='') {
441 global $SOBE;
442
443 // for debugging: just removes all module data from user settings
444 # $GLOBALS['BE_USER']->pushModuleData($SOBE->MCONF['name'],array());
445
446 unset($this->storedSettings[0]); // making sure, index 0 is not set!
447 $this->storedSettings = $this->cleanupStorageArray($this->storedSettings);
448 $writeArray[$this->prefix.'_storedSettings'] = serialize($this->storedSettings);
449
450 $SOBE->MOD_SETTINGS = t3lib_BEfunc::getModuleData($SOBE->MOD_MENU, $writeArray, ($mconfName?$mconfName:$SOBE->MCONF['name']), $this->type);
451
452 if ($this->writeDevLog) t3lib_div::devLog('Settings stored:'.$this->msg, 't3lib_modSettings', 0);
453 }
454
455
456
457 /********************************
458 *
459 * GUI
460 *
461 ********************************/
462
463
464
465 /**
466 * Returns the storage control box
467 *
468 * @param string List of elemetns which should be shown: load,remove,save
469 * @param boolean If set the box is wrapped with own form tag
470 * @return string HTML code
471 */
472 function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE) {
473 global $TYPO3_CONF_VARS;
474
475 $showElements = t3lib_div::trimExplode(',', $showElements, 1);
476
477 $this->initStorage();
478
479 // Preset selector
480 $opt=array();
481 $opt[] = '<option value="0"> </option>';
482 foreach($this->storedSettings as $id => $v) {
483 $opt[] = '<option value="'.$id.'">'.htmlspecialchars($v['title']).'</option>';
484 }
485 $storedEntries = count($opt)>1;
486
487
488
489 $codeTD = array();
490
491
492 // LOAD, REMOVE, but also show selector so you can overwrite an entry with SAVE
493 if($storedEntries AND (count($showElements))) {
494
495 // selector box
496 $onChange = 'document.forms[\''.$this->formName.'\'][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';';
497 $code = '
498 <select name="storeControl[STORE]" onChange="'.htmlspecialchars($onChange).'">
499 '.implode('
500 ', $opt).'
501 </select>';
502
503 // load button
504 if(in_array('load', $showElements)) {
505 $code.= '
506 <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
507 }
508
509 // remove button
510 if(in_array('remove', $showElements)) {
511 $code.= '
512 <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
513 }
514 $codeTD[] = '<td width="1%">Preset:</td>';
515 $codeTD[] = '<td nowrap="nowrap">'.$code.'&nbsp;&nbsp;</td>';
516 }
517
518
519 // SAVE
520 if(in_array('save', $showElements)) {
521 $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?\');';
522 $code = '<input name="storeControl[title]" value="" type="text" max="80" width="25"> ';
523 $code.= '<input type="submit" name="storeControl[SAVE]" value="Save" onClick="'.htmlspecialchars($onClick).'" />';
524 $codeTD[] = '<td nowrap="nowrap">'.$code.'</td>';
525 }
526
527
528 $codeTD = implode ('
529 ', $codeTD);
530
531 if (trim($code)) {
532 $code = '
533 <!--
534 Store control
535 -->
536 <table border="0" cellpadding="3" cellspacing="0" width="100%">
537 <tr class="bgColor4">
538 '.$codeTD.'
539 </tr>
540 </table>
541 ';
542 }
543
544 if ($this->msg) {
545 $code.= '
546 <div><strong>'.htmlspecialchars($this->msg).'</strong></div>';
547 }
548 #TODO need to add parameters
549 if ($useOwnForm AND trim($code)) {
550 $code = '
551 <form action="' . t3lib_div::getIndpEnv('SCRIPT_NAME') . '" method="post" name="' . $this->formName . '" enctype="' . $TYPO3_CONF_VARS['SYS']['form_enctype'] . '">' . $code . '</form>';
552 }
553
554 return $code;
555 }
556
557
558
559
560 /********************************
561 *
562 * Misc
563 *
564 ********************************/
565
566
567 /**
568 * Processing entry for the stored settings array
569 * Can be overwritten by extended class
570 *
571 * @param array $storageData: entry for the stored settings array
572 * @return array $storageData: entry for the stored settings array
573 */
574 function processEntry($storageArr) {
575 return $storageArr;
576 }
577 }
578
579 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']) {
580 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']);
581 }
582
583 ?>