<?php
	require_once '../../autoload.php';
	
	RightsCheck ("VIEW_LEGALDOCUMENT");
	
	$id		 	= -1;
	$name 		= "";
	$type 		= "";
	$comment	= "";
	
	if ( isset ( $_REQUEST['id'] ) 		)	$id		 	= $_REQUEST['id'];
	if ( isset ( $_REQUEST['name'] ) 	)	$name 		= $_REQUEST['name'];
	if ( isset ( $_REQUEST['type'] ) 	)	$type 		= $_REQUEST['type'];
	if ( isset ( $_REQUEST['comment'] )	)	$comment	= $_REQUEST['comment'];
		
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = "";
		
		if ( $id == -1 )
			$sql = "insert into legaldocument (name) values ('New') returning id";
		else
			$sql = "update legaldocument set name='".$name."', type='".$type."', comment='".$comment."' 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;
?>