<?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
	*/

	define ("IN-GEKKO", true);

	require_once "../../conf.php";
	require_once GEKKO_ROOT."lib/core.lib.php";
	require_once GEKKO_ROOT."modules/conf/main.lib.php";

	db_init();

	require_once GEKKO_ROOT."lib/auth.inc.php";

	if (!gauth("conf")) abort(accessDenied());

	gpGet("action", 1);
	gpGet("id", 1);
	gpGet("return", 1);

	switch ($action) {
		case "delete":
			execWithEach($id, "deleteConfKey");
		break;
		case "quickedit":

			gpGlobalize("key,keylist");

			// keylist is required when checkboxes are off...
			requireVars("keylist");

			$keys = explode(",", $keylist);
			foreach ($keys as $id) {
				$value = isset($key[$id]) ? $key[$id] : false;
				$db->query("SELECT KeyType FROM ".db_table("conf")." WHERE ID='".$id."'");
				if ($db->num_rows()) {
					$row = $db->fetch_row();
					$db->query("UPDATE ".db_table("conf")." SET KeyValue='".fix_keyvalue($value, $row["KeyType"])."', Modified='".time()."' WHERE ID = '".$id."'");
				}
			}
		break;
		case "create":

			gpGlobalize("module,keytype,name,value");

			requireVars($module, $name);

			if (keyname_err($name)) {
				abort(_l("E_INVALID_KEYNAME"));
			}

			// checking for duplicated key name
			$db->query ("SELECT ID FROM ".db_table("conf")." WHERE KeyName = '".$name."' AND Module = '".$module."'", __FILE__, __LINE__);
			if ($db->num_rows()) {
				abort(_l("E_DUPLICATED_KEY"));
			}

			// inserting into database
			confSetKey($module, $name, $value, $keytype);

			slog("conf: created key '$name'", "actions", 1);

		break;
		case "edit":

			gpGlobalize("id, module, keytype, name, value");

			requireVars($id, $module, $name);

			// checking for duplicated key name
			$db->query ("SELECT ID FROM ".db_table("conf")." WHERE KeyName = '".$name."' AND Module = '".$module."' AND ID != '$id'", __FILE__, __LINE__);
			if ($db->num_rows()) {
				abort(_l("E_DUPLICATED_KEY"));
			}

			$db->query ("
				UPDATE ".db_table("conf")." SET
					Module = '$module',
					KeyName = '$name',
					KeyValue = '".fix_keyvalue($value, $keytype)."',
					KeyType = '$keytype',
					Modified = '".time()."'
				WHERE ID = '$id'
			", __FILE__, __LINE__);

			slog("conf: modified key '$name'", "actions", 1);
		break;
	}

	redirect($return ? $return : $_SERVER["HTTP_REFERER"]);

	db_exit();
?>