// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

if (check_login () != 0) {
	audit_db ("Noauth", $config["REMOTE_ADDR"], "No authenticated access","Trying to access ticket viewer");
	require ("general/noaccess.php");
	exit;
}

$id_incident = (int) get_parameter ('id');
$incidents = incidents_get_incident_childs ($id_incident, false);


if (count ($incidents) == 0) {
	echo ui_print_error_message (__('There\'s no tickets associated to this ticket'), '', true, 'h3', true);
}
else {
	
	$table = new StdClass();
	$table->class = 'listing';
	$table->width = '100%';
	$table->head = array ();
	$table->head[0] = __('ID');
	$table->head[1] = __('Name');
	$table->head[2] = __('Group');
	$table->head[3] = __('Status');
     $idParentValue = sprintf('%d', $id_parent);
 }
 $timestamp = print_mysql_timestamp();
 $values = array('email_copy' => $email_copy, 'actualizacion' => $timestamp, 'id_creator' => $id_creator, 'titulo' => $titulo, 'estado' => $estado, 'id_grupo' => $grupo, 'id_usuario' => $user, 'closed_by' => $closed_by, 'notify_email' => $email_notify, 'prioridad' => $priority, 'descripcion' => $description, 'epilog' => $epilog, 'id_task' => $id_task, 'resolution' => $resolution, 'id_incident_type' => $id_incident_type, 'id_parent' => $idParentValue, 'affected_sla_id' => 0, 'sla_disabled' => $sla_disabled, 'blocked' => $blocked, 'old_status' => $old_status, 'old_resolution' => $old_resolution, 'old_status2' => $old_status2, 'old_resolution2' => $old_resolution2);
 // When close incident set close date to current date
 if ($estado == 7) {
     $values['cierre'] = $timestamp;
 }
 $result = process_sql_update('tincidencia', $values, array('id_incidencia' => $id));
 //Add traces and statistic information
 incidents_set_tracking($id, 'update', $priority, $estado, $resolution, $user, $grupo);
 audit_db($id_author_inc, $config["REMOTE_ADDR"], "Ticket updated", "User " . $config['id_user'] . " ticket updated #" . $id);
 if ($estado == 7) {
     $values_childs['estado'] = $estado;
     $values_childs['closed_by'] = $closed_by;
     $childs = incidents_get_incident_childs($id);
     if (!empty($childs)) {
         foreach ($childs as $id_child => $name) {
             $result_child = process_sql_update('tincidencia', $values_childs, array('id_incidencia' => $id_child));
             audit_db($id_author_inc, $config["REMOTE_ADDR"], "Ticket updated", "User " . $config['id_user'] . " ticket updated #" . $id_child);
         }
     }
 }
 $old_incident_inventories = array_keys(get_inventories_in_incident($id));
 $incident_inventories = get_parameter("inventories");
 /* Update inventory objects in incident */
 update_incident_inventories($id, get_parameter('inventories', $incident_inventories));
 if ($result === false) {
     $result_msg = "<h3 class='error'>" . __('There was a problem updating ticket') . "</h3>";
 } else {
     $result_msg = "<h3 class='suc'>" . __('Ticket successfully updated') . "</h3>";