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

	RightsCheck ("VIEW_STAFF");
	

	$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']);
	}

	if(isset($_GET['staffformationid']))
  	{
    	 $whereclause = " and staff_formation.id = {$_GET['staffformationid']}";
  	}
  	else
  	{
    	if(isset($_GET['staffid']))
    	{	
       		$whereclause = " and staff_formation.staff_id = {$_GET['staffid']}";
    	}
    	else 
    	{
      		$whereclause = " and staff_formation.staff_id = -1";
    	}
  	}

	$psql = "select staff_formation.id as id,staff_formation.status_id as status_id,statuslist.status_name as status_name,staff_id,to_char(formation_date,'YYYYMMDD') as formation_date,to_char(planned_date,'YYYYMMDD') as planned_date,staff_formation.title as title,staff_formation.type as type, comment
		from staff_formation,statuslist where staff_formation.status_id = statuslist.id and staff_formation.removed = false {$whereclause}";

	$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;
	
 
?>