<?php
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'class.api.php');

class API_NEWS extends API
{
	var $newsid = 0;
	var $title = '';
	var $content = '';
	var $lastupdated = '';
	var $visible = '';
	var $userid = '';

	var $fields = array (
	'newsid',
	'title',
	'content',
	'lastupdated',
	'visible',
	'userid'
	);

	var $pk = 'newsid';

	/**
	* create
	* create a new news item in the database
	*
	* @return bool was the creation successful ?
	*/
	function create()
	{
		$_POST['lastupdated'] = date('Y-m-d H:i:s');
		return parent::create();
	}

	/**
	* multiDelete
	* Delete multiple rating ids. Update each associated question.
	*
	* @return bool was the delete successful?
	*/
	function multiDelete($ids) {
		//Delete from the main table
		if (!parent::multiDelete($ids)) {
			return false;
		}
		return true;
	}

	/**
	* validate_newsid
	*
	* Ensure the news item id is a pos int
	*
	* @param string $var
	*
	* @return bool
	*/
	function validate_newsid($var)
	{
		return $this->is_positive_int($var);
	}

	/**
	* validate_userid
	*
	* Ensure the news item id is a pos int
	*
	* @param string $var
	*
	* @return bool
	*/
	function validate_userid($var)
	{
		return $this->is_positive_int($var);
	}

	/**
	* validate_visible
	*
	* Ensure visible is a 0 or a 1
	*
	* @param string $var
	*
	* @return bool
	*/
	function validate_visible($var)
	{
		return ($var == 0 || $var == 1);
	}

	/**
	* validate_lastupdated
	*
	* Ensure the lastupdated date of news item is in the standard date format
	*
	* @param string $var
	*
	* @return bool
	*/
	function validate_lastupdated($var)
	{
		return $this->is_standard_date($var);
	}

	/**
	* validate_title
	*
	* Ensure the title isn't empty and isn't too long
	*
	* @param string $var
	*
	* @return bool
	*/
	function validate_title($var)
	{
		if (empty($var)) {
			return false;
		}

		if (strlen($var) > 200) {
			return false;
		}

		return true;
	}

	/**
	* getTotalVisibleNews
	*
	* Get the total of current visible news items
	*
	* @return int
	*/
	function getTotalVisibleNews () {

		if ((isset($GLOBALS['newsQuestionNumber'])) && ((int)$GLOBALS['newsQuestionNumber'] > 0)) {
			$newsQuestionNumber = (int)$GLOBALS['newsQuestionNumber'];
		} else {
			$newsQuestionNumber = DEFAULT_NEWS;
		}

		// Get a list of visible News item sorted by score
		$query  = "SELECT COUNT(*) as total ";
		$query .= "FROM ".$GLOBALS['tablePrefix']."newss n, ".$GLOBALS['tablePrefix']."users u ";
		$query .= "WHERE 1=1 ";
		$query .= "AND n.userid = u.userid ";
		$query .= "AND n.visible = 1 ";
		return $GLOBALS['AKB_DB']->FetchOne($query);
	}
}

?>
