<?php

class AKB_NEWS
{
	var $_data = array();
	var $_newsid = 0;

	/**
	* Constructor
	* Setup some variables for this class
	*
	* @return void
	*/
	function AKB_NEWS()
	{
		// Get the news id (if any)
		$this->_SetId();
		$this->_SetData();
	}

	/**
	* HandlePage
	* The function which works out what to do with this page
	*
	* @return void
	*/
	function HandlePage()
	{
		// Determine which function to load
		$todo = isset($_REQUEST['ToDo']) ? $_REQUEST['ToDo'] : '';
		switch ($todo) {
			default:
			$this->ShowNewsPage();
		}
	}

	/**
	* _GetId
	* Gets the current news id
	*
	* @return int
	*/
	function _GetId()
	{
		return $this->_newsid;
	}

	/**
	* _GetId
	* Sets the current news id
	*
	* @return bool Returns true if clean urls is off and $_GET['news']
	* is an integer
	*/
	function _SetId()
	{
		// If we arn't using clean urls then getting the id is easy
		if (!$GLOBALS['cleanUrls'] && isset($_GET['newsid'])) {
			$this->_newsid = (int) $_GET['newsid'];
			return true;
		}

		// Get the category from the request_uri and work out its ID
		$path = $_SERVER['REQUEST_URI'];
		if (strstr($path, '/news/')) {
			$iPos = strpos($path, '/news/') + strlen('/news/');
		} else {
			$iPos = 0;
		}
		$id = substr($path, $iPos, strlen($path));
		$id = eregi_replace('/$', '', $id);

		$arrNews = explode('/', $id);

		if (sizeof($arrNews) >= 1 && is_numeric($arrNews[0])) {
			$id = $arrNews[0];
		}
		$this->_newsid = $id;
	}

	/**
	* _SetData
	* Populate the $this->_data array with information about the news
	*
	* @return void
	*/
	function _SetData()
	{
		if (is_numeric($this->_GetId())) {
			$query  = "SELECT n.newsid, n.title, n.content, n.lastupdated, n.visible, n.userid, u.username, u.firstname, u.lastname, u.email ";
			$query .= "FROM ".$GLOBALS['tablePrefix']."newss n, ".$GLOBALS['tablePrefix']."users u ";
			$query .= "WHERE 1=1 ";
			$query .= "AND n.userid = u.userid ";
			$query .= "AND n.newsid = '". $this->_GetId() ."'";
			$query .= $GLOBALS['AKB_DB']->AddLimit(0, 1);
			$result = $GLOBALS['AKB_DB']->Query($query);

			if ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
				foreach ($row as $k=>$v) {
					$this->_data[$k] = $v;
				}
			}
		}
	}

	/**
	* ShowNewsPage
	* Show the news page
	*
	* @return void
	*/
	function ShowNewsPage()
	{
		// Load the home page of the front end
		if (empty($this->_data)) {
			$this->_SetData();
		}

		$GLOBALS['NewsId']			= $this->_data['newsid'];

		if (trim($this->_data['firstname']) != '' || trim($this->_data['lastname']) != '') {
			$GLOBALS['NewsAuthor'] = htmlspecialchars($this->_data['firstname'], ENT_QUOTES, $GLOBALS['charset']) .' '.htmlspecialchars($this->_data['lastname'], ENT_QUOTES, $GLOBALS['charset']);
		} else {
			$GLOBALS['NewsAuthor'] = htmlspecialchars($this->_data['username'], ENT_QUOTES, $GLOBALS['charset']);
		}

		$GLOBALS['Content']				= $this->_data['content'];
		$GLOBALS['Title']				= htmlspecialchars($this->_data['title'], ENT_QUOTES, $GLOBALS['charset']);
		$GLOBALS['LastUpdatedDisplay'] 	= htmlspecialchars(sprintf(GetLang('NewsLastUpdated'), $GLOBALS['NewsAuthor'], date("jS \of F Y h:i A",strtotime($this->_data['lastupdated'])), ENT_QUOTES, $GLOBALS['charset']));
		$GLOBALS['LastUpdated'] 		= htmlspecialchars(date("jS \of F Y h:i A",strtotime($this->_data['lastupdated'])), ENT_QUOTES, $GLOBALS['charset']);

		// get the data from database if it's not exist.
		$GLOBALS['AKB_CLASS_TEMPLATE']->SetPageTitle($GLOBALS['knowledgebaseName']);
		$GLOBALS['AKB_CLASS_TEMPLATE']->SetTemplate('News');
		$GLOBALS['AKB_CLASS_TEMPLATE']->ParseTemplate();
	}
}

?>