<?php

include("../../../_init/init.php");
include("../../../_support/class.db.php");


$db = new db;
$db->SetDatabaseConnection(DATABASE_USER,DATABASE_PASS,DATABASE_NAME);

if ($_POST['action'] == 'add') {
	
	
	//$db->SQLexecute($sql);
	echo "/* ".$sql." */";
	//$id = $db->GetLastInsertId();
	
	

} elseif ($_POST['action'] == 'update') {
	if ($_POST['value'] > 0) {
		$sql = "INSERT INTO `".DATABASE_NAME."`.administration_pravice_user 
					SET pravica = ".$_POST['id'].", pravica_user = ".$_POST['user_id'];
		
	} else {
		$sql = "DELETE FROM `".DATABASE_NAME."`.administration_pravice_user 
				WHERE pravica = ".$_POST['id']." AND  pravica_user = ".$_POST['user_id'];
	}
	echo "/*".$sql."*/";
	$db->SQLexecute($sql);
	
} else {
	$data = array();
	if (!$_POST['user_id']) $_POST['user_id'] = 0;
	
	$sql = " SELECT a.*, IF(b.pravica,1,0) as dovoljenje ";	
	$sql .= " FROM `".DATABASE_NAME."`.administration_pravice  as  a
				LEFT JOIN  `".DATABASE_NAME."`.administration_pravice_user as b ON a.id = b.pravica AND b.pravica_user =  ".$_POST['user_id'];
	
	
	$sql .= " ORDER BY pravica_group, pravica_opis ";
	
	$result = $db->SQLexecute($sql);
	while ($dat = $db->fetchAssoc($result)) {
		
		$data[] = $dat;
	}
	echo "/*".$sql."*/";
	?>
	
	{
	    results: <?php if ($t = count($data)) echo $t; else echo "0"; ?>, 
	    rows: <?php echo json_encode($data); ?>        
	    
	}
<?php 
}
?>