<?php
/**
 * ###############################################
 *
 * SWIFT Framework
 * _______________________________________________
 *
 * @author		Varun Shoor
 *
 * @package		SWIFT
 * @copyright	Copyright (c) 2001-2012, Kayako
 * @license		http://www.kayako.com/license
 * @link		http://www.kayako.com
 *
 * ###############################################
 */

/**
 * The Knowledgebase Article View
 *
 * @author Varun Shoor
 */
class View_Article extends SWIFT_View
{
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __construct()
	{
		parent::__construct();

		return true;
	}

	/**
	 * Destructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __destruct()
	{
		parent::__destruct();

		return true;
	}

	/**
	 * Render the Knowledgebase Article Form
	 *
	 * @author Varun Shoor
	 * @param int $_mode The Render Mode
	 * @param object $_SWIFT_KnowledgebaseArticleObject The SWIFT_KnowledgebaseArticle Object Pointer (Only for EDIT Mode)
	 * @param int $_knowledgebaseCategoryID (OPTIONAL) The Knowledgebase Category ID
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function Render($_mode, SWIFT_KnowledgebaseArticle $_SWIFT_KnowledgebaseArticleObject = null, $_knowledgebaseCategoryID = false, SWIFT_TicketPost $_SWIFT_TicketPostObject = null,
			$_ticketID = 0, $_listType = 'inbox', $_departmentID = '-1', $_ticketStatusID = '-1', $_ticketTypeID = '-1')
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$_articleSubject = '';
		$_articleContents = '';
		$_allowComments = true;
		$_isFeatured = false;
		$_hasAttachments = false;
		$_ticketPostID = '0';

		$_knowledgebaseCategoryIDList = array();

		if (!empty($_ticketID))
		{
			$_SWIFT_TicketObject = new SWIFT_Ticket(new SWIFT_DataID($_ticketID));
			if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded() || !$_SWIFT_TicketObject->CanAccess($_SWIFT->Staff) ||
					$_SWIFT_TicketObject->GetTicketID() != $_SWIFT_TicketPostObject->GetProperty('ticketid'))
			{
				throw new SWIFT_Exception(SWIFT_INVALIDDATA);
			}

			// Decoding emoji characters
			$_emojiObject = new SWIFT_Emoji();

			$_parsedContents  = $_emojiObject->decode($_SWIFT_TicketPostObject->GetProperty('contents'));
			$_articleContents = nl2br($_parsedContents);

			$_ticketPostID = $_SWIFT_TicketPostObject->GetProperty('ticketpostid');
		}


		// Calculate the URL
		if ($_mode == SWIFT_UserInterface::MODE_EDIT)
		{
			$this->UserInterface->Start(get_class($this), '/Knowledgebase/Article/EditSubmit/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID(), SWIFT_UserInterface::MODE_EDIT, false, true);
		} else {
			$this->UserInterface->Start(get_class($this), '/Knowledgebase/Article/InsertSubmit/0', SWIFT_UserInterface::MODE_INSERT, false, true);
		}

		if (!empty($_knowledgebaseCategoryID))
		{
			$_knowledgebaseCategoryIDList[] = $_knowledgebaseCategoryID;
		}

		if (!count($_knowledgebaseCategoryIDList) && $_mode == SWIFT_UserInterface::MODE_INSERT)
		{
			$_knowledgebaseCategoryIDList[] = '0';
		}

		$_attachmentContainer = array();
		if ($_mode == SWIFT_UserInterface::MODE_EDIT && $_SWIFT_KnowledgebaseArticleObject->GetProperty('hasattachments') == '1')
		{
			$_attachmentContainer = SWIFT_Attachment::Retrieve(SWIFT_Attachment::LINKTYPE_KBARTICLE, $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID());
		}

		$_GeneralTabObject = $this->UserInterface->AddTab($this->Language->Get('tabgeneral'), 'icon_knowledgebase.png', 'generalkbinsert', true);
		$_AttachmentsTabObject = $this->UserInterface->AddTab($this->Language->Get('tabattachments'), 'icon_file.gif', 'kbattachments');
		$_AttachmentsTabObject->SetTabCounter(count($_attachmentContainer));
		$_AttachmentsTabObject->LoadToolbar();
		$_OptionsTabObject = $this->UserInterface->AddTab($this->Language->Get('taboptions'), 'icon_settings2.gif', 'kboptions');

		if ($_mode == SWIFT_UserInterface::MODE_EDIT)
		{
			if ($_SWIFT_KnowledgebaseArticleObject->GetProperty('articlestatus') == SWIFT_KnowledgebaseArticle::STATUS_DRAFT ||
					$_SWIFT_KnowledgebaseArticleObject->GetProperty('articlestatus') == SWIFT_KnowledgebaseArticle::STATUS_PENDINGAPPROVAL)
			{
				$this->UserInterface->Toolbar->AddButton($this->Language->Get('publish'), 'icon_check.gif', '/Knowledgebase/Article/EditSubmit/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID() . '/1', SWIFT_UserInterfaceToolbar::LINK_FORM);
				$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('publish'), 'icon_check.gif', '/Knowledgebase/Article/EditSubmit/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID() . '/1', SWIFT_UserInterfaceToolbar::LINK_FORM);

				$this->UserInterface->Toolbar->AddButton($this->Language->Get('update'), 'icon_savereload.gif');
				$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('update'), 'icon_savereload.gif');
			} else {
				$this->UserInterface->Toolbar->AddButton($this->Language->Get('update'), 'icon_check.gif');
				$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('update'), 'icon_check.gif');
			}

			$this->UserInterface->Toolbar->AddButton('');

			// Allow a user to unpublish (mark article as draft)
			if ($_SWIFT_KnowledgebaseArticleObject->GetProperty('articlestatus') == SWIFT_KnowledgebaseArticle::STATUS_PUBLISHED) {
				$this->UserInterface->Toolbar->AddButton($this->Language->Get('markasdraft'), 'icon_savereload.gif', '/Knowledgebase/Article/EditSubmit/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID() . '/-1', SWIFT_UserInterfaceToolbar::LINK_FORM);
				$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('markasdraft'), 'icon_savereload.gif', '/Knowledgebase/Article/EditSubmit/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID() . '/-1', SWIFT_UserInterfaceToolbar::LINK_FORM);
			}

			$this->UserInterface->Toolbar->AddButton($this->Language->Get('delete'), 'icon_delete.gif', '/Knowledgebase/Article/Delete/' .
					$_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID(), SWIFT_UserInterfaceToolbar::LINK_CONFIRM, '', '', false);
			$this->UserInterface->Toolbar->AddButton($this->Language->Get('help'), 'icon_help.gif', SWIFT_Help::RetrieveHelpLink('kbarticle'),
					SWIFT_UserInterfaceToolbar::LINK_NEWWINDOW);

			$_articleSubject = $_SWIFT_KnowledgebaseArticleObject->GetProperty('subject');
			$_articleContents = $_SWIFT_KnowledgebaseArticleObject->GetProperty('contents');
			$_isFeatured = intval($_SWIFT_KnowledgebaseArticleObject->GetProperty('isfeatured'));

			$_allowComments = intval($_SWIFT_KnowledgebaseArticleObject->GetProperty('allowcomments'));
			$_hasAttachments = intval($_SWIFT_KnowledgebaseArticleObject->GetProperty('hasattachments'));

			$_knowledgebaseCategoryIDList = SWIFT_KnowledgebaseArticleLink::RetrieveLinkIDListOnArticle($_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID(), SWIFT_KnowledgebaseArticleLink::LINKTYPE_CATEGORY);

		} else {
			$this->UserInterface->Toolbar->AddButton($this->Language->Get('publish'), 'icon_check.gif');
			$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('publish'), 'icon_check.gif');

			$this->UserInterface->Toolbar->AddButton('');
			$this->UserInterface->Toolbar->AddButton($this->Language->Get('saveasdraft'), 'icon_savereload.gif', '/Knowledgebase/Article/InsertSubmit/1', SWIFT_UserInterfaceToolbar::LINK_FORM);
			$this->UserInterface->Toolbar->AddButton($this->Language->Get('help'), 'icon_help.gif', SWIFT_Help::RetrieveHelpLink('kbarticle'), SWIFT_UserInterfaceToolbar::LINK_NEWWINDOW);
		}

		$_AttachmentsTabObject->Toolbar->AddButton('');
		$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('addfile'), 'icon_addplus.gif', "AddKBFile();", SWIFT_UserInterfaceToolbar::LINK_JAVASCRIPT, '', '', false);
		$_AttachmentsTabObject->Toolbar->AddButton($this->Language->Get('help'), 'icon_help.gif', SWIFT_Help::RetrieveHelpLink('kbarticle'), SWIFT_UserInterfaceToolbar::LINK_NEWWINDOW);

		/*
		 * ###############################################
		 * BEGIN GENERAL TAB
		 * ###############################################
		 */

		$_GeneralTabObject->SetColumnWidth('100');

		$_GeneralTabObject->Text('subject', $this->Language->Get('articletitle'), $this->Language->Get('desc_articletitle'), $_articleSubject, 'text', '90');

		$_checkBoxContainer = SWIFT_KnowledgebaseCategoryManager::GetCategoryOptions($_knowledgebaseCategoryIDList, false, true);

		$_GeneralTabObject->CheckBoxContainerList('kbcategoryidlist', $this->Language->Get('articlecategories'), $this->Language->Get('desc_articlecategories'), $_checkBoxContainer, 615);

		$_GeneralTabObject->HTMLEditor('articlecontents', $_articleContents);

		/*
		 * ###############################################
		 * END GENERAL TAB
		 * ###############################################
		 */


		/*
		 * ###############################################
		 * BEGIN OPTIONS TAB
		 * ###############################################
		 */

		$_OptionsTabObject->Title($this->Language->Get('generalsettings'), 'icon_doublearrows.gif');

		$_OptionsTabObject->YesNo('isfeatured', $this->Language->Get('isfeatured'), $this->Language->Get('desc_isfeatured'), $_isFeatured);
		$_OptionsTabObject->YesNo('allowcomments', $this->Language->Get('allowcomments'), $this->Language->Get('desc_allowcommentsarticle'), $_allowComments);

		/*
		 * ###############################################
		 * END OPTIONS TAB
		 * ###############################################
		 */

		/*
		 * ###############################################
		 * BEGIN ATTACHMENTS TAB
		 * ###############################################
		 */

		$_attachmentContainerHTML = '<tr class="tablerow1_tr"><td align="left" valign="top class="tablerow1"><div id="kbattachmentcontainer">';
		$_attachmentFileHTML = '<div class="ticketattachmentitem"><div class="ticketattachmentitemdelete" onclick="javascript: $(this).parent().remove();">&nbsp;</div><input name="kbattachments[]" type="file" size="20" class="swifttextlarge swifttextfile" /></div>';
		for ($index = 0; $index < 3; $index++) {
			$_attachmentContainerHTML .= $_attachmentFileHTML;
		}
		$_attachmentContainerHTML .= '</div></td></tr>';

		$_AttachmentsTabObject->RowHTML($_attachmentContainerHTML);


		if ($_mode == SWIFT_UserInterface::MODE_EDIT && $_SWIFT_KnowledgebaseArticleObject->GetProperty('hasattachments') == '1')
		{
			if (count($_attachmentContainer))
			{
				$_AttachmentsTabObject->Title($this->Language->Get('attachedfiles'), 'icon_doublearrows.gif');

				$_attachmentContainerHTML = '<tr class="tablerow1_tr"><td align="left" valign="top class="tablerow1"><div id="kbattachmentfilescontainer">';

				foreach ($_attachmentContainer as $_attachment)
				{
					$_attachmentContainerHTML .= '<div class="ticketattachmentitem"><div class="ticketattachmentitemdelete" onclick="javascript: $(this).parent().remove();">&nbsp;</div> ' . htmlspecialchars($_attachment['filename']) . '<input type="hidden" name="_existingAttachmentIDList[]" value="' . intval($_attachment['attachmentid']) . '" /></div>';
				}

				$_attachmentContainerHTML .= '</div></td></tr>';
				$_AttachmentsTabObject->RowHTML($_attachmentContainerHTML);
			}
		}

		/*
		 * ###############################################
		 * END ATTACHMENTS TAB
		 * ###############################################
		 */

		$this->UserInterface->Hidden('tredir_ticketid', $_ticketID);
		$this->UserInterface->Hidden('tredir_listtype', $_listType);
		$this->UserInterface->Hidden('tredir_departmentid', $_departmentID);
		$this->UserInterface->Hidden('tredir_ticketstatusid', $_ticketStatusID);
		$this->UserInterface->Hidden('tredir_tickettypeid', $_ticketTypeID);
		$this->UserInterface->Hidden('tredir_ticketpostid', $_ticketPostID);

		$this->UserInterface->End();

		return true;
	}

	/**
	 * Render the Knowledgebase Articles Grid
	 *
	 * @author Varun Shoor
	 * @param int $_searchStoreID (OPTIONAL) The Search Store ID
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function RenderGrid($_searchStoreID = false)
	{
		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$this->Load->Library('UserInterface:UserInterfaceGrid', array('kbarticlegrid'));

		if ($this->UserInterfaceGrid->GetMode() == SWIFT_UserInterfaceGrid::MODE_SEARCH)
		{
			$this->UserInterfaceGrid->SetSearchQuery(
			"SELECT kbarticles.* FROM " . TABLE_PREFIX . "kbarticles AS kbarticles
				LEFT JOIN " . TABLE_PREFIX . "kbarticledata AS kbarticledata ON (kbarticles.kbarticleid = kbarticledata.kbarticleid)
				WHERE ((" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.subject') . ") OR (" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticledata.contents') . ") OR
					(" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.author') . ") OR (" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.email') . "))",

			"SELECT COUNT(*) AS totalitems FROM " . TABLE_PREFIX . "kbarticles AS kbarticles
				LEFT JOIN " . TABLE_PREFIX . "kbarticledata AS kbarticledata ON (kbarticles.kbarticleid = kbarticledata.kbarticleid)
				WHERE ((" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.subject') . ") OR (" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticledata.contents') . ") OR
					(" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.author') . ") OR (" . $this->UserInterfaceGrid->BuildSQLSearch('kbarticles.email') . "))");
		}

		$this->UserInterfaceGrid->SetSearchStoreOptions($_searchStoreID,
				"SELECT kbarticles.* FROM " . TABLE_PREFIX . "kbarticles AS kbarticles
					WHERE kbarticles.kbarticleid IN (%s)",
				SWIFT_SearchStore::TYPE_KBARTICLE, '/Knowledgebase/Article/Manage/-1');

		$this->UserInterfaceGrid->SetQuery('SELECT kbarticles.* FROM ' . TABLE_PREFIX . 'kbarticles AS kbarticles',
				'SELECT COUNT(*) AS totalitems FROM ' . TABLE_PREFIX . 'kbarticles');

		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('kbarticleid', 'kbarticleid', SWIFT_UserInterfaceGridField::TYPE_ID));

		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('icon', '&nbsp;', SWIFT_UserInterfaceGridField::TYPE_CUSTOM, 16, SWIFT_UserInterfaceGridField::ALIGN_CENTER));
		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('kbarticles.subject', $this->Language->Get('articletitle'),SWIFT_UserInterfaceGridField::TYPE_DB, 0,
				SWIFT_UserInterfaceGridField::ALIGN_LEFT));
		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('kbarticles.author', $this->Language->Get('author'),SWIFT_UserInterfaceGridField::TYPE_DB, 160,
				SWIFT_UserInterfaceGridField::ALIGN_LEFT));
		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('kbarticles.articlestatus', $this->Language->Get('articlestatus'),SWIFT_UserInterfaceGridField::TYPE_DB, 100,
				SWIFT_UserInterfaceGridField::ALIGN_LEFT));
		$this->UserInterfaceGrid->AddField(new SWIFT_UserInterfaceGridField('kbarticles.dateline', $this->Language->Get('creationdate'),SWIFT_UserInterfaceGridField::TYPE_DB, 180,
				SWIFT_UserInterfaceGridField::ALIGN_LEFT, SWIFT_UserInterfaceGridField::SORT_DESC), true);


		$this->UserInterfaceGrid->SetRenderCallback(array('View_Article', 'GridRender'));
		$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('delete'), 'icon_delete.gif',
				array('Controller_Article', 'DeleteList'), $this->Language->Get('actionconfirm')));

		$this->UserInterfaceGrid->SetNewLinkViewport(SWIFT::Get('basename') . '/Knowledgebase/Article/Insert');

		$this->UserInterfaceGrid->Render();

		$this->UserInterfaceGrid->Display();

		return true;
	}

	/**
	 * The Grid Rendering Function
	 *
	 * @author Varun Shoor
	 * @param array $_fieldContainer The Field Record Value Container
	 * @return array The Processed Field Container Array
	 */
	static public function GridRender($_fieldContainer)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_staffCache = $_SWIFT->Cache->Get('staffcache');

		$_subjectSuffix = '';
		$_icon = 'icon_kbarticlesmall.png';
		if ($_fieldContainer['articlestatus'] == SWIFT_KnowledgebaseArticle::STATUS_DRAFT || $_fieldContainer['articlestatus'] == SWIFT_KnowledgebaseArticle::STATUS_PENDINGAPPROVAL)
		{
			$_icon = 'icon_kbarticlesmallfaded.png';
		}

		$_fieldContainer['icon'] = '<img src="' . SWIFT::Get('themepath') . 'images/' . $_icon .'" align="absmiddle" border="0" />';

		$_fieldContainer['kbarticles.subject'] = '<a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/Edit/' . intval($_fieldContainer['kbarticleid']) . '" viewport="1" title="' . $_SWIFT->Language->Get('edit') . '">' . htmlspecialchars($_fieldContainer['subject']) . '</a>' . $_subjectSuffix;

		$_fieldContainer['kbarticles.dateline'] = SWIFT_Date::Get(SWIFT_Date::TYPE_DATETIME, $_fieldContainer['dateline']);

		if ($_fieldContainer['creator'] == SWIFT_KnowledgebaseArticle::CREATOR_STAFF && isset($_staffCache[$_fieldContainer['creatorid']]))
		{
			$_fieldContainer['kbarticles.author'] = htmlspecialchars($_staffCache[$_fieldContainer['creatorid']]['fullname']);
		} else {
			$_fieldContainer['kbarticles.author'] = htmlspecialchars($_fieldContainer['author']);
		}

		$_fieldContainer['kbarticles.articlestatus'] = SWIFT_KnowledgebaseArticle::GetStatusLabel($_fieldContainer['articlestatus']);

		return $_fieldContainer;
	}

	/**
	 * Render the View All Page
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseCategoryID (OPTIONAL) The filter by knowledgebase category id
	 * @param int $_offset
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function RenderViewAll($_knowledgebaseCategoryID = 0, $_offset = 0)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$this->UserInterface->Start('viewallkb', '/Knowledgebase/Article/ViewAll', SWIFT_UserInterface::MODE_INSERT, false);

		$_parentCategoryID = intval($_knowledgebaseCategoryID);

		if (!empty($_parentCategoryID))
		{
			$_SWIFT_KnowledgebaseCategoryObject = false;
			if (!empty($_parentCategoryID))
			{
				try
				{
					$_SWIFT_KnowledgebaseCategoryObject = new SWIFT_KnowledgebaseCategory(new SWIFT_DataID($_parentCategoryID));
				} catch (SWIFT_Exception $_SWIFT_ExceptionObject) {
				}
			}

			if (!$_SWIFT_KnowledgebaseCategoryObject instanceof SWIFT_KnowledgebaseCategory || !$_SWIFT_KnowledgebaseCategoryObject->GetIsClassLoaded())
			{
				return false;
			} else if ($_SWIFT_KnowledgebaseCategoryObject->GetProperty('categorytype') == SWIFT_KnowledgebaseCategory::TYPE_PUBLIC ||
					($_SWIFT_KnowledgebaseCategoryObject->GetProperty('categorytype') == SWIFT_KnowledgebaseCategory::TYPE_INHERIT && !$_SWIFT_KnowledgebaseCategoryObject->IsParentCategoryOfType(array(SWIFT_KnowledgebaseCategory::TYPE_GLOBAL, SWIFT_KnowledgebaseCategory::TYPE_PRIVATE)))) {
				return false;
			}

			if ($_SWIFT_KnowledgebaseCategoryObject->GetProperty('staffvisibilitycustom') == '1')
			{
				$_filterKnowledgebaseCategoryIDList = SWIFT_StaffGroupLink::RetrieveListFromCacheOnStaffGroup(SWIFT_StaffGroupLink::TYPE_KBCATEGORY, $_SWIFT->Staff->GetProperty('staffgroupid'));

				if (!in_array($_SWIFT_KnowledgebaseCategoryObject->GetKnowledgebaseCategoryID(), $_filterKnowledgebaseCategoryIDList))
				{
					return false;
				}
			}
		}

		$_knowledgebaseMainCategoryContainer = SWIFT_KnowledgebaseCategory::Retrieve(array(SWIFT_KnowledgebaseCategory::TYPE_GLOBAL, SWIFT_KnowledgebaseCategory::TYPE_PRIVATE,
				SWIFT_KnowledgebaseCategory::TYPE_INHERIT), $_parentCategoryID, SWIFT::Get('staffgroupid'), 0);

		$_knowledgebaseCategoryContainer = $_knowledgebaseMainCategoryContainer[0];
		$_dispatchFilterKnowledgebaseCategoryIDList = $_knowledgebaseMainCategoryContainer[1];

		$_tdWidth = round(100/$this->Settings->Get('kb_categorycolumns'));

		$_index = 1;
		foreach ($_knowledgebaseCategoryContainer as $_knowledgebaseCategoryID => $_knowledgebaseCategory)
		{
			$_knowledgebaseCategoryContainer[$_knowledgebaseCategoryID]['tdwidth'] = $_tdWidth;

			if (!isset($_knowledgebaseCategory['title']))
			{
				$_knowledgebaseCategoryContainer[$_knowledgebaseCategoryID]['title'] = false;
			}

			if ($_index > $this->Settings->Get('kb_categorycolumns'))
			{
				$_index = 1;
				$_knowledgebaseCategoryContainer[$_knowledgebaseCategoryID]['jumptorow'] = true;
			} else {
				$_knowledgebaseCategoryContainer[$_knowledgebaseCategoryID]['jumptorow'] = false;
			}

			$_index++;
		}

		if ($_index <= $this->Settings->Get('kb_categorycolumns'))
		{
			$_difference = $this->Settings->Get('kb_categorycolumns') - ($_index - 1);
			for ($ii = 0; $ii < $_difference; $ii++) {
				$_knowledgebaseCategoryContainer[] = array('title' => false);
			}
		}

		$_knowledgebaseArticleContainer = array();
		$_knowledgebaseArticleContainer = SWIFT_KnowledgebaseArticle::Retrieve(array($_parentCategoryID));

		$_knowledgebaseCategoryCount = count($_knowledgebaseCategoryContainer);
		$_knowledgebaseArticleCount = count($_knowledgebaseArticleContainer);

		$_knowledgebaseArticleContainer_Popular = $_knowledgebaseArticleContainer_Recent = array();
		if (empty($_parentCategoryID) && $this->Settings->Get('kb_enpopulararticles') == '1')
		{
			$_knowledgebaseArticleContainer_Popular = SWIFT_KnowledgebaseArticle::RetrieveFilter(SWIFT_KnowledgebaseArticle::FILTER_POPULAR, $_dispatchFilterKnowledgebaseCategoryIDList, $_SWIFT->Staff->GetProperty('staffgroupid'), 0);
		}

		if (empty($_parentCategoryID) && $this->Settings->Get('kb_enlatestarticles') == '1')
		{
			$_knowledgebaseArticleContainer_Recent = SWIFT_KnowledgebaseArticle::RetrieveFilter(SWIFT_KnowledgebaseArticle::FILTER_RECENT, $_dispatchFilterKnowledgebaseCategoryIDList, $_SWIFT->Staff->GetProperty('staffgroupid'), 0);
		}

		$_showEmptyViewWarning = $_hasNoCategories = false;
		if (count($_knowledgebaseCategoryContainer) == 0 || (isset($_knowledgebaseCategoryContainer[0]['title']) && isset($_knowledgebaseCategoryContainer[1]['title']) && isset($_knowledgebaseCategoryContainer[2]['title']) &&
				$_knowledgebaseCategoryContainer[0]['title'] == false && $_knowledgebaseCategoryContainer[1]['title'] == false && $_knowledgebaseCategoryContainer[2]['title'] == false))
		{
			$_hasNoCategories = true;
		}

		if ($_hasNoCategories && count($_knowledgebaseArticleContainer) == 0)
		{
			$_showEmptyViewWarning = true;
		}

		/*
		 * ###############################################
		 * BEGIN VIEW ALL TAB
		 * ###############################################
		 */
		$_ViewAllTabObject = $this->UserInterface->AddTab($this->Language->Get('tabviewall'), 'icon_knowledgebase.png', 'viewall', true);

		$_renderHTML = '<div class="tabdatacontainer">';
		if ($_showEmptyViewWarning)
		{
			$_renderHTML .= '<div class="dashboardmsg">' . $this->Language->Get('noinfoinview') . '</div>';
		} else {
			$_renderHTML .= '<table cellpadding="0" cellspacing="0" width="100%" border="0">';
			$_renderHTML .= '<tr>';

			foreach ($_knowledgebaseCategoryContainer as $_kbCategoryID => $_knowledgebaseCategory)
			{
				if (isset($_knowledgebaseCategory['jumptorow']) && $_knowledgebaseCategory['jumptorow'] == true)
				{
					$_renderHTML .= '</tr><tr>';
				}

				if ($_knowledgebaseCategory['title'] == false)
				{
					$_renderHTML .= '<td>&nbsp;</td>';
				} else {

					$_renderHTML .= '<td width="' . $_knowledgebaseCategory['tdwidth'] . '%" align="left" valign="top">';
					$_renderHTML .= '<div class="kbcategorytitlecontainer"><div class="kbcategorytitle"><span class="kbcategorycount">' . IIF($_knowledgebaseCategory['totalarticles'] > 0, ' (' . $_knowledgebaseCategory['totalarticles'] . ')') . '</span><a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/ViewAll/' . $_knowledgebaseCategory['kbcategoryid'] . '" viewport="1">' . $_knowledgebaseCategory['title'] . '</a></div>';

					foreach ($_knowledgebaseCategory['articles'] as $_knowledgebaseArticle)
					{
						$_renderHTML .= '<div class="kbarticlecategorylistitem"><a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/ViewArticle/' . $_knowledgebaseArticle['kbarticleid'] . '/' . $_knowledgebaseCategory['kbcategoryid'] . '" viewport="1">' . $_knowledgebaseArticle['subject'] . '</a></div>';
					}

					$_renderHTML .= '</div></td>';
				}
			}

			$_renderHTML .= '</tr></table>';

			if ($_hasNoCategories == false)
			{
				$_renderHTML .= '<br />';
			}

			if ($_knowledgebaseArticleCount > 0)
			{
				foreach ($_knowledgebaseArticleContainer as $_knowledgebaseArticle)
				{
					$_renderHTML .= '<div class="kbarticlecontainer' . IIF($_knowledgebaseArticle['isfeatured'] == '1', ' kbarticlefeatured') . '">';
					$_renderHTML .= '<div class="kbarticle"><a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/ViewArticle/' . $_knowledgebaseArticle['kbarticleid'] . '/' . intval($_parentCategoryID) . '" viewport="1">' . $_knowledgebaseArticle['subject'] . '</a></div>';
					$_renderHTML .= '<div class="kbarticletext">' . $_knowledgebaseArticle['contentstext'] . '</div>';
					$_renderHTML .= '</div>';
				}
			}

			if ($_parentCategoryID == 0 && $_showEmptyViewWarning == false)
			{
				$_renderHTML .= '<table cellpadding="0" cellspacing="0" width="100%" border="0">';
				$_renderHTML .= '<tr>';

				if ($this->Settings->Get('kb_enpopulararticles') == '1')
				{
					$_renderHTML .= '<td width="50%" align="left" valign="top"><div class="kbrightstrip">';
					$_renderHTML .= '<table class="hlineheader hlinegray"><tr><th rowspan="2" nowrap>' . $this->Language->Get('mostpopular') . '</th><td>&nbsp;</td></tr><tr><td class="hlinelower">&nbsp;</td></tr></table>';
					$_renderHTML .= '</div>';
					$_renderHTML .= '<ol>';

					foreach ($_knowledgebaseArticleContainer_Popular as $_knowledgebaseArticle)
					{
						$_renderHTML .= '<li class="kbarticlelist">';
						$_renderHTML .= '<div class="kbarticlelistitem"><a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/ViewArticle/' . $_knowledgebaseArticle['kbarticleid'] . '/' . intval($_parentCategoryID) . '" viewport="1">' . $_knowledgebaseArticle['subject'] . '</a></div>';
						$_renderHTML .= '</li>';
					}

					$_renderHTML .= '</ol>';
					$_renderHTML .= '</td>';
				}

				if ($this->Settings->Get('kb_enlatestarticles') == '1')
				{
					$_renderHTML .= '<td width="' . IIF($this->Settings->Get('kb_enpopulararticles') == '1', '50%', '100%') . '" align="left" valign="top">';
					$_renderHTML .= '<div><table class="hlineheader hlinegray"><tr><th rowspan="2" nowrap>' . $this->Language->Get('recentarticles') . '</th><td>&nbsp;</td></tr><tr><td class="hlinelower">&nbsp;</td></tr></table></div>';
					$_renderHTML .= '<ol>';

					foreach ($_knowledgebaseArticleContainer_Recent as $_knowledgebaseArticle)
					{
						$_renderHTML .= '<li class="kbarticlelist">';
						$_renderHTML .= '<div class="kbarticlelistitem"><a href="' . SWIFT::Get('basename') . '/Knowledgebase/Article/ViewArticle/' . $_knowledgebaseArticle['kbarticleid'] . '/' . intval($_parentCategoryID) . '" viewport="1">' . $_knowledgebaseArticle['subject'] . '</a></div>';
						$_renderHTML .= '</li>';
					}

					$_renderHTML .= '</ol>';
					$_renderHTML .= '</td>';
				}

				$_renderHTML .= '</tr>';
				$_renderHTML .= '</table>';
			}

		}
		$_renderHTML .= '</div>';


		$_ViewAllTabObject->RowHTML('<tr><td>' . $_renderHTML . '</td></tr>');

		/*
		 * ###############################################
		 * END VIEW ALL TAB
		 * ###############################################
		 */

		$this->UserInterface->End();

		return true;
	}

	/**
	 * Render the Information Box
	 *
	 * @author Varun Shoor
	 * @param SWIFT_KnowledgebaseArticle $_SWIFT_KnowledgebaseArticleObject The SWIFT_KnowledgebaseArticle Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function RenderInfoBox(SWIFT_KnowledgebaseArticle $_SWIFT_KnowledgebaseArticleObject) {
		$_SWIFT = SWIFT::GetInstance();

		if (!$this->GetIsClassLoaded()) {
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$_staffCache = $this->Cache->Get('staffcache');

		$_informationHTML = '';

		$_authorName = $_SWIFT_KnowledgebaseArticleObject->GetProperty('author');
		if ($_SWIFT_KnowledgebaseArticleObject->GetProperty('creator') == SWIFT_KnowledgebaseArticle::CREATOR_STAFF && isset($_staffCache[$_SWIFT_KnowledgebaseArticleObject->GetProperty('creatorid')]))
		{
			$_authorName = $_staffCache[$_SWIFT_KnowledgebaseArticleObject->GetProperty('creatorid')]['fullname'];
		}

		if (!empty($_authorName))
		{
			$_informationHTML .= '<div class="navinfoitemtext">' . '<div class="navinfoitemtitle">' . $this->Language->Get('infobauthor') . '</div><div class="navinfoitemcontent">' . htmlspecialchars(StripName($_authorName, 20)) . '</div></div>';
		}

		$_informationHTML .= '<div class="navinfoitemtext">' . '<div class="navinfoitemtitle">' . $this->Language->Get('infobcreationdate') . '</div><div class="navinfoitemcontent">' . SWIFT_Date::Get(SWIFT_Date::TYPE_DATETIME, $_SWIFT_KnowledgebaseArticleObject->GetProperty('dateline')) . '</div></div>';

		if ($_SWIFT_KnowledgebaseArticleObject->GetProperty('isedited') == '1')
		{
			$_editedStaffName = $this->Language->Get('na');
			if (isset($_staffCache[$_SWIFT_KnowledgebaseArticleObject->GetProperty('editedstaffid')]))
			{
				$_editedStaffName = $_staffCache[$_SWIFT_KnowledgebaseArticleObject->GetProperty('editedstaffid')]['fullname'];
			}

			$_informationHTML .= '<div class="navinfoitemtext">' . '<div class="navinfoitemtitle">' . $this->Language->Get('infobeditedby') . '</div><div class="navinfoitemcontent">' . htmlspecialchars(StripName($_editedStaffName, 20)) . '</div></div>';

			$_informationHTML .= '<div class="navinfoitemtext">' . '<div class="navinfoitemtitle">' . $this->Language->Get('infobeditedon') . '</div><div class="navinfoitemcontent">' . SWIFT_Date::Get(SWIFT_Date::TYPE_DATETIME, $_SWIFT_KnowledgebaseArticleObject->GetProperty('editeddateline')) . '</div></div>';
		}


		$_informationHTML .= '<div class="navinfoitemtext">' . '<div class="navinfoitemtitle">' . $this->Language->Get('infobrating') . '</div><div class="navinfoitemcontent"><img src="' . SWIFT::Get('themepathimages') . 'icon_star_' . str_replace('.', '_', $_SWIFT_KnowledgebaseArticleObject->GetProperty('articlerating')) . '.gif" align="absmiddle" border="0" /></div></div>';

		$this->UserInterface->AddNavigationBox($this->Language->Get('informationbox'), $_informationHTML);

		return true;
	}
}
?>