<?php

	/**
	* PrepareCommentAuthor
	* Display a username / email depending on which value is not empty
	*
	* @param string $Name The name to use
	* @param string $Email The email to use
	*
	* @return string The html to display
	*/
	function PrepareCommentAuthor($Name, $Email)
	{
		$output = '';

		if ($Name != '' && $Email != '') {
			$output = sprintf("<a href='mailto:%s'>%s</a>", $Email, $Name);
		} else if ($Email != '') {
			$output = sprintf("<a href='mailto:%s'>%s</a>", $Email, $Email);
		} else if ($Name != '') {
			$output = $Name;
		} else {
			$output = GetLang('questUnnknownUser');
		}
		return $output;
	}

	/**
	* PrintHeader
	* Display the backend header with menus etc depending on who is logged in
	*
	* @return void
	*/
	function PrintHeader()
	{
		$GLOBALS['textLinks'] = '';
		$GLOBALS['menuRow'] = '';
		$GLOBALS['menuScript'] = '';
		$GLOBALS['LoggedInAs'] = '';
		$t = $GLOBALS['AKB_CLASS_TEMPLATE'];

		if ($GLOBALS["AKB_CLASS_AUTH"]->IsLoggedIn()) {
			$GLOBALS['LoggedInAs'] = sprintf(GetLang('LoggedInAs'), $_SESSION['user']->username);

			// Generate text links
			$GLOBALS["textLinks"] = '<a href="index.php">'.GetLang('Home').'</a>&nbsp;&nbsp;';

			$GLOBALS['Link'] = 'index.php?ToDo=editUser&amp;userid='.$_SESSION['user']->userid;
			$GLOBALS['Text'] = GetLang('MyAccount');
			$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLink');

			$GLOBALS["textLinks"] .= sprintf("
				|<a href='#' id='ToolsMenuButton' class='PopDownMenu MenuText'>%s<img src='images/arrow_down_white.gif' border='0' /></a>
			", GetLang('Tools'));

			$GLOBALS['textLinks'] .= "<div id='ToolsMenu' class='DropDownMenu DropShadow' style='display: none; width: 140px;'><ul>";

			if ($_SESSION['user']->verify('template', 'edit')) {
				$GLOBALS['Link'] = 'index.php?ToDo=viewTemplates';
				$GLOBALS['Text'] = GetLang('mTemplates');
				$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLinkPlain');
			}

			if ($_SESSION['user']->verify('backup', 'create')
				|| $_SESSION['user']->verify('backup', 'delete')) {
				$GLOBALS['Link'] = 'index.php?ToDo=manageBackups';
				$GLOBALS['Text'] = GetLang('Backups');
				$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLinkPlain');
			}

			if ($_SESSION['user']->verify('setting', 'edit') && isset($GLOBALS['enableAttachmentSearch']) && $GLOBALS['enableAttachmentSearch']) {
				$GLOBALS['Link'] = 'index.php?ToDo=indexAttachments';
				$GLOBALS['Text'] = GetLang('AttachmentIndexing');
				$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLinkPlain');
			}

			$GLOBALS['Link'] = 'index.php?ToDo=versionCheck';
			$GLOBALS['Text'] = GetLang('VersionCheck');
			$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLinkPlain');

			$GLOBALS['Link'] = 'index.php?ToDo=systemInfo';
			$GLOBALS['Text'] = GetLang('SystemInfo');
			$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLinkPlain');

			$GLOBALS["textLinks"] .= "</ul></div>";

			if ($_SESSION['user']->verify('setting', 'edit')) {
				$GLOBALS['Link'] = 'index.php?ToDo=editSettings';
				$GLOBALS['Text'] = GetLang('Settings');
				$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLink');
			}

			$GLOBALS['Link'] = $GLOBALS['pathToKB'];
			$GLOBALS['Text'] = GetLang('ViewYKB');
			$GLOBALS['textLinks'] .= $t->GetSnippet('NavNewWindowTextLink');

			$GLOBALS['Link'] = 'index.php?ToDo=logOut';
			$GLOBALS['Text'] = GetLang('Logout');
			$GLOBALS['textLinks'] .= $t->GetSnippet('NavTextLink');



			$GLOBALS['textLinks'] .= '';

			// Generate the tabs

			// {{{ New menu
			$show_manage_questions = (
				$_SESSION['user']->verify('question', 'edit') ||
				$_SESSION['user']->verify('question', 'delete') ||
				$_SESSION['user']->verify('question', 'approve')
			);

			$show_manage_pending_questions = (
				( $_SESSION['user']->verify('question', 'edit') ||
				$_SESSION['user']->verify('question', 'delete') ||
				$_SESSION['user']->verify('question', 'approve') ) &&
				validUrl()
			);

			$show_manage_news = (
				$_SESSION['user']->verify('news', 'edit') ||
				$_SESSION['user']->verify('news', 'delete')
			);

			$show_manage_categories = (
				$_SESSION['user']->verify('category', 'edit') ||
				$_SESSION['user']->verify('category', 'delete')
			);

			$show_manage_users = (
				$_SESSION['user']->verify('user', 'edit') ||
				$_SESSION['user']->verify('user', 'delete')
			);

			$show_manage_groups = (
				$_SESSION['user']->verify('group', 'edit') ||
				$_SESSION['user']->verify('group', 'delete')
			);

			$show_manage_comments = (
				$_SESSION['user']->verify('comment', 'edit') ||
				$_SESSION['user']->verify('comment', 'delete')
			);

			$menuItems = array (
				'mnuArticles' => array (
					array (
						'text' => GetLang('ManageQuestions'),
						'link' => 'index.php?ToDo=viewQuestions',
						'show' => $show_manage_questions,
						'help' => GetLang('MnuHelpManageArticles'),
						'icon' => 'article.gif',
					),
					array (
						'text' => GetLang('CreateQuestion'),
						'link' => 'index.php?ToDo=createQuestion',
						'show' => $_SESSION['user']->verify('question', 'edit'),
						'help' => GetLang('MnuHelpCreateArticle'),
						'icon' => 'article_add.gif',
					),
					array (
						'text' => GetLang('ManageWorkflowArticles'),
						'link' => 'index.php?ToDo=viewWorkflowQuestions',
						'show' => $show_manage_pending_questions,
						'help' => GetLang('MnuHelpManageWorkflowArticles'),
						'icon' => 'article.gif',
					),
					array (
						'text' => GetLang('AdvancedSearch'),
						'link' => 'index.php?ToDo=advSearch',
						'show' => $show_manage_questions,
						'help' => GetLang('MnuHelpAdvanceSearchArticles'),
						'icon' => 'search.gif',
					),
					array (
						'text' => GetLang('SubmittedQuestions'),
						'link' => 'index.php?ToDo=reviewSubmittedQuestions',
						'show' => $_SESSION['user']->verify('question', 'review'),
						'help' => GetLang('MnuHelpViewSubmittedArticles'),
						'icon' => 'article_add.gif',
					),
					array (
						'text' => GetLang('ImportQuestions'),
						'link' => 'index.php?ToDo=importQuestions',
						'show' => $_SESSION['user']->verify('question', 'edit'),
						'help' => GetLang('MnuHelpImportArticles'),
						'icon' => 'import_articles.gif',
					),
					array (
						'text' => GetLang('ManageSubscribers'),
						'link' => 'index.php?ToDo=viewSubscribers',
						'show' => $_SESSION['user']->verify('question', 'edit'),
						'help' => GetLang('MnuHelpManageSubscribers'),
						'icon' => 'article_subscribers.gif',
					),
					array (
						'text' => GetLang('CustomFields'),
						'link' => 'index.php?ToDo=viewCustomFields',
						'show' => $_SESSION['user']->verify('question', 'edit'),
						'help' => GetLang('MnuHelpCustomFields'),
						'icon' => 'customfield.gif',
					),
					array (
						'text' => GetLang('Glossary'),
						'link' => 'index.php?ToDo=viewGlossary',
						'show' => $_SESSION['user']->verify('glossary', 'edit'),
						'help' => GetLang('MnuHelpGlossary'),
						'icon' => 'glossary.gif',
					),
				),
				'mnuNews'	=> array (
					array (
						'text' => GetLang('ManageNews'),
						'link' => 'index.php?ToDo=viewNews',
						'show' => $show_manage_news,
						'help' => GetLang('MnuHelpManageNews'),
						'icon' => 'newspaper.png',

					),
					array (
						'text' => GetLang('CreateNews'),
						'link' => 'index.php?ToDo=createNews',
						'show' => $_SESSION['user']->verify('news', 'edit'),
						'help' => GetLang('MnuHelpCreateNews'),
						'icon' => 'newspaper_add.png',
					),
				),
				'mnuCategories'	=> array (
					array (
						'text' => GetLang('ManageCategories'),
						'link' => 'index.php?ToDo=viewCategories',
						'show' => $show_manage_categories,
						'help' => GetLang('MnuHelpManageCategories'),
						'icon' => 'category.gif',

					),
					array (
						'text' => GetLang('CreateCategory'),
						'link' => 'index.php?ToDo=createCategory',
						'show' => $_SESSION['user']->verify('category', 'edit'),
						'help' => GetLang('MnuHelpCreateCategory'),
						'icon' => 'add_category.gif',
					),
				),
				'mnuGroups' => array (
					array (
						'text' => GetLang('ManageGroups'),
						'link' => 'index.php?ToDo=viewGroups',
						'show' => $show_manage_groups,
						'help' => GetLang('MnuHelpManageGroups'),
						'icon' => 'group.gif',
					),
					array (
						'text' => GetLang('CreateGroup'),
						'link' => 'index.php?ToDo=createGroup',
						'show' => $_SESSION['user']->verify('group', 'edit'),
						'help' => GetLang('MnuHelpCreateGroups'),
						'icon' => 'group_add.gif',
					),
				),
				'mnuUsers' => array (
					array (
						'text' => GetLang('ManageUsers'),
						'link' => 'index.php?ToDo=viewUsers',
						'show' => $show_manage_users,
						'help' => GetLang('MnuHelpManageUsers'),
						'icon' => 'user.gif',
					),
					array (
						'text' => GetLang('CreateUser'),
						'link' => 'index.php?ToDo=createUser',
						'show' => $_SESSION['user']->verify('user', 'edit'),
						'help' => GetLang('MnuHelpCreateUser'),
						'icon' => 'user_add.gif',
					),
				),
				'mnuComments'	=> array (
					array (
						'text' => GetLang('ManageComments'),
						'link' => 'index.php?ToDo=viewComments',
						'show' => $show_manage_comments,
						'help' => GetLang('MnuHelpViewComments'),
						'icon' => 'comment.gif',
					),
					array (
						'text' => GetLang('UnhelpfulFeedback'),
						'link' => 'index.php?ToDo=viewRatings',
						'show' => $show_manage_comments,
						'help' => GetLang('MnuHelpViewUnhelpulFeedback'),
						'icon' => 'unhelpful.gif',
					),
				),
				'mnuStats'	=> array (
					array (
						'text' => GetLang('QuestionStatistics'),
						'link' => 'index.php?ToDo=viewStatsQuestions',
						'show' => $_SESSION['user']->verify('stats', 'viewarticle'),
						'help' => GetLang('MnuHelpArticleStats'),
						'icon' => 'article.gif',
					),
					array (
						'text' => GetLang('UserStatistics'),
						'link' => 'index.php?ToDo=viewStatsUsers',
						'show' => $_SESSION['user']->verify('stats', 'viewuser'),
						'help' => GetLang('MnuHelpUserStats'),
						'icon' => 'user.gif',
					),
					array (
						'text' => GetLang('CategoryStatistics'),
						'link' => 'index.php?ToDo=viewStatsCategories',
						'show' => $_SESSION['user']->verify('stats', 'viewcategory'),
						'help' => GetLang('MnuHelpCategoryStats'),
						'icon' => 'category.gif',
					),
					array (
						'text' => GetLang('SearchStatistics'),
						'link' => 'index.php?ToDo=viewStatsSearch',
						'show' => $_SESSION['user']->verify('stats', 'viewsearch'),
						'help' => GetLang('MnuHelpSearchStats'),
						'icon' => 'search.gif',
					),
				),

			);

			$imagesDir = dirname(__FILE__).'/../../images';

			$GLOBALS['menuRow'] = "\n".'<div id="headerMenu">'."\n".'<ul>'."\n";
			foreach ($menuItems as $image => $link) {
				// If the menu has sub menus, display them
				if (is_array($link)) {
					$first = true;
					$shown = false;
					foreach ($link as $id => $sub) {
						// If the child is forbidden by law, hide it
						if (!$sub['show']) {
							continue;
						} else {
							$shown = true;
						}
						// If its the first born, give it an image
						if ($first) {
							$GLOBALS['menuRow'] .= '<li class="dropdown"><a href="'.$sub['link'].'">';
							$filename = $imagesDir. DIRECTORY_SEPARATOR . $image . '.gif';
							if (file_exists($filename)) {
								list($width, $height, $type, $attr) = getimagesize($filename);
								$GLOBALS['menuRow'] .= '<img '.$attr.' src="images/'.$image.'.gif" border="0" hspace="2" alt="">';
							} else {
								$GLOBALS['menuRow'] .= $image;
							}
							$GLOBALS['menuRow'] .= '</a>'."\n";
							if (count($link) > 1) {
								$GLOBALS['menuRow'] .= '<ul>'."\n";
							}
							$first = false;
						}


						// If it's not an only child, don't show the first item as a child
						if (count($link) > 1) {
							$extraclass = '';
							if (isset($sub['class'])) {
								$extraclass = $sub['class'];
							}

							// Is there help text set for this item?
							if (isset($sub['help'])) {
								$icon = "images/" . $sub['icon'];

								if(isset($sub['target'])) {
									$target = "target='" . $sub['target'] . "' ";
								}
								else {
									$target = '';
								}

								$GLOBALS['menuRow'] .= '<li><a ' . $target . ' class="menu_'.$image.' '.$extraclass.'" href="'.$sub['link'].'" onclick="closeMenu()"><img src="'.$icon.'" border=0 style="margin:5px 10px 25px -2px; float:left" /><b>'.$sub['text'].'</b><span>'.$sub['help'].'</span></a></li>'."\n";
							} else {
								$GLOBALS['menuRow'] .= '<li><a class="menu_'.$image.' '.$extraclass.'" href="'.$sub['link'].'">'.$sub['text'].'</a></li>'."\n";
							}
						}
					}

					if ($shown) {
						if (count($link) > 1) {
							$GLOBALS['menuRow'] .= '</ul>'."\n";
						}
						$GLOBALS['menuRow'] .= '</li>'."\n";
					}
				}
			}
			$GLOBALS['menuRow'] .= '</ul></div>'."\n";
		}

		// If we arn't setup make sure we don't display any menu
		// This prevents install step 4 from looking strange
		if (!$GLOBALS['isSetup']) {
			$GLOBALS['textLinks'] = '';
			$GLOBALS['menuRow'] = '';
			$GLOBALS['menuScript'] = '';
		}

		$GLOBALS["AKB_CLASS_TEMPLATE"]->SetTemplate("pageheader");
		echo $GLOBALS["AKB_CLASS_TEMPLATE"]->ParseTemplate();
	}

	/**
	* PrintFooter
	* Display the backend footer.
	* This also processed the header so as to put the breadcrumb string in.
	*
	* @return void
	*/
	function PrintFooter()
	{
		// Build the breadcrumb trail
		$tempOutput = ob_get_clean();
		$tempOutput = str_replace("__BreadcrumbTrail__",BuildBreadcrumbTrail(),$tempOutput);

		//Echo the Page header + the processed body.
		echo $tempOutput;

		$GLOBALS["AKB_CLASS_TEMPLATE"]->SetTemplate("pagefooter");
		echo $GLOBALS["AKB_CLASS_TEMPLATE"]->ParseTemplate();
	}

	/**
	*	Each page/action that wants to display a breadcrum trail needs to create
	*	a $GLOBALS["BreadcrumbEntries"] array with each of the breadcrumb levels.
	*	For example, if I was on the add product page it would be:
	*
	*	$GLOBALS["BreadcrumEntries"] = array("Home" => "index.php", "Products" => "index.php?ToDo=viewProducts", "Add Product" => "index.php?ToDo=addProduct");
	*
	*	As you can see, the last entry doesn't need the URL because that's the page you're on
	*/
	function BuildBreadcrumbTrail()
	{
		$trail = "";
		$count = 0;

		if (isset($GLOBALS["BreadcrumEntries"]) && is_array($GLOBALS["BreadcrumEntries"])) {
			foreach ($GLOBALS["BreadcrumEntries"] as $k=>$v) {
				if ($count++ < sizeof($GLOBALS["BreadcrumEntries"])-1) {
					$trail .= sprintf("<a href='%s'>%s</a> &gt; ", $v, $k);
				} else {
					$trail .= $k;
				}
			}
		}

		return $trail;
	}




?>
