<?php
	require_once '../../autoload.php';

	RightsCheck ("VIEW_DEVICE");
	
	$id		 		= -1;
	$brand			= "";
	$name			= "";
	$serialnumber	= "";
	$model			= "";
	$ris_name		= "";
	$pacs_name		= "";
	$dacs_name		= "";
	$modalitytype	= "";
		
	if ( isset ( $_REQUEST['id'] ) 				)	$id			 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['brand'] )			)	$brand			 	= $_REQUEST['brand'];
	if ( isset ( $_REQUEST['name'] )			)	$name			 	= $_REQUEST['name'];
	if ( isset ( $_REQUEST['serialnumber'] )	)	$serialnumber	 	= $_REQUEST['serialnumber'];
	if ( isset ( $_REQUEST['model'] )			)	$model			 	= $_REQUEST['model'];
	if ( isset ( $_REQUEST['ris_name'] )		)	$ris_name		 	= $_REQUEST['ris_name'];
	if ( isset ( $_REQUEST['pacs_name'] )		)	$pacs_name		 	= $_REQUEST['pacs_name'];
	if ( isset ( $_REQUEST['dacs_name'] )		)	$dacs_name		 	= $_REQUEST['dacs_name'];
	if ( isset ( $_REQUEST['modalitytype'] )	)	$modalitytype	 	= $_REQUEST['modalitytype'];
	
			
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
		{
			
			$sql = 'insert into device(name) values(\'New device\') returning id;';
		}
		else
		{
			$sql = 'update device';
  			$sql .= ' set brand = \''.pg_escape_string ( $conn, $brand ).'\'';
  			$sql .= ' ,name = \''.pg_escape_string ( $conn, $name ).'\'';
  			$sql .= ' ,serialnumber = \''.pg_escape_string ( $conn, $serialnumber ).'\'';
  			$sql .= ' ,model = \''.pg_escape_string ( $conn, $model ).'\'';
  			$sql .= ' ,ris_name = \''.pg_escape_string ( $conn, $ris_name ).'\'';
  			$sql .= ' ,pacs_name = \''.pg_escape_string ( $conn, $pacs_name ).'\'';
  			$sql .= ' ,dacs_name = \''.pg_escape_string ( $conn, $dacs_name ).'\'';
  			$sql .= ' ,modalitytype = \''.pg_escape_string ( $conn, $modalitytype ).'\'';
  			$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;
?>