<?php

class AKB_FAVORITES_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()
	{
		$GLOBALS['FavoritesGrid'] = '';
		$GLOBALS['Message'] = '';
		$GLOBALS['ResultHeading'] = GetLang('hpFavoritedQuestions');

		if (!isset($_COOKIE[$GLOBALS['cookiePrefix'].'favs'])) {
			$GLOBALS['Message'] = GetLang('hpNoFavoriteQuestions');
			return;
		}

		$question_ids = CheckIntVars(explode(',', $_COOKIE[$GLOBALS['cookiePrefix'].'favs']));

		$acccessible_cats = $GLOBALS['AKB_CLASS_HELPER']->getAccessibleCats();
		
		if (!sizeof($acccessible_cats)) {
			$acccessible_cats = "''";
		} else {
			$acccessible_cats = implode(',', $acccessible_cats);
		}
		
		$workflowQry = '';
		if (validUrl()) {
			$workflowQry = " AND ( q.workflowstatus = '".WF_STATUS_APPROVED."' OR q.workflowstatus = '".WF_STATUS_AUTO_APPROVED."' OR q.workflowstatus = '".WF_STATUS_REPENDING."' ) ";	
		}
		$query = "SELECT q.questionid as questionid, title, answer, workflowstatus
		FROM ".$GLOBALS['tablePrefix']."questions q
		WHERE q.questionid IN (

			SELECT DISTINCT q.questionid
			FROM ".$GLOBALS['tablePrefix']."questions q, ".$GLOBALS['tablePrefix']."categoryassociations a
			WHERE q.questionid = a.questionid
			$workflowQry
			AND q.visible = '1'  
			AND q.questionid IN (".implode(',', $question_ids).")
			AND a.categoryid IN (".$acccessible_cats.")
		)";
		$result = $GLOBALS['AKB_DB']->Query($query);

		// If there are no results don't bother generating a list
		if ($GLOBALS['AKB_DB']->CountResult($query) == 0) {
			$GLOBALS['Message'] = GetLang('hpNoFavoriteQuestions');
			return;
		}

		$GLOBALS['FavoritesGrid'] = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionFavoritesListHeader');

		$tmpRow = array();
		$historyQuestionIds = array();
		while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
			if (validUrl()) {
				if ($row['workflowstatus'] != WF_STATUS_REPENDING) {
					$tmpRow[] = $row;
				} else {
					$row['title'] = '';
					$tmpRow[] = $row;
					$historyQuestionIds[] = trim($row['questionid']);
				}
			} else {
				$tmpRow[] = $row;
			}
		}
		
		if (sizeof($historyQuestionIds)) {
			$historyQuestionIdsStr = "'" . implode("','", $historyQuestionIds) . "'";
			$historyQuestionIdsQry = "SELECT questionid, title, answer, workflowstatus 
			FROM {$GLOBALS['tablePrefix']}questions_history
			WHERE (workflowstatus = '".WF_STATUS_AUTO_APPROVED."' OR workflowstatus = '".WF_STATUS_APPROVED."')
			ORDER BY lastupdated ASC
			";
			$historyQuestionIdsQryRes = $GLOBALS['AKB_DB']->Query($historyQuestionIdsQry);
			$historyQuestion = array();
			if ($GLOBALS['AKB_DB']->CountResult($historyQuestionIdsQry) > 0) {
				while ($row = $GLOBALS['AKB_DB']->Fetch($historyQuestionIdsQryRes)) {
					$historyQuestion[$row['questionid']] = $row;
				}
			}
		}
		
		foreach ($tmpRow as $row) {
			if (isset($historyQuestion[$row['questionid']])) {
				$row['title'] = $historyQuestion[$row['questionid']]['title'];
				$row['answer'] = $historyQuestion[$row['questionid']]['answer'];
				$row['workflowstatus'] = $historyQuestion[$row['questionid']]['workflowstatus'];
			}
			if ($row['title'] != '') {
				$GLOBALS['Title'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['title']);
				$GLOBALS['Description'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(ellipsize(strip_tags($row['answer']), 200));
				$GLOBALS['Link'] = GetUrl('question', $row['questionid']);
				$GLOBALS['FavoritesGrid'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionFavoritesListRow');
			}
		}

		$GLOBALS['FavoritesGrid'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionFavoritesListFooter');
	}

}

?>
