<?php

	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
	include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/guestbook/class/dao/guestbookrecord.class.php" );

	class GuestbookData extends Model
	{
		var $_prefix;

		var $_totalPosts;
		var $_totalPages;

		function GuestbookModel()
		{
			$this->Model();

			$this->_prefix = $this->getPrefix();

			$this->_totalPosts = 0;
			$this->_totalPages = 0;
		}

		function checkTables()
		{
			$table = $this->_prefix."guestbook";

			$field = "
				id I(10) NOTNULL AUTO PRIMARY,
				blog_id I(10) DEFAULT 0 NOTNULL INDEX,
				topic X NOTNULL DEFAULT '',
				content X NOTNULL DEFAULT '',
				date T,
				user_email C(64) DEFAULT '',
				user_url C(128) DEFAULT '',
				user_name C(30) DEFAULT '' NOTNULL,
				parent_id I(10) DEFAULT 0,
				client_ip C(15) DEFAULT '0.0.0.0' NOTNULL,
				private I(1) DEFAULT 0,
				spam_rate F DEFAULT 0
				";

			$dict = NewPDbDataDictionary( $this->_db );
			$sqlArray = $dict->ChangeTableSQL( $table, $field );
			$result = $dict->ExecuteSQLArray( $sqlArray );

			$query = Array(	"ALTER TABLE ".$table." ADD KEY blog_id(blog_id)",
							"ALTER TABLE ".$table." ADD KEY parent_id(parent_id)");

			foreach($query as $queryCode)
				$this->_db->Execute($queryCode);

			return true;
		}

		function deleteData($id)
		{
			$query = "DELETE FROM ".$this->_prefix."guestbook WHERE id = ".$id." LIMIT 1;";

			$result = $this->_db->Execute($query);

			if( $result == false )
				return false;

           	return true;
		}

		function insertData($blogId, $topic = "", $content = "", $date = null, $userName = "", $userEmail = "", $userUrl = "", $clientIp = "0.0.0.0", $parentId = 0, $private = false, $spamRate = 0)
		{
			$tf = new TextFilter();

			$topic = $tf->filterHTML($topic);
			$content = $tf->filterHTML($content);

			$query = "INSERT INTO ".$this->_prefix."guestbook (blog_id,topic,content,user_name,user_email,user_url,date,client_ip,parent_id,private,spam_rate) VALUES (".$blogId.",'".Db::qstr($topic)."','".Db::qstr($content)."','".$userName."','".$userEmail."','".$userUrl."','".$date."','".$clientIp."','".$parentId."','".$private."','".$spamRate."');";

			$result = $this->_db->Execute($query);

			if( $result == false )
				return false;

           	return true;
		}

		function getTotalPosts($blogId)
		{
			$query = "SELECT * FROM ".$this->_prefix."guestbook WHERE blog_id = ".$blogId.";";

			$result = $this->_db->Execute($query);

			if( $result )
				$this->_totalPosts = $result->RecordCount();
			else
				$this->_totalPosts = 0;

			return $this->_totalPosts;
		}

		function getTotalPages($blogId, $postPerPage)
		{
			if($this->_totalPosts == 0)
				$this->getTotalPosts($blogId);

			$this->_totalPages = (int)(($this->_totalPosts)/($postPerPage));

			if($this->_totalPosts>(($postPerPage)*($this->_totalPages)))
				$this->_totalPages += 1;

			return $this->_totalPages;
		}

		function getPagePosts($blogId, $currentPage, $postPerPage)
		{
			if($this->_totalPosts == 0)
				$this->getTotalPosts($blogId);

			if($this->_totalPages == 0)
				$this->getTotalPages($blogId, $postPerPage);

			if($currentPage > $this->_totalPages)
				$currentPage = $this->_totalPages;

			$pageStartPost = ($currentPage - 1)*($postPerPage);
			$pageEndPost = ($currentPage)*($postPerPage);

			$query = "SELECT * FROM ".$this->_prefix."guestbook WHERE blog_id = ".$blogId;
			$query .= " ORDER BY date DESC";
			$query .= " LIMIT ".$pageStartPost.",".$pageEndPost;
			$query .= ";";

			$result = $this->_db->Execute($query);

			if( !$result )
				return false;

			$pagePosts = Array();

			while($row = $result->FetchRow())
			{
				array_push($pagePosts, $this->_fillRecord($blogId, $row));
			}

			return $pagePosts;
		}

		function getPost($blogId, $id)
		{
			$query = "SELECT * FROM ".$this->_prefix."guestbook WHERE id = ".$id.";";

			$result = $this->_db->Execute($query);

			if( !$result )
				return false;

			$post = new GuestbookRecord($blogId);

			$row = $result->FetchRow();

			$post->setId($row["id"]);
			$post->setTopic($row["topic"]);
			$post->setContent($row["content"]);
			$post->setDate($row["date"]);
			$post->setUserName($row["user_name"]);
			$post->setUserEmail($row["user_email"]);
			$post->setUserUrl($row["user_url"]);
			$post->setClientIp($row["client_ip"]);
			$post->setParentId($row["parent_id"]);
			$post->setPrivate($row["private"]);
			$post->setSpamRate($row["spam_rate"]);

			return $post;
		}

		function _fillRecord($blogId, $row)
		{
			$record = new GuestbookRecord($blogId);

			$record->setId($row["id"]);
			$record->setTopic($row["topic"]);
			$record->setContent($row["content"]);
			$record->setDate($row["date"]);
			$record->setUserName($row["user_name"]);
			$record->setUserEmail($row["user_email"]);
			$record->setUserUrl($row["user_url"]);
			$record->setClientIp($row["client_ip"]);
			$record->setParentId($row["parent_id"]);
			$record->setPrivate($row["private"]);
			$record->setSpamRate($row["spam_rate"]);

			return $record;
		}
	}
?>