id = $_REQUEST['record'];
}
if(isset($_REQUEST['mode']))
{
	$focus->mode = $_REQUEST['mode'];
}
foreach($focus->column_fields as $fieldname => $val)
{
	if(isset($_REQUEST[$fieldname]))
	{
		$value = $_REQUEST[$fieldname];
		$focus->column_fields[$fieldname] = $value;
	}
		
}
//$focus->saveentity("HelpDesk");
$focus->save("HelpDesk");
$return_id = $focus->id;
if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "") $return_module = $_REQUEST['return_module'];
else $return_module = "HelpDesk";
if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "") $return_action = $_REQUEST['return_action'];
else $return_action = "DetailView";
if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "") $return_id = $_REQUEST['return_id'];
if($_REQUEST['mode'] == 'edit')
	$reply = 'Re : ';
else
	$reply = '';
$_REQUEST['name'] = '[ Ticket ID : '.$focus->id.' ] '.$reply.$_REQUEST['ticket_title'];
$bodysubject = ' Subject : '.$focus->id.' : '.$_REQUEST['ticket_title'].'
';
if($focus->column_fields['parent_id'] != '')
{
	$query = "select * from crmentity where crmid=".$focus->column_fields['parent_id'];
	$parent_module = $adb->query_result($adb->query($query),0,'setype');
	if($parent_module == 'Contacts')
	{
		$sql = "select * from contactdetails where contactid=".$focus->column_fields['parent_id'];
		$result = $adb->query($sql);
		$emailoptout = $adb->query_result($result,0,'emailoptout');
		$contactname = $adb->query_result($result,0,'firstname').' '.$adb->query_result($result,0,'lastname');
		$contact_mailid = $adb->query_result($result,0,'email');
	}
}
if($contact_mailid != '')
{
	$sql = "select * from PortalInfo where user_name='".$contact_mailid."'";
	$isactive = $adb->query_result($adb->query($sql),0,'isactive');
}
if($isactive == 1)
{
	$bodydetails = "Dear ".$contactname.",
";
	$bodydetails .= 'There is a reply to '.$_REQUEST['ticket_title'].' in the "Customer Portal" at VTiger.';
	$bodydetails .= "You can use the following link to view the replies made:
";
	//Provide your customer portal url
	$PORTAL_URL = "";//e.g : vtigercrm:90/customerportal
	$bodydetails .= "Ticket Details";
	$bodydetails .= "
Thanks,
 Vtiger Support Team ";
	$_REQUEST['description'] = $bodysubject.$bodydetails;
}
else
{
	$desc = 'Ticket ID : '.$focus->id.'
 Ticket Title : '.$reply.$_REQUEST['ticket_title'];
	$desc .= "
Dear ".$contactname.",
The Ticket is replied and the details are : 
";
	$desc .= "
 Status : ".$focus->column_fields['ticketstatus'];
	$desc .= "
 Category : ".$focus->column_fields['ticketcategories'];
	$desc .= "
 Severity : ".$focus->column_fields['ticketseverities'];
	$desc .= "
 Priority : ".$focus->column_fields['ticketpriorities'];
	$desc .= '
Description : 
'.$focus->column_fields['description'];
	$desc .= '
Solution : 
'.$focus->column_fields['solution'];
	$desc .= getTicketComments($focus->id);
	$_REQUEST['description'] = $desc;
}
//$_REQUEST['parent_id'] = $_REQUEST['contact_id'];
$_REQUEST['return_id'] = $return_id;
if($_REQUEST['product_id'] != '' && $focus->id != '' && $_REQUEST['mode'] != 'edit')
{
        $sql = 'insert into seticketsrel values('.$_REQUEST['product_id'].' , '.$focus->id.')';
        $adb->query($sql);
        $return_id = $_REQUEST['product_id'];
}
if($emailoptout == 0)
{
	require_once('modules/Emails/send_mail.php');
}
else
{
	header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
}
function getTicketComments($ticketid)
{
	global $adb;
	$commentlist = '';
	$sql = "select * from ticketcomments where ticketid=".$ticketid;
	$result = $adb->query($sql);
	for($i=0;$i<$adb->num_rows($result);$i++)
	{
		$comment = $adb->query_result($result,$i,'comments');
		if($comment != '')
		{
			$commentlist .= '
'.$comment;
		}
	}
	if($commentlist != '')
		$commentlist = '
 The comments are : '.$commentlist;
	return $commentlist;
}
?>