<?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/groups/main.lib.php";

	db_init();

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

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

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

	switch ($action) {
		case "delete":
			execWithEach($id, "deleteGroup");
		break;
		case "enable":
			groupSetStatus($id, 1);
		break;
		case "create":

			gpGlobalize("groupname, description, status, includegroups");

			requireVars($groupname);

			$db->query("SELECT ID FROM ".db_table("group")." WHERE Groupname = '$groupname'");
			if ($db->num_rows()) abort(_l("E_DUPLICATED_GROUPNAME"));

			$includegroups = checkGroups($includegroups, $id);

			$db->query ("
			INSERT INTO ".db_table("group")."
				(
					ID,
					Groupname,
					Description,
					Status,
					IncludeGroups
				)
					VALUES
				(
					'".$db->free_id("groups", "ID", 401, 499)."',
					'".$groupname."',
					'".$description."',
					'".$status."',
					'".$includegroups."'
				)
			", __FILE__, __LINE__);

			slog("groups: created group '$groupname'", "actions", 1);
		break;
		case "edit":

			gpGlobalize("id, groupname, description, status, includegroups");

			requireVars($id, $groupname);

			$includegroups = checkGroups($includegroups, $id);

			$db->query("SELECT ID FROM ".db_table("group")." WHERE Groupname = '$groupname' AND ID != '$id'");
			if ($db->num_rows()) abort(_l("E_DUPLICATED_GROUPNAME"));

			$db->query("
				UPDATE ".db_table("group")." SET
					Groupname = '$groupname',
					Description = '$description',
					Status = '$status',
					IncludeGroups = '$includegroups'
				WHERE ID = '$id'
			", __FILE__, __LINE__);

			slog("groups: group '$groupname' was modified", "actions", 1);
		break;
		default:
			abort(_l("L_UNDEFINED_ACTION"));
		break;
	}

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

	db_exit();
?>