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


	$use_id 		= $_REQUEST['use_id'];
	$param_name		= $_REQUEST['param_name'];
	$sub_param_name = '';
	if(isset($_REQUEST['sub_param_name']))
		$sub_param_name = $_REQUEST['sub_param_name'];

	$conn = get_dbconnection();

	$answer = "<answer>";

	$response = -1;
	$exist = 0;
	if ( $conn )
	{
		$data = 'data';

		if($sub_param_name != '')
			$data = " coalesce((data::jsonb)->>'{$sub_param_name}','{}')";

		$sql = "select {$data} from xusercfg,xusercfgparameter where xusercfg.cfgparameter_id=xusercfgparameter.cfgparameter_id and xusercfg.id=".$use_id." and xusercfgparameter.variable='".$param_name."'";

		if ( $query = pg_query($conn,$sql) )
		{
			if ( $row = pg_fetch_row($query) )
			{
				$response = $row [0];

				$answer = $answer."<cfg data='".$row [0]."'></cfg>";
				// $answer = $answer."<cfg data=\"".$row [0]."\"></cfg>";
				$exist = 1;
			}
		}

		if ( $exist == 0 )
		{
			$response = -1;
			$sql = "select vdefault from xusercfgparameter where variable='".$param_name."'";

			if ( $query = pg_query($conn,$sql) )
			{
				if ( $row = pg_fetch_row($query) )
				{
					$answer = $answer."<cfg data='".$row [0]."'></cfg>";
					$response = $row [0];
				}
			}
		}
	}
	$answer = $answer."</answer>";

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

	echo $answer;
?>