Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.sharing.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)
***************************************************************************/
/*$Id: class.sharing.php,v 1.9 2004/05/31 03:55:13 rthomp Exp $*/

class share
{	
	/*!
		@function share_select()
		@author Ryan Thompson
		@abstract Create a select box for user sharing (lists groups)
		@version 0.1
		@params $user_id
		@return $select_box
		@since 07-12-2003
		@access PUBLIC
	*/
	function share_select($user_id, $selected_groups, $all_selected = FALSE)
	{
		GLOBAL $db, $groups, $lang;
		
		$group_list = array();
		$group_list = $groups->get_user_groups($user_id);
		$select = $lang->get_msg('groups', 'gl');

		$select .= "<br /><select name=\"share_groups[]\" multiple=\"multiple\" size=\"6\">\n";
		
		foreach($group_list AS $key => $value)
		{
			$select .= "<option value=\"$key\"";
			if(is_array($selected_groups) || $selected)
			{
				if(in_array($key, $selected_groups) || $all_selected)
				{
					$select .= " selected=\"selected\"";	
				}
			}
			$select .= ">$value</option>\n";	
		}
		
		$select .= "</select>\n";
		$select .= "<br /><input type=\"submit\" name=\"select_group\" value=\"". $lang->get_msg('select_groups', 'gl') ."\" class=\"button1\">\n";
		return $select;
	}
	
	/*!
		@function share_select_users()
		@author Ryan Thompson
		@abstract Create a select box for user sharing (lists users)
		@version 0.1
		@params $group_id
		@params $selected_users
		@return $select
		@since 07-12-2003
		@access PUBLIC
	*/	
	function share_select_users($group_id, $selected_users)
	{
		
		GLOBAL $db, $groups, $lang;
		
		$user_list = $groups->get_user_list($group_id);	
		$select = $lang->get_msg('users', 'gl');
		$select .= "<br /><select name=\"share_users[]\" multiple=\"multiple\" size=\"6\">\n";
		if(is_array($user_list))
		{
			
			foreach($user_list AS $key => $value)
			{
				$select .= "<option value=\"$key\"";
				if(is_array($selected_users))
				{
				
					if(in_array($key, $selected_users))
					{

						$select .= " selected=\"selected\"";	
					}
				}
				$select .= ">$value</option>\n";	
			}
		}
		
		$select .= "</select>\n";
		$select .= "<br />\n";
		return $select;
	}
	
	/*!
		@function add_share()
		@author Ryan Thompson
		@abstract Adds users to a share
		@version 0.1
		@params $item_id
		@params $service
		@return $users[]
		@since 07-12-2003
		@access PUBLIC
	*/
	function add_share($item_id, $service, $users)
	{
		GLOBAL $db;

		if(is_array($users))
		{
			foreach($users AS $value)
			{
				$sql = "INSERT INTO o_share (share_item, user_id, service) VALUES ('$item_id','$value','$service')";
				$db->query($sql);
			}
		}
		
	}
	
	/*!
		@function delete()
		@author Ryan Thompson
		@abstract Delete a share item
		@version 0.1
		@params $item_id
		@params $service
		@return TRUE
		@since 07-12-2003
		@access PUBLIC
	*/
	function delete($item_id, $service)
	{
		GLOBAL $db;
		$sql = "DELETE FROM o_share WHERE share_item='$item_id' AND service='$service'";
		$db->query($sql);
		
		return;	
	}
	
	/*!
		@function get_share_groups()
		@author Ryan Thompson
		@abstract Gets groups based on users sharing an item.
		@version 0.1
		@params $item_id
		@params $service
		@params $user_id - Owner of item
		@return $return_group[]
		@since 08-12-2003
		@access PUBLIC
	*/	
	function get_share_groups($item_id, $service, $user_id)
	{
		GLOBAL $db;
		$sql = "SELECT * FROM o_share LEFT JOIN o_users ON o_share.user_id=o_users.user_id
			WHERE o_share.share_item='$item_id' AND o_share.service='$service'";
		$db->query($sql);
		$users = array();
		while($db->fetch_results())
		{
			$users[] = $db->record['user_id'];
			
		}
	
		$groups = array();
		foreach($users AS $value)
		{
			$sql = "SELECT * FROM o_group_access WHERE user_id='$value'";
			$db->query($sql);
			while($db->fetch_results())
			{
				$groups[] = $db->record['group_id'];
			}
			
		}
		$sql = "SELECT * FROM o_group_access WHERE user_id='$user_id'";
		$db->query($sql);
		while($db->fetch_results())
		{
			if(in_array($db->record['group_id'], $groups))
			{
				$return_groups[] = $db->record['group_id'];
			}
		}
		
		
		return $return_groups;
		
	}
	
	/*!
		@function update_share()
		@author Ryan Thompson
		@abstract Updates item sharing
		@version 0.1
		@params $item_id
		@params $service
		@return $users[]
		@since 08-12-2003
		@access PUBLIC
	*/
	function update_share($shared, $item_id, $service, $users)
	{
		GLOBAL $db;
		
		if(!$shared)
		{
			$sql = "DELETE FROM o_share WHERE share_item='$item_id' AND service='$service'";
			$db->query($sql);
			return;
		}
		
		$sql = "SELECT * FROM o_share WHERE share_item='$item_id' AND service='$service'";
		$db->query($sql);
		while($db->fetch_results())
		{
		 	$sharing_users[] = $db->record['user_id'];
		}
		
		if(!is_array($sharing_users))
		{
			$sharing_users = array();
		}
		
		$i = 0;
		if(empty($users))
		{
			$users = array();
		}
		foreach($users AS $value)
		{
			if(!in_array($value, $sharing_users))
			{
				$sql = "INSERT INTO o_share (user_id, share_item, service) VALUES ('$value','$item_id','$service')";
				$db->query($sql);
			}
			
		}
		foreach($sharing_users AS $value)
		{
			if(!in_array($value, $users))
			{
				$sql = "DELETE FROM o_share WHERE user_id='$value' AND share_item='$item_id' AND service='$service'";
				$db->query($sql);
			}
		}
		
		return;
	}
	
	/*!
		@function run_share_query()
		@author Ryan Thompson
		@abstract Gets users data stored in database
		@version 0.1
		@params $service
		@params $user_id
		@params $service_table
		@params $primary_key
		@params $sort_order
		@return $results[]
		@since 10-12-2003
		@access PUBLIC
	*/	
	function run_share_query($service, $user_id, $service_table, $primary_key, $sort_order)
	{
		GLOBAL $db;

		$sql = "SELECT $service_table.user_id, $service_table.*, o_users.username FROM o_share
			LEFT JOIN $service_table ON o_share.share_item=$service_table.$primary_key
			LEFT JOIN o_users ON $service_table.user_id=o_users.user_id
			WHERE o_share.user_id='$user_id' AND o_share.service='$service'
			OR $service_table.user_id='$user_id'$sort_order GROUP BY $service_table.$primary_key";	
		$db->query($sql);
		$results = array();
		
		while($db->fetch_results())
		{
			if($db->record['user_id'] != $user_id)
			{
				//Use until I figure out how to properly filter with database properly	
				$results[] = $db->record;
			}
		}
		return $results;
	}
		
	/*!
		@function get_shared_item()
		@author Ryan Thompson
		@abstract Gets users data stored in database
		@version 0.1
		@params $service
		@params $user_id
		@params $item_id
		@params $service_table
		@params $primary_key
		@return $results[]
		@since 10-12-2003
		@access PUBLIC
	*/	
	function get_shared_item($service, $service_table, $primary_key, $user_id, $item_id)
	{
		GLOBAL $db;	
		$sql = "SELECT o_users.username, $service_table.user_id, $service_table.* FROM $service_table 
			LEFT JOIN o_share ON o_share.share_item=$service_table.$primary_key
			LEFT JOIN o_users ON $service_table.user_id=o_users.user_id
			WHERE o_share.service='$service' AND o_share.user_id='$user_id' AND $service_table.$primary_key='$item_id'";
		$db->query($sql);
		$db->fetch_results();
		
		return $db->record;
		
		
	}
}
Return current item: O - OpenSource GroupWare