<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	Class Conf {
		/*
			loadConf(String Module);
			Loads configuration for the assigned module
		*/
		function Conf() {
			return true;
		}
		function Load($module = "core") {
			if (!isset($GLOBALS["conf"][$module])) {
				$q = $GLOBALS["db"]->query("SELECT KeyName, KeyValue FROM ".db_table("conf")." WHERE Module = '$module'", __FILE__, __LINE__, true);
				while ($row = $GLOBALS["db"]->fetch_row($q)) {
					$GLOBALS["conf"][$module][$row["KeyName"]] = $row["KeyValue"];
				}
			}
			return true;
		}
		function delKey($module, $key) {
			return $GLOBALS["db"]->query("DELETE FROM ".db_table("conf")." WHERE Module = '$module'".(($key != "*") ? "" : " AND Keyname = '$key'"), __FILE__, __LINE__, true);
		}
		function setKey($module, $key, $value = null, $type = "s") {
			switch (strtolower(substr($type, 0, 1))) {
				case "i": $type = 1; break;
				case "b": $type = 2; break;
				default: $type = 0; break;
			}
			$q = $GLOBALS["db"]->query("SELECT ID FROM ".db_table("conf")." WHERE Module = '$module' AND KeyName= '$key'", __FILE__, __LINE__, true);
			if ($GLOBALS["db"]->num_rows($q)) {
				$row = $GLOBALS["db"]->fetch_row($q);
				$GLOBALS["db"]->query("UPDATE ".db_table("conf")." SET KeyValue = '".($type ? fix_keyvalue($value, $type) : $value)."', Modified='".time()."' WHERE Module = '$module' AND KeyName= '$key'", __FILE__, __LINE__, true);
			} else {
				$GLOBALS["db"]->query("INSERT INTO ".db_table("conf")." (ID, Module, KeyName, KeyValue, KeyType) VALUES('".($GLOBALS["db"]->free_id("conf"))."','$module', '$key', '".fix_keyvalue($value, $type)."', '$type')", __FILE__, __LINE__, true);
			}
		}
		function getKey($module, $key, $default_value = false, $type = false) {
			if (!isset($GLOBALS["CONF"][$module][$key])) {
				$q = $GLOBALS["db"]->query("SELECT KeyValue FROM ".db_table("conf")." WHERE Module = '$module' AND KeyName = '$key'", __FILE__, __LINE__, true);
				if ($GLOBALS["db"]->num_rows($q)) {
					$row = $GLOBALS["db"]->fetch_row($q);
					$GLOBALS["CONF"][$module][$key] = $row["KeyValue"];
				} else {
					$GLOBALS["CONF"][$module][$key] = $default_value;
					// adding key if it doesn't exists
					if ($default_value !== false) {
						Conf::setKey($module, $key, $default_value, ($type) ? $type : (is_int($default_value) ? "int" : "string"));
					}
				}
			}
			return $GLOBALS["CONF"][$module][$key];
		}
	}

	// alias
	function _c($module, $key, $default_value = false) {
		return Conf::getKey($module, $key, $default_value);
	}
	function gconf($string) {
		$string = explode(":", $string);
		return Conf::getKey($string[0], $string[1]);
	}
	function fix_keyvalue($value, $type = 0) {
		switch ($type) {
			case "0":
				return "$value";
			break;
			case "1":
				return intval ($value);
			break;
			case "2":
				return ($value) ? true : false;
			break;
		}
	}

?>