<?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!");

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

	$action = _ext("action", 2);
	$module = _ext("module", 2);
	$page = _ext("page", 3);
	$id = _ext("id", 3);


	switch ($action) {
		case "setup":

			$tpl = new ContentBlock();

			$tpl->insert ("BLOCK_CONTENT", "conf/setup.admin.tpl");

			Class modConf {
				var $items = Array();
				function set($name, $value = false) {

					if (is_array($name)) {
						foreach ($name as $n => $v) {
							$this->set($n, $v);
						}
					} else {
						($name); // <-- phpbug? some servers stop responding without this line
						$this->items[$name] = $value;
					}
					return true;
				}
				function items() {
					return $this->items;
				}
			}

			$modConf = new modConf;

			$modules = modCore::getModules();

			// I want conf to be listed first
			foreach ($modules as $k => $src) {
				if ($src == "conf") {
					unset($modules[$k]);
				}
			}
			$modules = array_merge(array("conf"), $modules);


			foreach ($modules as $mod) {
				$conf_file = GEKKO_ROOT."modules/$mod/mod_conf.inc.php";
				if (file_exists($conf_file)) {
					// language file and common conf options
					$Lang->Load($mod, "conf");
					//echo $conf_file;
					include $conf_file;
				}
			}

			$keylist = Array();
			$lmd = "";
			foreach ($modConf->items() as $key => $val) {
				list($mod, $key) = explode(":", $key);

				$db->query("SELECT ID, KeyValue, KeyType FROM ".db_table("conf")."
				WHERE Module = '{$mod}' AND KeyName = '{$key}'", __FILE__, __LINE__);

				if ($db->num_rows()) {
					$row = $db->fetch_row();
					saneHTML($row);

					if (trim($val)) {
						$row["KEYVALUE.INPUT"] = str_replace("%id", $row["ID"], $val);
					} else {
						switch ($row["KeyType"]) {
							case "2":
								$row["KEYVALUE.INPUT"] = '<input type="checkbox" name="key['.$row["ID"].']" value="1" '.($row["KeyValue"] ? "checked=\"checked\" ": "").'/> '._l("L_ENABLED");
							break;
							default:
								$row["KEYVALUE.INPUT"] = '<input class="text" type="text" name="key['.$row["ID"].']" value="'.$row["KeyValue"].'" maxlenght="255" size="18" />';
							break;
						}
					}

					$row["KeyType"] = getKeyTypeByID($row["KeyType"]);
					$row["Actions"] = "";
					$row["KeyName"] = _l("K_".strtoupper($key));
					$tpl->set_arr(
						$row,
						"KEY"
					);
					$tpl->saveBuff("KEY");

					$keylist[] = $row["ID"];
				} else {
					trigger_error("Key $mod/$key doesn't exists.", E_USER_ERROR);
				}
			}

			$tpl->set_arr (
				Array (
					"BLOCK_TITLE" => _l("L_CONF"),
					"BLOCK_SUBTITLE" => _l("L_SITE_SETUP"),
					"BLOCK_ICON" => mkIcon("setup.png", 16),
					"ACTION" => "edit",
					"MODULE" => "",
					"ID" => 0,
					"CONF.FORM" => "",
					"PAGER" => "",
					"RETURN" => "",
					"KEYS" => count($keylist),
					"KEYLIST" => implode(",", $keylist)
				)
			);

			$mcBuff = $tpl->make();

		break;
		case "edit":

			$tpl = new ContentBlock();

			$tpl->insert("BLOCK_CONTENT", "conf/index.admin.tpl");
			$tpl->insert("CONF.FORM", "conf/conf.form.tpl");

			$db->query ("
				SELECT
					ID, Module, KeyName, KeyValue, KeyType, Modified
				FROM ".db_table("conf")."
				WHERE ID = '$id'
			", __FILE__, __LINE__);

			if (!$db->num_rows()) {
				$mcBuff = _l("E_UNKNOWN_KEY");
				break;
			}

			$row = $db->fetch_row();
			saneHTML($row);

			$tpl->set_arr($row);

			$tpl->set_arr(
				Array (
					"BLOCK_TITLE" => _l("L_CONF"),
					"BLOCK_SUBTITLE" => _l("L_EDIT"),
					"BLOCK_ICON" => mkIcon("setup.png", 16),
					"KEYTYPE.SEL" => mkSelect(array(_l("L_STRING"), _l("L_INTEGER"), _l("L_BOOLEAN")), $row["KeyType"]),
					"ACTION" => "edit",
					"KEYS" => false,
					"RETURN" => "../../index.php/admin/conf"
				)
			);

			$mcBuff = $tpl->make();
		break;
		default:

			if (!$module) $module = "core";

			$tpl = new ContentBlock();

			$tpl->insert("BLOCK_CONTENT", "conf/index.admin.tpl");
			$tpl->insert("CONF.FORM", "conf/conf.form.tpl");

			$pager = $db->pager (
				"SELECT ID FROM ".db_table("conf")." WHERE Module = '$module'
				ORDER BY KeyName, KeyValue ASC",
				"index.php/admin/conf/$module/%p", $page, 50
			);

			$db->query ("
				SELECT
					ID, Module, KeyName, KeyValue, KeyType, Modified
				FROM ".db_table("conf")."
				WHERE Module = '$module'
				ORDER BY KeyName, KeyValue ASC ".$pager["sql"]."
			", __FILE__, __LINE__);

			$keys = $db->num_rows();
			$keylist = Array();
			while ($row = $db->fetch_row()) {
				saneHTML($row);

				$row["Actions"] = Array();
				$row["Actions"][] = mkLink("index.php/admin/conf/edit/".$row["ID"], _l("L_EDIT"));
				$row["Actions"][] = mkLink("modules/conf/main.act.php?action=delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), false, 1);
				$row["Actions"] = mkButtonDisplay($row["Actions"]);

				$row["KeyValue"] = fix_keyvalue($row["KeyValue"], $row["KeyType"]);

				switch ($row["KeyType"]) {
					case "2":
						$row["KEYVALUE.INPUT"] = '<input type="checkbox" name="key['.$row["ID"].']" value="1" '.($row["KeyValue"] ? "checked=\"checked\" ": "").'/>';
					break;
					default:
						$row["KEYVALUE.INPUT"] = '<input class="text" type="text" name="key['.$row["ID"].']" value="'.$row["KeyValue"].'" maxlenght="255" size="18" />';
					break;
				}

				$row["KeyType"] = getKeyTypeByID($row["KeyType"]);
				$tpl->set_arr(
					$row,
					"KEY"
				);
				$tpl->saveBuff("KEY");

				$keylist[] = $row["ID"];
			}

			# listing modules...
			$db->query ("SELECT Module FROM ".db_table("conf")." ORDER BY Module ASC");
			$modules = $db->num_rows();
			$modname = Array();
			while ($row = $db->fetch_row()) {
				if (!in_array($row["Module"], $modname) && $row["Module"] != $module) {
					$tpl->set_Arr (
						$row,
						"MODULE"
					);
					$tpl->saveBuff("MODULE");
					$modname[] = $row["Module"];
				}
			}

			$tpl->set_arr(
				Array (
					"BLOCK_TITLE" => _l("L_CONF"),
					"BLOCK_SUBTITLE" => _l("L_NEW_KEY"),
					"BLOCK_ICON" => mkIcon("setup.png", 16),
					"KEYTYPE.SEL" => mkSelect(array(_l("L_STRING"), _l("L_INTEGER"), _l("L_BOOLEAN")), 0),
					"ACTION" => "create",
					"KEYNAME" => "",
					"KEYVALUE" => "",
					"MODULE" => "$module",
					"MODULES" => $modules,
					"ID" => 0,
					"RETURN" => "",
					"PAGER" => $pager["html"],
					"KEYLIST" => implode(",", $keylist),
					"KEYS" => $keys
				)
			);

			$mcBuff = $tpl->make();
		break;
	}



?>