<?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 Ticket Follow-Up Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_TicketFollowUp extends SWIFT_Model
{
	const TABLE_NAME		=	'ticketfollowups';
	const PRIMARY_KEY		=	'ticketfollowupid';

	const TABLE_STRUCTURE	=	"ticketfollowupid I PRIMARY AUTO NOTNULL,
								dateline I DEFAULT '0' NOTNULL,
								executiondateline I DEFAULT '0' NOTNULL,
								ticketid I DEFAULT '0' NOTNULL,
								staffid I DEFAULT '0' NOTNULL,
								dochangeproperties I2 DEFAULT '0' NOTNULL,
								ownerstaffid F DEFAULT '0' NOTNULL,
								departmentid F DEFAULT '0' NOTNULL,
								ticketstatusid F DEFAULT '0' NOTNULL,
								tickettypeid F DEFAULT '0' NOTNULL,
								priorityid F DEFAULT '0' NOTNULL,
								dochangeduedateline I2 DEFAULT '0' NOTNULL,
								duedateline I DEFAULT '0' NOTNULL,
								resolutionduedateline I DEFAULT '0' NOTNULL,
								timeworked I DEFAULT '0' NOTNULL,
								timebillable I DEFAULT '0' NOTNULL,
								donote I2 DEFAULT '0' NOTNULL,
								notetype C(50) DEFAULT '0' NOTNULL,
								notecolor I2 DEFAULT '0' NOTNULL,
								ticketnotes X2 NOTNULL,
								doreply I2 DEFAULT '0' NOTNULL,
								replycontents X2 NOTNULL,
								doforward I2 DEFAULT '0' NOTNULL,
								forwardemailto C(255) DEFAULT '' NOTNULL,
								forwardcontents X2 NOTNULL";

	const INDEX_1			=	'ticketid';
	const INDEX_2			=	'executiondateline';


	protected $_dataStore = array();

	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Data $_SWIFT_DataObject The SWIFT_Data Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Record could not be loaded
	 */
	public function __construct(SWIFT_Data $_SWIFT_DataObject)
	{
		parent::__construct();

		if (!$_SWIFT_DataObject instanceof SWIFT_Data || !$_SWIFT_DataObject->GetIsClassLoaded() || !$this->LoadData($_SWIFT_DataObject)) {
			throw new SWIFT_Exception('Failed to load Ticket Follow-Up Object');

			$this->SetIsClassLoaded(false);

			return false;
		}

		return true;
	}

	/**
	 * Destructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __destruct()
	{
		$this->ProcessUpdatePool();

		parent::__destruct();

		return true;
	}

	/**
	 * Processes the Update Pool Data
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_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 . 'ticketfollowups', $this->GetUpdatePool(), 'UPDATE', "ticketfollowupid = '" .
				intval($this->GetTicketFollowUpID()) . "'");

		$this->ClearUpdatePool();

		return true;
	}

	/**
	 * Retrieves the Ticket Follow-Up ID
	 *
	 * @author Varun Shoor
	 * @return mixed "ticketfollowupid" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetTicketFollowUpID()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

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

	/**
	 * Load the Data
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Data $_SWIFT_DataObject The SWIFT_Data Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Ticket_Exception If Invalid Data is Provided
	 */
	protected function LoadData($_SWIFT_DataObject)
	{
		$_SWIFT = SWIFT::GetInstance();

		// Is it a ID?
		if ($_SWIFT_DataObject instanceof SWIFT_DataID && $_SWIFT_DataObject->GetIsClassLoaded())
		{
			$_dataStore = $_SWIFT->Database->QueryFetch("SELECT * FROM " . TABLE_PREFIX . "ticketfollowups WHERE ticketfollowupid = '" .
					intval($_SWIFT_DataObject->GetDataID()) . "'");
			if (isset($_dataStore['ticketfollowupid']) && !empty($_dataStore['ticketfollowupid']))
			{
				$this->_dataStore = $_dataStore;

				return true;
			}

			// Is it a Store?
		} else if ($_SWIFT_DataObject instanceof SWIFT_DataStore && $_SWIFT_DataObject->GetIsClassLoaded()) {
			$this->_dataStore = $_SWIFT_DataObject->GetDataStore();

			if (!isset($this->_dataStore['ticketfollowupid']) || empty($this->_dataStore['ticketfollowupid']))
			{
				throw new SWIFT_Exception(SWIFT_INVALIDDATA);
			}

			return true;
		}

		throw new SWIFT_Exception(SWIFT_INVALIDDATA);

		return false;
	}

	/**
	 * Returns the Data Store Array
	 *
	 * @author Varun Shoor
	 * @return mixed "_dataStore" Array on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetDataStore()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_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_Exception If the Class is not Loaded
	 */
	public function GetProperty($_key)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

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

			return false;
		}

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

	/**
	 * Create a new Ticket Follow-Up ID
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Ticket $_SWIFT_TicketObject The Parent Ticket Object
	 * @param int $_executionDateline The Dateline when this follow up should be executed
	 * @param bool $_doChangeProperties Whether to change the ticket properties
	 * @param int $_ownerStaffID The Owner Staff ID
	 * @param int $_departmentID The New Department ID
	 * @param int $_ticketStatusID The New Ticket Status ID
	 * @param int $_ticketTypeID The New Ticket Type ID
	 * @param int $_ticketPriorityID The New Ticket Priority ID
	 * @param bool $_doCHangeDueDateline Whether to update the due datelines on the ticket
	 * @param int $_timeWorked Total Time Worked
	 * @param int $_timeBillable Total Time Billable
	 * @param bool $_doNote Create a ticket note?
	 * @param string $_noteType The Note Type
	 * @param int $_noteColor The Note Color
	 * @param string $_ticketNotes The Ticket Notes
	 * @param bool $_doReply Whether to create a reply
	 * @param string $_replyContents The Reply Contents
	 * @param bool $_doForward Whether to forward this ticket
	 * @param string $_forwardEmailTo The Email Address to forward to
	 * @param string $_forwardContents The Forward Contents
	 * @param SWIFT_Staff $_SWIFT_StaffObject (OPTIONAL) The Creator Staff Object
	 * @return int Ticket Follow-Up ID
	 * @throws SWIFT_Exception If Invalid Data is Provided or If the Object could not be created
	 */
	static public function Create(SWIFT_Ticket $_SWIFT_TicketObject, $_executionDateline,
			$_doChangeProperties, $_ownerStaffID, $_departmentID, $_ticketStatusID, $_ticketTypeID, $_ticketPriorityID,
			$_doChangeDueDateline, $_dueDateline, $_resolutionDueDateline,
			$_timeWorked, $_timeBillable,
			$_doNote, $_noteType, $_noteColor, $_ticketNotes,
			$_doReply, $_replyContents,
			$_doForward, $_forwardEmailTo, $_forwardContents,
			SWIFT_Staff $_SWIFT_StaffObject = null)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded() || empty($_executionDateline))
		{
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$_staffID = 0;
		if ($_SWIFT_StaffObject instanceof SWIFT_Staff && $_SWIFT_StaffObject->GetIsClassLoaded())
		{
			$_staffID = $_SWIFT_StaffObject->GetStaffID();
		}

		$_SWIFT->Database->AutoExecute(TABLE_PREFIX . 'ticketfollowups', array('dateline' => DATENOW, 'executiondateline' => intval($_executionDateline),
			'ticketid' => intval($_SWIFT_TicketObject->GetTicketID()), 'staffid' => intval($_staffID), 'dochangeproperties' => intval($_doChangeProperties),
			'ownerstaffid' => intval($_ownerStaffID), 'departmentid' => intval($_departmentID), 'ticketstatusid' => intval($_ticketStatusID),
			'tickettypeid' => intval($_ticketTypeID), 'priorityid' => intval($_ticketPriorityID), 'dochangeduedateline' => intval($_doChangeDueDateline),
			'duedateline' => intval($_dueDateline), 'resolutionduedateline' => intval($_resolutionDueDateline), 'timeworked' => intval($_timeWorked),
			'timebillable' => intval($_timeBillable), 'donote' => intval($_doNote), 'notetype' => $_noteType, 'notecolor' => intval($_noteColor),
			'ticketnotes' => $_ticketNotes, 'doreply' => intval($_doReply), 'replycontents' => $_replyContents, 'doforward' => intval($_doForward),
			'forwardemailto' => $_forwardEmailTo, 'forwardcontents' => $_forwardContents), 'INSERT');
		$_ticketFollowUpID = $_SWIFT->Database->Insert_ID();

		if (!$_ticketFollowUpID)
		{
			throw new SWIFT_Exception(SWIFT_CREATEFAILED);

			return false;
		}

		$_SWIFT_TicketObject->MarkHasFollowUp();

		return $_ticketFollowUpID;
	}

	/**
	 * Update the Ticket Follow-Up Record
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Ticket $_SWIFT_TicketObject The Parent Ticket Object
	 * @param int $_executionDateline The Dateline when this follow up should be executed
	 * @param bool $_doChangeProperties Whether to change the ticket properties
	 * @param int $_ownerStaffID The Owner Staff ID
	 * @param int $_departmentID The New Department ID
	 * @param int $_ticketStatusID The New Ticket Status ID
	 * @param int $_ticketTypeID The New Ticket Type ID
	 * @param int $_ticketPriorityID The New Ticket Priority ID
	 * @param bool $_doCHangeDueDateline Whether to update the due datelines on the ticket
	 * @param int $_timeWorked Total Time Worked
	 * @param int $_timeBillable Total Time Billable
	 * @param bool $_doNote Create a ticket note?
	 * @param string $_noteType The Note Type
	 * @param int $_noteColor The Note Color
	 * @param string $_ticketNotes The Ticket Notes
	 * @param bool $_doReply Whether to create a reply
	 * @param string $_replyContents The Reply Contents
	 * @param bool $_doForward Whether to forward this ticket
	 * @param string $_forwardEmailTo The Email Address to forward to
	 * @param string $_forwardContents The Forward Contents
	 * @param SWIFT_Staff $_SWIFT_StaffObject (OPTIONAL) The Creator Staff Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded or If Invalid Data is Provided
	 */
	public function Update(SWIFT_Ticket $_SWIFT_TicketObject, $_executionDateline,
			$_doChangeProperties, $_ownerStaffID, $_departmentID, $_ticketStatusID, $_ticketTypeID, $_ticketPriorityID,
			$_doChangeDueDateline, $_dueDateline, $_resolutionDueDateline,
			$_timeWorked, $_timeBillable,
			$_doNote, $_noteType, $_noteColor, $_ticketNotes,
			$_doReply, $_replyContents,
			$_doForward, $_forwardEmailTo, $_forwardContents,
			SWIFT_Staff $_SWIFT_StaffObject = null)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		} else if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded() || empty($_executionDateline)) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$_staffID = 0;
		if ($_SWIFT_StaffObject instanceof SWIFT_Staff && $_SWIFT_StaffObject->GetIsClassLoaded())
		{
			$_staffID = $_SWIFT_StaffObject->GetStaffID();
		}

		$this->UpdatePool('executiondateline', intval($_executionDateline));
		$this->UpdatePool('ticketid', intval($_SWIFT_TicketObject->GetTicketID()));
		$this->UpdatePool('staffid', intval($_staffID));
		$this->UpdatePool('dochangeproperties', intval($_doChangeProperties));
		$this->UpdatePool('ownerstaffid', intval($_ownerStaffID));
		$this->UpdatePool('departmentid', intval($_departmentID));
		$this->UpdatePool('ticketstatusid', intval($_ticketStatusID));
		$this->UpdatePool('tickettypeid', intval($_ticketTypeID));
		$this->UpdatePool('priorityid', intval($_ticketPriorityID));
		$this->UpdatePool('dochangeduedateline', intval($_doChangeDueDateline));
		$this->UpdatePool('duedateline', intval($_dueDateline));
		$this->UpdatePool('resolutionduedateline', intval($_resolutionDueDateline));
		$this->UpdatePool('timeworked', intval($_timeWorked));
		$this->UpdatePool('timebillable', intval($_timeBillable));
		$this->UpdatePool('donote', intval($_doNote));
		$this->UpdatePool('notetype', $_noteType);
		$this->UpdatePool('notecolor', intval($_noteColor));
		$this->UpdatePool('ticketnotes', $_ticketNotes);
		$this->UpdatePool('doreply', intval($_doReply));
		$this->UpdatePool('replycontents', $_replyContents);
		$this->UpdatePool('doforward', intval($_doForward));
		$this->UpdatePool('forwardemailto', $_forwardEmailTo);
		$this->UpdatePool('forwardcontents', $_forwardContents);
		$this->ProcessUpdatePool();

		return true;
	}

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

			return false;
		}

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

		$this->SetIsClassLoaded(false);

		return true;
	}

	/**
	 * Delete a list of Ticket Follow-Up's
	 *
	 * @author Varun Shoor
	 * @param array $_ticketFollowUpIDList
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function DeleteList($_ticketFollowUpIDList)
	{
		$_SWIFT = SWIFT::GetInstance();

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

		$_SWIFT->Database->Query("DELETE FROM " . TABLE_PREFIX . "ticketfollowups WHERE ticketfollowupid IN (" . BuildIN($_ticketFollowUpIDList) . ")");

		return true;
	}

	/**
	 * Delete the Ticket Follow-Up's based on a list of Ticket IDs
	 *
	 * @author Varun Shoor
	 * @param array $_ticketIDList The Ticket ID List
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If Invalid Data is Provided
	 */
	static public function DeleteOnTicket($_ticketIDList)
	{
		$_SWIFT = SWIFT::GetInstance();

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

		$_ticketFollowUpIDList = array();
		$_SWIFT->Database->Query("SELECT ticketfollowupid FROM " . TABLE_PREFIX . "ticketfollowups WHERE ticketid IN (" . BuildIN($_ticketIDList) . ")");
		while ($_SWIFT->Database->NextRecord())
		{
			$_ticketFollowUpIDList[] = intval($_SWIFT->Database->Record['ticketfollowupid']);
		}

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

		self::DeleteList($_ticketFollowUpIDList);

		return true;
	}

	/**
	 * Replace the current ticket id all tickets with the new one
	 *
	 * @author Varun Shoor
	 * @param array $_ticketIDList The Old Ticket ID List
	 * @param SWIFT_Ticket $_SWIFT_ParentTicketObject The Parent Ticket Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If Invalid Data is Provided
	 */
	static public function ReplaceTicket($_ticketIDList, SWIFT_Ticket $_SWIFT_ParentTicketObject) {
		$_SWIFT = SWIFT::GetInstance();

		if (!$_SWIFT_ParentTicketObject instanceof SWIFT_Ticket || !$_SWIFT_ParentTicketObject->GetIsClassLoaded()) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

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

		$_SWIFT->Database->AutoExecute(TABLE_PREFIX . 'ticketfollowups', array('ticketid' => intval($_SWIFT_ParentTicketObject->GetTicketID())),
				'UPDATE', "ticketid IN (" . BuildIN($_ticketIDList) . ")");

		return true;
	}

	/**
	 * Retrieve the follow up objects based on the given ticket
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Ticket $_SWIFT_TicketObject The SWIFT_Ticket Object Pointer
	 * @return array The Ticket Follow-Up Container
	 * @throws SWIFT_Exception If Invalid Data is Provided
	 */
	static public function RetrieveOnTicket(SWIFT_Ticket $_SWIFT_TicketObject)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);
		}

		$_ticketFollowUpContainer = array();
		$_SWIFT->Database->Query("SELECT * FROM " . TABLE_PREFIX . "ticketfollowups WHERE ticketid = '" . intval($_SWIFT_TicketObject->GetTicketID()) . "'");
		while ($_SWIFT->Database->NextRecord())
		{
			$_ticketFollowUpContainer[$_SWIFT->Database->Record['ticketfollowupid']] = new SWIFT_TicketFollowUp(new SWIFT_DataStore($_SWIFT->Database->Record));
		}

		return $_ticketFollowUpContainer;
	}
}
?>
