<?php
/*
 *
 * @copyright (c) 2011 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */

include_once (ROOT_PATH. '/include/config/db.inc.php');
include_once (ROOT_PATH.'/include/sqlwrapper.inc.php');


$GLOBALS['definesInitialized'] = FALSE;
$GLOBALS['definesEditable'] = array();

function initializeDefines() {
	$sql = 'SELECT * from defines';
	$qry = mysql_query($sql);
	while($row = mysql_fetch_assoc($qry)) {
		if(!defined($row['key'])) {
			define($row['key'], $row['value']);
			$GLOBALS['definesEditable'][$row['key']] = $row['editable'];
		}
	}
	$GLOBALS['definesInitialized'] = TRUE;
}


function defineIfNotDefined($key, $value, $editable = TRUE) {
	if(!$GLOBALS['definesInitialized']) {
		initializeDefines();
	}

	if(!defined($key)) {
		// we seem that we need to insert this key - value pair!
		$sql = 'INSERT INTO defines(`key`, `value`, `editable`) values(\''.$key.'\', \''.$value.'\', \''.$editable.'\')';
		mysql_query($sql);
		define($key, $value);
	} else if($GLOBALS['definesEditable'][$key] != $editable){
		// if the editableness of this value has been changed, adjust the database ;)
		$sql = 'UPDATE defines SET editable = ' . ($editable?'1':'0') . ' WHERE `key` = \'' .$key.'\'';
//		echo $sql.'<br>';
		mysql_query($sql);
	}
}


function getDefines($prefix = NULL) {
	if(!$GLOBALS['definesInitialized']) {
		initializeDefines();
	}
	
	$appendix = NULL;
	// readout the database (where editable true)
	if($prefix === NULL) {
		$appendix = '1';
	} else {
		$appendix = 'key like \''.$prefix.'%\'';
	}
	
	$sql = 'SELECT * FROM defines WHERE editable = 1 AND ' . $appendix;
	$qry = db_query();
	
	if(!$qry) {
		return 'Fehler in SQL-Anweisung';
	}
	
	$result = array();
	while($row = mysql_fetch_assoc($qry)) {
		$result[] = $row;
	}
	
	return $result;
}

function setDefine($key, $value) {
	if(!$GLOBALS['definesInitialized']) {
		initializeDefines();
	}
	// set where editable true
	$sql = 'UPDATE defines SET `value` = \''.$value.'\' WHERE `key` = \''.$key.'\' AND editable = TRUE';
	$qry = db_query($sql);
	if(!$qry || mysql_affected_rows() == 0) {
		return 'Konnte den define ' . $key . ' nicht auf ' . $value . ' setzen';
	}
	return NULL;
}


?>