<?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://www.gekkoware.org
	*/

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

	require_once GEKKO_ROOT."modules/users/main.lib.php";
	require_once GEKKO_ROOT."modules/groups/main.lib.php";

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

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

				use_ssl();

				$tpl = new ContentBlock();

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

				$db->query("
					SELECT
					ID, Username, Realname, Password, Email, SesLen, RegTime, LastLogin,
					AuthKey, Status, Groups, Preferences
					FROM ".db_table("user")." WHERE ID = '{$id}'
				");

				if ($db->num_rows()) {

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

					$row["GROUPS.CHECKBOX"] = mkGroupList("groups", $row["Groups"]);

					$row["BLOCK_TITLE"] = _l("L_USERS");
					$row["BLOCK_SUBTITLE"] = _l("L_EDIT");
					$row["BLOCK_ICON"] = mkIcon("users.png", 16);

					$row["STATUS.SEL"] = mkSelect(Array(_l("L_DISABLE"), _l("L_ENABLE")), $row["Status"], "status");
					$row["ACTION"] = "edit";
					$row["RETURN"] = "../../index.php/admin/users";

					$row["ACTIONS"] = mkButtonDisplay (
						Array (
							mkLink("modules/users/main.act.php?action=delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), false, 1),
							mkLink("index.php/users/profile/".$row["Username"]."/edit", _l("L_EDIT_USER_PROFILE"))
						)
					);

					$tpl->set_arr($row);

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

			$sort = isset($_GET["sort"]) ? check_value($_GET["sort"], "ID,Username,Email") : "Username";
			$mode = isset($_GET["mode"]) ? check_value($_GET["mode"], "ASC,DESC") : "ASC";

			$tpl = new ContentBlock();

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

			$users = 0;

			$pager = $db->pager (
				"SELECT ID FROM ".db_table("user")."",
				"index.php/admin/users/%p?sort=$sort&mode=$mode#manage", $page, 8
			);

			$q = $db->query ("
				SELECT
				ID, Username, Email, Status
				FROM ".db_table("user")."
				ORDER BY $sort $mode ".$pager["sql"]."
			", __FILE__, __LINE__);

			$users = $db->num_rows();
			$sw = 0;
			while ($row = $db->fetch_row($q)) {

				saneHTML($row);

				$row["Actions"] = Array();
				$row["Actions"][] = mkLink("index.php/admin/users/edit/".$row["ID"], _l("L_EDIT"));
				$row["Actions"][] = mkLink("modules/users/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/users/main.act.php?action=enable&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_ENABLE"));
				}
				$row["Actions"] = mkButtonDisplay($row["Actions"]);

				$row["Avatar"] = mkUserAvatar($row["ID"]);
				$row["SW_CLASS"] = sw_class($sw);
				$tpl->set_arr($row,"USER");
				$tpl->saveBuff("USER");
			}

			$tpl->set_arr(
				Array (
					"BLOCK_TITLE" => _l("L_USERS"),
					"BLOCK_ICON" => mkIcon("users.png", 16),
					"STATUS.SEL" => mkSelect(array(_l("L_DISABLED"), _l("L_ENABLED")), 1, "status"),
					"ACTION" => "register",
					"USERNAME" => "",
					"PASSWORD" => "",
					"EMAIL" => "",
					"REALNAME" => "",
					"SESLEN" => "",
					"GROUPS" => "",
					"ID" => 0,
					"RETURN" => "",
					"ACTIONS" => "",
					"USERS" => $users,
					"GROUPS.CHECKBOX" => mkGroupList("groups", "500"),
					"PAGER" => $pager["html"]
				)
			);

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

?>