<?php
	require_once '../../autoload.php';
	
	$id		 		= -1;
	$name 			= "";
		
	
	if ( isset ( $_REQUEST['id'] ) 				)	$id		 		= $_REQUEST['id'];
	if ( isset ( $_REQUEST['name'] )			)	$name		 	= $_REQUEST['name'];
	if ( isset ( $_REQUEST['scope_id'] )		)	$scope_id	 	= $_REQUEST['scope_id'];
			
	$conn = get_dbconnection();
	
	$response = -1;
	
	if ( $conn )
	{
		$sql = '';

		if ( $id == -1 )
			$sql = 'insert into staff_role(name,scope_id) values (\''.pg_escape_string ($conn,$name).'\', -1) returning id;';
		else
			$sql = 'update staff_role set name=\''.pg_escape_string ($conn,$name).'\', scope_id='.$scope_id.' where id='.$id.'; commit;';
		
		$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;
?>