<?php

	include_once( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
	include_once( PLOG_CLASS_PATH."class/view/plugintemplatedview.class.php");
	include_once( PLOG_CLASS_PATH."class/net/client.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/guestbook/class/dao/guestbookdata.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/guestbook/class/dao/guestbookrecord.class.php" );

	class GuestbookAction extends BlogAction
	{
		var $_pluginEnabled;
		var $_data;

		var $_owner;

		var $_topic;
		var $_content;
		var $_userName;
		var $_userEmail;
		var $_userUrl;
		var $_date;
		var $_clientIp;

		var $_blogId;
		var $_pagePosts;
		var $_totalPosts;
		var $_totalPages;
		var $_currentPage;
		var $_postPerPage;

		function GuestbookAction($actionInfo, $request)
		{
			$this->BlogAction($actionInfo, $request);

			$this->_blogId = $this->_blogInfo->getId();

			if($this->_request->getValue("page"))
				$this->_currentPage = $this->_request->getValue("page");
			else
				$this->_currentPage = 1;

			$blogSettings = $this->_blogInfo->getSettings();

			$this->_pluginEnabled = $blogSettings->getValue("plugin_guestbook_enabled");
			$this->_postPerPage = $blogSettings->getValue("plugin_guestbook_postperpage");

			if(!$this->_pluginEnabled)
				return;

			$this->_now = new Date();
			$this->_clientIp = Client::getIp();

			$this->checkOwner();

			$this->_data = new GuestbookData();

			$opId = $this->_request->getValue("op");

			if($opId=="AddGuestbookPost")
			{
				$this->addPost();
			}
			else if($opId=="ReplyGuestbookPost")
			{
				$this->replyPost();
			}
			else if($opId=="DeleteGuestbookPost")
			{
				$this->deletePost();
			}

			$this->getPosts();
		}

		function checkOwner()
		{
			$this->_owner = false;

			$userInfo = $this->_session->getValue("userInfo");

			if (isset($userInfo))
			{
				if($userInfo->getId()==$this->_blogInfo->getOwner())
				{
					$this->_owner = true;
				}
			}
		}

		function getPosts()
		{
			$this->_totalPosts = $this->_data->getTotalPosts($this->_blogId);
			$this->_totalPages = $this->_data->getTotalPages($this->_blogId, $this->_postPerPage);

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

			$this->_pagePosts = $this->_data->getPagePosts($this->_blogId, $this->_currentPage, $this->_postPerPage);
		}

		function addPost()
		{
			$this->_userEmail = trim($this->_request->getValue("userEmail"));
			$this->_userUrl = trim($this->_request->getValue("userUrl"));
			$this->_userName = trim($this->_request->getValue("userName"));

			$this->_content = trim($this->_request->getValue("content"));
			$this->_topic = trim($this->_request->getValue("topic"));

			$blogId = $this->_blogId;
			$topic = Db::qstr($this->_topic);
			$content = Db::qstr($this->_content);
			$date = $this->_now->getDate();
			$userEmail = $this->_userEmail;
			$userUrl = $this->_userUrl;
			$userName = $this->_userName;
			$clientIp = $this->_clientIp;
			$private = $this->_request->getValue("private");
			$parentId = $this->_request->getValue("parentId");
			$spamRate = 0;

			$this->_data->insertData($blogId, $topic, $content, $date, $userName, $userEmail, $userUrl, $clientIp, $private, $parentId, $spamRate);
		}

		function replyPost()
		{
		}

		function deletePost()
		{
			$id = $this->_request->getValue("id");

			$this->_data->deleteData($id);
		}

		function setCommonData()
		{
			$this->_view->setValue("blogId", $this->_blogId);

			$this->_view->setValue("posts", $this->_pagePosts);

			$this->_view->setValue("totalPosts", $this->_totalPosts);
			$this->_view->setValue("totalPages", $this->_totalPages);
			$this->_view->setValue("currentPage", $this->_currentPage);
			$this->_view->setValue("postPerPage", $this->_postPerPage);

			$this->_view->setValue("owner", $this->_owner);

			return true;
		}

		function perform()
		{
			$this->_view = new PluginTemplatedView($this->_blogInfo, "guestbook", "guestbook", SMARTY_VIEW_CACHE_DISABLED);

			$this->setCommonData();
		}
	}
?>
