<?php
	include '../../autoload.php';
	
	$answer="<root>";
	$quaadpoint_id=pg_escape_string($_REQUEST['quaadpoint_id']);
	$quaadpoint_tag_id=pg_escape_string($_REQUEST['quaadpoint_tag_id']);
	$scope=pg_escape_string($_REQUEST['scope']);
	$scope2=pg_escape_string($_REQUEST['scope2']);
	$global_scope=pg_escape_string($_REQUEST['global_scope']);
	$doc_needed=pg_escape_string($_REQUEST['doc_needed']);
	$proc_needed=pg_escape_string($_REQUEST['proc_needed']);		// Procedure type
	$add_param=pg_escape_string($_REQUEST['add_param']);
	$where_no_tagid=pg_escape_string($_REQUEST['where_no_tagid']);
	$document_scope=pg_escape_string($_REQUEST['document_scope']);
	$quaadpoint_tag_name=pg_escape_string($_REQUEST['tag_name']);
	$language=pg_escape_string($_REQUEST['language']);
	
	error_log ( "=================================================" );
	error_log ( "$quaadpoint_tag_id : ".$quaadpoint_tag_id );
	
	if($quaadpoint_tag_id >= 0)
	{
		$sql = "update quaadpoint_tag set scope='".$scope."', scope2='".$scope2."', isglobalscope='".$global_scope."', procedure_needed='".$proc_needed."', documents_needed='".$doc_needed."',document_scope='".$document_scope."', additional_param='".str_replace("'","''", $add_param)."', where_notagid='".$where_no_tagid."' where id=".$quaadpoint_tag_id." returning id";
		
		$sql_name= "update quaadpoint_tag_name set name='".$quaadpoint_tag_name."' where id_quaadpoint_tag=".$quaadpoint_tag_id." and language='".$language."';";
	}
	else
	{
	 	$sql = "insert into quaadpoint_tag(scope, scope2, isglobalscope, procedure_needed, documents_needed,document_scope, additional_param) values('".$scope."', '".$scope2."', '".$global_scope."', '".$proc_needed."', '".$doc_needed."','".$document_scope."', '".str_replace("'","''",$add_param)."') returning id";
		
	}
	$conn = get_dbconnection();
	
	$query = pg_query($conn, $sql);
	
	error_log ( " $sql :".$sql);
	
	if(!$query)
	{
		$err = pg_last_error($conn);
		$answer .= "<status value=\"".$err."\"></status>";
		error_log (" $err : ".$err);
	}
	else
	{	
		error_log (" Request OK." );
		if($quaadpoint_tag_id < 0)	// Insert
		{
			$row = pg_fetch_row($query);
		
			// Insert into link_quaad_tag
			$sql1 = "insert into link_quaad_tag(id_quaadpoint, id_tag) values(".$quaadpoint_id.", ".$row[0].")";
			// $sql_name="insert into quaadpoint_tag_name (name,language,id_quaadpoint_tag) values ('".$quaadpoint_tag_name."','".$language."',".$quaadpoint_tag_id.");";
			$sql_name="insert into quaadpoint_tag_name (name,language,id_quaadpoint_tag) values ('".$quaadpoint_tag_name."','".$language."',".$row[0].");";
			$query1 = pg_query($conn, $sql1);
			if(!$query1)
			{
				$err = pg_last_error($conn);
				$answer .= "<status value=\"".$err."\"></status>";
			}
			else
				$answer .= "<status value=\"ok\"></status>";
		}
		else	// Update
			$answer .= "<status value=\"ok\"></status>";
			
		error_log($sql_name);
		
		$query2=pg_query($conn,$sql_name);
		
		if ( !$query2 )
		{
			$err = pg_last_error($conn);
			$answer .= "<status value=\"".$err."\"></status>";
			error_log (" $err2 : ".$err);
		}
		else
		{
			error_log (" Request 2 OK." );
		}
		
	}
	
	
	$answer.="</root>";
	header('Content-Type: text/xml');
	echo $answer;
	
?>