class.t3lib_modsettings.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
00029  * Provides a presets box for BE modules.
00030  *
00031  * inspired by t3lib_fullsearch
00032  *
00033  * $Id: class.t3lib_modsettings.php 7905 2010-06-13 14:42:33Z ohader $
00034  *
00035  * @author  René Fritz <r.fritz@colorcube.de>
00036  */
00037 /**
00038  * [CLASS/FUNCTION INDEX of SCRIPT]
00039  *
00040  *
00041  *
00042  *  125: class t3lib_modSettings
00043  *
00044  *              SECTION: Init / setup
00045  *  181:     function init($prefix='', $storeList='')
00046  *  197:     function setSessionType($type='ses')
00047  *
00048  *              SECTION: Store list - which values should be stored
00049  *  218:     function setStoreList($storeList)
00050  *  231:     function addToStoreList($storeList)
00051  *  245:     function addToStoreListFromPrefix($prefix='')
00052  *
00053  *              SECTION: Process storage array
00054  *  279:     function initStorage()
00055  *  294:     function cleanupStorageArray($storedSettings)
00056  *  316:     function compileEntry($data)
00057  *  343:     function getStoredData($storeIndex, $writeArray=array())
00058  *  360:     function processStoreControl($mconfName='')
00059  *  442:     function writeStoredSetting($writeArray=array(), $mconfName='')
00060  *
00061  *              SECTION: GUI
00062  *  474:     function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE)
00063  *
00064  *              SECTION: Misc
00065  *  576:     function processEntry($storageArr)
00066  *
00067  * TOTAL FUNCTIONS: 13
00068  * (This index is automatically created/updated by the extension "extdeveval")
00069  *
00070  */
00071 
00072 
00073 
00074 
00075 /**
00076  * usage inside of scbase class
00077  *
00078  * ....
00079  *
00080  * $this->MOD_MENU = array(
00081  *  'function' => array(
00082  *      'xxx ...
00083  *  ),
00084  *  'tx_dam_select_storedSettings' => '',
00085  *
00086  * ....
00087  *
00088  * function main()  {
00089  *  // reStore settings
00090  * $store = t3lib_div::makeInstance('t3lib_modSettings');
00091  * $store->init('tx_dam_select');
00092  * $store->setStoreList('tx_dam_select');
00093  * $store->processStoreControl();
00094  *
00095  *  // show control panel
00096  * $this->content.= $this->doc->section('Settings',$store->getStoreControl(),0,1);
00097  *
00098  *
00099  *
00100  * Format of saved settings
00101  *
00102  *  $SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings'] = serialize(
00103  *      array (
00104  *          'any id' => array (
00105  *                  'title' => 'title for saved settings',
00106  *                  'desc' => 'descritpion text, not mandatory',
00107  *                  'data' => array(),  // data from MOD_SETTINGS
00108  *                  'user' => NULL, // can be used for extra data used by the application to identify this entry
00109  *                  'tstamp' => 12345, // $GLOBALS['EXEC_TIME']
00110  *              ),
00111  *          'another id' => ...
00112  *
00113  *          ) );
00114  *
00115  */
00116 
00117 /**
00118  * Manage storing and restoring of $GLOBALS['SOBE']->MOD_SETTINGS settings.
00119  * Provides a presets box for BE modules.
00120  *
00121  * @author  René Fritz <r.fritz@colorcube.de>
00122  * @package TYPO3
00123  * @subpackage t3lib
00124  */
00125 class t3lib_modSettings {
00126 
00127     /**
00128      * If type is set 'ses' then the module data will be stored into the session and will be lost with logout.
00129      * Type 'perm' will store the data permanently.
00130      */
00131     var $type = 'perm';
00132 
00133     /**
00134      * prefix of MOD_SETTING array keys that should be stored
00135      */
00136     var $prefix = '';
00137 
00138     /**
00139      * Names of keys of the MOD_SETTING array which should be stored
00140      */
00141     var $storeList = array();
00142 
00143     /**
00144      * The stored settings array
00145      */
00146     var $storedSettings = array();
00147 
00148     /**
00149      * Message from the last storage command
00150      */
00151     var $msg = '';
00152 
00153 
00154     /**
00155      * Name of the form. Needed for JS
00156      */
00157     var $formName = 'storeControl';
00158 
00159 
00160 
00161     var $writeDevLog = 0;               // write messages into the devlog?
00162 
00163 
00164 
00165 
00166     /********************************
00167      *
00168      * Init / setup
00169      *
00170      ********************************/
00171 
00172 
00173 
00174     /**
00175      * Initializes the object
00176      *
00177      * @param   string      Prefix of MOD_SETTING array keys that should be stored
00178      * @param   array       additional names of keys of the MOD_SETTING array which should be stored (array or comma list)
00179      * @return  void
00180      */
00181     function init($prefix='', $storeList='')    {
00182         $this->prefix = $prefix;
00183         $this->setStoreList($storeList);
00184         $this->type = 'perm';
00185 
00186             // enable dev logging if set
00187         if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) $this->writeDevLog = TRUE;
00188         if (TYPO3_DLOG) $this->writeDevLog = TRUE;
00189     }
00190 
00191     /**
00192      * Set session type to 'ses' which will store the settings data not permanently.
00193      *
00194      * @param   string      Default is 'ses'
00195      * @return  void
00196      */
00197     function setSessionType($type='ses')    {
00198         $this->type = $type;
00199     }
00200 
00201 
00202 
00203 
00204     /********************************
00205      *
00206      * Store list - which values should be stored
00207      *
00208      ********************************/
00209 
00210 
00211 
00212     /**
00213      * Set MOD_SETTINGS keys which should be stored
00214      *
00215      * @param   mixed       array or string (,) - set additional names of keys of the MOD_SETTING array which should be stored
00216      * @return  void
00217      */
00218     function setStoreList($storeList)   {
00219         $this->storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
00220 
00221         if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
00222     }
00223 
00224 
00225     /**
00226      * Add MOD_SETTINGS keys to the current list
00227      *
00228      * @param   mixed       array or string (,) - add names of keys of the MOD_SETTING array which should be stored
00229      * @return  void
00230      */
00231     function addToStoreList($storeList) {
00232         $storeList = is_array($storeList) ? $storeList : t3lib_div::trimExplode(',',$storeList,1);
00233         $this->storeList = array_merge($this->storeList, $storeList);
00234 
00235         if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
00236     }
00237 
00238 
00239     /**
00240      * Add names of keys of the MOD_SETTING array by a prefix
00241      *
00242      * @param   string      prefix of MOD_SETTING array keys that should be stored
00243      * @return  void
00244      */
00245     function addToStoreListFromPrefix($prefix='') {
00246         global $SOBE;
00247 
00248         $prefix = $prefix ? $prefix : $this->prefix;
00249 
00250         foreach ($SOBE->MOD_SETTINGS as $key => $value) {
00251             if (preg_match('/^'.$prefix.'/',$key)) {
00252                 $this->storeList[$key]=$key;
00253             }
00254         }
00255 
00256         unset($this->storeList[$this->prefix.'_storedSettings']);
00257 
00258         if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
00259     }
00260 
00261 
00262 
00263 
00264 
00265     /********************************
00266      *
00267      * Process storage array
00268      *
00269      ********************************/
00270 
00271 
00272 
00273     /**
00274      * Get the stored settings from MOD_SETTINGS and set them in $this->storedSettings
00275      *
00276      * @return  void
00277      */
00278     function initStorage()  {
00279         global $SOBE;
00280 
00281         $storedSettings = unserialize($SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings']);
00282         $this->storedSettings = $this->cleanupStorageArray($storedSettings);
00283     }
00284 
00285 
00286 
00287     /**
00288      * Remove corrupted data entries from the stored settings array
00289      *
00290      * @param   array       $storedSettings
00291      * @return  array       $storedSettings
00292      */
00293     function cleanupStorageArray($storedSettings)   {
00294 
00295         $storedSettings = is_array($storedSettings) ? $storedSettings : array();
00296 
00297             // clean up the array
00298         foreach($storedSettings as $id => $sdArr)   {
00299             if (!is_array($sdArr)) unset($storedSettings[$id]);
00300             if (!is_array($sdArr['data'])) unset($storedSettings[$id]);
00301             if (!trim($sdArr['title'])) $storedSettings[$id]['title'] = '[no title]';
00302         }
00303 
00304         return $storedSettings;
00305     }
00306 
00307 
00308     /**
00309      * Creates an entry for the stored settings array
00310      * Collects data from MOD_SETTINGS selected by the storeList
00311      *
00312      * @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
00313      * @return  array       $storageArr: entry for the stored settings array
00314      */
00315     function compileEntry($data)    {
00316         global $SOBE;
00317 
00318         $storageData = array();
00319         foreach($this->storeList as $MS_key)    {
00320             $storageData[$MS_key] = $SOBE->MOD_SETTINGS[$MS_key];
00321         }
00322         $storageArr = array (
00323                         'title' => $data['title'],
00324                         'desc' => (string)$data['desc'],
00325                         'data' => $storageData,
00326                         'user' => NULL,
00327                         'tstamp' => $GLOBALS['EXEC_TIME'],
00328                     );
00329         $storageArr = $this->processEntry($storageArr);
00330 
00331         return $storageArr;
00332     }
00333 
00334 
00335     /**
00336      * Copies the stored data from entry $index to $writeArray which can be used to set MOD_SETTINGS
00337      *
00338      * @param   mixed       The entry key
00339      * @param   array       Preset data array. Will be overwritten by copied values.
00340      * @return  array       Data array
00341      */
00342     function getStoredData($storeIndex, $writeArray=array())    {
00343         if ($this->storedSettings[$storeIndex]) {
00344             foreach($this->storeList as $k) {
00345                 $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k];
00346             }
00347         }
00348         return $writeArray;
00349     }
00350 
00351 
00352 
00353     /**
00354      * Processing of the storage command LOAD, SAVE, REMOVE
00355      *
00356      * @param   string      Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
00357      * @return  string      Storage message. Also set in $this->msg
00358      */
00359     function processStoreControl($mconfName='') {
00360 
00361         $this->initStorage();
00362 
00363         $storeControl = t3lib_div::_GP('storeControl');
00364         $storeIndex = $storeControl['STORE'];
00365 
00366         $msg = '';
00367         $saveSettings = FALSE;
00368         $writeArray = array();
00369 
00370         if (is_array($storeControl)) {
00371             if ($this->writeDevLog) {
00372                 t3lib_div::devLog('Store command: '.t3lib_div::arrayToLogString($storeControl), 't3lib_modSettings', 0);
00373             }
00374 
00375             //
00376             // processing LOAD
00377             //
00378 
00379             if ($storeControl['LOAD'] AND $storeIndex)  {
00380                     $writeArray = $this->getStoredData($storeIndex, $writeArray);
00381                     $saveSettings = TRUE;
00382                     $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset loaded!";
00383 
00384             //
00385             // processing SAVE
00386             //
00387 
00388             } elseif ($storeControl['SAVE'])    {
00389                 if (trim($storeControl['title'])) {
00390 
00391                         // get the data to store
00392                     $newEntry = $this->compileEntry($storeControl);
00393 
00394                         // create an index for the storage array
00395                     if (!$storeIndex) {
00396                         $storeIndex = t3lib_div::shortMD5($newEntry['title']);
00397                     }
00398 
00399                         // add data to the storage array
00400                     $this->storedSettings[$storeIndex] = $newEntry;
00401 
00402                     $saveSettings = TRUE;
00403                     $msg = "'".$newEntry['title']."' preset saved!";
00404 
00405                 } else {
00406                     $msg = 'Please enter a name for the preset!';
00407                 }
00408 
00409             //
00410             // processing REMOVE
00411             //
00412 
00413             } elseif ($storeControl['REMOVE'] AND $storeIndex)  {
00414                     // Removing entry
00415                 $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset entry removed!";
00416                 unset($this->storedSettings[$storeIndex]);
00417 
00418                 $saveSettings = TRUE;
00419             }
00420 
00421 
00422             $this->msg = $msg;
00423 
00424             if ($saveSettings)  {
00425                 $this->writeStoredSetting($writeArray, $mconfName);
00426             }
00427 
00428         }
00429         return $this->msg;
00430     }
00431 
00432 
00433     /**
00434      * Write the current storage array and update MOD_SETTINGS
00435      *
00436      * @param   array       Array of settings which should be overwrite current MOD_SETTINGS
00437      * @param   string      Name of the module to store the settings for. Default: $GLOBALS['SOBE']->MCONF['name'] (current module)
00438      * @return  void
00439      */
00440     function writeStoredSetting($writeArray=array(), $mconfName='') {
00441         global $SOBE;
00442 
00443             // for debugging: just removes all module data from user settings
00444         # $GLOBALS['BE_USER']->pushModuleData($SOBE->MCONF['name'],array());
00445 
00446         unset($this->storedSettings[0]);    // making sure, index 0 is not set!
00447         $this->storedSettings = $this->cleanupStorageArray($this->storedSettings);
00448         $writeArray[$this->prefix.'_storedSettings'] = serialize($this->storedSettings);
00449 
00450         $SOBE->MOD_SETTINGS = t3lib_BEfunc::getModuleData($SOBE->MOD_MENU, $writeArray, ($mconfName?$mconfName:$SOBE->MCONF['name']), $this->type);
00451 
00452         if ($this->writeDevLog) t3lib_div::devLog('Settings stored:'.$this->msg, 't3lib_modSettings', 0);
00453     }
00454 
00455 
00456 
00457     /********************************
00458      *
00459      * GUI
00460      *
00461      ********************************/
00462 
00463 
00464 
00465     /**
00466      * Returns the storage control box
00467      *
00468      * @param   string      List of elemetns which should be shown: load,remove,save
00469      * @param   boolean     If set the box is wrapped with own form tag
00470      * @return  string      HTML code
00471      */
00472     function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE)    {
00473         global $TYPO3_CONF_VARS;
00474 
00475         $showElements = t3lib_div::trimExplode(',', $showElements, 1);
00476 
00477         $this->initStorage();
00478 
00479             // Preset selector
00480         $opt=array();
00481         $opt[] = '<option value="0">   </option>';
00482         foreach($this->storedSettings as $id => $v) {
00483             $opt[] = '<option value="'.$id.'">'.htmlspecialchars($v['title']).'</option>';
00484         }
00485         $storedEntries = count($opt)>1;
00486 
00487 
00488 
00489         $codeTD = array();
00490 
00491 
00492             // LOAD, REMOVE, but also show selector so you can overwrite an entry with SAVE
00493         if($storedEntries AND (count($showElements))) {
00494 
00495                 // selector box
00496             $onChange = 'document.forms[\''.$this->formName.'\'][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';';
00497             $code = '
00498                     <select name="storeControl[STORE]" onChange="'.htmlspecialchars($onChange).'">
00499                     '.implode('
00500                         ', $opt).'
00501                     </select>';
00502 
00503                 // load button
00504             if(in_array('load', $showElements)) {
00505                     $code.= '
00506                     <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
00507             }
00508 
00509                 // remove button
00510             if(in_array('remove', $showElements)) {
00511                     $code.= '
00512                     <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
00513             }
00514             $codeTD[] = '<td width="1%">Preset:</td>';
00515             $codeTD[] = '<td nowrap="nowrap">'.$code.'&nbsp;&nbsp;</td>';
00516         }
00517 
00518 
00519             // SAVE
00520         if(in_array('save', $showElements)) {
00521             $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?\');';
00522             $code = '<input name="storeControl[title]" value="" type="text" max="80" width="25"> ';
00523             $code.= '<input type="submit" name="storeControl[SAVE]" value="Save" onClick="'.htmlspecialchars($onClick).'" />';
00524             $codeTD[] = '<td nowrap="nowrap">'.$code.'</td>';
00525         }
00526 
00527 
00528         $codeTD = implode ('
00529             ', $codeTD);
00530 
00531         if (trim($code)) {
00532             $code = '
00533             <!--
00534                 Store control
00535             -->
00536             <table border="0" cellpadding="3" cellspacing="0" width="100%">
00537                 <tr class="bgColor4">
00538                 '.$codeTD.'
00539                 </tr>
00540             </table>
00541             ';
00542         }
00543 
00544         if ($this->msg) {
00545             $code.= '
00546             <div><strong>'.htmlspecialchars($this->msg).'</strong></div>';
00547         }
00548 #TODO need to add parameters
00549         if ($useOwnForm AND trim($code)) {
00550             $code = '
00551         <form action="' . t3lib_div::getIndpEnv('SCRIPT_NAME') . '" method="post" name="' . $this->formName . '" enctype="' . $TYPO3_CONF_VARS['SYS']['form_enctype'] . '">' . $code . '</form>';
00552         }
00553 
00554         return $code;
00555     }
00556 
00557 
00558 
00559 
00560     /********************************
00561      *
00562      * Misc
00563      *
00564      ********************************/
00565 
00566 
00567     /**
00568      * Processing entry for the stored settings array
00569      * Can be overwritten by extended class
00570      *
00571      * @param   array       $storageData: entry for the stored settings array
00572      * @return  array       $storageData: entry for the stored settings array
00573      */
00574     function processEntry($storageArr) {
00575         return $storageArr;
00576     }
00577 }
00578 
00579 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php'])   {
00580     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']);
00581 }
00582 
00583 ?>

Generated on Sat Jul 24 04:17:17 2010 for TYPO3 API by  doxygen 1.4.7