Location: PHPKode > projects > OpenNitro > trunk/Nitro/NitroSettings.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Settings                                                         |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003-2006 June Systems BV                                   |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Jesper Avot <hide@address.com>                             |
// +---------------------------------------------------------------------------+
//
// $Id: NitroSettings.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * This file contains the Nitro Settings class.
 * 
 * @author			Jesper Avot <hide@address.com>
 * @copyright		2006 June Systems BV
 * @package			Classes
 * @subpackage	Settings
 */
class NitroSettings {
	
	var $_ModuleSettings;
	
	function NitroSettings()
	{
	}
	
	function GetSettingsDefinition()
	{
		return array();
	}
	
	function GetDefaultSettings()
	{
		$ReturnValue = FALSE;
		$ModuleSettings = $this->GetSettingsDefinition();
		if (is_array($ModuleSettings)) {
			$ReturnValue = array();
			foreach ($ModuleSettings AS $ModuleVariable => $Vars) {
				if (strpos($ModuleVariable, '/')) {
					$Variable = explode("/", $ModuleVariable);
					$Arr = "";
					foreach ($Variable AS $var) $Arr.= "['" . $var . "']";
				} else {
					$Arr = "['" . $ModuleVariable . "']";
				}
				eval('$SetVariable =& $ReturnValue' . $Arr . ';');
				$SetVariable = $Vars["Default"];
				unset($SetVariable);
			}
		}
		return $ReturnValue;
	}
	
	function GetSettingsFromDefinition($ModuleSettings, $ignoreSession = FALSE)
	{
		$ReturnValue = FALSE;
		if ($ignoreSession !== TRUE) $ignoreSession = isset($this->ignoreSession) ? $this->ignoreSession : FALSE;
		if (is_array($ModuleSettings)) {
			$ReturnValue = array();
			foreach ($ModuleSettings AS $ModuleVariable => $Vars) {
				if (strpos($ModuleVariable, '/')) {
					$Variable = explode("/", $ModuleVariable);
					$Arr = "";
					foreach ($Variable AS $var) $Arr.= "['" . $var . "']";
				} else {
					$Arr = "['" . $ModuleVariable . "']";
				}
				eval('$SetVariable =& $ReturnValue' . $Arr . ';');
				
				if ($Vars["SessionVariable"] && ($ignoreSession !== TRUE || ($Vars['FormVariable'] !== TRUE && $Vars['Default'] !== TRUE))) {
					$tmpSession = GetSession($Vars["SessionVariable"]);
					
					if (isset($tmpSession)) {
						$SetVariable = $tmpSession;
					} elseif (isset($Vars["Default"])) {
						$SetVariable = $Vars["Default"];
					}
				} elseif ($Vars["FormVariable"] && isset($_REQUEST[$Vars["FormVariable"]])) {
					if (is_array($_REQUEST[$Vars["FormVariable"]])) {
						$Value = $_REQUEST[$Vars["FormVariable"]];
					} else {
						$Value = rawurldecode($_REQUEST[$Vars["FormVariable"]]);
						if (substr($Vars["FormVariable"], -1, 1) == '/') {
							$Value = str_replace("|", ",", $Value);
							$Value = array_unique(explode(",", $Value));
						}
					}
					$SetVariable = $Value;
				} else {
					$SetVariable = $Vars["Default"];
				}
				unset($SetVariable);
			}
		}
		return $ReturnValue;	
	}
	
	function GetSettings()
	{
		return $this->Settings;
	}
	
	function GetSetting($Setting)
	{
		$ReturnValue = FALSE;
		if (isset($this->Settings[$Setting])) $ReturnValue = $this->Settings[$Setting];
		return $ReturnValue;
	}
	
	function SetSettings($ModuleSettings)
	{
		if (!$this->_ModuleSettings) $this->GetSettingsDefinition();
		if (count($this->_ModuleSettings) == 0) { // Workaround for old modules that do not yet define settings in the module
			foreach ((array)$ModuleSettings AS $id => $value) {
				$this->SetSetting($id, $value);
			}
		} else {
			foreach ($this->_ModuleSettings AS $id => $settings) {
				if (strpos($id, '/')) {
					$Variable = explode('/', $id);
					$Arr = '';
					foreach ($Variable AS $var) $Arr.= "['" . $var . "']";
					eval('$value = $ModuleSettings' . $Arr . ';');
				} else {
					$value = $ModuleSettings[$id];
				}
	
				if (isset($value)) {
					$this->SetSetting($id, $value);
				} else {
					$this->SetSetting($id, $settings['Default']);
				}
			}
		}
		return TRUE;
	}
	
	function SetSetting($Setting, $Value)
	{
		if (!$this->_ModuleSettings) $this->GetSettingsDefinition();
		if (count($this->_ModuleSettings) == 0) { // Workaround for modules that do not yet define settings in the module
			$this->Settings[$Setting] = $Value;
		} elseif (isset($this->_ModuleSettings[$Setting]) && isset($Value)) {
			if (strpos($Setting, '/')) {
				$Variable = explode('/', $Setting);
				$Arr = '';
				foreach ($Variable AS $var) $Arr.= "['" . $var . "']";
				eval('$this->Settings' . $Arr . ' = $Value;');
			} else {
				$this->Settings[$Setting] = $Value;
			}
		}
		return TRUE;
	}
}
?>
Return current item: OpenNitro