<?php

	CLASS AKB_CATEGORIES_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['Categories'] = $this->GetCategoryTable();
			
			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 '__addarticle':
					$GLOBALS['AddArticle'] = '1';
				break;
			}

			
		}

		/**
		* GetCategoryTable
		* Get an html table of categories for displaying to the user
		*
		* @return string the html to display
		*/
		function GetCategoryTable()
		{
			if (isset($GLOBALS['CategoryId'])) {
				$catid = (int) $GLOBALS['CategoryId'];
			} else {
				$catid = 0;
			}

			$output = '';
			$count = 0;

			$GLOBALS['AKB_CLASS_HELPER']->getCatsInfo();
			
			if (isset($GLOBALS['categoryRestrictOption']) && $GLOBALS['categoryRestrictOption'] == "1") {
				$accessible_cats = $GLOBALS['AKB_CLASS_HELPER']->GetPermittedCats();
			} else {
				$accessible_cats = false;
			}
			
			if (isset($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid])
				&& is_array($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid])
				&& !empty($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid])) {
				// Return a 3xn table of categories ordered by name
				$output = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridHeader');
				
				foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid] as $rowid) {
					$row = $GLOBALS['AKB_CLASS_HELPER']->catsById[$rowid];
					
					if (($accessible_cats !== false) && (!in_array($row['categoryid'],$accessible_cats)) && ($row['security'] == "private")) {
						continue;
					}
					
					$children = $GLOBALS['AKB_CLASS_HELPER']->GetNumQuestionsInCategory($row['categoryid']);
					
					// Every 3rd category print start a new row
					if ($count % CATEGORIES_PER_ROW == 0) {
						$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowHeader');
					}

					$GLOBALS['Icon'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['icon']);
					$GLOBALS['Description'] = strip_tags($row['description'],"<a>");
					$GLOBALS['Link'] = GetUrl('category', $row['categoryid']);
					$GLOBALS['Text'] = htmlspecialchars($GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['name']),ENT_QUOTES,$GLOBALS['charset']);
					$GLOBALS['QuestionCount'] = $children;

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

					// Every 3rd category end a row
					if ($count % CATEGORIES_PER_ROW == CATEGORIES_PER_ROW - 1) {
						$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowFooter');
					}

					$count++;
				}

				if ($count % CATEGORIES_PER_ROW != CATEGORIES_PER_ROW) {
					$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowFooter');
				}

				$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridFooter');
			} else {
				if ($catid == 0) {
					$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowHeader');
					$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowCellNoCategories');
					$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowFooter');
					$GLOBALS['HideSubList'] = '';
				} else {
					if (isInlineHelp()) {
						$GLOBALS['HideSubList'] = 'none';
					} else {
						$output .= GetLang('catNoSubCategories');
					}
				}
			}

			return $output;
		}

	}

?>
