<?php

class AKB_NEWSQUESTIONS_PANEL  extends AKB_PANEL
{
	/**
		* SetPanelSettings
		* This function is a chance to set any last minute global variables for
		* the template to use
		*
		* @return void
		*/
	function SetPanelSettings()
	{
		// Parse the panel of tokens, etc
		$GLOBALS['NewsQuestionList'] = $this->GetNewsQuestionList();
		$GLOBALS['Link'] = GetUrl('rss', '', 't=news');
		if ((empty($GLOBALS['frontendUser']) || empty($GLOBALS['frontendPass']))) {
			$GLOBALS['RssButton'] = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RssLink');
		} else {
			$GLOBALS['RssButton'] = '';
		}
	}

	/**
		* GetNewsQuestionList
		* Get a list of news questions and generate the html for displaying
		* the list to the user
		*
		* @return string The html for the list
		*/
	function GetNewsQuestionList()
	{
		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 n.newsid, n.title, 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.visible = 1 ";
		$query .= "ORDER BY n.lastupdated DESC ";
		$query .= $GLOBALS['AKB_DB']->AddLimit(0, $newsQuestionNumber);

		$result = $GLOBALS['AKB_DB']->Query($query);

		if ($GLOBALS['AKB_DB']->CountResult($query) > 0) {
			$output = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('NewsQuestionsListHeader');

			while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
				$link = GetUrl('news', $row['newsid']);
				if ($GLOBALS['cleanUrls']) {
					$link .= $GLOBALS['AKB_CLASS_HELPER']->_MakeURLSafe($row['title']);
				}
				$GLOBALS['Link'] = $link;
				$GLOBALS['Text'] = htmlspecialchars($GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['title']),ENT_QUOTES,$GLOBALS['charset']);
				if (trim($row['firstname']) != '' || trim($row['lastname']) != '') {
					$newsAuthor = htmlspecialchars($row['firstname'], ENT_QUOTES, $GLOBALS['charset']) .' '.htmlspecialchars($row['lastname'], ENT_QUOTES, $GLOBALS['charset']);
				} else {
					$newsAuthor = htmlspecialchars($row['username'], ENT_QUOTES, $GLOBALS['charset']);
				}
				$GLOBALS['LastUpdated'] = htmlspecialchars(sprintf(GetLang('NewsLastUpdated'), $newsAuthor, date("jS \of F Y @ h:i A",strtotime($row['lastupdated'])), ENT_QUOTES, $GLOBALS['charset']));

				$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('NewsQuestionsListRow');
			}

			$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('NewsQuestionsListFooter');
		} else {
			$output = GetLang('hpNoNewsQuestions');
		}

		return $output;
	}
}
?>
