<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");

if ($_POST['action'] == 'getProjects') {
	$idProject = StringChecker::Check($_POST['idProject']);
	
	if ( $_SESSION['UserInfo']['user_type'] == 0 ) {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions 
					WHERE id_project = '".$idProject."' 
					AND presentation_team IN (SELECT id_group  FROM  ".DB_WARPIT_WEBCATI_BASE."._group_link  WHERE id = ".$_SESSION['UserInfo']['id'].")
					AND presentation_team_lock > NOW()";
	} else {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions WHERE id_project = '".$idProject."'";
	}
	
	$sql .= " ORDER BY presentation_date, presentation_location, presentation_time";
	
	$res = $db->SQLexecute($sql);
	
	while($zad = $db->fetchAssoc($res)){
		$label = $zad['presentation_date']." ".$zad['presentation_time']." ".$zad['presentation_location']." ".$zad['presentation_city'];
		$data[] = array("id" => $zad['id'], "idProj" => $zad['id_project'], "project" => $label, "hotel" => $zad['presentation_location'], "dan" => $zad['cati_day'], "ura" => $zad['cati_hour'], "zipId" => $zad['cati_location']);
	}
	
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data != null ? $data : '';
	echo json_encode($results);
}

else if($_POST['action'] == "getCount"){
	$idProject = StringChecker::Check($_POST['idProject']);
	
	if ( $_SESSION['UserInfo']['user_type'] == 0 ) {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions 
					WHERE id_project = '".$idProject."' 
					AND presentation_team IN (SELECT id_group  FROM  ".DB_WARPIT_WEBCATI_BASE."._group_link  WHERE id = ".$_SESSION['UserInfo']['id'].")
					AND presentation_team_lock > NOW()";
	} else {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions WHERE id_project = '".$idProject."'";
	}
	
	$res = $db->SQLexecute($sql);
	while($zad = $db->fetchAssoc($res)){
		$label = $zad['presentation_date']." ".$zad['presentation_time']." ".$zad['presentation_location']." ".$zad['presentation_city'];
		$data[] = array("id" => $zad['id'], "idProj" => $zad['id_project'], "project" => $label, "hotel" => $zad['presentation_location'], "dan" => $zad['cati_day'], "ura" => $zad['cati_hour'], "zipId" => $zad['cati_location']);
	}
	$results['count'] = $db->NumRows($res);
	$results['rows'] = $data;
	
	echo json_encode($results); 
}
?>