<?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_news 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_NEWS);

		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()
	{
		// ======= NEWSITEMDATA =======
		$this->AddTable('newsitemdata', new SWIFT_SetupDatabaseTable(TABLE_PREFIX ."newsitemdata", "newsitemdataid I PRIMARY AUTO NOTNULL,
																				newsitemid I DEFAULT '0' NOTNULL,
																				contents X2"));
		$this->AddIndex('newsitemdata', new SWIFT_SetupDatabaseIndex("newsitemdata1", TABLE_PREFIX ."newsitemdata", "newsitemid"));

		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:widgetnews', 'news', APP_NEWS, '/News/List', '{$themepath}icon_widget_news.png', '{$themepath}icon_widget_news_small.png', 5, true, true, true, true, SWIFT_Widget::VISIBLE_ALL, false);

		// ======= CRON =======
		SWIFT_Cron::Create('newssync', 'News', 'NewsMinute', 'Sync', '0', '30', '0', true);

		$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;
		}

		// Create a news category
		$_newsCategoryID = SWIFT_NewsCategory::Create($this->Language->Get('sample_newscatname'), SWIFT_PUBLIC);

		$_staffContainer = SWIFT_Staff::RetrieveOnEmail($_POST['email']);

		// Create a news article
		SWIFT_NewsItem::Create(SWIFT_NewsItem::TYPE_PRIVATE, SWIFT_NewsItem::STATUS_PUBLISHED, $_staffContainer['fullname'], $_staffContainer['email'], $this->Language->Get('sample_newsarticletitle1'),
							   $this->Language->Get('sample_newsarticletitle1'),
							   sprintf($this->Language->Get('sample_newsarticlecontent1'), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl'])),
							   $_staffContainer['staffid'], false, true, false, array(), false, array(), false, array($_newsCategoryID));

		SWIFT_NewsItem::Create(SWIFT_NewsItem::TYPE_GLOBAL, SWIFT_NewsItem::STATUS_PUBLISHED, $_staffContainer['fullname'], $_staffContainer['email'], $this->Language->Get('sample_newsarticletitle2'),
							   $this->Language->Get('sample_newsarticletitle2'),
							   sprintf($this->Language->Get('sample_newsarticlecontent2'), RemoveTrailingSlash($_POST['producturl'] . SWIFT_BASENAME), RemoveTrailingSlash($_POST['producturl'] . SWIFT_BASENAME), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl']), RemoveTrailingSlash($_POST['producturl'])),
							   $_staffContainer['staffid'], false, true, false, array(), false, array(), false, array($_newsCategoryID));

		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_NEWS));

		SWIFT_Cron::DeleteOnName(array('newssync'));

		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);
	}
}
?>