<?php

include_once("includes/SQLConnect.php"); //This is (rather confusingly) a relative path from where this file is being included from

class Affiliations{
	private $user_ID;

	public function __construct($user_ID){
		$this->user_ID = (int)$user_ID;
	}
	
	public function getAffiliatedUsers($affiliation_ID){
		$affiliation_ID = (int)$affiliation_ID;
		$result = mysql_query("SELECT Users.* FROM Users,Affiliations_Users WHERE Affiliations_Users.user_ID = Users.ID AND Affiliations_Users.affiliation_ID = ".$affiliation_ID." ORDER BY Affiliations_Users.join_TS DESC");
		while($user = mysql_fetch_assoc($result))
			$users[] = $user;
		return $users;		
	}
	
	public function getAffiliations(){
		$result = mysql_query("SELECT Affiliations.*,Users.first_name,Users.last_name,Users.email,Users.photo_URL FROM Affiliations,Affiliations_Users,Users WHERE Users.ID = Affiliations.contact_user_ID AND Affiliations.ID = Affiliations_Users.affiliation_ID AND Affiliations_Users.user_ID=".$this->user_ID." ORDER BY Affiliations_Users.join_TS DESC");
		$affiliations = null;
		while($affiliation = mysql_fetch_assoc($result))
			$affiliations[] = $affiliation;
		return $affiliations;
	}
	
	public function addAffiliation($affiliation_ID){
		$affiliation_ID = (int)$affiliation_ID;
		return mysql_query("INSERT INTO Affiliations_Users(affiliation_ID,user_ID) VALUES(".$affiliation_ID.",".$this->user_ID.")");
	}
	
	public function removeAffiliation($affiliation_ID){
		$affiliation_ID = (int)$affiliation_ID;
		return mysql_query("DELETE FROM Affiliations_Users WHERE affiliation_ID = ".$affiliation_ID." AND user_ID = ".$this->user_ID." LIMIT 1");
	}
	
	

}
/*
	//Testing the class
	//include_once("../SQLConnect.php"); This is for testing the class, has to be moved to the top to work... obviously

	$Affiliations = new Affiliations(2);
	print "<h3>Affiliations of UserID=2</h3>";
	print_r($Affiliations->getAffiliations());
	print "<h3>Members of Affiliation ID =1</h3>";
	print_r($Affiliations->getAffiliatedUsers(1));
*/
?>