Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_positionmap
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_modsettings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (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 * usage inside of scbase class
75 *
76 * ....
77 *
78 * $this->MOD_MENU = array(
79 * 'function' => array(
80 * 'xxx ...
81 * ),
82 * 'tx_dam_select_storedSettings' => '',
83 *
84 * ....
85 *
86 * function main() {
87 * // reStore settings
88 * $store = t3lib_div::makeInstance('t3lib_modSettings');
89 * $store->init('tx_dam_select');
90 * $store->setStoreList('tx_dam_select');
91 * $store->processStoreControl();
92 *
93 * // show control panel
94 * $this->content.= $this->doc->section('Settings',$store->getStoreControl(),0,1);
95 *
96 *
97 *
98 * Format of saved settings
99 *
100 * $SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings'] = serialize(
101 * array (
102 * 'any id' => array (
103 * 'title' => 'title for saved settings',
104 * 'desc' => 'descritpion text, not mandatory',
105 * 'data' => array(), // data from MOD_SETTINGS
106 * 'user' => NULL, // can be used for extra data used by the application to identify this entry
107 * 'tstamp' => 12345, // $GLOBALS['EXEC_TIME']
108 * ),
109 * 'another id' => ...
110 *
111 * ) );
112 *
113 */
114
115 /**
116 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
117 * Provides a presets box for BE modules.
118 *
119 * @author René Fritz <r.fritz@colorcube.de>
120 * @package TYPO3
121 * @subpackage t3lib
122 */
123 class t3lib_modSettings {
124
125 /**
126 * If type is set 'ses' then the module data will be stored into the session and will be lost with logout.
127 * Type 'perm' will store the data permanently.
128 */
129 var $type = 'perm';
130
131 /**
132 * prefix of MOD_SETTING array keys that should be stored
133 */
134 var $prefix = '';
135
136 /**
137 * Names of keys of the MOD_SETTING array which should be stored
138 */
139 var $storeList = array();
140
141 /**
142 * The stored settings array
143 */
144 var $storedSettings = array();
145
146 /**
147 * Message from the last storage command
148 */
149 var $msg = '';
150
151
152 /**
153 * Name of the form. Needed for JS
154 */
155 var $formName = 'storeControl';
156
157
158 var $writeDevLog = 0; // write messages into the devlog?
159
160
161 /********************************
162 *
163 * Init / setup
164 *
165 ********************************/
166
167
168 /**
169 * Initializes the object
170 *
171 * @param string Prefix of MOD_SETTING array keys that should be stored
172 * @param array additional names of keys of the MOD_SETTING array which should be stored (array or comma list)
173 * @return void
174 */
175 function init($prefix = '', $storeList = '') {
176 $this->prefix = $prefix;
177 $this->setStoreList($storeList);
178 $this->type = 'perm';
179
180 // enable dev logging if set
181 if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) {
182 $this->writeDevLog = TRUE;
183 }
184 if (TYPO3_DLOG) {
185 $this->writeDevLog = TRUE;
186 }
187 }
188
189 /**
190 * Set session type to 'ses' which will store the settings data not permanently.
191 *
192 * @param string Default is 'ses'
193 * @return void
194 */
195 function setSessionType($type = 'ses') {
196 $this->type = $type;
197 }
198
199
200 /********************************
201 *
202 * Store list - which values should be stored
203 *
204 ********************************/
205
206
207 /**
208 * Set MOD_SETTINGS keys which should be stored
209 *
210 * @param mixed array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored
211 * @return void
212 */
213 function setStoreList($storeList) {
214 $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',', $storeList, 1);
215
216 if ($this->writeDevLog) {
217 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0);
218 }
219 }
220
221
222 /**
223 * Add MOD_SETTINGS keys to the current list
224 *
225 * @param mixed array or string (,) - add names of keys of the MOD_SETTING array which should be stored
226 * @return void
227 */
228 function addToStoreList($storeList) {
229 $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',', $storeList, 1);
230 $this->storeList = array_merge($this->storeList, $storeList);
231
232 if ($this->writeDevLog) {
233 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0);
234 }
235 }
236
237
238 /**
239 * Add names of keys of the MOD_SETTING array by a prefix
240 *
241 * @param string prefix of MOD_SETTING array keys that should be stored
242 * @return void
243 */
244 function addToStoreListFromPrefix($prefix = '') {
245 global $SOBE;
246
247 $prefix = $prefix ? $prefix : $this->prefix;
248
249 foreach ($SOBE->MOD_SETTINGS as $key => $value) {
250 if (preg_match('/^' . $prefix . '/', $key)) {
251 $this->storeList[$key] = $key;
252 }
253 }
254
255 unset($this->storeList[$this->prefix . '_storedSettings']);
256
257 if ($this->writeDevLog) {
258 t3lib_div::devLog('Store list:' . implode(',', $this->storeList), 't3lib_modSettings', 0);
259 }
260 }
261
262
263 /********************************
264 *
265 * Process storage array
266 *
267 ********************************/
268
269
270 /**
271 * Get the stored settings from MOD_SETTINGS and set them in $this->storedSettings
272 *
273 * @return void
274 */
275 function initStorage() {
276 global $SOBE;
277
278 $storedSettings = unserialize($SOBE->MOD_SETTINGS[$this->prefix . '_storedSettings']);
279 $this->storedSettings = $this->cleanupStorageArray($storedSettings);
280 }
281
282
283 /**
284 * Remove corrupted data entries from the stored settings array
285 *
286 * @param array $storedSettings
287 * @return array $storedSettings
288 */
289 function cleanupStorageArray($storedSettings) {
290
291 $storedSettings = is_array($storedSettings) ? $storedSettings : array();
292
293 // clean up the array
294 foreach ($storedSettings as $id => $sdArr) {
295 if (!is_array($sdArr)) {
296 unset($storedSettings[$id]);
297 }
298 if (!is_array($sdArr['data'])) {
299 unset($storedSettings[$id]);
300 }
301 if (!trim($sdArr['title'])) {
302 $storedSettings[$id]['title'] = '[no title]';
303 }
304 }
305
306 return $storedSettings;
307 }
308
309
310 /**
311 * Creates an entry for the stored settings array
312 * Collects data from MOD_SETTINGS selected by the storeList
313 *
314 * @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
315 * @return array $storageArr: entry for the stored settings array
316 */
317 function compileEntry($data) {
318 global $SOBE;
319
320 $storageData = array();
321 foreach ($this->storeList as $MS_key) {
322 $storageData[$MS_key] = $SOBE->MOD_SETTINGS[$MS_key];
323 }
324 $storageArr = array(
325 'title' => $data['title'],
326 'desc' => (string) $data['desc'],
327 'data' => $storageData,
328 'user' => NULL,
329 'tstamp' => $GLOBALS['EXEC_TIME'],
330 );
331 $storageArr = $this->processEntry($storageArr);
332
333 return $storageArr;
334 }
335
336
337 /**
338 * Copies the stored data from entry $index to $writeArray which can be used to set MOD_SETTINGS
339 *
340 * @param mixed The entry key
341 * @param array Preset data array. Will be overwritten by copied values.
342 * @return array Data array
343 */
344 function getStoredData($storeIndex, $writeArray = array()) {
345 if ($this->storedSettings[$storeIndex]) {
346 foreach ($this->storeList as $k) {
347 $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k];
348 }
349 }
350 return $writeArray;
351 }
352
353
354 /**
355 * Processing of the storage command LOAD, SAVE, REMOVE
356 *
357 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
358 * @return string Storage message. Also set in $this->msg
359 */
360 function processStoreControl($mconfName = '') {
361
362 $this->initStorage();
363
364 $storeControl = t3lib_div::_GP('storeControl');
365 $storeIndex = $storeControl['STORE'];
366
367 $msg = '';
368 $saveSettings = FALSE;
369 $writeArray = array();
370
371 if (is_array($storeControl)) {
372 if ($this->writeDevLog) {
373 t3lib_div::devLog('Store command: ' . t3lib_div::arrayToLogString($storeControl), 't3lib_modSettings', 0);
374 }
375
376 //
377 // processing LOAD
378 //
379
380 if ($storeControl['LOAD'] AND $storeIndex) {
381 $writeArray = $this->getStoredData($storeIndex, $writeArray);
382 $saveSettings = TRUE;
383 $msg = "'" . $this->storedSettings[$storeIndex]['title'] . "' preset loaded!";
384
385 //
386 // processing SAVE
387 //
388
389 } elseif ($storeControl['SAVE']) {
390 if (trim($storeControl['title'])) {
391
392 // get the data to store
393 $newEntry = $this->compileEntry($storeControl);
394
395 // create an index for the storage array
396 if (!$storeIndex) {
397 $storeIndex = t3lib_div::shortMD5($newEntry['title']);
398 }
399
400 // add data to the storage array
401 $this->storedSettings[$storeIndex] = $newEntry;
402
403 $saveSettings = TRUE;
404 $msg = "'" . $newEntry['title'] . "' preset saved!";
405
406 } else {
407 $msg = 'Please enter a name for the preset!';
408 }
409
410 //
411 // processing REMOVE
412 //
413
414 } elseif ($storeControl['REMOVE'] AND $storeIndex) {
415 // Removing entry
416 $msg = "'" . $this->storedSettings[$storeIndex]['title'] . "' preset entry removed!";
417 unset($this->storedSettings[$storeIndex]);
418
419 $saveSettings = TRUE;
420 }
421
422
423 $this->msg = $msg;
424
425 if ($saveSettings) {
426 $this->writeStoredSetting($writeArray, $mconfName);
427 }
428
429 }
430 return $this->msg;
431 }
432
433
434 /**
435 * Write the current storage array and update MOD_SETTINGS
436 *
437 * @param array Array of settings which should be overwrite current MOD_SETTINGS
438 * @param string Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
439 * @return void
440 */
441 function writeStoredSetting($writeArray = array(), $mconfName = '') {
442 global $SOBE;
443
444 // for debugging: just removes all module data from user settings
445 // $GLOBALS['BE_USER']->pushModuleData($SOBE->MCONF['name'],array());
446
447 unset($this->storedSettings[0]); // making sure, index 0 is not set!
448 $this->storedSettings = $this->cleanupStorageArray($this->storedSettings);
449 $writeArray[$this->prefix . '_storedSettings'] = serialize($this->storedSettings);
450
451 $SOBE->MOD_SETTINGS = t3lib_BEfunc::getModuleData($SOBE->MOD_MENU, $writeArray, ($mconfName ? $mconfName : $SOBE->MCONF['name']), $this->type);
452
453 if ($this->writeDevLog) {
454 t3lib_div::devLog('Settings stored:' . $this->msg, 't3lib_modSettings', 0);
455 }
456 }
457
458
459 /********************************
460 *
461 * GUI
462 *
463 ********************************/
464
465
466 /**
467 * Returns the storage control box
468 *
469 * @param string List of elemetns which should be shown: load,remove,save
470 * @param boolean If set the box is wrapped with own form tag
471 * @return string HTML code
472 */
473 function getStoreControl($showElements = 'load,remove,save', $useOwnForm = TRUE) {
474 global $TYPO3_CONF_VARS;
475
476 $showElements = t3lib_div::trimExplode(',', $showElements, 1);
477
478 $this->initStorage();
479
480 // Preset selector
481 $opt = array();
482 $opt[] = '<option value="0"> </option>';
483 foreach ($this->storedSettings as $id => $v) {
484 $opt[] = '<option value="' . $id . '">' . htmlspecialchars($v['title']) . '</option>';
485 }
486 $storedEntries = count($opt) > 1;
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 * Misc
561 *
562 ********************************/
563
564
565 /**
566 * Processing entry for the stored settings array
567 * Can be overwritten by extended class
568 *
569 * @param array $storageData: entry for the stored settings array
570 * @return array $storageData: entry for the stored settings array
571 */
572 function processEntry($storageArr) {
573 return $storageArr;
574 }
575 }
576
577 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']) {
578 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']);
579 }
580
581 ?>