<?php
require_once(__DIR__."/../includes/sysinit.php");

class SmsMessage
{
	// Properties of the class:
	var $hddnSmsId, $edtSmsNumber, $edtSmsMessage, $cbSmsSent, $edtSmsReply, $edtSmsKey, $selClientId;
	function __construct(){}
	function __destruct(){}

	function addSmsMessage()
	{
		if (empty($this->hddnSmsId))
		{
			if ($this->checkSmsMessageExists())
			SableSystem::ajaxResponse(false, "", "This Sms Message has already been added.", 
				5, "edtSmsNumber", false, "edtSmsNumber");
			$this->loadSmsMessage();
		}
		else
		{
			$this->updateSmsMessage();
		}
	}

	function loadSmsMessage()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO sms_message (
						sms_number, sms_message, sms_sent, sms_reply, sms_key, client_id, sms_timestamp, sms_ip, sms_user
						) VALUES (
						'".$this->edtSmsNumber."',
						'".addslashes($this->edtSmsMessage)."',
						".$this->cbSmsSent.",
						'".addslashes($this->edtSmsReply)."',
						'".addslashes($this->edtSmsKey)."',
						'".$this->selClientId."',
						'".date('Y-m-d H:i:s')."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Sms Message details were successfully added:
		if ($res)
		{
			$this->setSmsMessageId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Sms Message added: '.$this->edtSmsNumber, $this->hddnSmsId);
			SableSystem::ajaxResponse(true, '/sms_message/', 'The Sms Message was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Sms Message failed. Please try again or contact support.", 
				5, "edtSmsNumber", false, "edtSmsNumber");
	}

	function updateSmsMessage()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnSmsId);
		$updateQuery = "UPDATE sms_message set 
						sms_number = '".$this->edtSmsNumber."', 
						sms_message = '".addslashes($this->edtSmsMessage)."', 
						sms_sent = ".$this->cbSmsSent.", 
						sms_reply = '".addslashes($this->edtSmsReply)."', 
						sms_key = '".addslashes($this->edtSmsKey)."', 
						client_id = '".$this->selClientId."', 
						sms_timestamp = '".date('Y-m-d H:i:s')."', 
						sms_ip = '".$_SESSION['user']['user_code']."', 
						sms_user = '".SableGeneralFunctions::getRealIpAddr()."' 
						where sms_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Sms Message details modified: '.$this->edtSmsNumber, $code);
			SableSystem::ajaxResponse(true, '/sms_message/', 'The Sms Message was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Sms Message failed. Please try again or contact support.", 
				5, "edtSmsNumber", false, "edtSmsNumber");
	}

	private function getSmsMessageId()
	{
		global $sableDB;
		$sql = "select sms_id from sms_message where sms_id = '".$this->hddnSmsId."'";
		return $sableDB->getValue($sql);
	}

	private function checkSmsMessageExists()
	{
		$cid = $this->getSmsMessageId();
		if (!empty($cid)) return true; else return false;
	}

	private function setSmsMessageId()
	{
		$this->hddnSmsId = $this->getSmsMessageId();
	}

	function viewSmsMessage()
	{
		$url = '/sms_message/view.php?'.SableSystem::sableDecode($this->hddnSmsId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function viewSMSReply()
	{
		$url = '/sms_message/view_reply.php?'.SableSystem::sableDecode($this->hddnSmsId);
		SableSystem::ajaxPageRedirect($url);
	}		

	function editSmsMessage()
	{
		$url = '/sms_message/edit.php?'.SableSystem::sableDecode($this->hddnSmsId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteSmsMessage()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnSmsId);
		$cnt = $sableDB->getValue("select count(*) from sms_message where sms_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Sms Message selected.", 
				5, "edtSmsNumber", false, "edtSmsNumber");
		else
		{
			$sql = "delete from sms_message where sms_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Sms Message record deleted.', $code);
				SableSystem::ajaxPageRedirect('/sms_message/','The Sms Message record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Sms Message failed to delete. Please try again later or contact support.", 
				5, "edtSmsNumber", false, "edtSmsNumber");
		}
	}

} //classSmsMessage
?>
