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