<?php

	CLASS AKB_VIEWQUESTION_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['FavLink'] = GetUrl('question', $GLOBALS['AKB_CLASS_QUESTIONS']->_GetId(), 'todo=__fav');
			$GLOBALS['FavJs'] = '';

			if ($GLOBALS['cleanUrls'] === true) {
				$arrURI = explode('/', $_SERVER['REQUEST_URI']);
				$todo = $arrURI[sizeof($arrURI)-1];
			} else if (isset($_GET['todo'])) {
				$todo = $_GET['todo'];
			} else {
				$todo = '';
			}

			switch ($todo) {
				case '__fp':
					$this->ShowFavortiesTab();
				break;
				case '__fav':
					$this->UpdateFavorites();
				break;
			}

			$this->SetupFavLink();
			
			//If logged in then we prefil the unhelpful ratings email.
			if (AKB_AUTH::IsLoggedIn()) {
				$GLOBALS['uhrEmail'] = $_SESSION['user']->email;
			} else {
				$GLOBALS['uhrEmail'] = GetLang('WhyUnhelpfulEmail');
			}
		}

		/**
		* ReloadLeftFrame
		*
		* Print javascript to reload the left frame
		*
		* @return void
		*/
		function ShowFavortiesTab()
		{
			$GLOBALS['FavJs'] = 'if (parent.leftFrame) { parent.leftFrame.location = "'.$GLOBALS['pathToKB'].'/loadpanel.php?Panel=Tree&Favs=1"; }'."\n";
		}

		/**
		* UpdateFavorites
		* Update the favorites cookie then reload the current page to
		*
		* @return void
		*/
		function UpdateFavorites()
		{
			$todo= '';
			$c = '';
			$favs = array();
			$questionid = $GLOBALS['AKB_CLASS_QUESTIONS']->_GetId();

			// Work out if we are adding or deleting a question from the favorites
			if (isset($_COOKIE[$GLOBALS['cookiePrefix'].'favs'])) {
				$favs = explode(',', $_COOKIE[$GLOBALS['cookiePrefix'].'favs']);
				if (in_array($questionid, $favs)) {
					$add = false;
				} else {
					$add = true;
				}
			} else {
				$add = true;
			}

			// Add/Delete the question from our favorites array as required
			if ($add) {
				$GLOBALS['InFavs'] = true;
				if (is_array($favs)) {
					$favs[] = $questionid;
				}
			} else {
				if (is_array($favs)) {
					foreach ($favs as $key => $val) {
						if ($val == $questionid) {
							unset($favs[$key]);
						}
					}
				}
			}

			$c = implode(',', $favs);

			@ob_end_clean();
			// We have to set the $_COOKIE var after the cookie is set so our
			// change is reflected on the page
			setcookie($GLOBALS['cookiePrefix'].'favs', $c, time() + ( 3600 * 24 * 365), '/');
			$_COOKIE[$GLOBALS['cookiePrefix'].'favs'] = $c;

			// Redirect back to the question
			$path = GetUrl('question', $questionid, 'todo=__fp', false);

			$GLOBALS['FavJs'] = 'window.setTimeout(\'document.location.href = "'.$path.'";\', 100);'."\n";
		}

		/**
		* SetupFavLink
		* Create a link to either add or remove a question from the list of
		* favorites depending on if it is already a favorite or not
		*
		* @return void
		*/
		function SetupFavLink()
		{
			if (isset($_COOKIE[$GLOBALS['cookiePrefix'].'favs'])) {
				$favs = explode(',', $_COOKIE[$GLOBALS['cookiePrefix'].'favs']);
				if (in_array($GLOBALS['AKB_CLASS_QUESTIONS']->_GetId(), $favs) || isset($GLOBALS['InFavs'])) {
					$GLOBALS['FavText'] = GetLang('questRemoveFavs');
				} else {
					$GLOBALS['FavText'] = GetLang('questAddFavs');
				}
			} else {
				$GLOBALS['FavText'] = GetLang('questAddFavs');
			}
		}

	}

?>
