<?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/_CRM_config.php");
include_once (PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");
include_once (PATH_TO_ROOT.'CRM/class/class.logger.php');

$Logger = new LoggerX($db);

ini_set("display_errors",1);

$LangPack = new LanguagePack($db, $aLanguage);


if($_POST['action'] == "submit"){
	
	
	$comment = StringChecker::Check($_POST['comment']);	
	$uploadDir = ACTIVITE_UPLOAD_PATH."diagnostic_lists/";
	$fileName = time()."-".$_FILES['uploadFile']['name'];
	
	$r = array();
	if(!file_exists($uploadDir)){
		@mkdir($uploadDir, 0777);
		$r['makeDir'] = $uploadDir;
	}
	
	
	if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadDir.$fileName)){
		
		$sql = "UPDATE ".WARPIT_CRM."._PresentatorPermissions
					SET
						diagnostic_list_added 	= NOW(),
						diagnostic_list_file 	= '".$fileName."',
						diagnostic_list_comment	= '".$comment."'
				WHERE 
						cati_location 	= 	".$_POST['cati_location']."
					AND	cati_day		= 	".$_POST['dan']."
					AND cati_hour		=	".$_POST['ura']."
					AND id_project		=	".$_POST['projectId'];
					  
		//echo "/*".$sql."*/";			  
		if($db->SQLexecute($sql)){
			$r['success'] = true;
		}else{
			$r['error'] = "Unable to save to database";
			$r['success'] = false;
		}
	}else{
		$r['error'] = "Unable to upload";
		$r['success'] = false;
	}
	
	echo json_encode($r);
}
?>