<?php

class threads{

	var $host;
	var $username;
	var $password;
	var $table;
	
	/**
	* Contructor
	* @param String $host
	* @param String $username
	* @param String $password
	* @param String $db
	*/
	public function threads($host, $username, $password, $db){
		$this->host = $host;
		$this->username = $username;
		$this->password = $password;
		$this->table = $db;
		$this->connect();
	}
	
	/**
	deletes a report by is id
	*/
	public function delete_thread($id){
    mysql_query("DELETE FROM comments WHERE report = '$id'");
	  return mysql_query("DELETE FROM threads WHERE id = '$id'")or die(mysql_error());
	}
		
	/**
	diplays a list of exception reports, this list will link to the indiviual reports
	*/
	public function display_report_list() {
		/* Instantiate class */ 
		require_once("pager.php"); 
		$p = new Pager; 
		
		/* Show many results per page? */ 
		$limit = 5; 
		
		/* Find the start depending on $_GET['page'] (declared if it's null) */ 
		$start = $p->findStart($limit); 
		
		/* Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query */ 
		$count = mysql_num_rows(mysql_query("SELECT * FROM threads")); 
		
		/* Find the number of pages based on $count and $limit */ 
		$pages = $p->findPages($count, $limit); 
		
		/* Now we use the LIMIT clause to grab a range of rows */ 
		$r = mysql_query("SELECT * FROM threads ORDER BY id LIMIT ".$start.", ".$limit); 
		
		/* Now get the page list and echo it */ 
		$pagelist = $p->pageList($_GET['page'], $pages); 

		/*
		get all the reports
		*/
    	$entry_display = '<br/>';
		while($thread = mysql_fetch_assoc($r)) {
		$title = $thread['title'];
		$id = $thread['id'];
		$author = $thread['author'];
		$entry_display .= <<<ENTRY_DISPLAY
    <div class="post">
    <a href="forum.php?thread=$id" >#$id $title</a> Started by: $author
    </div>
    <br/>
ENTRY_DISPLAY;
			
	}
  $entry_display .= '<div align="center">'.$pagelist.'</div>'; 
		return $entry_display;
	}


	/**
	This will display the specified report
	*/
	public function display_thread($id) {

    	$id = mysql_real_escape_string($id);

		/*
		get all the reports
		*/
		$r = mysql_query("SELECT * FROM threads WHERE id=".$id);

		if($thread = mysql_fetch_assoc($r)) {
		
		  $title = $thread['title'];
		  $author = $thread['author'];
		  $time = $thread['time'];
		
		  $entry_display = <<<THREAD
		  
		  <h1>$title</h1>
		  Started By: $author ~ $time 
THREAD;
    
        if($_SESSION['access'] == "admin" || $_SESSION['username'] == $author)    
          echo <<<DELETE
          <a align="right" href="forum.php?delete=$id" onclick="return confirm('Are you sure You want to delete this thread forever?');">Delete Thread</a> 
DELETE;

		    /*
		    comments
		    */
		    require_once('comments.php');
 		    $comments = new comments("comments",DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
  		
  		    $entry_display .= $comments->get_comments($id);
  		    $entry_display .= $comments->display_post($id);;
		} else
		  return "<b>No Thread Found: $id</b>";
		return $entry_display;

	}
	
	/*
	Files a new exception report into the database
	*/
	public function file_thread($title, $author, $time){
		/*
		add escapes to the data
		*/
		$title = mysql_real_escape_string($title);
		
		 /*
		 insert the new report
		 */
		 $insert =  "INSERT INTO threads (title, author, time) VALUES ('$title', '$author', '$time')";
		mysql_query($insert);
		return  mysql_insert_id();
	}
	
	/*
	Connects the the database
	*/
	public function connect() {
    		mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
    		mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
		return $this->buildDB();
  	}

	/**
	Builds the database that will be used to for exception reports
	*/
  	private function buildDB() {
    		$sql = <<<MySQL_QUERY
			CREATE TABLE IF NOT EXISTS threads (
			id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
			title TEXT,
			author TEXT,
			time TEXT
			)
MySQL_QUERY;

    		return mysql_query($sql);
    	}
}
?>
