<?php
/* Comments class. Loganc3.0 - Oct 2014.   
*/

class Comments {
	
	var $oDbCall;

	function __construct($oDbCall) {
		$this->oDbCall = $oDbCall;
	}
	
	// Get comment
	function GetComment($id) {
		return $this->oDbCall->GetComment($id);
	}
	
	// Get all threads ordered by most recent comment.
	function GetAllThreads() {
		return $this->oDbCall->GetAllThreads();
	}
	
	// Get all comments for thread.
	function GetCommentThread($thread_id, $thread_type) {
		return $this->oDbCall->GetComments($thread_id, $thread_type);
	}
	
	// Get thread type for given thread_id.
	function GetThreadType($thread_id) {
		return $this->oDbCall->GetThreadType($thread_id);
	}
	
	// Get all comments for thread all types 
	function GetCommentThreadAllTypes($thread_id) {
		return $this->oDbCall->GetCommentsAllTypes($thread_id);
	}
	
	// Get all comments for specific image id.
	function GetImageComments($img_id) {
		
		// Fetch message_id from image.
		if(!$thread_id = $this->oDbCall->GetImageMessageId($img_id)) {
			return false;
		}
		return $this->oDbCall->GetComments($thread_id, 'image');
	}

	// Get all comments for specific blog entry ID.
	function GetBlogComments($img_id) {
		
		// Fetch comments_id from blog_entries
		if(!$thread_id = $this->oDbCall->GetBlogMessageId($img_id)) {
			return false;
		}
		return $this->oDbCall->GetComments($thread_id, 'blog');
	}	
	
	// Insert new comment.
	function InsertComment($thread_id, $thread_name, $comment, $username, $type, $email) {
		$this->oDbCall->InsertComment($thread_id, $thread_name, $comment, $username, $type, $email);
	}
	
	// Insert image comment. 
	function InsertImageComment($img_id, $comment, $username, $name, $email) {
	
		// Fetch message_id from image.
		if(!$thread_id = $this->oDbCall->GetImageMessageId($img_id)) {
			
		
			// No thread id associated with image so create new thread_id. 
			$thread_id = $this->oDbCall->GetCommentsHighestId();
			$thread_id = $thread_id + 1;
			
			// Insert new thread_id into image message_id.
			$this->oDbCall->UpdateImageMessageId($img_id, $thread_id);
		}
		
		// Insert new comment.
		$this->oDbCall->InsertComment($thread_id, 'image', $comment, $username, $name, $email);
		
		// Send comment notify email to admin.
		$this->_SendEmail($comment, $username, $name, $email);
		
		return true;
	}	
	
	// Insert blog comment. 
	function InsertBlogComment($blog_id, $comment, $username, $name, $email) {
	
		// Fetch message_id from blog.
		if(!$thread_id = $this->oDbCall->GetBlogMessageId($blog_id)) {
			
			// No thread id associated with blog so create new thread_id. 
			$thread_id = $this->oDbCall->GetCommentsHighestId();
			$thread_id = $thread_id + 1;
			
			// Insert new thread_id into blog comments_id.
			$this->oDbCall->UpdateBlogMessageId($blog_id, $thread_id);
		}
		
		// Insert new comment.
		$this->oDbCall->InsertComment($thread_id, 'blog', $comment, $username, $name, $email);
		
		// Send comment notify email to admin.
		$this->_SendEmail($comment, $username, $name, $email);
		return true;
	}	
	
	// Update comment
	function UpdateComment($id, $comment) {
		$this->oDbCall->UpdateComment($id, $comment);
	}
	
	// Delete comment
	function DeleteComment($id) {
		return $this->oDbCall->DeleteComment($id);
	}
	
	// Delete a comment and check if last comment in thread. 
	// If this is the last comment in this thread then also go thorugh and remove links to Images, Blogs etc. 
	function DeleteCommentComplete($id) {

		$comment = $this->GetComment($id);
		$this->DeleteComment($id);
	
		if(!$this->oDbCall->GetThreadMessageCount($comment['thread_id'])) {
			
			// Delete refernece to comment thread in Images and Blog tables;
			$this->oDbCall->DeleteAllThreadRef($comment['thread_id']);
		}
		
		return true;
		
	}	
		
	// Delete comment thread
	function DeleteCommentThread($thread_id) {
		return $this->oDbCall->DeleteCommentThread($thread_id, $thread_type);
	}
	
	// Glab blog entry details from given comment_id
	function GetBlogDetails($id) {
		return $this->oDbCall->GetBlogEntryCommentId($id);
	}
	
	// Glab image details from given comment_id. 
	function GetImageDetails($id) {
		return $this->oDbCall->GetImageLimMessageId($id);
	}
	
	// Send email - notifying admin of any new user comments
	function _SendEmail($comment, $username, $name, $email) {
		
		$comment = $this->RemoveHtmlNewlines($comment);
		$comment = $this->AddHtmlNewlines($comment);
		
		$message = "Someone has commented on Loganc website content";
		$message .= "\r\n";
		$message .= "Name: " . $name . "\n";
		$message .= "Username: " . $username . "\n"; 
		$message .= "Email: " . $email . "\n\n";
		$message .= $comment . "\n"; 
		
		$to = 'logan.crerar@outlook.com';
		$subject = "Someone " . $name . " has commented on Loganc website";
		$headers = 'From: admin@staylittle.net' . "\r\n" .
				'Reply-To: logan.crerar@outlook.com' . "\r\n" .
				'Content-type: text/plain; charset=iso-8859-1';		

		mail($to, $subject, $message, $headers);
	}
	
	// Replace newlines \n with html <br /> - used to encode textarea newlines for jquery submitions. 
	function AddHtmlNewlines($str) {
		return str_replace("\n", '<br />', $str); 
	}
	
	// Replace html <br /> with  newines \n used in textarea boxes.
	function RemoveHtmlNewlines($str) {
		return str_replace("<br>", '&#10;', $str); 
	}
}
?>