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

	RightsCheck ("VIEW_EXAMLIST");

	error_log ( "=== GetDTAllDocuments ===" );

	$toProcess = true;
	
	$draw			= $_GET ['draw'];
	$start			= $_GET ['start'];
	$length			= $_GET ['length'];
	
	$columns 		= $_GET ['columns'];
	$order			= $_GET ['order'];
	$orderCol		= $order [0]['column'];
	$orderDir		= $order [0]['dir'];
	$orderColName	= $columns [$orderCol]['data'];
	$orderby		= " ORDER BY ".$orderColName." ".$orderDir;
        
	$limit = '';

	if ( isset($_GET['start']) && $_GET['length'] != -1 ) 
	{
		$limit = " OFFSET ".intval($_GET['start'])." LIMIT ".intval($_GET['length']);
	}

	$name 		= ( isset($_GET['name']) )? $_GET['name'] : "";
	$type 		= ( isset($_GET['type']) )? $_GET['type'] : "";
	$status_id 	= ( isset($_GET['status_id']) )? $_GET['status_id'] : "";
	$taglist 	= ( isset($_GET['taglist']) )? $_GET['taglist'] : "";
	
	$where = "where name ilike '%" .$name. "%'";

	if ( $type != "-1" )
		$where .= " and document_type = '".$type."'";

	if ( $status_id != "-1" )
		$where .= " and status_id = '".$status_id."'";

	if ( strlen($taglist)>0 )
		$where .= " and string_to_array(tag_ids::text,',')::int[] && ARRAY[".$taglist."]";

   			$psql = "
					select 
						doc_id,
						ext_id, 
						document_type, 
						string_agg(department_fullname, ',<br>') as department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_procedure_instdep 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate 
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						string_agg(department_fullname, ',<br>') as department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_device_instdep 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate 
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						string_agg(department_fullname, ',<br>') as department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_staff_member_instdep 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate 
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						string_agg(department_fullname, ',<br>') as department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_staff_formation_instdep 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate 
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						'' as department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_legaldocument 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_institutiondocument 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						department_fullname,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate 
					union 
					select 
						doc_id,
						ext_id, 
						document_type, 
						department_fullname, 
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id, 
						extension,
						status_begdate 
					from 
						v_document_departmentdocument 
					".$where." 
				   group by 
						doc_id,
						ext_id,
						document_type,
						department_fullname,
						name,
						document_name,
						document_label,
						revision, 
						status_name,
						status_id,
						extension,
						status_begdate
   			";


	
	$recordsTotal 		= 1000;
	$recordsFiltered	= 0;
	
	$data				= Array ();
	
	if ( $toProcess == true )
		$data = GetDataFromDB ( $psql, $recordsFiltered, $limit, $orderby );
	
	$arr = array (
			"draw"            => intval( $draw ),
			"recordsTotal"    => intval( $recordsTotal ),
			"recordsFiltered" => intval( $recordsFiltered ),
			"data"            => $data,
			"sql"			  => $psql.$orderby.$limit
	);

	$result = json_encode ( $arr );

	echo $result;
	
 
?>