<?php

	CLASS AKB_CATEGORYQUESTIONS_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
			$numQuestions = $GLOBALS['AKB_CLASS_CATEGORIES']->numQuestions;
			$GLOBALS['QuestionList'] = $this->GetQuestionList();
			$GLOBALS['CategoryDescription'] = $this->GetCategoryDesc();

			if ($numQuestions == 1) {
				$GLOBALS['QuestionsInCategory'] = GetLang('cat1Question');
			} else {
				if ($numQuestions == 0) {
					if (isInlineHelp()) {
						$GLOBALS['CategoryName'] = "";
					} else {
						$GLOBALS['QuestionsInCategory'] = sprintf(GetLang('catNumQuestions'), $numQuestions).'.';
					}
					
				} else {
					$GLOBALS['QuestionsInCategory'] = sprintf(GetLang('catNumQuestions'), $numQuestions).':';
				}
			}
		}

		/**
		* GetCategoryDesc
		* Get the description of the category
		*
		* @return string The description
		*/
		function GetCategoryDesc()
		{
			$GLOBALS['Description'] = '';
			if (!isset($GLOBALS['CategoryId'])) {
				return '';
			}

			if (isset($GLOBALS['AKB_CLASS_HELPER']->catsById[$GLOBALS['CategoryId']]['description'])) {
				$GLOBALS['Description'] = $GLOBALS['AKB_CLASS_HELPER']->catsById[$GLOBALS['CategoryId']]['description'];
				$GLOBALS['Description'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($GLOBALS['Description']);
			}

			if (!empty($GLOBALS['Description'])) {
				return $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryDescription');
			}

		}

		/**
		* GetQuestionList
		* Get the list of questions along with a summary of the question to
		* show to the users
		*
		* @return string The html to display the list
		*/
		function GetQuestionList()
		{
			$categoryRestrictSQL = "";
			if (isset($GLOBALS['categoryRestrictOption']) && $GLOBALS['categoryRestrictOption'] == "1") {
				$accessible_cats = $GLOBALS['AKB_CLASS_HELPER']->GetPermittedCats();
				if (isset($GLOBALS['AKB_CLASS_CATEGORIES']->security) && $GLOBALS['AKB_CLASS_CATEGORIES']->security == 'public') {
					$accessible_cats[] = $GLOBALS['CategoryId'];
				}
				$accessible_cats = array_unique($accessible_cats);
				$categoryRestrictSQL = "AND a.categoryid IN ('".implode("', '", $accessible_cats)."')";
			}
			
			$workflowQry = '';
			if (validUrl()) {
				$workflowQry = 'AND (workflowstatus = \''.WF_STATUS_APPROVED.'\' OR workflowstatus = \''.WF_STATUS_AUTO_APPROVED.'\' OR workflowstatus = \''.WF_STATUS_REPENDING.'\') ';
			}
			$query = "
				SELECT q.questionid, q.title, q.answer, q.workflowstatus
				FROM ".$GLOBALS['tablePrefix']."questions q WHERE q.questionid in
				(
				SELECT  distinct  q.questionid
				FROM 
				".$GLOBALS['tablePrefix']."categories c,
				".$GLOBALS['tablePrefix']."categoryassociations a,
				(".$GLOBALS['tablePrefix']."question_auth qa RIGHT JOIN ".$GLOBALS['tablePrefix']."questions q ON qa.questionid = q.questionid)
			WHERE a.categoryid='".$GLOBALS['CategoryId'].'\'
			AND a.questionid = q.questionid
			AND c.categoryid = a.categoryid
			AND q.visible=1
			'.$workflowQry.'
			AND	
				(
				((q.startdate < \''.$GLOBALS['currDatetime']."') AND ((q.enableexpiry = 1) AND q.expirydate > '".$GLOBALS['currDatetime']."'))
				OR
				((q.startdate < '".$GLOBALS['currDatetime']."') AND (q.enableexpiry = 0))
				)
			".$categoryRestrictSQL."
			AND ((qa.groupid IS NULL) ".AKB_AUTH::deriveGroupSQL(false).") 
			
			)
			
			
			ORDER BY q.sortorder DESC,q.lastupdated DESC, q.title ASC
			
			
			
			".$GLOBALS['AKB_DB']->AddLimit($GLOBALS['start'], $GLOBALS['end']);
			$GLOBALS['Start'] = $GLOBALS['start'] + 1;
			
			$result = $GLOBALS['AKB_DB']->Query($query);
			
			if ($GLOBALS['AKB_DB']->CountResult($query) > 0) {
				$output = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionSummaryHeader');

				$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, lastupdated, workflowstatus, answer 
					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;
						}
					}
				}
				
				// while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
				foreach ($tmpRow as $row) {
					if (isset($historyQuestion[$row['questionid']])) {
						$row['title'] = $historyQuestion[$row['questionid']]['title'];
						$row['answer'] = $historyQuestion[$row['questionid']]['answer'];
						$row['lastupdated'] = $historyQuestion[$row['questionid']]['lastupdated'];
						$row['workflowstatus'] = $historyQuestion[$row['questionid']]['workflowstatus'];
					}
					if ($row['title'] != '') {
						$summary = $row['answer'];
						$summary = preg_replace('%<(style|script).*?</\1>%si', ' ', $summary);
						$summary = preg_replace('%<(br|div|td|tr|li|p).*?>%si', ' ', $summary);
						$summary = preg_replace("/\s+/", ' ', $summary);
						$summary = str_replace('&nbsp;', ' ', $summary);
						$summary = ellipsize(strip_tags($summary), 300);
						$summary = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($summary);
	
						$GLOBALS['questionid'] = $row['questionid'];
						$GLOBALS['urlsafetitle'] = $GLOBALS['AKB_CLASS_HELPER']->_MakeURLSafe($row['title']);
						
						$GLOBALS['title'] = htmlspecialchars($GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['title']),ENT_QUOTES,$GLOBALS['charset']);
	
						$GLOBALS['summary'] = $summary;
	
						$GLOBALS['href'] = GetUrl('question', $row['questionid']);
	
						if ($GLOBALS['cleanUrls']) {
							$GLOBALS['href'] .= $GLOBALS['urlsafetitle'];
						}
	
						$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionSummary');
					}
				}
				$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('QuestionSummaryFooter');
				return $output;
			} else {
				return '';
			}
		}
	}

?>
