<?
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	supportRequest
* Version :	1.0
* Date    :	20 October 2009
* Author  :	Darwish Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class supportRequest
{
	var $TrackingID;
	var $Summary;
	var $Description;
	var $ProductID;
	var $TypeID;
	var $FileName; 					 // Used for attachment
	var $StatusNotification; // Status Change Email Notification
	var $SupportComments;
	var $StatusID;
	var $CreationDate;
	var $CustomerID;

	/*
	 * Class constructor
	 */
  function supportRequest($TrackingID = -1)
  {
  	if ((!empty($TrackingID)) && ($TrackingID != -1))
  	{
  		$this->TrackingID = $TrackingID;
			$this->getInfo();
		}
	}

	/*
	 * Retrieves supportRequest information
	 */
	function getInfo()
	{
		$query = "
							Select * From support_request
					 	  Where TrackingID = '$this->TrackingID'
					 	 ";

		$result = mysql_query($query);
		$row = mysql_fetch_object($result);

    // Set class data members
    $this->Summary 			= stripslashes($row->Summary);
    $this->Description 	= stripslashes($row->Description);
    $this->FileName 		= stripslashes($row->FileName);
    $this->ProductID 		= $row->ProductID;
    $this->TypeID 			= $row->TypeID;
    $this->StatusID 		= $row->StatusID;
    $this->CreationDate = $row->CreationDate;
    $this->CustomerID 	= $row->CustomerID;
    $this->SupportComments 		= stripslashes($row->SupportComments);
    $this->StatusNotification = $row->StatusNotification;
  }

	/*
   * Retrieves the count of support requests available according to the given search criteria
   */
	function getTotal($Criteria = '')
	{
		global $language;

		$query  = "Select COUNT(*) From support_request Where CustomerID = $this->CustomerID And Language = '$language' ";

		// Tracking ID ------------------------------------------------------------------
		if($Criteria['TrackingID'] != '') {
			$query .= "And (TrackingID Like '%" . $Criteria['TrackingID'] . "%') ";
		}
		// Keyword ---------------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(Summary Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(FileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(SupportComments Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Related Product ------------------------------------------------------------
		if($Criteria['ProductID'] != '-1') {
			$query .= "And (ProductID = " . $Criteria['ProductID'] . ") ";
		}
		// Request Type ------------------------------------------------------------
		if($Criteria['TypeID'] != '-1') {
			$query .= "And (TypeID = " . $Criteria['TypeID'] . ") ";
		}
		// Status  --------------------------------------------------------------------
		if($Criteria['StatusID'] != '-1') {
			$query .= "And (StatusID = '" . $Criteria['StatusID'] . "') ";
		}

	  // echo $query;
   	$result = mysql_result(mysql_query($query), 0, 0);

    return $result;
	}

	/*
	 * Retrieve a list of support requests regarding a certain search criteria
	 */
	function getList($Criteria = '', $SortField = 'sr.CreationDate', $SortType = 'DESC', $Start = -1, $Num = -1)
	{
		global $language;

		$query  =
							"
								Select sr.TrackingID, sr.Summary, sr.Description, sr.FileName, sr.SupportComments,
								sr.StatusNotification, sr.StatusID, sr.ProductID, p.Title ProductName,
								srt.Name Type, srs.Name Status, sr.CreationDate
								From support_request sr Inner Join support_request_type srt On sr.TypeID = srt.TypeID
								Inner Join support_request_status srs On sr.StatusID = srs.StatusID
								Inner Join product p On sr.ProductID = p.ProductID
								Where sr.CustomerID = $this->CustomerID And sr.Language = '$language' ";

		// Tracking ID ------------------------------------------------------------------
		if($Criteria['TrackingID'] != '') {
			$query .= "And (sr.TrackingID Like '%" . $Criteria['TrackingID'] . "%') ";
		}
		// Keyword ---------------------------------------------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(sr.Summary Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(sr.Description Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(sr.FileName Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(sr.SupportComments Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Related Product ------------------------------------------------------------
		if($Criteria['ProductID'] != '-1') {
			$query .= "And (sr.ProductID = " . $Criteria['ProductID'] . ") ";
		}
		// Request Type ------------------------------------------------------------
		if($Criteria['TypeID'] != '-1') {
			$query .= "And (sr.TypeID = " . $Criteria['TypeID'] . ") ";
		}
		// Status  --------------------------------------------------------------------
		if($Criteria['StatusID'] != '-1') {
			$query .= "And (sr.StatusID = '" . $Criteria['StatusID'] . "') ";
		}

		// Order By
		$query .= "Order By $SortField $SortType ";

		// Limit Results
		if (($Start!=-1) && ($Num!=-1)) {
     $query .= "Limit $Start,$Num";
    }

		// echo($query);
		$result = mysql_query($query);
		return $result;
	}

  /*
   * Generates a random Tracking ID equals to the first n bytes of md5hash of some random data based
   * on the system date time.
   */
	function generateTrackingID($chars)
	{
		$n = 0;
		$allowchars = '';
		// $allowchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
		$allowchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
		while(++$n < ($chars+1)) {
			$password .= $allowchars[realRand(0, strlen($allowchars)-1)];
		}
		return strtoupper(substr(md5($password), 0, $chars));
	}

	// Add new support request
	function add($obj)
	{
		global $language;

		// Add slashes to object variables
	 	addslashes_object_vars(&$obj);

	 	// Gets current date/time
	 	$CreationDate = date("Y-m-d H:i:s");

	 	// Get the filename of the attachment
		$path_parts = pathinfo("$obj->FileName");
		$FileName  = $path_parts["basename"];

		$query =
						"
							Insert Into support_request(TrackingID, Summary, Description, FileName, ProductID, TypeID,
							CreationDate, StatusNotification, CustomerID, Language)
							Values('$obj->TrackingID','$obj->Summary','$obj->Description','$FileName','$obj->ProductID','$obj->TypeID',
							'$CreationDate','$obj->StatusNotification', $obj->CustomerID, '$language')
						";

		// exit($query);
		mysql_query($query) or die(mysql_error());
	}

	/*
	 * Returns the type name of a certain TypeID
	 */
	function getTypeName()
	{
		$query  = "Select Name From support_request_type Where TypeID = $this->TypeID";
		$result = mysql_query($query);
		if (mysql_num_rows($result)>0) {
			$row = mysql_fetch_object($result);
			return $row->Name;
		}
		return '';
	}

	/*
	 * Returns the type name of a certain TypeID
	 */
	function getRelatedProduct()
	{
		$query  = "Select Title From product Where ProductID = $this->ProductID";
		$result = mysql_query($query);
		if (mysql_num_rows($result)>0) {
			$row = mysql_fetch_object($result);
			return $row->Title;
		}
		return '';
	}

	/*
	 * Retrieves a lis of support types
	 */
	function getTypes()
	{
		global $language;

		$query =
						"
							Select TypeID, Name, Description
							FROM support_request_type
							Where Enabled = 1 And Deleted = 0 And Language = '$language'
							Order By Name
						";
	  return mysql_query($query);
	}

	/*
	 * Retrieves a lis of support statuses
	 */
	function getStatusList()
	{
		global $language;

		$query =
						"
							Select StatusID, Name, Description
							FROM support_request_status
							Where Enabled = 1 And Deleted = 0 And Language = '$language'
							Order By StatusID
						";
	  return mysql_query($query);
	}

	/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*                         Send Email Functions
	*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
	/*
	 * Sends request to support department
	 */
	function sendEmailToSupport()
	{
		global $config;
		global $customer;

		// Prepare headers for sending an html mail
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=windows-1256\r\n";

		if ($config['USE_CUSTOMER_EMAIL'])
		{
			$headers .= "From: " . $customer->Email . "\r\n";
		}
		else
		{
			$headers .= "From: " . $config['SUPPORT_REQUEST_EMAIL'] . "\r\n";
		}

		$to = $config['SUPPORT_EMAIL'];

		$TypeName = $this->getTypeName();
		$subject = "FinFisher Support Request [$TypeName] - TrackingID: " . $this->TrackingID;

		$message = $this->generateSupportRequestMessage();

		// Send Message
		if(eregi("win",strtolower(PHP_OS)))
		{
			include_once "mail.class.php";
			$msg = new email();
	 		$msg->send_mail(0, $to, $subject, $message, $headers);
			// exit($message);
		}
	  elseif(eregi("linux",strtolower(PHP_OS)))
	  {
			mail($to, $subject, $message, $headers);
		}
	}

	/*
	 * Generates the contents of the email to be sent to the support department
	 */
	function generateSupportRequestMessage()
	{
		global $config;
		global $customer;

		$TypeName = $this->getTypeName();
		$RelatedProduct = $this->getRelatedProduct();

		$msg =
					'
						<html>
							<head>
								<link href="'.$config['ROOT_URL'].'/Styles/style.css.php?Lang=en" rel="stylesheet" type="text/css">
							</head>
							<body class="en">
								<table border="0" cellpadding="0" cellspacing="0" width="100%">
									<tr>
										<td class="Success">
											Dear Support Team,<br><br>

											You have received a support request <b>('.$TypeName.')</b> from <b>'.$customer->Username.'</b>.<br><br>

											<b>TrackingID:</b> '.$this->TrackingID.'<br>
											<b>Related Product:</b> '.$RelatedProduct.'<br>
											<b>Request Type:</b> '.$TypeName.'<br>
											<b>Summary:</b> '.$this->Summary.'<br><br>
											<b>Description:</b><pre>'.$this->Description.'</pre><br>';
			if (!empty($this->FileName))
			{
				$msg .= '<b>Attachment:</b> <a href='.$config['ROOT_URL'].'Support/Attachments/'.$this->FileName.' target="_blank" style="color:#32606D;">'.$this->FileName.'</a><br>';
			}
			$msg .=				'</td>
									</tr>
								</table>
							</body>
						</html>
					';
		return stripslashes(trim($msg));
	}

	/*
	 * Sends confirmation email to customer
	 */
	function sendEmailToCustomer()
	{
		global $config;
		global $customer;

		// Prepare headers for sending an html mail
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=windows-1256\r\n";
		$headers .= "From: " . $config['EMAIL_FROM'] . "\r\n";

		$to = $customer->Email;

		$subject = "[Ticket#:" . $this->TrackingID . "] " . $this->Summary;

		$message = $this->generateConfirmationMessage();

		// Send Message
		if(eregi("win",strtolower(PHP_OS)))
		{
			include_once "mail.class.php";
			$msg = new email();
	 		$msg->send_mail(0, $to, $subject, $message, $headers);
			// exit($message);
		}
	  elseif(eregi("linux",strtolower(PHP_OS)))
	  {
			mail($to, $subject, $message, $headers);
		}
	}

	/*
	 * Generates the contents of the email to be sent to the customer
	 */
	function generateConfirmationMessage()
	{
		global $config;
		global $customer;

		$msg =
					'
						<html>
							<head>
								<link href="'.$config['ROOT_URL'].'/Styles/style.css.php?Lang=en" rel="stylesheet" type="text/css">
							</head>
							<body class="en">
								<table border="0" cellpadding="0" cellspacing="0" width="100%">
									<tr>
										<td class="Success">
											Dear Customer '.$customer->Username.',<br><br>

											Thank you for contacting <span style="color:#0C5EAB">FinFisher</span>.<br>
											This is to confirm that your request has been recieved. Your ticket number is: '.$this->TrackingID.'.<br><br>

											A support agent will be assigned to your ticket shortly.<br><br>

											Sincerely yours,<br>
											FinFisher Customer Support
										</td>
									</tr>
								</table>
							</body>
						</html>
					';
		return stripslashes(trim($msg));
	}

}
?>