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