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

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

	switch ($action) {
		case "edit":
			if (!$id) break;

			$tpl = new ContentBlock();

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

			$db->query("
				SELECT
					ID, Groupname, Description, Status, IncludeGroups
				FROM ".db_table("group")." WHERE ID = '".$id."'
			");

			if (!$db->num_rows()) break;

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

			$row["BLOCK_TITLE"] = _l("L_GROUPS");
			$row["BLOCK_SUBTITLE"] = _l("L_EDIT");
			$row["BLOCK_ICON"] = mkIcon("groups", 16);
			$row["STATUS.SEL"] = mkSelect(array(_l("L_DISABLED"), _l("L_ENABLED")), $row["Status"], "status");
			$row["ACTION"] = "edit";
			$row["RETURN"] = "../../index.php/admin/groups";

			$tpl->set_arr($row);

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

			$tpl = new ContentBlock();

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

			$q = $db->query ("
				SELECT ID, Groupname, Description, Status, IncludeGroups
				FROM ".db_table("group")."
				ORDER BY ID, Groupname DESC
			", __FILE__, __LINE__);

			$groups = $db->num_rows();

			while ($row = $db->fetch_row($q)) {

				saneHTML($row);

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

				$row["IncludeGroups"] = getGroupNames(getAllLinkedGroups($row["IncludeGroups"]));

				if (!$row["IncludeGroups"]) $row["IncludeGroups"] = "("._l("L_NONE").")";

				$tpl->set_arr(
					$row,
					"GROUP"
				);
				$tpl->saveBuff("GROUP");
			}


			$tpl->set_arr(
				Array (
					"BLOCK_TITLE" => _l("L_GROUPS"),
					"BLOCK_SUBTITLE" => _l("L_CREATE_GROUP"),
					"BLOCK_ICON" => mkIcon("groups.png", 16),
					"GROUPNAME" => "",
					"DESCRIPTION" => "",
					"ACTION" => "create",
					"ID" => 0,
					"RETURN" => "",
					"STATUS.SEL" => mkSelect(array(_l("L_DISABLED"), _l("L_ENABLED")), 1, "status"),
					"GROUPS" => $groups,
					"INCLUDEGROUPS" => ""
				)
			);

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


?>