<?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 Controller
 *
 * @author Varun Shoor
 */
class Controller_ViewKnowledgebase extends Controller_staff
{
	// Core Constants
	const MENU_ID = 4;
	const NAVIGATION_ID = 1;

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

		$this->Load->Library('Render:KnowledgebaseRenderManager');
		$this->Load->Library('Article:KnowledgebaseArticleManager');
		$this->Load->Library('Comment:CommentManager');

		if (SWIFT_App::IsInstalled(APP_TICKETS))
		{
			SWIFT_Loader::LoadModel('Ticket:Ticket', APP_TICKETS);
			SWIFT_Loader::LoadModel('Ticket:TicketPost', APP_TICKETS);
		}

		$this->Language->Load('staff_knowledgebase');

		return true;
	}

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

		return true;
	}

	/**
	 * Loads the Display Data
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function _LoadDisplayDataForViewArticle()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$this->UserInterface->AddNavigationBox($this->Language->Get('quickfilter'), $this->KnowledgebaseRenderManager->RenderViewKnowledgebaseTree());

		return true;
	}

	/**
	 * View All Knowledgebase Articles
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseCategoryID (OPTIONAL) The filter by knowledgebase category id
	 * @param int $_offset
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded or If Invalid Data is Provided
	 */
	public function Index($_knowledgebaseCategoryID = 0)
	{
		$_SWIFT = SWIFT::GetInstance();

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

			return false;
		}

		$_knowledgebaseCategoryID = intval($_knowledgebaseCategoryID);

		$this->_LoadDisplayDataForViewArticle();

		$this->UserInterface->Header($this->Language->Get('knowledgebase') . ' > ' . $this->Language->Get('view'), self::MENU_ID, 0);

		if ($_SWIFT->Staff->GetPermission('staff_kbcanviewarticles') == '0')
		{
			$this->UserInterface->DisplayError($this->Language->Get('titlenoperm'), $this->Language->Get('msgnoperm'));
		} else {
			$this->View->RenderViewAll($_knowledgebaseCategoryID);
		}

		$this->UserInterface->Footer();

		return true;
	}

	/**
	 * View the News Item
	 *
	 * @author Varun Shoor
	 * @param int $_newsItemID The News Item ID
	 * @param int $_newsCategoryID (OPTIONAL) The filter by news category id
	 * @param int $_offset
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function ViewItem($_newsItemID, $_newsCategoryID = 0, $_offset = 0)
	{
		$_SWIFT = SWIFT::GetInstance();

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

			return false;
		} else if (empty($_newsItemID)) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$this->Load->Library('Comment:CommentManager');

		$_SWIFT_NewsItemObject = new SWIFT_NewsItem($_newsItemID);
		if (!$_SWIFT_NewsItemObject instanceof SWIFT_NewsItem || !$_SWIFT_NewsItemObject->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$this->View->RenderInfoBox($_SWIFT_NewsItemObject);
		$this->_LoadDisplayDataForViewNews();

		$this->UserInterface->Header($this->Language->Get('news') . ' > ' . htmlspecialchars($_SWIFT_NewsItemObject->GetProperty('subject')), self::MENU_ID, self::NAVIGATION_ID);

		if ($_SWIFT->Staff->GetPermission('staff_kbcanviewarticles') == '0')
		{
			$this->UserInterface->DisplayError($this->Language->Get('titlenoperm'), $this->Language->Get('msgnoperm'));
		} else {
			$this->View->RenderViewItem($_SWIFT_NewsItemObject, $_newsCategoryID, $_offset);
		}

		$this->UserInterface->Footer();

		return true;
	}

	/**
	 * The Knowledgebase Article Viewing Function
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseArticleID The Knowledgebase Article ID
	 * @param int $_knowledgebaseCategoryID (OPTIONAL) The Knowledgebase Category ID
	 * @param string $_seoSubject (OPTIONAL) The SEO Subject
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function Article($_knowledgebaseArticleID, $_knowledgebaseCategoryID = 0, $_seoSubject = '')
	{
		$_SWIFT = SWIFT::GetInstance();

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

			return false;
		}

		$_knowledgebaseArticleObjectContainer = $this->KnowledgebaseArticleManager->RetrieveForStaff($_knowledgebaseArticleID, $_knowledgebaseCategoryID);
		if (!$_knowledgebaseArticleObjectContainer)
		{
			return false;
		}

		$_SWIFT_KnowledgebaseArticleObject = $_knowledgebaseArticleObjectContainer[0];

		$this->View->RenderInfoBox($_SWIFT_KnowledgebaseArticleObject);
		$this->_LoadDisplayDataForViewArticle();

		$_SWIFT_KnowledgebaseArticleObject->IncrementViews();

		$this->UserInterface->Header($this->Language->Get('knowledgebase') . ' > ' . htmlspecialchars($_SWIFT_KnowledgebaseArticleObject->GetProperty('subject')), self::MENU_ID, self::NAVIGATION_ID);

		if ($_SWIFT->Staff->GetPermission('staff_kbcanviewarticles') == '0')
		{
			$this->UserInterface->DisplayError($this->Language->Get('titlenoperm'), $this->Language->Get('msgnoperm'));
		} else {
			$this->View->RenderViewArticle($_knowledgebaseArticleObjectContainer);
		}

		$this->UserInterface->Footer();

		return true;
	}

	/**
	 * Process the article rating properties
	 *
	 * @author Varun Shoor
	 * @param SWIFT_KnowledgebaseArticle $_SWIFT_KnowledgebaseArticleObject The Knowledgebase Article Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function _ProcessArticleRating(SWIFT_KnowledgebaseArticle $_SWIFT_KnowledgebaseArticleObject)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$this->Cookie->Parse('articleratings');
		$_hasNotRated = true;

		if ($this->Cookie->GetVariable('articleratings', $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID()) == true)
		{
			$_hasNotRated = false;
		}

		$_articleRating = str_replace('.', '_', $_SWIFT_KnowledgebaseArticleObject->GetProperty('articlerating'));

		return array('_hasNotRated' => $_hasNotRated, '_articleRating' => $_articleRating);
	}

	/**
	 * The Knowledgebase Article Rating Function
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseArticleID The Knowledgebase Article ID
	 * @param bool $_isHelpful (OPTIONAL) Whether the article was helpful
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function Rate($_knowledgebaseArticleID, $_isHelpful = 0)
	{
		$_SWIFT = SWIFT::GetInstance();

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

			return false;
		}

		$_isHelpful = intval($_isHelpful);

		$_knowledgebaseArticleObjectContainer = $this->KnowledgebaseArticleManager->RetrieveForStaff($_knowledgebaseArticleID);
		if (!$_knowledgebaseArticleObjectContainer)
		{
			return false;
		}

		$_SWIFT_KnowledgebaseArticleObject = $_knowledgebaseArticleObjectContainer[0];
		if (!$_SWIFT_KnowledgebaseArticleObject instanceof SWIFT_KnowledgebaseArticle || !$_SWIFT_KnowledgebaseArticleObject->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

		if ($_isHelpful)
		{
			$_SWIFT_KnowledgebaseArticleObject->MarkAsHelpful();
		} else {
			$_SWIFT_KnowledgebaseArticleObject->MarkAsNotHelpful();
		}

		$this->Cookie->Parse('articleratings');
		$this->Cookie->AddVariable('articleratings', $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID(), '1');
		$this->Cookie->Rebuild('articleratings', true);

		$_variableContainer = $this->_ProcessArticleRating($_SWIFT_KnowledgebaseArticleObject);
		extract($_variableContainer);

		echo $this->View->GetRating($_SWIFT_KnowledgebaseArticleObject->RetrieveStore(), false, $_articleRating);

		return true;
	}

	/**
	 * Dispatch the Attachment
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseArticleID The Knowledgebase Article ID
	 * @param int $_attachmentID The Attachment ID
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetAttachment($_knowledgebaseArticleID, $_attachmentID)
	{
		$_SWIFT = SWIFT::GetInstance();

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

			return false;
		}

		$_knowledgebaseArticleObjectContainer = $this->KnowledgebaseArticleManager->RetrieveForStaff($_knowledgebaseArticleID);
		if (!$_knowledgebaseArticleObjectContainer)
		{
			return false;
		}

		$_SWIFT_KnowledgebaseArticleObject = $_knowledgebaseArticleObjectContainer[0];
		if (!$_SWIFT_KnowledgebaseArticleObject instanceof SWIFT_KnowledgebaseArticle || !$_SWIFT_KnowledgebaseArticleObject->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

		$_SWIFT_AttachmentObject = new SWIFT_Attachment($_attachmentID);
		// Did the object load up?
		if (!$_SWIFT_AttachmentObject instanceof SWIFT_Attachment || !$_SWIFT_AttachmentObject->GetIsClassLoaded()) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

		if ($_SWIFT_AttachmentObject->GetProperty('linktype') != SWIFT_Attachment::LINKTYPE_KBARTICLE || $_SWIFT_AttachmentObject->GetProperty('linktypeid') != $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID()) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

		$_SWIFT_AttachmentObject->Dispatch();

		return true;
	}
}
?>