<?php

	include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/guestbook/class/view/adminguestbookview.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/guestbook/class/dao/guestbookdata.class.php" );

	class AdminUpdateGuestbookAction extends AdminAction
	{
		var $_pluginEnabled;
		var $_postPerPage;

		function AdminUpdateGuestbookAction($actionInfo, $request)
		{
			$this->AdminAction($actionInfo, $request);
		}

		function checkTables()
		{
			$guestbookData = new GuestbookData();

			$guestbookData->checkTables();
		}

		function validate()
		{
			$this->_pluginEnabled = $this->_request->getValue("pluginEnabled");
			$this->_pluginEnabled = ($this->_pluginEnabled != "" );

			if($this->_pluginEnabled)
			{
				$this->checkTables();
			}

			$this->_postPerPage = $this->_request->getValue("postPerPage");

			if($this->_postPerPage <= 0  || !ctype_digit($this->_postPerPage))
			{
				$this->_view = new AdminGuestbookView($this->_blogInfo);
				$this->_view->setErrorMessage($this->_locale->tr("guestbook_error_postperpage"));
				$this->setCommonData();

				return false;
			}

			return true;
		}

		function perform()
		{
			$blogSettings = $this->_blogInfo->getSettings();

			$blogSettings->setValue("plugin_guestbook_enabled", $this->_pluginEnabled);
			$blogSettings->setValue("plugin_guestbook_postperpage", $this->_postPerPage);

			$this->_blogInfo->setSettings($blogSettings);

			$blogs = new Blogs();

			if( !$blogs->updateBlog( $this->_blogInfo ))
			{
				$this->_view = new AdminGuestbookView( $this->_blogInfo );
				$this->_view->setErrorMessage( $this->_locale->tr("error_updating_settings"));

				$this->setCommonData();

				return false;
			}

			$this->_blogInfo->setSettings($blogSettings);
			$this->_session->setValue("blogInfo", $this->_blogInfo);

			$this->saveSession();

			$this->_view = new AdminGuestbookView($this->_blogInfo);
			$this->_view->setSuccessMessage($this->_locale->tr("guestbook_settings_saved_ok"));

			$this->setCommonData();

			CacheControl::resetBlogCache($this->_blogInfo->getId());

			return true;
		}
	}
?>
