<?php
	require_once '../../autoload.php';
	
	$id		 		= -1;
	$lastname		= "";
	$firstname		= "";
	$active			= false;
	$login			= "";
	$password		= "";
	$login_role_id	= -1;
	$ris_name		= "";
	$pacs_name		= "";
	$dacs_name		= "";
	$photo			= "";
		
	/*
	if ( isset ( $_REQUEST['id'] ) 				)	$id		 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['lastname'] )		)	$lastname	 	= $_REQUEST['lastname'];
	if ( isset ( $_REQUEST['firstname'] )		)	$firstname	 	= $_REQUEST['firstname'];
	if ( isset ( $_REQUEST['active'] )			)	$active	 		= $_REQUEST['active'];
	if ( isset ( $_REQUEST['login'] )			)	$login	 		= $_REQUEST['login'];
	if ( isset ( $_REQUEST['password'] )		)	$password 		= $_REQUEST['password'];
	if ( isset ( $_REQUEST['login_role_id'] )	)	$login_role_id	= $_REQUEST['login_role_id'];
	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['photo'] )			)	$photo			= $_REQUEST['photo'];
	*/

	if ( isset ( $_POST['id'] ) 			)	$id		 		= $_POST['id'];
	if ( isset ( $_POST['lastname'] )		)	$lastname	 	= $_POST['lastname'];
	if ( isset ( $_POST['firstname'] )		)	$firstname	 	= $_POST['firstname'];
	if ( isset ( $_POST['active'] )			)	$active	 		= $_POST['active'];
	if ( isset ( $_POST['login'] )			)	$login	 		= $_POST['login'];
	if ( isset ( $_POST['password'] )		)	$password 		= $_POST['password'];
	if ( isset ( $_POST['login_role_id'] )	)	$login_role_id	= $_POST['login_role_id'];
	if ( isset ( $_POST['ris_name'] )		)	$ris_name		= $_POST['ris_name'];
	if ( isset ( $_POST['pacs_name'] )		)	$pacs_name		= $_POST['pacs_name'];
	if ( isset ( $_POST['dacs_name'] )		)	$dacs_name		= $_POST['dacs_name'];
	
	$imageData = NULL;

	if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) 
	{
		error_log ( "image exist ...");
    	$fileTmpPath = $_FILES['image']['tmp_name'];
    	$fileName 	= $_FILES['image']['name'];
    
    	$imageData = file_get_contents($fileTmpPath);
    }
			
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
		{
			$sql = 'insert into staff_member(lastname,login,password,login_role_id) values ('.pg_escape_literal($conn,$lastname).',to_char(now(),\'YYYYMMDDHH24MISS\'),\'\',0) returning id;';
		}
		else
		{
			$sql = 'update staff_member set active = '.$active;
  			$sql .= ', lastname = '.pg_escape_literal($conn, $lastname);
  			$sql .= ', firstname = '.pg_escape_literal($conn,$firstname);
  			$sql .= ', login = \''.$login.'\'';
  			$sql .= ', password = encode(\''.$password.'\',\'base64\')';
  			$sql .= ', login_role_id = '.$login_role_id;
  			$sql .= ', ris_name = '.pg_escape_literal($conn,$ris_name);
  			$sql .= ', pacs_name = '.pg_escape_literal($conn,$pacs_name);
  			$sql .= ', dacs_name = '.pg_escape_literal($conn,$dacs_name);
  			if ( $imageData  != NULL )
  			{
  				// $sql .= ', photo = \''.pg_escape_bytea($conn,$imageData).'\'';
  				$sql .= ', photo = '.$imageData.'';
  			}
  			$sql .=' where id = '.$id.';';
		}

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