<?php

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

$num_errors = 0;
$dateTimeNow = dateTimeNow();

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

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

	//*****************************************
	//SET ALL INACTIVE FROM t_kategori
	//*****************************************
	$kat_Array = array();
	$kat_Array[] = array('aktif', 0, PDO::PARAM_INT);
	$whereCondition = "";
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_UPDATE($PDO, 't_kategori', $kat_Array, $whereCondition);
	
	//*****************************************
	//SET ALL INACTIVE FROM t_tetingkap
	//*****************************************
	$kat_Array = array();
	$kat_Array[] = array('aktif', 0, PDO::PARAM_INT);
	$whereCondition = "";
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_UPDATE($PDO, 't_tetingkap', $kat_Array, $whereCondition);
	
	
	
	//*****************************************
	//UPDATE INFO HERE
	//*****************************************
	$allTingkap = explode(',', fetch_postget_val("tingkap", $_POST, $_GET));
	for($num = 0 ; $num < count($allTingkap) ; $num++)
	{
		if($allTingkap[$num] != '')
		{
			$info = explode('_', $allTingkap[$num]);
			if($info[0] == 'k')//kategori
			{
				$table = 't_kategori';
				$whereCondition = "WHERE id_kategori = ".$info[1];
			}
			else if($info[0] == 't')//kategori
			{
				$table = 't_tetingkap';
				$whereCondition = "WHERE id_tetingkap = ".$info[1];
			}
			
			if($info[1] > 0) //play safe, only update if got value
			{
				$update_Array = array();
				$update_Array[] = array('aktif', 1, PDO::PARAM_INT);
				$PASS_VALUE += 1;
				$PASS_COUNTER += PDO_UPDATE($PDO, $table, $update_Array, $whereCondition);
			}
		}
		
	}

	//*****************************************
	//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();
}

?> 

	





