<?php
	require_once '../autoload.php';


	$use_id 		= $_REQUEST['use_id'];
	$param_name		= $_REQUEST['param_name'];
	$value 			= $_REQUEST['value'];

	$conn = get_dbconnection();

	$response = -1;
	$exist = 0;
	$vtype = 0;


	if ( $conn )
	{
		$param_id 		= -1;

		$sql = "select cfgparameter_id,vtype from xusercfgparameter where variable='".$param_name."'";
		if ( $query = pg_query($conn,$sql) )
		{
			if ( $row = pg_fetch_row($query) )
			{
				$param_id = $row [0];
				$vtype = $row[1];
			}
		}

		if ( $param_id != -1 )
		{
			$sql = "select id from xusercfg where id=".$use_id." and cfgparameter_id=".$param_id;

			if ( $query = pg_query($conn,$sql) )
			{
				if ( $row = pg_fetch_row($query) )
				{
					$exist = 1;
				}
			}

			if ( $exist == 0 )
				$sql = "insert into xusercfg ( id, cfgparameter_id, data ) values (".$use_id.", ".$param_id.", '".$value."')";
			else
			{
				$datavalue = "data='".$value."'";
				if($vtype == 4)
					$datavalue = 	$datavalue = "data= data::jsonb || '".$value."'::jsonb";
				$sql = "update xusercfg set ".$datavalue." where id=".$use_id." and cfgparameter_id=".$param_id;

			}

			$result = pg_query ( $conn, $sql );

			if ( $result )
			{
				$response = 1;
			}
		}
		else
		{
			error_log ( "Parameter ".$param_name." undefined in xusercfgparameter !!!" );
		}
	}


	header ('Content-type: text/xml' );

	echo $response;
?>