<?php
	require_once '../../autoload.php';
	
	$id		 		= -1;
	$civility_id	= -1;
	$lastname		= "";
	$firstname		= "";
	$active			= false;
	$login			= "";
	$password		= "";
	$email			= "";
	$login_role_id	= -1;
	$ris_name		= "";
	$pacs_name		= "";
	$dacs_name		= "";
	$photo			= "";
		
	if ( isset ( $_POST['id'] ) 			)	$id		 		= $_POST['id'];
	if ( isset ( $_POST['civility_id'] )	)	$civility_id	= $_POST['civility_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['email'] )			)	$email	 		= $_POST['email'];
	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) 
	{
    	$fileTmpPath = $_FILES['image']['tmp_name'];
    	$fileName 	= $_FILES['image']['name'];
    
    	$imageData = file_get_contents($fileTmpPath);
    }
			
	$pdo = getPDO ();
	
	$response = -1;
	
	if ( $pdo )
	{
		$sql = '';

		if ( $id == -1 )
		{
			$sql = 'insert into staff_member(lastname,login,password,login_role_id) values (:lastname,to_char(now(),\'YYYYMMDDHH24MISS\'),\'\',0) returning id;';
			$stmt = $pdo->prepare ( $sql );
			$stmt->bindParam ( ':lastname', $lastname );
		}
		else
		{
			$sql = 'update staff_member set active = :active';
  			$sql .= ', civility_id = :civility_id';
  			$sql .= ', lastname = :lastname';
  			$sql .= ', firstname = :firstname';
  			$sql .= ', login = :login';
  			$sql .= ', password = encode(:password,\'base64\')';
  			$sql .= ', email = :email';
  			$sql .= ', login_role_id = :login_role_id';
  			$sql .= ', ris_name = :ris_name';
  			$sql .= ', pacs_name = :pacs_name';
  			$sql .= ', dacs_name = :dacs_name';
  			if ( $imageData  != NULL )
  			{
  				$sql .= ', photo = :imageData';
  			}
  			$sql .=' where id = :id;';

  			error_log ( "SaveStaffMember : " .$sql );

  			$stmt = $pdo->prepare ( $sql );

  			$stmt->bindParam ( ':active',	 		$active 		);
  			$stmt->bindParam ( ':civility_id', 		$civility_id	);
  			$stmt->bindParam ( ':lastname', 		$lastname 		);
  			$stmt->bindParam ( ':firstname', 		$firstname 		);
  			$stmt->bindParam ( ':login', 			$login 			);
  			$stmt->bindParam ( ':password',			$password		);
  			$stmt->bindParam ( ':email',			$email			);
  			$stmt->bindParam ( ':login_role_id',	$login_role_id	);
  			$stmt->bindParam ( ':ris_name',			$ris_name		);
  			$stmt->bindParam ( ':pacs_name',		$pacs_name		);
  			$stmt->bindParam ( ':dacs_name',		$dacs_name		);
  			if ( $imageData  != NULL )
  				$stmt->bindParam ( ':imageData',	$imageData, PDO::PARAM_LOB	);
  			$stmt->bindParam ( ':id',				$id		);
		}

		$stmt->execute ();
		if ( $id == -1 )
			$response = $stmt->fetchColumn ();
		else
			$response = $id;
	}
	
	header ('Content-type: text/xml' );
	
	echo $response;
?>