<?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 Parser Log Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_ParserLog extends SWIFT_Model
{
	const TABLE_NAME		=	'parserlogs';
	const PRIMARY_KEY		=	'parserlogid';

	const TABLE_STRUCTURE	=	"parserlogid I PRIMARY AUTO NOTNULL,
								typeid I DEFAULT '0' NOTNULL,
								dateline I DEFAULT '0' NOTNULL,
								emailqueueid I DEFAULT '0' NOTNULL,
								logtype C(20) DEFAULT 'failure' NOTNULL,
								subject C(255) DEFAULT '' NOTNULL,
								fromemail C(255) DEFAULT '' NOTNULL,
								toemail C(255) DEFAULT '' NOTNULL,
								size I DEFAULT '0' NOTNULL,
								description X NOTNULL,
								parsetimetaken F DEFAULT '0.0' NOTNULL,

								responsetype C(20) DEFAULT 'ticket' NOTNULL,
								ticketpostid I DEFAULT '0' NOTNULL,
								ticketmaskid C(20) DEFAULT '' NOTNULL,
								messageid C(100) DEFAULT '' NOTNULL";

	const INDEX_1			=	'ticketpostid';
	const INDEX_2			=	'dateline';
	const INDEX_3			=	'emailqueueid';
	const INDEX_4			=	'logtype, dateline';


	protected $_dataStore = array();

	// Core Constants
	const TYPE_SUCCESS = 'success';
	const TYPE_FAILURE = 'failure';

	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @param int $_parserLogID The Parser Log ID
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Record could not be loaded
	 */
	public function __construct($_parserLogID)
	{
		parent::__construct();

		if (!$this->LoadData($_parserLogID)) {
			throw new SWIFT_Log_Exception('Failed to load Parser Log ID: ' . intval($_parserLogID));

			$this->SetIsClassLoaded(false);

			return false;
		}

		return true;
	}

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

		return true;
	}

	/**
	 * Processes the Update Pool Data
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Class is not Loaded
	 */
	public function ProcessUpdatePool()
	{
		if (!$this->GetIsClassLoaded())
		{
			return false;
		} else if (!_is_array($this->GetUpdatePool())) {
			return false;
		}

		$this->Database->AutoExecute(TABLE_PREFIX . 'parserlogs', $this->GetUpdatePool(), 'UPDATE', "parserlogid = '" .
				intval($this->GetParserLogID()) . "'");

		$this->ClearUpdatePool();

		return true;
	}

	/**
	 * Retrieves the Parser Log ID
	 *
	 * @author Varun Shoor
	 * @return mixed "parserlogid" on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Class is not Loaded
	 */
	public function GetParserLogID()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Log_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		return $this->_dataStore['parserlogid'];
	}

	/**
	 * Load the Data
	 *
	 * @author Varun Shoor
	 * @param int $_parserLogID The Parser Log ID
	 * @return bool "true" on Success, "false" otherwise
	 */
	protected function LoadData($_parserLogID)
	{
		$_dataStore = $this->Database->QueryFetch("SELECT parserlogs.*, parserlogdata.contents AS contents FROM " . TABLE_PREFIX .
				"parserlogs AS parserlogs LEFT JOIN " . TABLE_PREFIX . "parserlogdata AS parserlogdata ON
					(parserlogs.parserlogid = parserlogdata.parserlogid) WHERE parserlogs.parserlogid = '" . intval($_parserLogID) . "'");
		if (isset($_dataStore['parserlogid']) && !empty($_dataStore['parserlogid']))
		{
			if (!isset($_dataStore['contents']))
			{
				$_dataStore['contents'] = '';
			}

			$this->_dataStore = $_dataStore;

			return true;
		}

		return false;
	}

	/**
	 * Returns the Data Store Array
	 *
	 * @author Varun Shoor
	 * @return mixed "_dataStore" Array on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Class is not Loaded
	 */
	public function GetDataStore()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Log_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		return $this->_dataStore;
	}

	/**
	 * Retrieves a Property Value from Data Store
	 *
	 * @author Varun Shoor
	 * @param string $_key The Key Identifier
	 * @return mixed Property Data on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Class is not Loaded
	 */
	public function GetProperty($_key)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Log_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		} else if (!isset($this->_dataStore[$_key])) {
			throw new SWIFT_Log_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		return $this->_dataStore[$_key];
	}

	/**
	 * Check to see if its a valid log type
	 *
	 * @author Varun Shoor
	 * @param string $_logType The Log Type
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function IsValidLogType($_logType)
	{
		if ($_logType == self::TYPE_SUCCESS || $_logType == self::TYPE_FAILURE)
		{
			return true;
		}

		return false;
	}

	/**
	 * Create a new Parser Log
	 *
	 * @author Varun Shoor
	 * @param constant $_logType The Log Type Result
	 * @param int $_emailQueueID The Email Queue ID
	 * @param int $_typeID The Type ID
	 * @param string $_subject The Email Subject
	 * @param string $_fromEmail The From Email Address
	 * @param string $_toEmail The Destination Email Address
	 * @param int $_size The Data Size
	 * @param string $_description The Description for this Entry
	 * @param float $_parseTimeTaken The Time Taken to Parse this Email
	 * @param array $_extendedArguments The Extended Arguments for this Log Entry
	 * @param string $_messageID The email Message ID
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If Invalid Data is Provided
	 */
	static public function Create($_logType, $_emailQueueID, $_typeID, $_subject, $_fromEmail, $_toEmail, $_size, $_description, $_logContents,
			$_parseTimeTaken, $_extendedArguments = array(), $_messageID = '')
	{
		$_SWIFT = SWIFT::GetInstance();

		$_emailQueueID = intval($_emailQueueID);
		$_typeID = intval($_typeID);

		//if ($_SWIFT->Settings->Get('pr_enablelog') != '1')
		//{
		//	continue;
		//}

		if (!self::IsValidLogType($_logType) || !is_array($_extendedArguments))
		{
			throw new SWIFT_Log_Exception('Invalid Data Provided');

			return false;
		}

		$_fieldsContainer = array('emailqueueid' => intval($_emailQueueID), 'typeid' => intval($_typeID), 'dateline' => DATENOW,
			'logtype' => $_logType, 'subject' => $_subject, 'fromemail' => $_fromEmail, 'toemail' => $_toEmail, 'size' => floatval($_size),
			'description' => $_description, 'parsetimetaken' => floatval($_parseTimeTaken), 'messageid' => $_messageID);

		$_fieldsContainer = array_merge($_fieldsContainer, $_extendedArguments);

		$_SWIFT->Database->AutoExecute(TABLE_PREFIX . 'parserlogs', $_fieldsContainer, 'INSERT');
		$_parserLogID = $_SWIFT->Database->Insert_ID();
		if (!$_parserLogID)
		{
			throw new SWIFT_Log_Exception('Unable to Load Parser Log Record');

			return false;
		}

		$_logLength = mb_strlen($_logContents);
		$_maxSize = $_SWIFT->Settings->Get('pr_maxlogsize') * 1024;
		if ($_logLength > $_maxSize)
		{
			$_logContents = '';
		}

		$_SWIFT->Database->AutoExecute(TABLE_PREFIX . 'parserlogdata', array('parserlogid' => intval($_parserLogID),
			'contents' => $_logContents), 'INSERT');

		return $_parserLogID;
	}

	/**
	 * Delete the Parser Log record
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Log_Exception If the Class is not Loaded
	 */
	public function Delete()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Log_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		self::DeleteList(array($this->GetParserLogID()));

		$this->SetIsClassLoaded(false);

		return true;
	}

	/**
	 * Delete a list of Parser Logs
	 *
	 * @author Varun Shoor
	 * @param array $_parserLogIDList The Parser Log ID List
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function DeleteList($_parserLogIDList)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!_is_array($_parserLogIDList))
		{
			return false;
		}

		$_finalParserLogIDList = array();
		$_index = 1;

		$_finalText = '';
		$_SWIFT->Database->Query("SELECT * FROM ". TABLE_PREFIX ."parserlogs WHERE parserlogid IN (" . BuildIN($_parserLogIDList) . ")");
		while ($_SWIFT->Database->NextRecord())
		{
			$_finalParserLogIDList[] = $_SWIFT->Database->Record['parserlogid'];

			$_finalText .= $_index . '. ' . htmlspecialchars($_SWIFT->Database->Record['subject']) . ' (' .
					$_SWIFT->Language->Get('queuefromemail') . ': ' . htmlspecialchars($_SWIFT->Database->Record['fromemail']) . ') <br />';
			$_index++;
		}

		SWIFT::Info(sprintf($_SWIFT->Language->Get('titleparserlogdel'), count($_finalParserLogIDList)), $_SWIFT->Language->Get('msgparserlogdel') .
				'<br />' . $_finalText);

		if (!count($_finalParserLogIDList))
		{
			return false;
		}

		$_SWIFT->Database->Query("DELETE FROM " . TABLE_PREFIX . "parserlogs WHERE parserlogid IN (" . BuildIN($_finalParserLogIDList) . ")");
		$_SWIFT->Database->Query("DELETE FROM " . TABLE_PREFIX . "parserlogdata WHERE parserlogid IN (" . BuildIN($_finalParserLogIDList) . ")");

		return true;
	}

	/**
	 * Cleanup all old logs according to settings
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If Invalid Data is Provided
	 */
	static public function CleanUp()
	{
		$_SWIFT = SWIFT::GetInstance();

		$_dateThreshold = DATENOW - (86400 * $_SWIFT->Settings->Get('pr_logchurndays'));

		$_parserLogIDList = array();

		$_SWIFT->Database->Query("SELECT parserlogid FROM " . TABLE_PREFIX . "parserlogs WHERE dateline < '" . intval($_dateThreshold) . "'");
		while ($_SWIFT->Database->NextRecord())
		{
			$_parserLogIDList[] = intval($_SWIFT->Database->Record['parserlogid']);
		}

		if (!count($_parserLogIDList))
		{
			return false;
		}

		self::DeleteList($_parserLogIDList);

		return true;
	}

	/**
	 * Displays the Dashboard Widget
	 *
	 * @author Varun Shoor
	 * @return mixed "_parserLogContainer" (ARRAY) on Success, "false" otherwise
	 */
	static public function GetDashboardContainer() {
		$_SWIFT = SWIFT::GetInstance();

		$_SWIFT_StaffObject = $_SWIFT->Staff;
		if (!$_SWIFT_StaffObject instanceof SWIFT_Staff || !$_SWIFT_StaffObject->GetIsClassLoaded()) {
			throw new SWIFT_Staff_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$_emailQueueCache = $_SWIFT->Cache->Get('queuecache');

		$_parserLogContainer = array();

		$_timeLine = $_SWIFT_StaffObject->GetProperty('lastvisit');

		$_countContainer = $_SWIFT->Database->QueryFetch("SELECT COUNT(*) AS totalitems FROM " . TABLE_PREFIX . "parserlogs
			WHERE logtype = '" . self::TYPE_FAILURE . "' AND dateline > '" . intval($_timeLine) . "'");
		$_totalRecordCount = 0;
		if (isset($_countContainer['totalitems']))
		{
			$_totalRecordCount = intval($_countContainer['totalitems']);
		}

		$_parserLogsContainer = array();
		$_SWIFT->Database->QueryLimit("SELECT * FROM " . TABLE_PREFIX . "parserlogs
			WHERE logtype = '". self::TYPE_FAILURE . "' AND dateline > '" . intval($_timeLine) . "'
			ORDER BY parserlogid DESC", 7);
		while ($_SWIFT->Database->NextRecord()) {
			$_parserLogsContainer[$_SWIFT->Database->Record['parserlogid']] = $_SWIFT->Database->Record;
		}

		if (count($_parserLogsContainer)) {
			foreach ($_parserLogsContainer as $_parserLogID => $_parserLog) {
				$_displayText = $_SWIFT->Language->Get('logfailure');
				$_displayClass = 'blocknotecounterred';

				$_titleSuffix = '';
				if (isset($_emailQueueCache['list'][$_parserLog['emailqueueid']]))
				{
					$_titleSuffix = ' (' . htmlspecialchars($_emailQueueCache['list'][$_parserLog['emailqueueid']]['email']) . ')';
				}

				$_finalText = '<b>' . $_SWIFT->Language->Get('plogfrom') . ' ' . htmlspecialchars($_parserLog['fromemail']) . '<br />';
				$_finalText .= '<b>' . $_SWIFT->Language->Get('plogto') . ' ' . htmlspecialchars($_parserLog['toemail']) . '<br />';
				$_finalText .= '<b>' . $_SWIFT->Language->Get('plogsize') . ' ' . FormattedSize($_parserLog['size']) . '<br />';

				$_logSubject = htmlspecialchars($_parserLog['subject']);
				if (empty($_logSubject))
				{
					$_logSubject = $_SWIFT->Language->Get('plognosubject');
				}

				$_parserLogContainer[] = array('title' => htmlspecialchars($_logSubject) . $_titleSuffix, 'date' => SWIFT_Date::Get(SWIFT_Date::TYPE_DATETIME, $_parserLog['dateline']) . ' (' . SWIFT_Date::ColorTime(DATENOW - $_parserLog['dateline']) . ')', 'contents' => $_finalText . '<br /><div class="' . $_displayClass . '">' . $_displayText . '</div> ' . htmlspecialchars($_parserLog['description']));
			}
		}

		return array($_totalRecordCount, $_parserLogContainer);
	}

	/**
	 * Check if Message ID Exist
	 *
	 * @author Ravi Sharma <ravi.sharma@kayako.com>
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function IsMessageIDExist($_messageID)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_dataContaner = $_SWIFT->Database->QueryFetch("SELECT parserlogid FROM " . TABLE_PREFIX . "parserlogs WHERE messageid = '" .$_messageID . "'");

		if (isset($_dataContaner['parserlogid'])) {
			return $_dataContaner['parserlogid'];
		}

		return false;
	}
}
?>