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

	header("Content-Type: text/xml");

	$selectednrdid = '-1';
	if(isset($_POST['nrdid']))
	{
		$selectednrdid = $_POST['nrdid'];
	}

	$nrdtype = '';
	$wherenrdtype = '';
	if(isset($_POST['nrdtype']))
	{
		$nrdtype = $_POST['nrdtype'];

		if ( $nrdtype != '' )
			$wherenrdtype = " where nrdtype='".$nrdtype."'";
	}

	$conn = get_dbconnection();
	/*	
	$sql = "select alertname,string_agg(nrd_id::text,',') as nrd_id,bool_or(nrd_id = {$selectednrdid})  from (
	 select distinct alertname,nrd_id from studyalertwarning{$wherenrdtype}) as alertwarning group by alertname order by alertname";
	 */
	 $sql = "select alertname,string_agg(nrd_id::text,',') as nrd_id,bool_or(nrd_id = {$selectednrdid})  from (
	 select distinct alert_name as alertname,nrd_id from alertwarning) as alertwarning group by alertname order by alertname";

	 error_log ( "get_alert : " .$sql );

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

	$arr = array ();
	while ( $row = pg_fetch_row ($query) )
	{
		$res = array ( 
	        		"id" 		=> $row [1],
	        		"name" 		=> $row [0],
	        		"selected"	=> $row [2]
	        	);

	    array_push ( $arr, $res );
	}

	header('Content-type: application/json');
	
	echo json_encode($arr);
?>
