<?php
include("postget.php");
include_once("common.php");
include("ticketsfunc.php");
$pagetitle = lang("_TICKETS");

checklevel(1,$_REQUEST['clientid']);


///////////////////////////////////////// verifica que el modulo est� activo para los clientes
if (substr($config[64][1],2,1)=='0' AND substr($config[65][1],2,1)=='0'){
	no_access();
}



$thisclient=1;

//////////////////////////////////////////////////////////////// si se envi� el formulario, cargar los errores
if ($_REQUEST[replyclick]==""._SEND."") {

	////////////////////////////////////// CAPTCHA
	if ($config[78][1]==1) {
		session_start();
		include_once 'securimage/securimage.php';
		$securimage = new Securimage();
	}
	////////////////////////////////////// FIN CAPTCHA

	$estiloerror = "class='formfielderror'";
	$errors = '';
	if ($_POST['title']=='') {$errors.= "<br>".ucfirst(_SUBJECT).": "._NOT_SPECIFIED.""; $estilotitle=$estiloerror;}
	if ($_POST['description']=='') {$errors.= "<br>".ucfirst(_DESCRIPTION).": "._NOT_SPECIFIED.""; $estilodescription=$estiloerror;}
	if ($_FILES['attachment']['name']<>'') {
		$extension = explode('.',$_FILES['attachment']['name']) ;
		$extension = array_pop($extension) ;
		if (substr_count($config[9][1],$extension)==0) {$errors.= "<br>".ucfirst(_INVALID_FILE_TYPE_IN_ATTACHMENT)."";}
	}
	/////// chequear spam
	if (antispam('Subject',$_POST['title'])) {$errors.= antispam('Subject',$_POST['title']);}
	if (antispam('Body',$_POST['description'])) {$errors.= antispam('Body',$_POST['description']);}

	////////////////////////////////////// CAPTCHA
	if ($config[78][1]==1) {
		if ($securimage->check($_POST['captcha_code']) == false) {$errors.= "<br>".ucfirst(_SECURITY_CODE).": "._WRONG.""; $estilocaptcha=$estiloerror;}
	}
	////////////////////////////////////// FIN CAPTCHA

}




/////////////////////////////////////////////////////////// agregar canned response a la respuesta
if ($_POST['canned']<>'') {
	$resprow = query("SELECT id,name,description FROM hd_tickets_canned WHERE id=".$_POST['canned'],1);
	$_POST['description'] = $_POST['description']." ".$resprow['description'];
}

/////////////////////////////////////////////////////////// agregar faq a la respuesta
if ($_POST['faq']<>'') {
	$resprow = query("SELECT id,question,answer,category,subcategory FROM hd_faq WHERE id=".$_POST['faq'],1);
	$respuesta = "
	<b>".strtoupper(lang('_QUESTION',$_SESSION[preflang])).":</b> ".$resprow[question]."
	<br><b>".strtoupper(lang('_ANSWER',$_SESSION[preflang])).":</b><br>".$resprow[answer]."
	<br>";

	if ($resprow[subcategory]==0) {
		$linkref = $serverurl."/faq.php?c=".$resprow[category]."&i=".$resprow[id];
	}else{
		$linkref = $serverurl."/faq.php?s=".$resprow[subcategory]."&i=".$resprow[id];
	}

	$respuesta .= "<b>".lang('_REFERENCE',$_SESSION[preflang]).":</b> <a href='".$linkref."' target='_blank'>".$linkref."</a>";

	$_POST['description'] = $_POST['description']." ".$respuesta;
}


/////////////////////////////////////////////////////////// agregar manual a la respuesta
if ($_POST['manual']<>'') {
	$resprow = query("SELECT id,question,answer,category,subcategory FROM hd_manuals_pages WHERE id=".$_POST['manual'],1);
	$respuesta = "
	<b>".strtoupper(lang('_QUESTION',$_SESSION[preflang])).":</b> ".$resprow[question]."
	<br><b>".strtoupper(lang('_ANSWER',$_SESSION[preflang])).":</b><br>".$resprow[answer]."
	<br>";

	if ($resprow[subcategory]==0) {
		$linkref = $serverurl."/manuals.php?c=".$resprow[category]."&i=".$resprow[id];
	}else{
		$linkref = $serverurl."/manuals.php?s=".$resprow[subcategory]."&i=".$resprow[id];
	}

	$respuesta .= "<b>".lang('_REFERENCE',$_SESSION[preflang]).":</b> <a href='".$linkref."' target='_blank'>".$linkref."</a>";

	$respuesta = replacellaves($respuesta);
	
	$_POST['description'] = $_POST['description']." ".$respuesta;
}






//////////////////////////////////////////////////////////////// si no hay errores y se presion� SEND
if($errors=='' AND $_REQUEST[replyclick]==""._SEND."") {


	//////////////////////////////////////////////////////// subir el adjunto
	if ($_FILES['attachment']['name']) 	{
		$newfilename = time() . '_' . $_FILES['attachment']['name'];
		$attachdir = 'attachments/';
		if(!@copy ($_FILES['attachment']['tmp_name'], $attachdir . $newfilename)) {
			$errors= error_get_last();
			echo "COPY ERROR: ".$errors['type'];
			echo "<br />\n".$errors['message']; exit;
		}
		$attachment = $newfilename;
	}


	openticket(
	$_SESSION['hd_clientid'],
	$_POST['title'],
	$_POST['description'],
	$_POST['status'],
	$_POST['priority'],
	'',
	$_POST['department'],
	$attachment,
	$_SESSION['hd_clientid'],
	'',
	''
	);


	go2("tickets.php?clientid=".$_SESSION['hd_clientid']);



}else{

	include("head_template.php");
	
	
?>
<script language="javascript" type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
	mode : "textareas",
	theme : "simple"
});
</script>
<?php

//	if($_SESSION['hd_accesslvl']==1) {
//		include("menu_client.php");
//	}

	echo '
<div class="container">
	<div class="row">


<table width="98%" class="table table-bordered">
		<tr>
		<td align="center" width="100%">
';

	if ($fromcontact) {
		$imagenAmostrar='contact_.png';
		echo "".ucfirst(_menu_CONTACT)."";
	}else{
		$imagenAmostrar='ticket_.png';
		echo "".ucfirst(_OPEN_NEW_TICKET)."";
	}


echo '
		</td>
		</tr>
';	
echo '
			<tr valign="top">
				<td>

';

	print errors($errors); $errors = '';

echo '<table width="98%" class="table table-bordered">';

	print("<form method='POST' enctype='multipart/form-data'> ");

	print("
		<tr>
				<td align='right'>".ucfirst(_DEPARTMENT)."</td>
				<td>
				<select name='department'>");
	$query="SELECT depid,name FROM hd_tickets_dep";
	$Deptos=mysql_query($query,$db_link);
	while($depart=mysql_fetch_array($Deptos)) {
		print ("<option value='".$depart[depid]."' ".sele($_POST['department'],$depart[depid]).">".$depart[name]."</option>");
	};
	print("
		</select>
				</td>
		</tr>");


	print("
		<tr>
				<td align='right'>".ucfirst(_PRIORITY)."</td>
				<td>
				<select name='priority'>
					<option value='low' ".sele($_POST['priority'],'low').">".ucfirst(_LOW)."</option>
					<option value='medium' ".sele($_POST['priority'],'medium').">".ucfirst(_MEDIUM)."</option>
					<option value='high' ".sele($_POST['priority'],'high').">".ucfirst(_HIGH)."</option>
				</select>
				</td>
		</tr>");

	if ($_SESSION['hd_accesslvl']==1) {
		print("<input type='hidden' name='status' value='1'>");
		//		print("<select name='status'><option value='1' >".ucfirst(_NEW)."</option></select> ");
	}else{
		print("
		<tr>
				<td align='right'>".ucfirst(_STATUS)."</td>
				<td>");
		print("<select name='status'>
							<option value='1' style='color:white ;background-color:red ;font-size:12px;'>".ucfirst(_NEW)."</option>
							<option value='3' style='color:white ;background-color:green ;font-size:12px;' selected>".ucfirst(_ANSWERED)."</option>
							<option value='2' style='color:black ;background-color:yellow ;font-size:12px;'>".ucfirst(_PENDING)."</option>
							</select> ");
	}
	print("
				</td>
		</tr>");

	print("
		<tr>
				<td align='right'>".ucfirst(_SUBJECT)."</td>
				<td>
				<input $estilotitle name='title' size='78' value='".$_POST['title']."'>
				</td>
		</tr>");

		if ($_SESSION['hd_accesslvl']>1) {
			$textareacolumns = 140;
		}else{
			$textareacolumns = 80;
		}

	print("
		<tr>
				<td align='right'>".ucfirst(_DESCRIPTION)."</td>
				<td>
						<TEXTAREA class='input-xxlarge' $estilodescription cols='".$textareacolumns."' rows=8 name=description>".$_POST['description']."</TEXTAREA>
				</td>
		</tr>");



//////////////////////////////////// respuestas insertadas
if ($_SESSION['hd_accesslvl']>1) {

			print("<tr><td align='right'>".ucfirst(_CANNED_RESPONSES).": </td><td>
		<select name='canned' onChange='this.form.submit()'>");
			$query="SELECT id,name,langs FROM hd_tickets_canned WHERE langs='".$_SESSION['preflang']."' ORDER BY orderby";
			$canned=mysql_query($query,$db_link);
			print ("<option value=''>"._SELECT_AN_OPTION."</option>");
			while($cannedrow=mysql_fetch_array($canned)) {
				print ("<option value='".$cannedrow[id]."'>[".$cannedrow[langs]."] ".substr($cannedrow[name],0,40)."</option>");
			};
			print("
		</select>
		</td></tr>
		");

			//////////////////////// insertar faq
			print("<tr><td align='right'>".ucfirst(_INSERT_FROM_FAQS).": </td><td>
		<select name='faq' onChange='this.form.submit()'>");
			$query="SELECT hd_faq.id AS id, hd_faq.question AS question, hd_faq.langs AS langs, hd_faq_category.name AS category, hd_faq_subcategory.name AS subcategory
			FROM hd_faq 
			LEFT JOIN hd_faq_category ON hd_faq.category = hd_faq_category.id 
			LEFT JOIN hd_faq_subcategory ON hd_faq.subcategory = hd_faq_subcategory.id 
			WHERE hd_faq.langs='".$_SESSION['preflang']."' 
			ORDER BY category,subcategory,question";
			$faq=mysql_query($query,$db_link);
			print ("<option value=''>"._SELECT_AN_OPTION."</option>");
			while($faqrow=mysql_fetch_array($faq)) {
				$grupo1 = "[".$faqrow[langs]."] ".strtoupper($faqrow[category])." > ".strtoupper($faqrow[subcategory])."";
				if (substr($grupo1,-2)=='> ') {$grupo1=substr($grupo1,0,-2);}
				if ($grupo1 <> $grupo2) {
					if (isset($grupo2)) {
						print ("</optgroup>");
					}
					print ("<optgroup label='".$grupo1."'>");
				}
				print ("<option value='".$faqrow[id]."'>".substr($faqrow[question],0,40)."</option>");
				$grupo2 = "";
					
				$grupo2 = "[".$faqrow[langs]."] ".strtoupper($faqrow[category])." > ".strtoupper($faqrow[subcategory])."";
				if (substr($grupo2,-2)=='> ') {$grupo2=substr($grupo2,0,-2);}
				
			};
			print ("</optgroup>");
			print("
		</select>
		</td></tr>
		");

			//////////////////////// insertar manual
			print("<tr><td align='right'>".ucfirst(_INSERT_FROM_MANUALS).": </td><td>
		<select name='manual' onChange='this.form.submit()'>");
			$query="SELECT hd_manuals_pages.id AS id, hd_manuals_pages.question AS question, hd_manuals_pages.langs AS langs, hd_manuals_manuals.name AS manual, hd_manuals_chapters.name AS chapter
			FROM hd_manuals_pages 
			LEFT JOIN hd_manuals_manuals ON hd_manuals_pages.category = hd_manuals_manuals.id 
			LEFT JOIN hd_manuals_chapters ON hd_manuals_pages.subcategory = hd_manuals_chapters.id 
			WHERE hd_manuals_pages.langs='".$_SESSION['preflang']."' 
			ORDER BY manual,chapter,question";
			$manual=mysql_query($query,$db_link);
			print ("<option value=''>"._SELECT_AN_OPTION."</option>");
			while($manualrow=mysql_fetch_array($manual)) {
				$grupo1 = "[".$manualrow[langs]."] ".strtoupper($manualrow[manual])." > ".strtoupper($manualrow[chapter])."";
				if (substr($grupo1,-2)=='> ') {$grupo1=substr($grupo1,0,-2);}
				if ($grupo1 <> $grupo2) {
					if (isset($grupo2)) {
						print ("</optgroup>");
					}
					print ("<optgroup label='".$grupo1."'>");
				}
				print ("<option value='".$manualrow[id]."'>".substr($manualrow[question],0,40)."</option>");
				$grupo2 = "";
					
				$grupo2 = "[".$manualrow[langs]."] ".strtoupper($manualrow[manual])." > ".strtoupper($manualrow[chapter])."";
				if (substr($grupo2,-2)=='> ') {$grupo2=substr($grupo2,0,-2);}
			};
			print ("</optgroup>");
			print("
		</select>
		</td></tr>
		");

}
//////////////////////////////////// fin respuestas insertadas

	print("
		<tr>
				<td align='right'>".ucfirst(_ATTACHMENT)."</td>
				<td>
				<input type='file' name='attachment'>
				<br>".ucfirst(_ALLOWED_FILE_TYPES).": ".$config[9][1]."<br>
				</td>
		</tr>");


	////////////////////////////////////// CAPTCHA
	if ($config[78][1]==1) {
		print("
		<tr>
				<td align='right'>".ucfirst(_TYPE_THE_FOLLOWING_CODE)."<br>
				<img id='captcha' src='securimage/securimage_show.php' alt='CAPTCHA Image' />
				</td>
				<td>
				<input $estilocaptcha type='text' name='captcha_code' size='20' maxlength='20' />
				<a href='#' onclick=\"document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false\">".ucfirst(_RELOAD_IMAGE)."</a>
				</td>
		</tr>");
	}
	////////////////////////////////////// FIN DEL CAPTCHA

	print("</table>
	<input type=submit name=replyclick value='"._SEND."'>
	</form>");

echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>