<?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/modules/main.lib.php";
	require_once GEKKO_ROOT."modules/packages/main.lib.php";

	$module = _ext("module", 2);

	function text2html($text) {
		$text = nl2br(htmlspecialchars(trim($text)));
		return $text;
	}

	if ($module) {

		$tpl = new ContentBlock();
		$tpl->insert("BLOCK_CONTENT", "modules/module.edit.tpl");

		$db->query("SELECT ID, Module, auth_Exec, auth_Manage,
		Modified, Status, Hidden FROM ".db_table("module")." WHERE Module = '$module'");

		if ($db->num_rows()) {

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

			$buttons = Array(
				mkLink("modules/modules/main.act.php?action=manage&opt=U&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_UNINSTALL"), "delete.png", true),
				mkLink("modules/modules/main.act.php?action=manage&opt=R&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_REINSTALL"), "redo.png", true),
				mkLink("modules/modules/main.act.php?action=manage&opt=P&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_UPGRADE"), "up.png", true)
			);

			$tpl->set_arr ($row);
			$tpl->set_arr (
				Array (
					"HIDDEN.CHECKBOX" => mkCheckBox("hidden", _l("L_HIDDEN"), $row["Hidden"]),
					"STATUS.CHECKBOX" => mkCheckBox("status", _l("L_ACTIVE"), $row["Status"]),
					"BLOCK_TITLE" => _l("L_MODULES"),
					"BLOCK_ICON" => mkIcon("modules.png", 16),
					"BLOCK_SUBTITLE" => _l("L_MODIFY_MODULE"),
					"AUTH_EXEC" => mkAuthSelect($row["auth_Exec"], "auth_exec"),
					"AUTH_MANAGE" => mkAuthSelect($row["auth_Manage"], "auth_manage"),
					"ACTIONS" => mkButtonDisplay($buttons),
					"RETURN" => "../../index.php/admin/modules"
				)
			);

			$mcBuff = $tpl->make();
		} else {
			$mcBuff = "Not found";
		}

	} else {

		$tpl = new ContentBlock();

		$tpl->insert("BLOCK_CONTENT", "modules/index.admin.tpl");

		$db->query ("
			SELECT
				ID, Module, auth_Exec, auth_Manage, Status
			FROM ".db_table("module")."
			ORDER BY Module
		", __FILE__, __LINE__);

		$rmods = Array();
		$modules = $db->num_rows();
		$default_m = Conf::getKey("modules", "default");

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

			if (file_exists(GEKKO_ROOT."modules/".$row["Module"]."/main.inc.php")) {
				$row["DEFAULT.RADIO"] = mkRadio("default", "", ($default_m == $row["Module"]), $row["ID"]);
			} else {
				$row["DEFAULT.RADIO"] = "";
			}

			$xml_file = GEKKO_ROOT."modules/{$row["Module"]}/package.xml";

			if (file_exists($xml_file)) {

				$pkg_xml = Array();

				pkg_parse_xml($xml_file, $pkg_xml);

				$tpl->set_arr (
					Array (
						"AUTHOR" => 			$pkg_xml["module"]["author"],
						"HOMEPAGE" => 			$pkg_xml["module"]["homepage"],
						"VERSION" => 			$pkg_xml["release"]["version"],
						"RELEASE_AUTHOR" => 	$pkg_xml["release"]["author"],
						"NOTES" => 				text2html($pkg_xml["release"]["notes"]),
						"CHANGELOG" => 			text2html($pkg_xml["release"]["changelog"])
					),
				"MODULE");
			}

			$tpl->set_arr (
				$row,
				"MODULE"
			);

			$tpl->saveBuff("MODULE");
			$tpl->clear("MODULE");

			$rmods[$row["Module"]] = true;
		}

		$modlist = ModCore::getModules();
		$packages = 0;
		foreach ($modlist as $module) {
			if (isset($rmods[$module])) continue;
			$packages++;
			$tpl->set_arr (
				Array (
					"MODULE" => "$module",
					"TIMESTAMP" => dateFormat(filemtime(GEKKO_ROOT."modules/$module"))
				),
				"PACKAGE"
			);
			$tpl->saveBuff("PACKAGE");
		}

		$tpl->set_arr (
			Array (
				"BLOCK_TITLE" => _l("L_MODULES"),
				"BLOCK_SUBTITLE" => _l("L_MODULE_MANAGER"),
				"BLOCK_ICON" => mkIcon("modules.png", 16),
				"MODULES" => $modules,
				"PACKAGES" => $packages,
				"RETURN" => ""
			)
		);

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


?>