<?php
	require_once '../../autoload.php';
	
	RightsCheck ("VIEW_INCIDENTREPORTING");
	
	$id		 		= -1;
	$title			= "";
	$dep_id			= -1;
	$reporter		= -1;
	$severity		= -1;
	$status_id		= -1;
	$comment		= "";
	$reportdate		= "";
	$closeddate		= "";
	$statuscomment	= "";
			
	if ( isset ( $_REQUEST['id'] ) 				)	$id			 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['title'] )			)	$title			 	= $_REQUEST['title'];
	if ( isset ( $_REQUEST['dep_id'] )			)	$dep_id			 	= $_REQUEST['dep_id'];
	if ( isset ( $_REQUEST['reporter'] )		)	$reporter		 	= $_REQUEST['reporter'];
	if ( isset ( $_REQUEST['severity'] )		)	$severity		 	= $_REQUEST['severity'];
	if ( isset ( $_REQUEST['status_id'] )		)	$status_id		 	= $_REQUEST['status_id'];
	if ( isset ( $_REQUEST['comment'] )			)	$comment		 	= $_REQUEST['comment'];
	if ( isset ( $_REQUEST['reportdate'] )		)	$reportdate		 	= $_REQUEST['reportdate'];
	if ( isset ( $_REQUEST['closeddate'] )		)	$closeddate		 	= $_REQUEST['closeddate'];
	if ( isset ( $_REQUEST['statuscomment'] )	)	$statuscomment	 	= $_REQUEST['statuscomment'];

	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
		{
			$sql = 'insert into report(title,dep_id,reporter,reportdate,status_id,severity,comment) values (\''.$title.'\','.$dep_id.','.$reporter.',to_timestamp(\''.$reportdate.'\',\'YYYY-MM-DD\'),'.STATUS_OPEN_ID.','.$severity.',\''.$comment.'\') returning id;';
		}
		else
		{
			$sql = 'update report';
  			$sql .= ' set severity = '.$severity;
  			$sql .= ' ,title = \''.$title.'\'';
  			$sql .= ' ,comment = \''.$comment.'\'';
  			$sql .= ' ,dep_id = '.$dep_id;
  			if($reportdate != '')
    			$sql .= ' ,reportdate = to_timestamp(\''.$reportdate.'\',\'YYYY-MM-DD\')';
  			else
    			$sql .= ' ,reportdate = null';
    		if($closeddate != '')
    			$sql .= ' ,closeddate = to_timestamp(\''.$closeddate.'\',\'YYYY-MM-DD\')';
  			else
    			$sql .= ' ,closeddate = null';
    		$sql .= ' ,statuscomment = \''.$statuscomment.'\'';
    		$sql .= ' ,status_id = '.$status_id;
  			$sql .= ' where id =  '.$id.' returning id;';
  		}

		$result = pg_query ( $conn, $sql );
		
		if ( $result )
		{
			if ( $row = pg_fetch_row($result) )
				$response = $row [0];
		}
	}
	
	header ('Content-type: text/xml' );
	
	echo $response;
?>