Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.groups.php
<?php
/**************************************************************************
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/

class groups
{
	function add_user($uid, $gid)
	{
		GLOBAL $O, $db;

		$time = time();
		$sql = "INSERT INTO o_group_access (group_id, user_id, permission, added) VALUES ('$gid','$uid','1','$time')";
		$db->query($sql);
		return;
	}

	function add_group($data)
	{
		GLOBAL $O, $db, $user;

		$gid = $this->get_gid();

		if(!$this->search_groups($data['group_name']))
		{

			return FALSE;
		}

		$owner = $user->user_id;
		$time = time();
		
		$sql = "INSERT INTO o_groups (group_id, group_name, owner, created)
			VALUES ('$gid','$data[group_name]','$owner','$time')";
		$db->query($sql);
		$i = 0;

		while($i < count($data['users']))
		{
			$this->add_user($data['users'][$i], $gid);
			
			$i++;
		}

		$sql = "SELECT location FROM o_services";
		$db->query($sql);
		while($db->fetch_results())
		{
		
			$service = $db->record['location'];
			$ag = $O->change_str($O->dir ."/". $service ."/config/add_group.php");
			if(file_exists($ag))
			{
				include($ag);
			}
			
		
		}
		
	}
	
	
	function get_gid()
	{
		GLOBAL $db;
		$gid = rand(10000,99999);

		$sql = "SELECT group_id FROM o_groups WHERE group_id='$gid'";
		$db->query($sql);

		if($db->num_rows >= 1)
		{
			$this->get_gid();
		} else {
			return $gid;
		}
	}
	
	
	function search_groups($group_name)
	{
		GLOBAL $db;
		$sql = "SELECT group_name FROM o_groups WHERE group_name='$group_name'";
		$db->query($sql);

		if($db->num_rows >= 1)
		{

			//Found a group with same name
			return FALSE;
		}
		return TRUE;
	}
	
	function update_group($data)
	{
		GLOBAL $db;
		$gid = $data['gid'];
		$sql = "DELETE FROM o_group_access WHERE group_id=$gid";
		$db->query($sql);

		$i = 0;
		while($i < count($_POST['users']))
		{
			$sql = "INSERT INTO o_group_access (group_id, user_id, permission) VALUES ('$gid','". $data['users'][$i] ."','1')";
 			$db->query($sql);
			$i++;
		}
		
		$sql = "UPDATE o_groups SET owner='$data[owner]' WHERE group_id='$gid'";
		$db->query($sql);
		return TRUE;

	}

	/*!
		@function get_user_groups()
		@author Ryan Thompson
		@abstract Gets groups that a user belongs to
		@version 0.2
		@params $user_id
		@return $groups[]
		@since 07-12-2003
		@access PUBLIC
	*/	
	function get_user_groups($user_id)
	{
		GLOBAL $db;
		$sql = "SELECT o_groups.group_id, o_groups.group_name FROM o_groups LEFT JOIN o_group_access 
			ON o_group_access.group_id=o_groups.group_id WHERE o_group_access.user_id='$user_id'";
		$db->query($sql);
		while($db->fetch_results())
		{
			$groups[$db->record['group_id']] = $db->record['group_name'];
		}
		if(!is_array($groups))
		{
			$groups = array();
		}
		return $groups;
		
	}
	
	/*!
		@function get_user_list()
		@author Ryan Thompson
		@abstract Gets list of users belonging to one or more groups
		@version 0.2
		@params $group_id - can be array
		@return $user_list
		@since 07-12-2003
		@access PUBLIC
	*/	
	function get_user_list($group_id)
	{
		GLOBAL $db;
		
		if(is_array($group_id))
		{
			foreach($group_id AS $value)
			{
				$sql = "SELECT o_users.user_id, o_users.username FROM o_users LEFT JOIN o_group_access 
					ON o_users.user_id=o_group_access.user_id WHERE o_group_access.group_id='$value'";
				$db->query($sql);
				while($db->fetch_results())
				{
					$user_list[$db->record['user_id']] = $db->record['username'];
				}
			}
		} else {
				$sql = "SELECT o_users.user_id, o_users.username FROM o_users LEFT JOIN o_group_access 
					ON o_users.user_id=o_group_access.user_id WHERE o_group_access.group_id='$group_id'";
				$db->query($sql);
				while($db->fetch_results())
				{
					$user_list[$db->record['user_id']] = $db->record['username'];
				}
		}
		
		
		return $user_list;
		
	}
}

?>
Return current item: O - OpenSource GroupWare