Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_modsettings.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2004 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 ***************************************************************/
00125 class t3lib_modSettings {
00126 
00131    var $type = 'perm';
00132 
00136    var $prefix = '';
00137 
00141    var $storeList = array();
00142 
00146    var $storedSettings = array();
00147 
00151    var $msg = '';
00152 
00153 
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 
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 ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_modSettings.php']['writeDevLog']) $this->writeDevLog = TRUE;
00188       if (TYPO3_DLOG) $this->writeDevLog = TRUE;
00189    }
00190 
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 
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 
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 
00245    function addToStoreListFromPrefix($prefix='') {
00246       global $SOBE;
00247 
00248       $prefix = $prefix ? $prefix : $this->prefix;
00249 
00250       reset($SOBE->MOD_SETTINGS);
00251       while(list($key)=each($SOBE->MOD_SETTINGS))  {
00252          if (ereg('^'.$prefix,$key)) {
00253             $this->storeList[$key]=$key;
00254          }
00255       }
00256 
00257       unset($this->storeList[$this->prefix.'_storedSettings']);
00258 
00259       if ($this->writeDevLog) t3lib_div::devLog('Store list:'.implode(',',$this->storeList), 't3lib_modSettings', 0);
00260    }
00261 
00262 
00263 
00264 
00265 
00266    /********************************
00267     *
00268     * Process storage array
00269     *
00270     ********************************/
00271 
00272 
00273 
00279    function initStorage()  {
00280       global $SOBE;
00281 
00282       $storedSettings = unserialize($SOBE->MOD_SETTINGS[$this->prefix.'_storedSettings']);
00283       $this->storedSettings = $this->cleanupStorageArray($storedSettings);
00284    }
00285 
00286 
00287 
00294    function cleanupStorageArray($storedSettings)   {
00295 
00296       $storedSettings = is_array($storedSettings) ? $storedSettings : array();
00297 
00298          // clean up the array
00299       foreach($storedSettings as $id => $sdArr) {
00300          if (!is_array($sdArr)) unset($storedSettings[$id]);
00301          if (!is_array($sdArr['data'])) unset($storedSettings[$id]);
00302          if (!trim($sdArr['title']))   $storedSettings[$id]['title'] = '[no title]';
00303       }
00304 
00305       return $storedSettings;
00306    }
00307 
00308 
00316    function compileEntry($data)  {
00317       global $SOBE;
00318 
00319       $storageData = array();
00320       foreach($this->storeList as $MS_key)   {
00321          $storageData[$MS_key] = $SOBE->MOD_SETTINGS[$MS_key];
00322       }
00323       $storageArr = array (
00324                   'title' => $data['title'],
00325                   'desc' => (string)$data['desc'],
00326                   'data' => $storageData,
00327                   'user' => NULL,
00328                   'tstamp' => time(),
00329                );
00330       $storageArr = $this->processEntry($storageArr);
00331 
00332       return $storageArr;
00333    }
00334 
00335 
00343    function getStoredData($storeIndex, $writeArray=array()) {
00344       if ($this->storedSettings[$storeIndex])   {
00345          foreach($this->storeList as $k)  {
00346             $writeArray[$k] = $this->storedSettings[$storeIndex]['data'][$k];
00347          }
00348       }
00349       return $writeArray;
00350    }
00351 
00352 
00353 
00360    function processStoreControl($mconfName='')  {
00361 
00362       $this->initStorage();
00363 
00364       #debug($this->storedSettings, '$this->storedSettings', __LINE__, __FILE__);
00365 
00366       $storeControl = t3lib_div::_GP('storeControl');
00367       $storeIndex = $storeControl['STORE'];
00368 
00369       if ($this->writeDevLog) t3lib_div::devLog('Store command: '.t3lib_div::arrayToLogString($storeControl), 't3lib_modSettings', 0);
00370 
00371       $msg = '';
00372       $saveSettings = FALSE;
00373       $writeArray = array();
00374 
00375       if (is_array($storeControl)) {
00376 
00377          //
00378          // processing LOAD
00379          //
00380 
00381          if ($storeControl['LOAD'] AND $storeIndex)   {
00382                $writeArray = $this->getStoredData($storeIndex, $writeArray);
00383                $saveSettings = TRUE;
00384                $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset loaded!";
00385 
00386          //
00387          // processing SAVE
00388          //
00389 
00390          } elseif ($storeControl['SAVE']) {
00391             if (trim($storeControl['title'])) {
00392 
00393                   // get the data to store
00394                $newEntry = $this->compileEntry($storeControl);
00395 
00396                   // create an index for the storage array
00397                if (!$storeIndex) {
00398                   $storeIndex = t3lib_div::shortMD5($newEntry['title']);
00399                }
00400 
00401                   // add data to the storage array
00402                $this->storedSettings[$storeIndex] = $newEntry;
00403 
00404                $saveSettings = TRUE;
00405                $msg = "'".$newEntry['title']."' preset saved!";
00406 
00407             } else {
00408                $msg = 'Please enter a name for the preset!';
00409             }
00410 
00411          //
00412          // processing REMOVE
00413          //
00414 
00415          } elseif ($storeControl['REMOVE'] AND $storeIndex) {
00416                // Removing entry
00417             $msg = "'".$this->storedSettings[$storeIndex]['title']."' preset entry removed!";
00418             unset($this->storedSettings[$storeIndex]);
00419 
00420             $saveSettings = TRUE;
00421          }
00422 
00423 
00424          $this->msg = $msg;
00425 
00426          if ($saveSettings)   {
00427             $this->writeStoredSetting($writeArray, $mconfName);
00428          }
00429 
00430       }
00431       return $this->msg;
00432    }
00433 
00434 
00442    function writeStoredSetting($writeArray=array(), $mconfName='') {
00443       global $SOBE;
00444 
00445          // for debugging: just removes all module data from user settings
00446       # $GLOBALS['BE_USER']->pushModuleData($SOBE->MCONF['name'],array());
00447 
00448       unset($this->storedSettings[0]); // making sure, index 0 is not set!
00449       $this->storedSettings = $this->cleanupStorageArray($this->storedSettings);
00450       $writeArray[$this->prefix.'_storedSettings'] = serialize($this->storedSettings);
00451 
00452       $SOBE->MOD_SETTINGS = t3lib_BEfunc::getModuleData($SOBE->MOD_MENU, $writeArray, ($mconfName?$mconfName:$SOBE->MCONF['name']), $this->type);
00453 
00454       if ($this->writeDevLog) t3lib_div::devLog('Settings stored:'.$this->msg, 't3lib_modSettings', 0);
00455    }
00456 
00457 
00458 
00459    /********************************
00460     *
00461     * GUI
00462     *
00463     ********************************/
00464 
00465 
00466 
00474    function getStoreControl($showElements='load,remove,save', $useOwnForm=TRUE)  {
00475       global $TYPO3_CONF_VARS;
00476 
00477       $showElements = t3lib_div::trimExplode(',', $showElements, 1);
00478 
00479       $this->initStorage();
00480 
00481          // Preset selector
00482       $opt=array();
00483       $opt[] = '<option value="0">   </option>';
00484       foreach($this->storedSettings as $id => $v)  {
00485          $opt[] = '<option value="'.$id.'">'.htmlspecialchars($v['title']).'</option>';
00486       }
00487       $storedEntries = count($opt)>1;
00488 
00489 
00490 
00491       $codeTD = array();
00492 
00493 
00494          // LOAD, REMOVE, but also show selector so you can overwrite an entry with SAVE
00495       if($storedEntries AND (count($showElements))) {
00496 
00497             // selector box
00498          $onChange = 'document.forms[\''.$this->formName.'\'][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';';
00499          $code = '
00500                <select name="storeControl[STORE]" onChange="'.htmlspecialchars($onChange).'">
00501                '.implode('
00502                   ', $opt).'
00503                </select>';
00504 
00505             // load button
00506          if(in_array('load', $showElements)) {
00507                $code.= '
00508                <input type="submit" name="storeControl[LOAD]" value="Load" /> ';
00509          }
00510 
00511             // remove button
00512          if(in_array('remove', $showElements)) {
00513                $code.= '
00514                <input type="submit" name="storeControl[REMOVE]" value="Remove" /> ';
00515          }
00516          $codeTD[] = '<td width="1%">Preset:</td>';
00517          $codeTD[] = '<td nowrap="nowrap">'.$code.'&nbsp;&nbsp;</td>';
00518       }
00519 
00520 
00521          // SAVE
00522       if(in_array('save', $showElements)) {
00523          $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?\');';
00524          $code = '<input name="storeControl[title]" value="" type="text" max="80" width="25"> ';
00525          $code.= '<input type="submit" name="storeControl[SAVE]" value="Save" onClick="'.htmlspecialchars($onClick).'" />';
00526          $codeTD[] = '<td nowrap="nowrap">'.$code.'</td>';
00527       }
00528 
00529 
00530       $codeTD = implode ('
00531          ', $codeTD);
00532 
00533       if (trim($code)) {
00534          $code = '
00535          <!--
00536             Store control
00537          -->
00538          <table border="0" cellpadding="3" cellspacing="0" width="100%">
00539             <tr class="bgColor4">
00540             '.$codeTD.'
00541             </tr>
00542          </table>
00543          ';
00544       }
00545 
00546       if ($this->msg)   {
00547          $code.= '
00548          <div><strong>'.htmlspecialchars($this->msg).'</strong></div>';
00549       }
00550 #TODO need to add parameters
00551       if ($useOwnForm AND trim($code)) {
00552          $code = '
00553       <form action="'.t3lib_div::getIndpEnv('SCRIPT_NAME').'" method="POST" name="'.$this->formName.'" enctype="'.$TYPO3_CONF_VARS['SYS']['form_enctype'].'">'.$code.'</form>';
00554       }
00555 
00556       return $code;
00557    }
00558 
00559 
00560 
00561 
00562    /********************************
00563     *
00564     * Misc
00565     *
00566     ********************************/
00567 
00568 
00576    function processEntry($storageArr) {
00577       return $storageArr;
00578    }
00579 }
00580 
00581 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php'])   {
00582    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_modSettings.php']);
00583 }
00584 ?>

Generated on Sun Oct 3 01:05:48 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913