<?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 Client Message Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_Message extends SWIFT_MessageManager
{
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @param int $_messageID The Message ID
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Message_Exception If the Record could not be loaded
	 */
	public function __construct($_messageID)
	{
		parent::__construct($_messageID);

		if ($this->GetProperty('messagetype') != self::MESSAGE_CLIENT && $this->GetProperty('messagetype') != self::MESSAGE_STAFF)
		{
			throw new SWIFT_Message_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		return true;
	}

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

		return true;
	}

	/**
	 * Creates the message, handles the routing and inserts the record if necessary
	 *
	 * @author Varun Shoor
	 * @param string $_fullName The User Full Name
	 * @param string $_email The User Email
	 * @param string $_subject The User Subject
	 * @param int $_departmentID The Department ID
	 * @param string $_messageContents The Message Contents
	 * @param int $_templateGroupID The Template Group ID
	 * @return mixed "_SWIFT_MessageObject" (OBJECT) on Success, "false" otherwise
	 * @throws SWIFT_Message_Exception If Invalid Data is Provided
	 */
	static public function Create($_fullName, $_email, $_subject, $_departmentID, $_messageContents, $_templateGroupID) {
		$_SWIFT = SWIFT::GetInstance();

		$_departmentID = intval($_departmentID);

		if (empty($_fullName) || empty($_email) || empty($_departmentID) || empty($_subject) || empty($_messageContents) || !IsEmailValid($_email))
		{
			throw new SWIFT_Message_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$_departmentCache = $_SWIFT->Cache->Get('departmentcache');
		$_templateGroupCache = $_SWIFT->Cache->Get('templategroupcache');

		$_messageRouting = self::GetMessageRoutingRule($_departmentID);

		$_insertMessageRecord = false;
		if (!$_messageRouting || (_is_array($_messageRouting) && $_messageRouting['preservemessage'])) {
			$_insertMessageRecord = true;
		}

		$_SWIFT_MessageObject = true;
		if ($_insertMessageRecord) {
			$_messageID = parent::Create($_fullName, $_email, $_subject, $_departmentID, $_messageContents, self::MESSAGE_CLIENT, 0);

			if (!$_messageID) {
				throw new SWIFT_Message_Exception(SWIFT_CREATEFAILED);

				return false;
			}

			$_SWIFT_MessageObject = new SWIFT_Message($_messageID);
			if (!$_SWIFT_MessageObject instanceof SWIFT_Message || !$_SWIFT_MessageObject->GetIsClassLoaded())
			{
				throw new SWIFT_Message_Exception(SWIFT_CREATEFAILED);

				return false;
			}
		}

		if (isset($_messageRouting['routetotickets']) && isset($_messageRouting['ticketdepartmentid']) && !empty($_messageRouting['routetotickets']) &&
				!empty($_messageRouting['ticketdepartmentid']) && isset($_departmentCache[$_messageRouting['ticketdepartmentid']]) &&
				SWIFT_App::IsInstalled(APP_TICKETS) && isset($_templateGroupCache[$_templateGroupID])) {
			$_templateGroup = $_templateGroupCache[$_templateGroupID];

			SWIFT_Loader::LoadModel('Ticket:Ticket', APP_TICKETS);

			$_SWIFT->Language->Load('tickets');
			SWIFT_Ticket::LoadLanguageTable();

			$_userID = SWIFT_Ticket::GetOrCreateUserID($_fullName, $_email, $_templateGroup['regusergroupid']);
			$_ownerStaffID = 0;

			$_SWIFT_TicketObject = SWIFT_Ticket::Create($_subject, $_fullName, $_email, $_messageContents, $_ownerStaffID, $_messageRouting['ticketdepartmentid'],
														$_templateGroup['ticketstatusid'], $_templateGroup['priorityid'], $_templateGroup['tickettypeid'],
														$_userID, 0, SWIFT_Ticket::TYPE_DEFAULT, SWIFT_Ticket::CREATOR_CLIENT, SWIFT_Ticket::CREATIONMODE_SUPPORTCENTER, '', 0, true);

			$_SWIFT_TicketObject->SetTemplateGroup($_SWIFT->TemplateGroup->GetTemplateGroupID());
		}

		if ($_messageRouting['routetoemail'] && isset($_messageRouting['forwardemails']) && !empty($_messageRouting['routetoemail']) && !empty($_messageRouting['forwardemails'])) {
			$_destinationEmailList = $_pendingProcessEmailList = array();
			$_messageRouting['forwardemails'] = trim($_messageRouting['forwardemails']);
			if (stristr($_messageRouting['forwardemails'], ';')) {
				$_pendingProcessEmailList = explode(';', $_messageRouting['forwardemails']);
				if (_is_array($_pendingProcessEmailList)) {
					foreach ($_pendingProcessEmailList as $_pendingEmail) {
						if (IsEmailValid(trim($_pendingEmail))) {
							$_destinationEmailList[] = trim($_pendingEmail);
						}
					}
				} else if (IsEmailValid(trim(str_replace(';', '', $_messageRouting['forwardemails'])))) {
					$_destinationEmailList[] = trim(str_replace(';', '', $_messageRouting['forwardemails']));
				}
			} else if (IsEmailValid($_messageRouting['forwardemails'])) {
				$_destinationEmailList[] = $_messageRouting['forwardemails'];
			}

			if (_is_array($_destinationEmailList)) {
				foreach ($_destinationEmailList as $_destinationEmail) {

					$_SWIFT_MailObject = new SWIFT_Mail();
					$_SWIFT_MailObject->SetToField($_destinationEmail);
					$_SWIFT_MailObject->SetFromField($_email, $_fullName);
					$_SWIFT_MailObject->SetSubjectField($_subject);
					$_SWIFT_MailObject->SetDataText($_messageContents);
					$_SWIFT_MailObject->SendMail(false);
				}
			}
		}

		// GeoIP
		if ($_SWIFT_MessageObject instanceof SWIFT_Message && $_SWIFT_MessageObject->GetIsClassLoaded())
		{
			$_SWIFT_MessageObject->UpdateGeoIP();
		}


		return $_SWIFT_MessageObject;
	}

	/**
	 * Retrieve the Message Routing Rules for a given department
	 *
	 * @author Varun Shoor
	 * @param int $_departmentID The Department ID to fetch the rule from
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Message_Exception If Invalid Data is Provided
	 */
	static public function GetMessageRoutingRule($_departmentID) {
		$_SWIFT = SWIFT::GetInstance();

		if (empty($_departmentID))
		{
			throw new SWIFT_Message_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$_messageRouting = $_SWIFT->Database->QueryFetch("SELECT * FROM ". TABLE_PREFIX ."messagerouting WHERE departmentid = '". intval($_departmentID) ."'");
		if (!isset($_messageRouting['messageroutingid']) || empty($_messageRouting['messageroutingid'])) {
			return false;
		}

		return $_messageRouting;
	}
}
?>