<?php
/*


	Main class for processing comments.
	
	CHANGE THE COMMENTS SO THAT THEY'RE IN-DISTRICT ONLY FOR THE SITE_AREA part!!!!

*/


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

class Comments{
	private $site_area_ID;
	private $forum_topic_ID;
	private $user_ID;
	public $comment_ID;
	
	public function __construct($user_ID, $site_area_ID = 0,$forum_topic_ID = 0){
		$this->site_area_ID = (int)$site_area_ID;
		$this->forum_topic_ID = (int)$forum_topic_ID;
		$this->user_ID = (int)$user_ID;
	}
	
	public function getComments(){			
		//Note that both comments_results contain the same information, but they take different "arguments" in the form of forum_topic_ID or site_area_ID
		if($this->forum_topic_ID>0) //These are forum posts //No limit
			$comments_result = mysql_query("SELECT Comments.ID, Comments.user_ID,Comments.body,UNIX_TIMESTAMP(Comments.TS) as 'timestamp',Users.first_name,Users.last_name,Users.city,Users.state,Users.join_TS,Districts.state,Districts.district FROM Comments,Users,Districts WHERE Districts.ID = Users.district_ID AND Users.ID = Comments.user_ID AND forum_topic_ID=".$this->forum_topic_ID." ORDER BY Comments.TS ASC");
		elseif($this->site_area_ID>0) //These are regular site area comments //No limit //Make sure that this is limited by district_ID for deployment
			$comments_result = mysql_query("SELECT Comments.ID,Comments.user_ID,Comments.body,UNIX_TIMESTAMP(Comments.TS) as 'timestamp',Users.first_name,Users.last_name,Users.city,Users.state,Users.join_TS,Districts.state,Districts.district FROM Comments,Users,Districts WHERE Districts.ID = Users.district_ID AND Users.ID = Comments.user_ID AND site_area_ID=".$this->site_area_ID." ORDER BY Comments.TS ASC");
		elseif($this->user_ID>0) //Return last 10 comments by a given user // LIMIT 10
			$comments_result = mysql_query("SELECT Comments.ID,Comments.user_ID,Comments.body,UNIX_TIMESTAMP(Comments.TS) as 'timestamp',Users.first_name,Users.last_name,Users.city,Users.state,Users.join_TS,Districts.state,Districts.district,Forum_Topics.topic,Forum_Topics.ID as 'forum_topic_ID' FROM Comments,Users,Districts,Forum_Topics WHERE Forum_Topics.ID = Comments.forum_topic_ID AND Districts.ID = Users.district_ID AND Users.ID = Comments.user_ID AND Comments.user_ID = ".$this->user_ID." ORDER BY Comments.TS ASC LIMIT 10");
		else //Get *all* forum posts LIMIT 20
			$comments_result = mysql_query("SELECT Comments.ID,Comments.user_ID,Comments.body,UNIX_TIMESTAMP(Comments.TS) as 'timestamp',Users.first_name,Users.last_name,Users.city,Users.state,Users.join_TS,Districts.state,Districts.district,Forum_Topics.topic,Forum_Topics.ID as 'forum_topic_ID' FROM Comments,Users,Districts,Forum_Topics WHERE Forum_Topics.ID = Comments.forum_topic_ID AND Districts.ID = Users.district_ID AND Users.ID = Comments.user_ID ORDER BY Comments.TS ASC LIMIT 20");

		if(mysql_num_rows($comments_result)>0){
			while($comment = mysql_fetch_assoc($comments_result))
				$comments[] = $comment;
			
			return $comments; //Return an array of the comments
		}
		return false; //There are no comments to get, report this back
	}
	
	public function getComment($comment_ID){
		if($this->site_area_ID>0){
			$comment_result = mysql_query("SELECT Comments.ID,Comments.user_ID,Comments.body,UNIX_TIMESTAMP(Comments.TS) as 'timestamp',Users.first_name,Users.last_name,Users.city,Users.state,Users.join_TS,Districts.state,Districts.district FROM Comments,Users,Districts WHERE Districts.ID = Users.district_ID AND Users.ID = Comments.user_ID AND Comments.ID=".$comment_ID." LIMIT 1");
 			return mysql_fetch_assoc($comment_result);
 		}
 		else
 			return false;
	}
	
	public function newComment($body){
		$body = strip_tags($body); //Remove HTML
		$body = htmlentities($body); //Encode any symbols into HTML characters
		
		if($this->forum_topic_ID>0){ //This is a forum post
			$return = mysql_query("INSERT INTO Comments(user_ID,body,forum_topic_ID) VALUES(".$this->user_ID.",'".mysql_real_escape_string(strip_tags($body))."',".$this->forum_topic_ID.")");
		}
		else{
			$return = mysql_query("INSERT INTO Comments(user_ID,body,site_area_ID) VALUES(".$this->user_ID.",'".mysql_real_escape_string(strip_tags($body))."',".$this->site_area_ID.")");
		}
		
		$this->comment_ID = mysql_insert_ID();
		return $return;
		
	}
	
}
	

?>