<?php

/*

	This class manages the representative for a given district (i.e. NY03) and the senators for a given state.
	The constructor takes either a numerical district_ID (representatives) or it takes a state (senators)

*/

require_once($_SERVER['DOCUMENT_ROOT']."/includes/SQLConnect.php");

Class CongressPerson{
	private $is_rep_flag;
	private $district_ID;
	private $state;
	
	private $people; //Stores information about the congressperson or the senators
	
	function __construct($identifier){ //identifier can be a state or a district_ID		
		if(filter_var($identifier,FILTER_VALIDATE_INT) === FALSE){ //Dealing with a state and senators
		
			$this->is_rep_flag = false;
			$this->state = strtoupper(substr($identifier,0,2));
			
			$result = mysql_query("SELECT Representatives.* FROM Representatives,Districts WHERE Representatives.district_ID = Districts.ID AND Districts.state = '".$this->state."' AND Districts.district = 0 AND Representatives.is_current_flag=1 AND Representatives.is_senator_flag=1 LIMIT 2");
			$this->people[] = mysql_fetch_assoc($result); //Get first senator
			$this->people[] = mysql_fetch_assoc($result); //Second senator
				
		}
		else{ //Dealing with a district_ID		
			$this->is_rep_flag= true;
			$this->district_ID = (int)$identifier;
		
			$result = mysql_query("SELECT Representatives.* FROM Representatives WHERE Representatives.district_ID = ".$this->district_ID." AND Representatives.is_current_flag=1 AND Representatives.is_senator_flag=0 LIMIT 1");
			$this->people[] = mysql_fetch_assoc($result);
		}

	
		$this->setDaysInOffice();
		$this->setStartTS();
		$this->setEndTS();
		$this->setCommittees();
	
	}
	
	function setDaysInOffice(){
		foreach($this->people as $index=>$person){
			$daysInOffice = 0;
			$result = mysql_query("SELECT DATEDIFF(end_date,start_date) FROM Representatives WHERE gov_track_ID = ".$person['gov_track_ID']);
			if(mysql_num_rows($result)>=1){
				while($row = mysql_fetch_row($result)){
					@$daysInOffice += $row[0];
				}
				$this->people[$index]['yearsInOffice'] = $yearsInOffice = round($daysInOffice/365.25,1);
			}
			else{
				$result = mysql_query("SELECT UNIX_TIMESTAMP(start_date) FROM Representatives WHERE gov_track_ID = ".$person['gov_track_ID']." ORDER BY start_date DESC LIMIT 1");
				$this->people[$index]['yearsInOffice'] = date("Y") - (int)date("Y",(mysql_result($result,0)));	//If this is their first session, make it just output the number of years since they started this term	
			}	
		}
	}
	function setStartTS(){
		foreach($this->people as $index=>$person){
			$sql = "SELECT UNIX_TIMESTAMP(start_date) FROM Representatives WHERE gov_track_ID = ".$person['gov_track_ID']." ORDER BY start_date ASC";
			$this->people[$index]['startTS'] = mysql_result(mysql_query($sql),0);
		}
	}
	function setEndTS(){
		foreach($this->people as $index=>$person){
			$sql = "SELECT UNIX_TIMESTAMP(end_date) FROM Representatives WHERE gov_track_ID = ".$person['gov_track_ID']." ORDER BY end_date DESC";
			$this->people[$index]['endTS'] = mysql_result(mysql_query($sql),0);
		}
	}
	function setCommittees(){
		foreach($this->people as $index=>$person){
			$result = mysql_query("SELECT name FROM Committees WHERE rep_ID = ".$person['ID']);
			
			while($row = mysql_fetch_assoc($result)){
				$this->people[$index]['committees'][] = $row['name'];
			}
			
		}		
	}
	
	function setPhotoURL(){
		foreach($this->people as $index=>$person)
			$this->people[$index]['photoURL'] = 'http://'.$_SERVER['SERVER_NAME'].'/assets/photos/photos/'.$person['gov_track_ID'].'-200px.jpeg';
	}
	
	
	
	function getRepresentative(){
		if($this->is_rep_flag)
			return $this->people[0];
		else
			return false;
	}
	
	function getSenator($index){
		if(!$this->is_rep_flag)
			return $this->people[$index];
		else
			return false;
		
	}
	
}


?>