<?php
	require_once '../../autoload.php';
	
	RightsCheck ("VIEW_SCHEDULEDTASK");
	
	$id		 		= -1;
	$description 	= "";
	$deadline		= "null";
	$enddate		= "null";
	$personincharge	= -1;
	$status_id		= -1;
	$dep_id			= -1;
		
	if ( isset ( $_REQUEST['id'] ) 				)	$id		 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['begdate'] ) 		)	$begdate 		= $_REQUEST['begdate'];
	if ( isset ( $_REQUEST['deadline'] ) 		)	if ( empty ($_REQUEST['deadline']) == false ) $deadline 	= "'" . $_REQUEST['deadline'] . "'";
	if ( isset ( $_REQUEST['enddate'] ) 		)	if ( empty ($_REQUEST['enddate']) == false )  $enddate 		= "'" . $_REQUEST['enddate'] . "'";
	if ( isset ( $_REQUEST['description'] ) 	)	$description 	= $_REQUEST['description'];
	if ( isset ( $_REQUEST['origine'] ) 		)	$origine 		= $_REQUEST['origine'];
	if ( isset ( $_REQUEST['personincharge'] ) 	)	$personincharge = $_REQUEST['personincharge'];
	if ( isset ( $_REQUEST['status_id'] ) 		)	$status_id 		= $_REQUEST['status_id'];
	if ( isset ( $_REQUEST['dep_id'] ) 			)	$dep_id 		= $_REQUEST['dep_id'];
			
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = "";
		
		if ( $id == -1 )
			$sql = "insert into action (description,begdate) values ('New','".$begdate."') returning id";
		else
			$sql = "update action set description='".$description."', begdate='".$begdate."', deadline=".$deadline.", enddate=".$enddate.", origine='".$origine."', personincharge=".$personincharge.", status_id=".$status_id.", dep_id=".$dep_id." 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;
?>