*** empty log message ***
[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 * This class is in pre-beta state!!
32 *
33 * $Id$
34 *
35 * @author Kasper Skaarhoj <kasper@typo3.com>
36 * @coauthor René Fritz <r.fritz@colorcube.de>
37 */
38 /**
39 * [CLASS/FUNCTION INDEX of SCRIPT]
40 *
41 *
42 *
43 * 104: class t3lib_modSettings
44 * 114: function init($prefix='',$storeList='')
45 * 123: function setStoreList($storeList)
46 * 132: function addToStoreList($storeList)
47 * 144: function addToStoreListFromPrefix ($prefix='')
48 * 167: function initStoreArray()
49 * 187: function cleanStoreConfigs($storeConfigs,$storeArray)
50 * 204: function addToStoreConfigs($storeConfigs,$index)
51 * 222: function loadStoreConfigs($storeConfigs,$storeIndex,$writeArray)
52 * 238: function getStoreControl($show='load,remove,save')
53 * 294: function procesStoreControl($mconfName='')
54 * 374: function saveQueryInAction($uid)
55 *
56 * TOTAL FUNCTIONS: 11
57 * (This index is automatically created/updated by the extension "extdeveval")
58 *
59 */
60
61
62
63
64
65
66
67
68 /**
69 * usage inside of scbase class
70 *
71 * ....
72 *
73 * $this->MOD_MENU = array(
74 * 'function' => array(
75 * 'xxx ...
76 * ),
77 * 'tx_dam_selectStoreArray' => '',
78 * 'tx_dam_selectStoreConfigs' => '',
79 *
80 * ....
81 *
82 *
83 * function main() {
84 * // reStore settings
85 * $store = t3lib_div::makeInstance('t3lib_modSettings');
86 * $store->init('tx_dam_select');
87 * $store->addToStoreListFromPrefix('tx_dam_select');
88 * $storeMsg=$store->procesStoreControl();
89 *
90 * // show control panel
91 * $this->content.= $this->doc->section('store',$store->makeStoreControl(),0,1);
92 * if ($storeMsg) {
93 * $this->content.= $this->doc->section('','<strong>'.$storeMsg.'</strong>');
94 * }
95 */
96
97 /**
98 * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
99 * Provides a presets box for BE modules.
100 *
101 * @author Kasper Skaarhoj <kasper@typo3.com>
102 * @coauthor René Fritz <r.fritz@colorcube.de>
103 * @package TYPO3
104 * @subpackage t3lib
105 */
106 class t3lib_modSettings {
107
108 var $prefix='';
109 var $storeList=array();
110
111 /**
112 * @param string prefix of MOD_SETTING array keys that should be stored
113 * @param array additional names of keys of the MOD_SETTING array which should be stored
114 * @return [type] ...
115 */
116 function init($prefix='',$storeList='') {
117 $this->prefix = $prefix;
118 $this->setStoreList($storeList);
119 }
120
121 /**
122 * @param mixed array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored
123 * @return [type] ...
124 */
125 function setStoreList($storeList) {
126 $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
127 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
128 }
129
130 /**
131 * @param mixed array or string (,) - add names of keys of the MOD_SETTING array which should be stored
132 * @return [type] ...
133 */
134 function addToStoreList($storeList) {
135 $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
136 $this->storeList = array_merge($this->storeList, $storeList);
137 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
138 }
139
140 /**
141 * add names of keys of the MOD_SETTING array which should be stored by a prefix
142 *
143 * @param string prefix of MOD_SETTING array keys that should be stored
144 * @return [type] ...
145 */
146 function addToStoreListFromPrefix ($prefix='') {
147 $prefix = $prefix ? $prefix : $this->prefix;
148
149 reset($GLOBALS['SOBE']->MOD_SETTINGS);
150 while(list($key)=each($GLOBALS['SOBE']->MOD_SETTINGS)) {
151 if (ereg('^'.$prefix,$key)) {
152 $this->storeList[$key]=$key;
153 }
154 }
155
156 unset($this->storeList[$this->prefix.'StoreArray']);
157 unset($this->storeList[$this->prefix.'StoreConfigs']);
158
159 #debug($GLOBALS['SOBE']->MOD_SETTINGS, 'store: $GLOBALS[SOBE]->MOD_SETTINGS', __LINE__, __FILE__);
160 #debug($prefix, '$prefix', __LINE__, __FILE__);
161 #debug($this->storeList, '$this->storeList', __LINE__, __FILE__);
162 }
163
164 /**
165 * get and init the stored settings
166 *
167 * @return [type] ...
168 */
169 function initStoreArray() {
170 $storeArray=array(
171 '0' => ' '
172 );
173
174 $savedStoreArray = unserialize($GLOBALS['SOBE']->MOD_SETTINGS[$this->prefix.'StoreArray']);
175
176 if (is_array($savedStoreArray)) {
177 $storeArray = array_merge($storeArray,$savedStoreArray);
178 }
179 return $storeArray;
180 }
181
182 /**
183 * [Describe function...]
184 *
185 * @param [type] $storeConfigs: ...
186 * @param [type] $storeArray: ...
187 * @return [type] ...
188 */
189 function cleanStoreConfigs($storeConfigs,$storeArray) {
190 if (is_array($storeConfigs)) {
191 reset($storeConfigs);
192 while(list($k,$v)=each($storeConfigs)) {
193 if (!isset($storeArray[$k])) unset($storeConfigs[$k]);
194 }
195 }
196 return $storeConfigs;
197 }
198
199 /**
200 * [Describe function...]
201 *
202 * @param [type] $storeConfigs: ...
203 * @param [type] $index: ...
204 * @return [type] ...
205 */
206 function addToStoreConfigs($storeConfigs,$index) {
207 reset($this->storeList);
208 $storeConfigs[$index]=array();
209 foreach($this->storeList as $k) {
210 $storeConfigs[$index][$k]=$GLOBALS['SOBE']->MOD_SETTINGS[$k];
211 }
212 #debug($storeConfigs.'$storeConfigs', __LINE__, __FILE__);
213 return $storeConfigs;
214 }
215
216 /**
217 * [Describe function...]
218 *
219 * @param [type] $storeConfigs: ...
220 * @param [type] $storeIndex: ...
221 * @param [type] $writeArray: ...
222 * @return [type] ...
223 */
224 function loadStoreConfigs($storeConfigs,$storeIndex,$writeArray) {
225 if ($storeConfigs[$storeIndex]) {
226 foreach($this->storeList as $k) {
227 #debug($k,'key', __LINE__, __FILE__);
228 $writeArray[$k]=$storeConfigs[$storeIndex][$k];
229 }
230 }
231 return $writeArray;
232 }
233
234 /**
235 * [Describe function...]
236 *
237 * @param [type] $show: ...
238 * @return [type] ...
239 */
240 function getStoreControl($show='load,remove,save') {
241 // Load/Save
242 $show = t3lib_div::trimexplode(',',$show,1);
243 $storeArray = $this->initStoreArray();
244 $cur='';
245
246 // Store Array:
247 $opt=array();
248 reset($storeArray);
249 while(list($k,$v)=each($storeArray)) {
250 $opt[]='<option value="'.$k.'"'.(!strcmp($cur,$v)?" selected":"").'>'.htmlspecialchars($v).'</option>';
251 }
252
253 // Actions:
254 if (t3lib_extMgm::isLoaded('sys_action')) {
255 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', 'type=2', '', 'title');
256 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
257 $opt[]='<option value="0">__Save to Action:__</option>';
258 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
259 $opt[]='<option value="-'.$row['uid'].'"'.(!strcmp($cur,"-".$row['uid'])?" selected":"").'>'.htmlspecialchars($row['title'].' ['.$row['uid'].']').'</option>';
260 }
261 }
262 }
263
264 $TDparams=' nowrap="nowrap" class="bgColor4"';
265 $tmpCode='
266 <table border=0 cellpadding=3 cellspacing=1 width="100%">
267 <tr'.$TDparams.'>
268 <td width="1%">Preset:</td>';
269
270 if(in_array('load',$show) OR in_array('remove',$show)) {
271 $tmpCode.='<td nowrap>';
272 $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>';
273 if(in_array('load',$show)) {
274 $tmpCode.=' <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
275 }
276 if(in_array('remove',$show)) {
277 $tmpCode.=' <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
278 }
279 $tmpCode.='&nbsp;&nbsp;</td>';
280 }
281 if(in_array('save',$show)) {
282 $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>';
283 }
284 $tmpCode.='</tr>
285 </table>
286 ';
287 return $tmpCode;
288 }
289
290 /**
291 * [Describe function...]
292 *
293 * @param [type] $mconfName: ...
294 * @return [type] ...
295 */
296 function procesStoreControl($mconfName='') {
297 $storeArray = $this->initStoreArray();
298 $storeConfigs = unserialize($GLOBALS['SOBE']->MOD_SETTINGS[$this->prefix.'StoreConfigs']);
299 #debug($storeConfigs);
300 $storeControl = t3lib_div::_GP('storeControl');
301 $storeIndex = intval($storeControl['STORE']);
302 #debug($storeControl);
303 $saveStoreArray=0;
304 $writeArray=array();
305 if (is_array($storeControl)) {
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 ?>