<?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
 *
 * ###############################################
 */

/**
 * Comments Controller: Knowledgebase Article
 *
 * @author Varun Shoor
 */
class Controller_Comments extends Controller_client
{
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __construct()
	{
		parent::__construct();

        /*
        * BUG FIX - Parminder Singh
        *
        * SWIFT-2528: Widget particular pages shows up using direct URIs irrespective of whether the widget's visibility is restricted.
        *
        * Comments: None
        */
        if (!SWIFT_App::IsInstalled(APP_KNOWLEDGEBASE) || !SWIFT_Widget::IsWidgetVisible(APP_KNOWLEDGEBASE, 'knowledgebase'))
        {
            $this->UserInterface->Error(true, $this->Language->Get('nopermission'));
            $this->Load->Controller('Default', 'Core')->Load->Index();

            exit;
        }

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

		return true;
	}

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

		return true;
	}

	/**
	 * Submit a new Comment
	 *
	 * @author Varun Shoor
	 * @param int $_knowledgebaseArticleID The Knowledgebase Article ID
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function Submit($_knowledgebaseArticleID)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

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

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

		$_commentResult = $this->CommentManager->ProcessPOSTUser(SWIFT_Comment::TYPE_KNOWLEDGEBASE, $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID(),
				SWIFT::Get('basename') . '/Knowledgebase/Article/View/' . $_SWIFT_KnowledgebaseArticleObject->GetKnowledgebaseArticleID());

		if ($_commentResult) {
			unset($_POST['fullname']); unset($_POST['email']); unset($_POST['comments']);
		}

		$this->Load->Controller('Article', 'Knowledgebase')->Load->Method('View', $_knowledgebaseArticleID);

		return true;
	}
}
?>