TYPO3 CMS  TYPO3_6-2
ModuleSettings.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend;
3 
18 
63 
70  public $type = 'perm';
71 
77  public $prefix = '';
78 
84  public $storeList = array();
85 
91  public $storedSettings = array();
92 
98  public $msg = '';
99 
105  public $formName = 'storeControl';
106 
107  // Write messages into the devlog?
111  public $writeDevLog = 0;
112 
113  /********************************
114  *
115  * Init / setup
116  *
117  ********************************/
126  public function init($prefix = '', $storeList = '') {
127  $this->prefix = $prefix;
128  $this->setStoreList($storeList);
129  $this->type = 'perm';
130  // Enable dev logging if set
131  if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) {
132  $this->writeDevLog = TRUE;
133  }
134  if (TYPO3_DLOG) {
135  $this->writeDevLog = TRUE;
136  }
137  }
138 
146  public function setSessionType($type = 'ses') {
147  $this->type = $type;
148  }
149 
150  /********************************
151  *
152  * Store list - which values should be stored
153  *
154  ********************************/
162  public function setStoreList($storeList) {
163  $this->storeList = is_array($storeList) ? $storeList : GeneralUtility::trimExplode(',', $storeList, TRUE);
164  if ($this->writeDevLog) {
165  GeneralUtility::devLog('Store list:' . implode(',', $this->storeList), 'TYPO3\\CMS\\Backend\\ModuleSettings', 0);
166  }
167  }
168 
176  public function addToStoreList($storeList) {
178  $this->storeList = array_merge($this->storeList, $storeList);
179  if ($this->writeDevLog) {
180  GeneralUtility::devLog('Store list:' . implode(',', $this->storeList), 'TYPO3\\CMS\\Backend\\ModuleSettings', 0);
181  }
182  }
183 
191  public function addToStoreListFromPrefix($prefix = '') {
193  $prefix = preg_quote($prefix, '/');
194  foreach ($GLOBALS['SOBE']->MOD_SETTINGS as $key => $value) {
195  if (preg_match('/^' . $prefix . '/', $key)) {
196  $this->storeList[$key] = $key;
197  }
198  }
199  unset($this->storeList[$this->prefix . '_storedSettings']);
200  if ($this->writeDevLog) {
201  GeneralUtility::devLog('Store list:' . implode(',', $this->storeList), 'TYPO3\\CMS\\Backend\\ModuleSettings', 0);
202  }
203  }
204 
205  /********************************
206  *
207  * Process storage array
208  *
209  ********************************/
216  public function initStorage() {
217  $storedSettings = unserialize($GLOBALS['SOBE']->MOD_SETTINGS[$this->prefix . '_storedSettings']);
218  $this->storedSettings = $this->cleanupStorageArray($storedSettings);
219  }
220 
229  $storedSettings = is_array($storedSettings) ? $storedSettings : array();
230  // Clean up the array
231  foreach ($storedSettings as $id => $sdArr) {
232  if (!is_array($sdArr)) {
233  unset($storedSettings[$id]);
234  }
235  if (!is_array($sdArr['data'])) {
236  unset($storedSettings[$id]);
237  }
238  if (!trim($sdArr['title'])) {
239  $storedSettings[$id]['title'] = '[no title]';
240  }
241  }
242  return $storedSettings;
243  }
244 
253  public function compileEntry($data) {
254  $storageData = array();
255  foreach ($this->storeList as $MS_key) {
256  $storageData[$MS_key] = $GLOBALS['SOBE']->MOD_SETTINGS[$MS_key];
257  }
258  $storageArr = array(
259  'title' => $data['title'],
260  'desc' => (string) $data['desc'],
261  'data' => $storageData,
262  'user' => NULL,
263  'tstamp' => $GLOBALS['EXEC_TIME']
264  );
265  $storageArr = $this->processEntry($storageArr);
266  return $storageArr;
267  }
268 
277  public function getStoredData($storeIndex, $writeArray = array()) {
278  if ($this->storedSettings[$storeIndex]) {
279  foreach ($this->storeList as $k) {
280  $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k];
281  }
282  }
283  return $writeArray;
284  }
285 
293  public function processStoreControl($mconfName = '') {
294  $this->initStorage();
295  $storeControl = GeneralUtility::_GP('storeControl');
296  $storeIndex = $storeControl['STORE'];
297  $msg = '';
298  $saveSettings = FALSE;
299  $writeArray = array();
300  if (is_array($storeControl)) {
301  if ($this->writeDevLog) {
302  GeneralUtility::devLog('Store command: ' . GeneralUtility::arrayToLogString($storeControl), 'TYPO3\\CMS\\Backend\\ModuleSettings', 0);
303  }
304  // Processing LOAD
305  if ($storeControl['LOAD'] and $storeIndex) {
306  $writeArray = $this->getStoredData($storeIndex, $writeArray);
307  $saveSettings = TRUE;
308  $msg = '\'' . $this->storedSettings[$storeIndex]['title'] . '\' preset loaded!';
309  } elseif ($storeControl['SAVE']) {
310  if (trim($storeControl['title'])) {
311  // Get the data to store
312  $newEntry = $this->compileEntry($storeControl);
313  // Create an index for the storage array
314  if (!$storeIndex) {
315  $storeIndex = GeneralUtility::shortMD5($newEntry['title']);
316  }
317  // Add data to the storage array
318  $this->storedSettings[$storeIndex] = $newEntry;
319  $saveSettings = TRUE;
320  $msg = '\'' . $newEntry['title'] . '\' preset saved!';
321  } else {
322  $msg = 'Please enter a name for the preset!';
323  }
324  } elseif ($storeControl['REMOVE'] and $storeIndex) {
325  // Removing entry
326  $msg = '\'' . $this->storedSettings[$storeIndex]['title'] . '\' preset entry removed!';
327  unset($this->storedSettings[$storeIndex]);
328  $saveSettings = TRUE;
329  }
330  $this->msg = $msg;
331  if ($saveSettings) {
332  $this->writeStoredSetting($writeArray, $mconfName);
333  }
334  }
335  return $this->msg;
336  }
337 
346  public function writeStoredSetting($writeArray = array(), $mconfName = '') {
347  // Making sure, index 0 is not set
348  unset($this->storedSettings[0]);
349  $this->storedSettings = $this->cleanupStorageArray($this->storedSettings);
350  $writeArray[$this->prefix . '_storedSettings'] = serialize($this->storedSettings);
351  $GLOBALS['SOBE']->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData($GLOBALS['SOBE']->MOD_MENU, $writeArray, $mconfName ? $mconfName : $GLOBALS['SOBE']->MCONF['name'], $this->type);
352  if ($this->writeDevLog) {
353  GeneralUtility::devLog('Settings stored:' . $this->msg, 'TYPO3\\CMS\\Backend\\ModuleSettings', 0);
354  }
355  }
356 
357  /********************************
358  *
359  * GUI
360  *
361  ********************************/
370  public function getStoreControl($showElements = 'load,remove,save', $useOwnForm = TRUE) {
371  $showElements = GeneralUtility::trimExplode(',', $showElements, TRUE);
372  $this->initStorage();
373  // Preset selector
374  $opt = array();
375  $opt[] = '<option value="0"> </option>';
376  foreach ($this->storedSettings as $id => $v) {
377  $opt[] = '<option value="' . $id . '">' . htmlspecialchars($v['title']) . '</option>';
378  }
379  $storedEntries = count($opt) > 1;
380  $codeTD = array();
381  // LOAD, REMOVE, but also show selector so you can overwrite an entry with SAVE
382  if ($storedEntries and count($showElements)) {
383  // Selector box
384  $onChange = 'document.forms[\'' . $this->formName . '\'][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';';
385  $code = '
386  <select name="storeControl[STORE]" onChange="' . htmlspecialchars($onChange) . '">
387  ' . implode('
388  ', $opt) . '
389  </select>';
390  // Load button
391  if (in_array('load', $showElements)) {
392  $code .= '
393  <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
394  }
395  // Remove button
396  if (in_array('remove', $showElements)) {
397  $code .= '
398  <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
399  }
400  $codeTD[] = '<td width="1%">Preset:</td>';
401  $codeTD[] = '<td nowrap="nowrap">' . $code . '&nbsp;&nbsp;</td>';
402  }
403  // SAVE
404  if (in_array('save', $showElements)) {
405  $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?\');';
406  $code = '<input name="storeControl[title]" value="" type="text" max="80" width="25"> ';
407  $code .= '<input type="submit" name="storeControl[SAVE]" value="Save" onClick="' . htmlspecialchars($onClick) . '" />';
408  $codeTD[] = '<td nowrap="nowrap">' . $code . '</td>';
409  }
410  $codeTD = implode('
411  ', $codeTD);
412  if (trim($code)) {
413  $code = '
414  <!--
415  Store control
416  -->
417  <table border="0" cellpadding="3" cellspacing="0" width="100%">
418  <tr class="bgColor4">
419  ' . $codeTD . '
420  </tr>
421  </table>
422  ';
423  }
424  if ($this->msg) {
425  $code .= '
426  <div><strong>' . htmlspecialchars($this->msg) . '</strong></div>';
427  }
428  // TODO need to add parameters
429  if ($useOwnForm and trim($code)) {
430  $code = '
431  <form action="' . GeneralUtility::getIndpEnv('SCRIPT_NAME') . '" method="post" name="' . $this->formName . '" enctype="' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'] . '">' . $code . '</form>';
432  }
433  return $code;
434  }
435 
436  /********************************
437  *
438  * Misc
439  *
440  ********************************/
449  public function processEntry($storageArr) {
450  return $storageArr;
451  }
452 
453 }
init($prefix='', $storeList='')
getStoredData($storeIndex, $writeArray=array())
static devLog($msg, $extKey, $severity=0, $dataVar=FALSE)
static arrayToLogString(array $arr, $valueList=array(), $valueLength=20)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]