<?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 Main Installer
 *
 * @author Varun Shoor
 */
class SWIFT_SetupDatabase_knowledgebase extends SWIFT_SetupDatabase
{
	// Core Constants
	const PAGE_COUNT = 1;

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

		return true;
	}

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

		return true;
	}

	/**
	 * Loads the table into the container
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function LoadTables()
	{
		// ======= KBARTICLEDATA =======
		$this->AddTable('kbarticledata', new SWIFT_SetupDatabaseTable(TABLE_PREFIX . "kbarticledata", "kbarticledataid I PRIMARY AUTO NOTNULL,
																kbarticleid I DEFAULT '0' NOTNULL,
																contents X2,
																contentstext X2"));
		$this->AddIndex('kbarticledata', new SWIFT_SetupDatabaseIndex("kbarticledata1", TABLE_PREFIX . "kbarticledata", "kbarticleid"));

		return true;
	}

	/**
	 * Get the Page Count for Execution
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function GetPageCount()
	{
		return self::PAGE_COUNT;
	}

	/**
	 * Function that does the heavy execution
	 *
	 * @author Varun Shoor
	 * @param int $_pageIndex The Page Index
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function Install($_pageIndex)
	{
		parent::Install($_pageIndex);

		// ======= WIDGET =======
		SWIFT_Widget::Create('PHRASE:widgetknowledgebase', 'knowledgebase', APP_KNOWLEDGEBASE, '/Knowledgebase/List', '{$themepath}icon_widget_knowledgebase.png', '{$themepath}icon_widget_knowledgebase_small.png', 4, true, true, true, true, SWIFT_Widget::VISIBLE_ALL, false);

		$this->InstallSampleData();

		return true;
	}


	/**
	 * @author Saloni Dhall <saloni.dhall@kayako.com>
	 * @author Utsav Handa <utsav.handa@kayako.com>
	 *
	 * @return bool
	 */
	public function InstallSampleData()
	{
		if (!defined('INSTALL_SAMPLE_DATA') || INSTALL_SAMPLE_DATA != true) {
			return false;
		}

		// Creating a knowledgebase category
		$_knowledgebaseCategoryID = SWIFT_KnowledgebaseCategory::Create('0', $this->Language->Get('sample_kbcategoryname'), SWIFT_KnowledgebaseCategory::TYPE_GLOBAL, 1, 1, 1, 1, 1);

		// Retrieving the Staff Details based on email address
		$_staffContainer = SWIFT_Staff::RetrieveOnEmail($_POST['email']);

		// Creating two knowledgebase articles
		SWIFT_KnowledgebaseArticle::Create(SWIFT_KnowledgebaseArticle::CREATOR_STAFF, $_staffContainer['staffid'], $_staffContainer['fullname'], $_staffContainer['email'], SWIFT_KnowledgebaseArticle::STATUS_PUBLISHED,
										   $this->Language->Get('sample_kbarticlesubject1'),
										   sprintf($this->Language->Get('sample_kbarticlecontent1'), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl'])),
										   false, true, array($_knowledgebaseCategoryID));

		SWIFT_KnowledgebaseArticle::Create(SWIFT_KnowledgebaseArticle::CREATOR_STAFF, $_staffContainer['staffid'], $_staffContainer['fullname'], $_staffContainer['email'], SWIFT_KnowledgebaseArticle::STATUS_PUBLISHED,
										   $this->Language->Get('sample_kbarticlesubject2'),
										   $this->Language->Get('sample_kbarticlecontent2'),
										   false, true, array($_knowledgebaseCategoryID));

		return true;
	}

	/**
	 * Uninstalls the App
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function Uninstall()
	{
		parent::Uninstall();

		SWIFT_Widget::DeleteOnApp(array(APP_KNOWLEDGEBASE));

		return true;
	}

	/**
	 * Upgrade the App
	 *
	 * @author Varun Shoor
	 * @param bool $_isForced
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function Upgrade($_isForced = false)
	{
		return parent::Upgrade($_isForced);
	}

        /**
	 * Upgrade from 4.01.204
	 *
	 * @author Mahesh Salaria
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
        public function Upgrade_4_01_204() {
		if (!$this->GetIsClassLoaded()) {
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$_SWIFT_SearchEngineObject = new SWIFT_SearchEngine();

		$this->Database->Query("SELECT kbdata.kbarticledataid, kbdata.kbarticleid, kbdata.contentstext, kbarticles.subject FROM " . TABLE_PREFIX . "kbarticledata AS kbdata LEFT JOIN " . TABLE_PREFIX . "kbarticles AS kbarticles ON (kbdata.kbarticleid = kbarticles.kbarticleid) ORDER BY kbdata.kbarticleid ASC");
		while ($this->Database->NextRecord())
		{
			$_SWIFT_SearchEngineObject->Insert($this->Database->Record['kbarticleid'], $this->Database->Record['kbarticledataid'], SWIFT_SearchEngine::TYPE_KNOWLEDGEBASE, $this->Database->Record['subject'] . " " . $this->Database->Record['contentstext']);
		}

		return true;
	}
}
?>