<?php
	require_once '../../autoload.php';
	
	RightsCheck ("VIEW_STAFFMEMBER");
	
	$id				 		= -1;
	$staff_id		 		= -1;
	$dep_id			 		= -1;
	$staff_role_id	 		= -1;
	$begdate				= "";
	$enddate				= "";
	
		
	
	if ( isset ( $_REQUEST['id'] )				)	$id				= $_REQUEST['id'];
	if ( isset ( $_REQUEST['staff_id'] )		)	$staff_id		= $_REQUEST['staff_id'];
	if ( isset ( $_REQUEST['dep_id'] )			)	$dep_id			= $_REQUEST['dep_id'];
	if ( isset ( $_REQUEST['staff_role_id'] )	)	$staff_role_id	= $_REQUEST['staff_role_id'];
	if ( isset ( $_REQUEST['begdate'] )			)	$begdate		= $_REQUEST['begdate'];
	if ( isset ( $_REQUEST['enddate'] )			)	$enddate		= $_REQUEST['enddate'];
				
	
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
			$sql = "insert into staff_job(dep_id,staff_role_id,staff_id,begdate) values(-1,-1,{$staff_id},now()) returning id;";
		else
			$sql = "update staff_job set begdate ='{$begdate}',enddate ='{$enddate}',dep_id ={$dep_id}, staff_role_id = {$staff_role_id} where id = {$id};";
		
		$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;
?>