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

	$id 	= $_POST["id"];

	$conn = get_dbconnection();
	
	$arr = array (
			"result"		=> 'Error'
	);
	
	if ( $conn )
	{
		/*
		$sql = "select document.id as id, document_link.type as document_type, revision, document_link.ext_id as ext_id, document.uploadedby as uploaded_by,document_master.name as name, document.label as label, v_last_documentstatus.status_id as status_id, to_char(document.creation_date,'DD/MM/YYYY') as create_date,document.comment as comment,(select array_to_string(array_agg(id_quaadtag),',')  as bquaad_tag_list from quaadtag_doc_link  where quaadtag_doc_link.id_doclink=(select id from document_link where doc_id=".$id.")),extern_link,nullif(to_char(valid_until_date,'DD/MM/YYYY'),'31/12/2999') as valid_until_date
		from document_master,document_link,document, v_last_documentstatus where document_master.removed = false and document_master.id = document.doc_master_id and document_link.doc_id = document.id and document.id = v_last_documentstatus.doc_id and document.id = ".$id;
		*/

		$sql = "select document.id as id, document_link.type as document_type, revision, document_link.ext_id as ext_id, document.uploadedby as uploaded_by,document_master.name as name, document.label as label, v_last_documentstatus.status_id as status_id, to_char(document.creation_date,'YYYYMMDD') as create_date,document.comment as comment,(select array_to_string(array_agg(id_quaadtag),',')  as bquaad_tag_list from quaadtag_doc_link  where quaadtag_doc_link.id_doclink=(select id from document_link where doc_id=".$id.")),extern_link,nullif(to_char(valid_until_date,'YYYYMMDD'),'29991231') as valid_until_date
		from document_master,document_link,document, v_last_documentstatus where document_master.removed = false and document_master.id = document.doc_master_id and document_link.doc_id = document.id and document.id = v_last_documentstatus.doc_id and document.id = ".$id;

		
		if ( $query = pg_query($conn,$sql) )
		{
			if ( $row = pg_fetch_object($query) )
			{
				$arr["id"]					= $row->id;
				$arr["name"]				= $row->name;
				$arr["label"]				= $row->label;
				$arr["revision"]			= $row->revision;
				$arr["comment"]				= $row->comment;
				$arr["create_date"]			= $row->create_date;
				$arr["status_id"]			= $row->status_id;
				$arr["valid_until_date"]	= $row->valid_until_date;
				$arr["bquaad_tag_list"]		= $row->bquaad_tag_list;

				$arr["result"] = "OK";
			}
		}
	}

	
	header('Content-type: application/json');

	
	
	$result = json_encode ( $arr );

	echo $result;
?>