<?php
class blog {
	var $host;
	var $username;
	var $password;
	var $table;

	/**
	 * Contructor
	 * @param String $host
	 * @param String $username
	 * @param String $password
	 * @param String $db
	 */
	public function blog($host, $username, $password, $db){
		$this->host = $host;
		$this->username = $username;
		$this->password = $password;
		$this->table = $db;
		$this->connect();
	}
	
	/**
	 deletes all the comments in the blog database for the suppled id
	 */
	public function delete_thread($id){
		$q = "DELETE FROM blog WHERE id = '$report'";
		return mysql_query($q);
	}

	/**
	 Display all comment entrys for the specified report
	 */
	public function get_blog() {
		 
		/* 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 blog"));

		/* 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 blog ORDER BY id DESC LIMIT ".$start.", ".$limit);

		/* Now get the page list and echo it */
		$pagelist = $p->pageList($_GET['page'], $pages);

		if ( $r !== false && mysql_num_rows($r) > 0 ) {
			while ( $a = mysql_fetch_assoc($r) ) {
				$title = stripslashes($a['title']);
				$bodytext = stripslashes($a['bodytext']);
				$username = $a['name'];
				$gravatar = 'http://www.gravatar.com/avatar/' . $a['email'] . '?s=48';
				$date = $a['created'];

				$entry_display .= <<<ENTRY_DISPLAY
<div class="post">
<img src="$gravatar" />   <b>$username : $title</b><hr/>
<p>     $bodytext
    <hr class="commentfooter" /> Posted: $date</p>
</div>
<br/>
ENTRY_DISPLAY;
			}
			$entry_display .= '<div align="center">'.$pagelist.'</div>';
		}
		else {
			$entry_display .= <<<ENTRY_DISPLAY
    <p>
      No entries have been made on this page. 
    </p>

ENTRY_DISPLAY;
		}

		/*
		 $entry_display .= <<<ADMIN_OPTION

		<p class="admin_link">
		<a href="{$_SERVER['PHP_SELF']}?new_comment=1">Add a New Entry</a>
		</p>

		ADMIN_OPTION;
		*/
		return $entry_display;
	}

	/*
	 This function will be used to display the new comment entry form
	*/
	public function display_post() {
		return <<<ADMIN_FORM
    <h2>
    New Blog Entry
    </h2>
    <form action="{$_SERVER['PHP_SELF']}" method="post">
    
      <input type="hidden" name="blog" value="1" />
      
      <div class="clearfix">
        <label for="title">Title</label><br />
        <div class="input">
          <input name="title" id="title" type="text" maxlength="55"  class="xlarge" required/>
        </div>
      </div>
     
     <div class="clearfix">
      <label for="bodytext">Body Text</label><br />
      <div class="input">
        <textarea name="bodytext" id="bodytext" rows=10 cols=56 class="xxlarge"required></textarea>
      </div>
      </div>
      
      <div class="actions">
        <button type="submit" class="btn primary" onclick="checkRequired(this.forum)" >Create This Entry</button>
        <button type="reset" class="btn">Cancel</button>
      </div>
    </form>

ADMIN_FORM;
	}

	public function write($p) {
		if ( $p['title'] )
		$title = mysql_real_escape_string(strip_tags($_POST['title']));
		if ( $p['bodytext'])
		$bodytext = mysql_real_escape_string(strip_tags($_POST['bodytext']));
		$name = $_SESSION['username'];
		$email = $_SESSION['email'];

		if ( $title && $bodytext ) {
			$created = date("g:iA M d, Y");
			$sql = "INSERT INTO blog (title, bodytext, created, name, email)VALUES('$title','$bodytext','$created','$name', '$email' )";
			return mysql_query($sql) or die("Could not select database. " . mysql_error());
		} else {
			return false;
		}
	}

	/**
	 This function connects to 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();
	}

	private function buildDB() {
		$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS blog (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title		VARCHAR(150),
bodytext	TEXT,
created		VARCHAR(100),
name		VARCHAR(20),
email		VARCHAR(100)
)
MySQL_QUERY;

		return mysql_query($sql);
	}

}

?>