<?php
/**
 This class is used to maintian the guests table in the database
 @author ricky barrette
 @author Twenty Codes, LLC
 */
class guest {

	var $host;
	var $username;
	var $password;
	var $table;

	/**
	 * Contructor
	 * @param String $host
	 * @param String $username
	 * @param String $password
	 * @param String $db
	 */
	public function guest($host, $username, $password, $db){
		$this->host = $host;
		$this->username = $username;
		$this->password = $password;
		$this->table = $db;
		$this->connect();
	}

	/**
	 Generates and sends a email to notify users of their email
	 */
	function sendEmail($userEmail, $title, $body){
		mail( $userEmail , $title , $body);
	}

	public function email_guests($title, $body){
		$r = mysql_query("SELECT * FROM guests");
		if ( $r !== false && mysql_num_rows($r) > 0 )
		while ( $a = mysql_fetch_assoc($r) )
		$this->sendEmail($a['email'], $title, $body);
	}

	/**
	 Display all users as links that remove them
	 */
	public function display_guest_list() {

		$entry_display .= '<h2>Guest List:</h2>';
		 
		/* Instantiate class */
		require_once("pager.php");
		$p = new Pager;

		/* Show many results per page? */
		$limit = 10;

		/* 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 guests"));

		/* 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 guests ORDER BY f_name 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) ) {
				$address = $a['address'];
				$email = $a['email'];
				$name = $a['f_name'].' '.$a['l_name'];
				$plus_one = $a['plus_one_f_name'].' '.$a['plus_one_l_name'];

				$entry_display .= <<<GUEST
          <b>$name & $plus_one</b> 
          <br/>
				$address
          <br/>
				$email
        <br/>
        <br/>
GUEST;
			}
			$entry_display .= '<div align="center">'.$pagelist.'</div>';
		}
		else
		$entry_display .= '<p>No entries have been made on this page. </p>';

		return  $entry_display;
	}

	public function get_guest_count(){
		$sql="select * from guests";
		$result=mysql_query($sql);
		return mysql_num_rows($result);
	}

	/**
	 inserts a new user into the database
	 @author ricky barrette
	 */
	public function new_guest($f_name, $l_name, $address, $email, $plus_one_f_name, $plus_one_l_name) {
		$sql="select * from guests where email='$email'";
		$result=mysql_query($sql);
		$count=mysql_num_rows($result);
		if($count==1) // If there is a match.
		die("User/Email already exists");
		$email = mysql_real_escape_string(strip_tags($email));
		$f_name = mysql_real_escape_string(strip_tags($f_name));
		$l_name = mysql_real_escape_string(strip_tags($l_name));
		$address = mysql_real_escape_string(strip_tags($address));
		$hash=md5($email);

		$sql = "INSERT INTO guests (f_name, l_name, address, email, hash, plus_one_f_name, plus_one_l_name) VALUES('$f_name','$l_name','$address','$email','$hash','$plus_one_f_name','$plus_one_l_name')";
		return mysql_query($sql) or die("Could not insert. " . mysql_error());
	}

	/**
	 Connects to the database
	 @author ricky barrette
	 */
	public function connect() {
		$connection = 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 users table
	 @author ricky barrette
	 */
	private function buildDB() {
		$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS guests (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
f_name		TEXT,
l_name		TEXT,
address		TEXT,
email     TEXT,
hash      TEXT,
plus_one_f_name		TEXT,
plus_one_l_name		TEXT)
MySQL_QUERY;
		return mysql_query($sql);
	}

}
?>
