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

		if ( $afg_id == -1 )
		{
			$now = new DateTime ();
			$sql = "insert into alertfilter_group(name) values ('".$now->format("Hisv")."') returning afg_id;";
		}
		else
			$sql = "update alertfilter_group set name = '".$name."' where afg_id=".$afg_id.";commit;";

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