guestbook.php 3.12 KB
<?php

/**
 * Project: Guestbook of OEDL Homepage. This is derived from the Smarty
 *          Guestbook example. The original comment to this code can be found
 *          below.
 * Changes: First of all this guestbook uses gettext to achive i18n
 *          On mozilla i try to figure out the language to use by the 
 *          information provided by the browser. It seems that this works
 *          also for IE, sadly konqueror behaves different. Well, on
 *          IE and konquerer it is not possible to view the HP correctly
 *          'cause of different reasons, at the moment.
 *          The next big change is, that i moved the page from Smarty to
 *				Savant2 this was done with absolutely no pain at all.
 *				There might ba additions to the functionality of this class
 *				in future.
 *	Author: Georg Steffers <georg [AT] steffers [DOT] org>
 *	Date: Oct 26th, 2006
 *	File: guestbook.php
 *	Version: 0.1
 */

/**
 * Original comment
 * Project: Guestbook Sample Smarty Application
 * Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
 * Date: March 14th, 2005
 * File: guestbook.lib.tpl
 * Version: 1.0
 */

require_once (OEDL_DIR . 'libs/guestbook_db.php');

/**
 * guestbook application library
 *
 */
class Guestbook 
{
	// database object
	var $gdDb  = null;
	// error number
	var $error = 0;
	// error messages
	var $err_msg = array ();

	/**
	 * class constructor
	 */
	function __construct ()
	{
		// instantiate the sql object
		$this->gbDb =& new GuestbookDb (OEDL_DIR . 'db/guestbook.db');
		$this->gbDb->open ();

		// initialize error messages
		array_push 
			(
				$this->err_msg, 
				_("No error"),
				_("You must supply a name"), 
				_("You must supply a comment")
			);
	}

	function __destruct ()
	{
		if ($this->gbDb)
			$this->gbDb->close ();
	}

	/**
	 * display the guestbook entry form
	 *
	 * @param array $formvars the form variables
	 */
	function getFormData ($formvars = array ('Name' => '', 'Comment' => ''))
	{
		return array (
			'post' => $formvars,
			'error' => $this->error,
			'err_msg' => $this->err_msg[$this->error],
			'contentTpl' => 'guestbook_form.tpl.php'
		);
	}

	/**
	 * fix up form data if necessary
	 *
	 * @param array $formvars the form variables
	 */
	function mungeFormData (&$formvars)
	{
		// trim off excess whitespace
		$formvars['Name']    = trim ($formvars['Name']);
		$formvars['Comment'] = trim ($formvars['Comment']);
	}

	/**
	 * test if form information is valid
	 *
	 * @param array $formvars the form variables
	 */
	function isValidForm ($formvars)
	{
		// reset error message
		$this->error = null;

		// test if "Name" is empty
		if (strlen ($formvars['Name']) == 0) 
		{
			$this->error = 1;
			return false; 
		}

		// test if "Comment" is empty
		if (strlen ($formvars['Comment']) == 0) 
		{
			$this->error = 2;
			return false; 
		}

		// form passed validation
		return true;
	}

	/**
	 * add a new guestbook entry
	 *
	 * @param array $formvars the form variables
	 */
	function addEntry ($formvars) 
	{
		return $this->gbDb->addEntry ($formvars['Name'], $formvars['Comment']);
	}

	/**
	 * get the guestbook entries
	 */
	function getEntries () 
	{
		return $this->gbDb->getEntries ();   
	}
}

?>