<?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 Chat Note Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_ChatNote extends SWIFT_VisitorNoteManager {
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @param int $_visitorNoteID The Visitor Note ID
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __construct($_visitorNoteID) {
		return parent::__construct($_visitorNoteID);
	}

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

		return true;
	}

	/**
	 * Create a new Chat Note
	 *
	 * @author Varun Shoor
	 * @param object $_SWIFT_ChatObject The Chat Object
	 * @param string $_noteContents The Note Contents
	 * @param int $_noteColor The Note Color
	 * @return mixed "SWIFT_ChatNote" Object on Success, "false" otherwise
	 * @throws SWIFT_Note_Exception If Invalid Data is Provided or If Creation Fails
	 */
	static public function Create(SWIFT_Chat $_SWIFT_ChatObject, $_noteContents, $_noteColor = 1) {
		$_SWIFT = SWIFT::GetInstance();

		if (!$_SWIFT_ChatObject instanceof SWIFT_Chat || !$_SWIFT_ChatObject->GetIsClassLoaded()) {
			throw new SWIFT_Note_Exception(SWIFT_INVALIDDATA);

			return false;
		} else if (!$_SWIFT->Staff instanceof SWIFT_Staff || !$_SWIFT->Staff->GetIsClassLoaded()) {
			throw new SWIFT_Note_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$_visitorNoteID = parent::Create(self::LINKTYPE_CHAT, $_SWIFT_ChatObject->GetChatObjectID(), $_SWIFT->Staff->GetStaffID(), $_SWIFT->Staff->GetProperty('fullname'), $_noteContents, $_noteColor);
		if (!$_visitorNoteID) {
			throw new SWIFT_Note_Exception(SWIFT_CREATEFAILED);

			return false;
		}

		return new SWIFT_ChatNote($_visitorNoteID);
	}

	/**
	 * Delete Visitor Notes based on Chat Object ID List
	 *
	 * @author Varun Shoor
	 * @param array $_chatObjectIDList The Chat Object ID List
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function DeleteOnChat($_chatObjectIDList)
	{
		$_SWIFT = SWIFT::GetInstance();

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

		$_visitorNoteIDList = array();
		$_SWIFT->Database->Query("SELECT * FROM " . TABLE_PREFIX . "visitornotes WHERE linktype = '" . self::LINKTYPE_CHAT . "' AND linktypevalue IN (" . BuildIN($_chatObjectIDList) . ")");
		while ($_SWIFT->Database->NextRecord())
		{
			$_visitorNoteIDList[] = $_SWIFT->Database->Record['visitornoteid'];
		}

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

		self::DeleteList($_visitorNoteIDList);

		return true;
	}
}
?>