Changed lots of stuff...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_modsettings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 * $Id$
32 *
33 * @author Kasper Skaarhoj <kasper@typo3.com>
34 * @coauthor René Fritz <r.fritz@colorcube.de>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 104: class t3lib_modSettings
42 * 114: function init($prefix='',$storeList='')
43 * 123: function setStoreList($storeList)
44 * 132: function addToStoreList($storeList)
45 * 144: function addToStoreListFromPrefix ($prefix='')
46 * 167: function initStoreArray()
47 * 187: function cleanStoreConfigs($storeConfigs,$storeArray)
48 * 204: function addToStoreConfigs($storeConfigs,$index)
49 * 222: function loadStoreConfigs($storeConfigs,$storeIndex,$writeArray)
50 * 238: function getStoreControl($show='load,remove,save')
51 * 295: function procesStoreControl($mconfName='')
52 * 376: function saveQueryInAction($uid)
53 *
54 * TOTAL FUNCTIONS: 11
55 * (This index is automatically created/updated by the extension "extdeveval")
56 *
57 */
58
59
60
61
62
63
64
65
66 /**
67 * usage inside of scbase class
68 *
69 * ....
70 *
71 * $this->MOD_MENU = array(
72 * 'function' => array(
73 * 'xxx ...
74 * ),
75 * 'tx_dam_selectStoreArray' => '',
76 * 'tx_dam_selectStoreConfigs' => '',
77 *
78 * ....
79 *
80 *
81 * function main() {
82 * // reStore settings
83 * $store = t3lib_div::makeInstance('t3lib_modSettings');
84 * $store->init('tx_dam_select');
85 * $store->addToStoreListFromPrefix('tx_dam_select');
86 * $storeMsg=$store->procesStoreControl();
87 *
88 * // show control panel
89 * $this->content.= $this->doc->section('store',$store->makeStoreControl(),0,1);
90 * if ($storeMsg) {
91 * $this->content.= $this->doc->section('','<strong>'.$storeMsg.'</strong>');
92 * }
93 */
94
95 /**
96 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
97 * Provides a presets box for BE modules.
98 *
99 * @author Kasper Skaarhoj <kasper@typo3.com>
100 * @coauthor René Fritz <r.fritz@colorcube.de>
101 * @package TYPO3
102 * @subpackage t3lib
103 */
104 class t3lib_modSettings {
105
106 var $prefix='';
107 var $storeList=array();
108
109 /**
110 * @param string prefix of MOD_SETTING array keys that should be stored
111 * @param array additional names of keys of the MOD_SETTING array which should be stored
112 * @return [type] ...
113 */
114 function init($prefix='',$storeList='') {
115 $this->prefix = $prefix;
116 $this->setStoreList($storeList);
117 }
118
119 /**
120 * @param mixed array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored
121 * @return [type] ...
122 */
123 function setStoreList($storeList) {
124 $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
125 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
126 }
127
128 /**
129 * @param mixed array or string (,) - add names of keys of the MOD_SETTING array which should be stored
130 * @return [type] ...
131 */
132 function addToStoreList($storeList) {
133 $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
134 $this->storeList = array_merge($this->storeList, $storeList);
135 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
136 }
137
138 /**
139 * add names of keys of the MOD_SETTING array which should be stored by a prefix
140 *
141 * @param string prefix of MOD_SETTING array keys that should be stored
142 * @return [type] ...
143 */
144 function addToStoreListFromPrefix ($prefix='') {
145 $prefix = $prefix ? $prefix : $this->prefix;
146
147 reset($GLOBALS['SOBE']->MOD_SETTINGS);
148 while(list($key)=each($GLOBALS['SOBE']->MOD_SETTINGS)) {
149 if (ereg('^'.$prefix,$key)) {
150 $this->storeList[$key]=$key;
151 }
152 }
153
154 unset($this->storeList[$this->prefix.'StoreArray']);
155 unset($this->storeList[$this->prefix.'StoreConfigs']);
156
157 #debug($GLOBALS['SOBE']->MOD_SETTINGS, 'store: $GLOBALS[SOBE]->MOD_SETTINGS', __LINE__, __FILE__);
158 #debug($prefix, '$prefix', __LINE__, __FILE__);
159 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
160 }
161
162 /**
163 * get and init the stored settings
164 *
165 * @return [type] ...
166 */
167 function initStoreArray() {
168 $storeArray=array(
169 '0' => ' '
170 );
171
172 $savedStoreArray = unserialize($GLOBALS['SOBE']->MOD_SETTINGS[$this->prefix.'StoreArray']);
173
174 if (is_array($savedStoreArray)) {
175 $storeArray = array_merge($storeArray,$savedStoreArray);
176 }
177 return $storeArray;
178 }
179
180 /**
181 * [Describe function...]
182 *
183 * @param [type] $storeConfigs: ...
184 * @param [type] $storeArray: ...
185 * @return [type] ...
186 */
187 function cleanStoreConfigs($storeConfigs,$storeArray) {
188 if (is_array($storeConfigs)) {
189 reset($storeConfigs);
190 while(list($k,$v)=each($storeConfigs)) {
191 if (!isset($storeArray[$k])) unset($storeConfigs[$k]);
192 }
193 }
194 return $storeConfigs;
195 }
196
197 /**
198 * [Describe function...]
199 *
200 * @param [type] $storeConfigs: ...
201 * @param [type] $index: ...
202 * @return [type] ...
203 */
204 function addToStoreConfigs($storeConfigs,$index) {
205 reset($this->storeList);
206 $storeConfigs[$index]=array();
207 foreach($this->storeList as $k) {
208 $storeConfigs[$index][$k]=$GLOBALS['SOBE']->MOD_SETTINGS[$k];
209 }
210 #debug($storeConfigs.'$storeConfigs', __LINE__, __FILE__);
211 return $storeConfigs;
212 }
213
214 /**
215 * [Describe function...]
216 *
217 * @param [type] $storeConfigs: ...
218 * @param [type] $storeIndex: ...
219 * @param [type] $writeArray: ...
220 * @return [type] ...
221 */
222 function loadStoreConfigs($storeConfigs,$storeIndex,$writeArray) {
223 if ($storeConfigs[$storeIndex]) {
224 foreach($this->storeList as $k) {
225 #debug($k,'key', __LINE__, __FILE__);
226 $writeArray[$k]=$storeConfigs[$storeIndex][$k];
227 }
228 }
229 return $writeArray;
230 }
231
232 /**
233 * [Describe function...]
234 *
235 * @param [type] $show: ...
236 * @return [type] ...
237 */
238 function getStoreControl($show='load,remove,save') {
239 // Load/Save
240 $show = t3lib_div::trimexplode(',',$show,1);
241 $storeArray = $this->initStoreArray();
242 $cur='';
243
244 // Store Array:
245 $opt=array();
246 reset($storeArray);
247 while(list($k,$v)=each($storeArray)) {
248 $opt[]='<option value="'.$k.'"'.(!strcmp($cur,$v)?" selected":"").'>'.htmlspecialchars($v).'</option>';
249 }
250
251 // Actions:
252 if (t3lib_extMgm::isLoaded('sys_action')) {
253 $query = 'SELECT * FROM sys_action WHERE type=2 ORDER BY title';
254 $res = mysql(TYPO3_db,$query);
255 if (mysql_num_rows($res)) {
256 $opt[]='<option value="0">__Save to Action:__</option>';
257 while($row=mysql_fetch_assoc($res)) {
258 $opt[]='<option value="-'.$row['uid'].'"'.(!strcmp($cur,"-".$row['uid'])?" selected":"").'>'.htmlspecialchars($row['title'].' ['.$row['uid'].']').'</option>';
259 }
260 }
261 }
262
263 $TDparams=' nowrap="nowrap" class="bgColor4"';
264 $tmpCode='
265 <table border=0 cellpadding=3 cellspacing=1 width="100%">
266 <tr'.$TDparams.'>
267 <td width="1%">Preset:</td>';
268
269 if(in_array('load',$show) OR in_array('remove',$show)) {
270 $tmpCode.='<td nowrap>';
271 $tmpCode.='<select name="storeControl[STORE]" onChange="document.forms[0][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';">'.implode(chr(10),$opt).'</select>';
272 if(in_array('load',$show)) {
273 $tmpCode.=' <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
274 }
275 if(in_array('remove',$show)) {
276 $tmpCode.=' <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
277 }
278 $tmpCode.='&nbsp;&nbsp;</td>';
279 }
280 if(in_array('save',$show)) {
281 $tmpCode.='<td nowrap><input name="storeControl[title]" value="" type="text" max=80 width="25"><input type="submit" name="storeControl[SAVE]" value="Save" onClick="if (document.forms[0][\'storeControl[STORE]\'].options[document.forms[0][\'storeControl[STORE]\'].selectedIndex].value<0) return confirm(\'Are you sure you want to overwrite the existing query in this action?\');" /></td>';
282 }
283 $tmpCode.='</tr>
284 </table>
285 ';
286 return $tmpCode;
287 }
288
289 /**
290 * [Describe function...]
291 *
292 * @param [type] $mconfName: ...
293 * @return [type] ...
294 */
295 function procesStoreControl($mconfName='') {
296 $storeArray = $this->initStoreArray();
297 $storeConfigs = unserialize($GLOBALS['SOBE']->MOD_SETTINGS[$this->prefix.'StoreConfigs']);
298 #debug($storeConfigs);
299 $storeControl = t3lib_div::_GP('storeControl');
300 $storeIndex = intval($storeControl['STORE']);
301 #debug($storeControl);
302 $saveStoreArray=0;
303 $writeArray=array();
304 if (is_array($storeControl)) {
305 $storeControl = t3lib_div::slashArray($storeControl,'strip');
306 if ($storeControl['LOAD']) {
307 if ($storeIndex>0) {
308 $writeArray=$this->loadStoreConfigs($storeConfigs,$storeIndex,$writeArray);
309 $saveStoreArray=1;
310 $msg="'".$storeArray[$storeIndex]."' preset loaded!";
311 } elseif ($storeIndex<0 && t3lib_extMgm::isLoaded('sys_action')) {
312 $actionRecord=t3lib_BEfunc::getRecord('sys_action',abs($storeIndex));
313 if (is_array($actionRecord)) {
314 $dA = unserialize($actionRecord['t2_data']);
315 $dbSC=array();
316 if (is_array($dA['qC'])) {
317 $dbSC[0] = $dA['qC'];
318 }
319 $writeArray=$this->loadStoreConfigs($dbSC,'0',$writeArray);
320 $saveStoreArray=1;
321 $acTitle=htmlspecialchars($actionRecord['title']);
322 $msg="Query from action '".$acTitle."' loaded!";
323 }
324 }
325
326 } elseif ($storeControl['SAVE']) {
327 if ($storeIndex<0) {
328 $qOK = $this->saveQueryInAction(abs($storeIndex));
329 if ($qOK) {
330 $msg='Preset OK and saved.';
331 } else {
332 $msg='No preset saved!';
333 }
334 } else {
335 if (trim($storeControl['title'])) {
336 if ($storeIndex>0) {
337 $storeArray[$storeIndex]=$storeControl['title'];
338 } else {
339 $storeArray[]=$storeControl['title'];
340 end($storeArray);
341 $storeIndex=key($storeArray);
342 }
343 $storeConfigs=$this->addToStoreConfigs($storeConfigs,$storeIndex);
344 $saveStoreArray=1;
345 $msg="'".$storeArray[$storeIndex]."' preset saved!";
346 }
347 }
348 } elseif ($storeControl['REMOVE']) {
349 if ($storeIndex>0) {
350 $msg="'".$storeArray[$storeControl['STORE']]."' preset entry removed!";
351 unset($storeArray[$storeControl['STORE']]); // Removing
352 $saveStoreArray=1;
353 }
354 }
355 }
356 if ($saveStoreArray) {
357 unset($storeArray[0]); // making sure, index 0 is not set!
358 $writeArray[$this->prefix.'StoreArray']=serialize($storeArray);
359 $writeArray[$this->prefix.'StoreConfigs']=serialize($this->cleanStoreConfigs($storeConfigs,$storeArray));
360 $GLOBALS['SOBE']->MOD_SETTINGS = t3lib_BEfunc::getModuleData($GLOBALS['SOBE']->MOD_MENU, $writeArray, ($mconfName?$mconfName:$GLOBALS['SOBE']->MCONF['name']), 'ses');
361
362 #debug($GLOBALS['SOBE']->MOD_MENU, '$GLOBALS[SOBE]->MOD_MENU', __LINE__, __FILE__);
363 #debug($storeArray, '$storeArray', __LINE__, __FILE__);
364 #debug($writeArray, '$writeArray', __LINE__, __FILE__);
365 #debug($GLOBALS['SOBE']->MOD_SETTINGS, 'store: $GLOBALS[SOBE]->MOD_SETTINGS', __LINE__, __FILE__);
366 }
367 return $msg;
368 }
369
370 /**
371 * [Describe function...]
372 *
373 * @param [type] $uid: ...
374 * @return [type] ...
375 */
376 function saveQueryInAction($uid) {
377 if (t3lib_extMgm::isLoaded('sys_action')) {
378 }
379 }
380 }
381
382 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']) {
383 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']);
384 }
385 ?>
386