<?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!");

	/*
		checkGroups(String GroupList, [String Group ID]);
		returns: sanitized GroupList
	*/
	function checkGroups($groups, $groupid = null) {
		$result = array();
		$groups = explode(",", $groups);
		foreach ($groups as $group) {
			$i = intval(trim($group));
			if ($i && (!$groupid || $groupid != $i)) {
				/*
					Checking that:
						* Every Group has an integer value
						* A Group is not inside itself
						* Included Group already exists
				*/
				$q = $GLOBALS["db"]->query("SELECT ID FROM ".db_table("group")." WHERE ID = '$i'", __FILE__, __LINE__, true);
				if ($GLOBALS["db"]->num_rows($q))
					$result[] = $i;
			}
		}
		$result = implode(",", $result);
		return $result;
	}
	function mkGroupList($name, $groups = null) {
		$groups = explode(",", $groups);
		$return = Array();
		$q = $GLOBALS["db"]->query("SELECT ID, Groupname, Description FROM ".db_table("group")." ORDER BY ID ASC", __FILE__, __LINE__, true);
		while ($row2 = $GLOBALS["db"]->fetch_row($q)) {
			$return[] = mkCheckBox("".$name."[]", $row2["Groupname"], in_array($row2["ID"], $groups), $row2["ID"])."<div class=\"whisper\">{$row2["Description"]}</div>";
		}
		return "<div class=\"info\">\n".implode("<hr />\n", $return)."</div>\n";
	}
	function getLinkedGroups($groupid, $exclude = Array()) {
		$b = array();
		if (!isset($GLOBALS["cache"]["groups"][$groupid])) {
			$q = $GLOBALS["db"]->query("
				SELECT IncludeGroups FROM ".db_table("group")." WHERE ID = '$groupid' AND Status = '1'
			", __FILE__, __LINE__, true);
			$GLOBALS["cache"]["groups"][$groupid] = $GLOBALS["db"]->fetch_row($q);
		}
		$r = $GLOBALS["cache"]["groups"][$groupid];

		if ($r["IncludeGroups"]) {
			$b = explode(",", $r["IncludeGroups"]);
			foreach ($b as $g) {
				if (!is_array($exclude) || !in_array($g, $exclude)) {
					$b = array_merge($b, getLinkedGroups($g, array_merge(array($groupid),$b)));
				}
			}
		}
		return $b;
	}
	function getAllLinkedGroups($groups_string) {
		$buff = Array();
		$grouplist = explode(",", $groups_string);
		foreach ($grouplist as $group) {
			$buff[] = getLinkedGroups($group);
		}
		foreach ($buff as $tmp) {
			if (is_array($tmp)) {
				foreach ($tmp as $group) {
					if (!in_array($group, $grouplist)) {
						$grouplist[] = $group;
					}
				}
			}
		}
		sort($grouplist, SORT_NUMERIC);
		return $grouplist;
	}
	function getGroupNameById($id, $description = false) {
		if (is_integer(intval($id)) && $id > 0) {
			$field = $description ? "Description": "Groupname";
			$q = $GLOBALS["db"]->query("SELECT $field FROM ".db_table("group")." WHERE ID = '$id'", __FILE__, __LINE__, true);
			$r = $GLOBALS["db"]->fetch_row($q);
			return $r[$field];
		} else {
			switch ($id) {
				case "*": return _l("L_GROUP_ALL"); break;
				case "$": return _l("L_GROUP_ACCOUNT"); break;
				case "?": return _l("L_GROUP_VISITOR"); break;
				case "#": return _l("L_GROUP_MANAGEMENT"); break;
				case "@": return _l("L_GROUP_MANAGEMENT_CORE"); break;
			}
		}
	}
	function getGroupNames($groups) {
		$return = Array();
		foreach ($groups as $group) {
			if (($tmp = getGroupNameById($group)) != null) {
				$return[] = $tmp;
			}
		}
		return implode(", ", $return);
	}
	function deleteGroup($id) {
		slog("groups: deleted group id '$id'", "actions", 1);
		return $GLOBALS["db"]->query ("DELETE FROM ".db_table("group")." WHERE ID='".$id."'", __FILE__, __LINE__, true);
	}
	function groupSetStatus($id, $status) {
		return $GLOBALS["db"]->query ("UPDATE ".db_table("group")." SET Status = '".$status."' WHERE ID='".$id."'", __FILE__, __LINE__, true);
	}
?>