<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, 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.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

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

	LoadLib (
		"conf"
	);

	jscriptLoad("modules/conf/main.js");

	Extract (
		URLGetArgs("null/null/action=alpha/package=alpha?id=int&page=page&option=alpha")
	);

	switch ($action) {
		case "clean_cache":
			cacheClean(GEKKO_CACHEDIR, 0);
			redirect("index.php/module=admin");
			shutdown();
		break;
		case "setup":

			$tpl = new ContentBlock();

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

			// this class will store module configuration variables
			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); // <-- php bug? I don't know why... but some servers stop responding without this line
						$this->items[$name] = $value;
					}
					return true;
				}
				function items() {
					return $this->items;
				}
				function clean() {
					$this->items = Array();
				}
			}

			$modConf = new modConf();

			$modules = gekkoModule::getList();

			// I want "conf" module to be listed as the first
			foreach ($modules as $i => $mod) {
				if ($mod == "conf")
					unset($modules[$i]);
			}
			array_unshift($modules, "conf");

			$keylist = Array();

			foreach ($modules as $mod) {

				// including mod_conf.inc.php, please see this module's mod_conf.inc.php
				// for a commented example.
				$conf_file = GEKKO_SOURCE_DIR."modules/$mod/mod_conf.inc.php";
				if (file_exists($conf_file)) {

					$tpl->reset("MODULE.KEY");

					// cleaning old keys
					$modConf->clean();

					// loading configuration language file (conf. key descriptions)
					$Lang->loadFromModule($mod, "conf");

					// fetching most important configuration keys
					include $conf_file;

					// listing keys specified by included file
					foreach ($modConf->items() as $key => $keyinput) {

						list($module, $keyname) = explode(":", $key);

						// querying stored value for specified key
						$q = $db->query("SELECT id, keyvalue, keytype FROM ".db_table("conf")."
						WHERE locked='0' AND module = '{$module}' AND keyname = '{$keyname}'",
						__FILE__, __LINE__);

						if ($db->numRows($q)) {

							$row = $db->fetchRow($q);

							saneHTML($row);

							if ($keyinput) // custom key input
								$row["KEYVALUE.INPUT"] = str_replace("%id", $row["id"], $keyinput);

							// key description
							$row["keyname"] = $keyname;
							$row["keydesc"] = _l("K_".strtoupper($keyname));

							$tpl->setArray (
								$row,
								"MODULE.KEY"
							);

							$tpl->saveBlock("MODULE.KEY");

							$keylist[] = $row["id"];
						} else {
							// fatal error, this is developer's fault! blame him! :P
							trigger_error("Key $module/$keyname doesn't exists.", E_USER_ERROR	);
						}
					}

					$tpl->setArray (
						Array (
							"MODULE"		=> $module,
							"MODULE_NAME"	=> _l("L_MODULE_".strtoupper($module))
						)
					, "MODULE");

					$tpl->saveBlock("MODULE");
				}
			}

			$tpl->setArray (
				Array (
					"BLOCK_TITLE"	=> _l("L_BASIC_SETUP"),
					"BLOCK_ICON"	=> mkIcon("setup.png", 16),
					"ACTION"		=> "edit",
					"MODULE"		=> "",
					"ID"			=> 0,
					"RETURN"		=> "",
					"KEYLIST"		=> implode(",", $keylist)
				)
			);

			$mcBuff = $tpl->make();
		break;
		case "edit_key":

			$tpl = new ContentBlock();

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

			$options = new optionBox($tpl, $option);
				$options->add (null, "index.php/module=admin/base=conf", "{L_LIST}", "conf.png");
				$options->add (null, "index.php/module=admin/base=conf/action=new_key", "{L_NEW_KEY}", "new.png");
				$options->add (null, "modules/conf/main.act.php?action=delete_key&id=$id&auth={C_AUTH_STR}", "{L_DELETE}", "delete.png", true);
			$options->make();

			$q = $db->query ("
				SELECT
					id, module, keyname, keyvalue, keytype, date_modified, locked
				FROM ".db_table("conf")."
				WHERE id = '{$id}'
			", __FILE__, __LINE__);

			if ($db->numRows($q)) {

				$row = $db->fetchRow($q);

				saneHTML($row);

				$tpl->setArray($row);

				$tpl->setArray (
					Array (
						"BLOCK_TITLE"		=> _l("L_CONF"),
						"BLOCK_ICON"		=> mkIcon("setup.png", 16),
						"ACTION"			=> "edit_key",
						"RETURN"			=> "index.php/module=admin/base=conf".($package ? "/package=$package" : "")
					)
				);

				$mcBuff = $tpl->make();

			} else {
				$mcBuff = _l("E_UNKNOWN_KEY");
			}
		break;
		case "new_key":

			$tpl = new ContentBlock();

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

			$options = new optionBox($tpl, $option);
				$options->add (null, "index.php/module=admin/base=conf", "{L_LIST}", "conf.png");
			$options->make();

			$tpl->setArray(
				Array (
					"BLOCK_TITLE"	=> _l("L_CONF"),
					"BLOCK_ICON"	=> mkicon("conf.png", 16),
					"MODULE"		=> "core",
					"KEYTYPE"		=> "",
					"KEYVALUE"		=> "",
					"KEYNAME"		=> "",
					"ACTION"		=> "new_key",
					"LOCKED"		=> false
				)
			);

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

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

			$tpl = new ContentBlock ();

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

			$options = new optionBox($tpl, $option);
				$options->add (null, "index.php/module=admin/base=conf/action=new_key", "{L_NEW_KEY}", "new.png");
			$options->make();

			$q = $db->query ("SELECT module FROM ".db_table("conf")." ORDER BY module ASC");
			$modules = $db->numRows($q);

			$passed = Array();
			$passed[$module] = true;
			while ($row = $db->fetchRow($q)) {
				if (!isset($passed[$row["module"]])) {

					$tpl->setArray (
						Array (
							"MODULE"		=> $row["module"],
							"MODULE_LINK"	=> mklink("index.php/module=admin/base=conf/action=list/package={$row["module"]}", $row["module"])
						),
						"MODULE"
					);

					$tpl->saveBlock("MODULE");

					$passed[$row["module"]] = true;
				}
			}

			$tpl->setArray (
				Array (
					"BLOCK_TITLE"	=> _l("L_CONF"),
					"BLOCK_SUBTITLE" => _l("L_CREATE"),
					"BLOCK_ICON"	=> mkIcon("setup.png", 16),
					"RETURN"		=> ""
				)
			);

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