<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id = fetch_postget_val("id", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

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();

	$peranan_Array = array();
	$peranan_Array[] = array('desk_peranan', fetch_postget_val("desk", $_POST, $_GET), PDO::PARAM_STR);
	$peranan_Array[] = array('susunan', fetch_postget_val("susunan", $_POST, $_GET), PDO::PARAM_INT);
	$peranan_Array[] = array('aktif', 1, PDO::PARAM_INT);
	//$peranan_Array[] = array('aktif', fetch_postget_val("aktif", $_POST, $_GET), PDO::PARAM_INT);

	if($id == 0)
	{
		//*****************************************
		//NEW t_peranan
		//*****************************************
		$PASS_COUNTER += PDO_INSERT($PDO, 't_peranan', $peranan_Array);
		$id = $PDO->lastInsertId();
	}
	else
	{
		//*****************************************
		//UPDATE t_master_pengguna
		//*****************************************
		$whereCondition = "WHERE id_peranan = $id";
		$PASS_COUNTER += PDO_UPDATE($PDO, 't_peranan', $peranan_Array, $whereCondition);
	}
	
	$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
	//*****************************************
	//DELETE ALL FROM t_pautan
	//*****************************************
	$whereCondition = "WHERE id_peranan = $id";
	$PASS_COUNTER += PDO_DELETE($PDO, 't_pautan', $whereCondition);
	
	//*****************************************
	//NEW t_pautan
	//*****************************************
	$allTingkap = explode(',', fetch_postget_val("tingkap", $_POST, $_GET));
	for($num = 0 ; $num < count($allTingkap) ; $num++)
	{
		if($allTingkap[$num] != '')
		{
			$tingkap_Array = array();
			$tingkap_Array[] = array('id_peranan', $id, PDO::PARAM_INT);
			$tingkap_Array[] = array('id_tetingkap', $allTingkap[$num], PDO::PARAM_INT);
			$tingkap_Array[] = array('aktif', 1, PDO::PARAM_INT);

			$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
			$PASS_COUNTER += PDO_INSERT($PDO, 't_pautan', $tingkap_Array);
		}
	}

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

?> 

	





