<?php
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}

$ds          = DIRECTORY_SEPARATOR;  //1
$uploadId = fetch_postget_val('uploadId',$_POST, $_GET);
$filename = fetch_postget_val('filename',$_POST, $_GET);
$uploadFolder = fetch_postget_val('uploadFolder',$_POST, $_GET);

$storeFolder = $uploadFolder. $ds .$uploadId;

$targetPath = dirname( __FILE__ ) . $ds. $storeFolder; 
$targetFile =  $targetPath . $ds . $filename;

$deleteTempPath = dirname( __FILE__ ) . $ds . 'upload_delete' . $ds . $storeFolder; 
if(!is_dir($deleteTempPath)){
	mkdir($deleteTempPath, 0777, true);
}
$deleteTempFile =  $deleteTempPath . $ds . $filename;
//unlink($targetFile); //delete permenant
rename($targetFile, $deleteTempFile); //move to temp folder, dont delete

//save into dokumenupload log
try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 1; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();

	$dokumen_Array = array();
	$dokumen_Array[] = array('path_dokumenupload', $targetFile.' to '.$deleteTempFile, PDO::PARAM_STR, 1);
	$dokumen_Array[] = array('id_pemohon', $uploadId, PDO::PARAM_INT);
	$dokumen_Array[] = array('id_upload', $my_details->usr_id, PDO::PARAM_INT);
	$dokumen_Array[] = array('id_peranan', $my_details->user_level, PDO::PARAM_INT);
	$dokumen_Array[] = array('tindakan', 'D', PDO::PARAM_INT);
	$dokumen_Array[] = array('tkh_tindakan', dateTimeNow(), PDO::PARAM_STR);

	$PASS_COUNTER += PDO_INSERT($PDO, 'm_dokumenupload', $dokumen_Array);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
	}
	else
	{
		$PDO->rollBack();
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>  