<?php
function get_delegato($deleg_id,$anagr_id){
	$result = $GLOBALS['mysqli']->query("SELECT * FROM servizi_delegati WHERE deleg_id = ".intval($deleg_id)." AND anagr_id = ".$anagr_id);
	if($result)
		return $result->fetch_assoc();
	return false;
}

function del_delegato($deleg_id,$serv_cod){

	if($_SESSION['usr']['grp'] == 'usr')
		$w = "AND anagrafiche.user_id = ".$_SESSION['user']['id'];

	//estraggo anagrafica id per il log
	$result = $GLOBALS['mysqli']->query("SELECT anagr_id FROM servizi_delegati WHERE deleg_id = ".intval($deleg_id));
	$anagr_id = $result->fetch_object()->anagr_id;

	$GLOBALS['mysqli']->query("DELETE servizi_delegati FROM servizi_delegati LEFT JOIN anagrafiche ON servizi_delegati.anagr_id = anagrafiche.anagr_id WHERE servizi_delegati.deleg_id = ".intval($deleg_id)." ".$w." AND anagrafiche.ente_id = ".$_SESSION['user']['ente_id']);

	if($GLOBALS['mysqli']->affected_rows == 1)
		echo 'OK';

	//log
	usrlog("Servizio ".get_serv_name($serv_cod)." - Alunno: %s. Eliminato delegato",$anagr_id,true); //log 
		
	die();
}

function get_gruppi($serv_cod,$rif_id=false){
	$array = array();
	$result = $GLOBALS['mysqli']->query($q="SELECT * FROM gruppi WHERE serv_cod = '".$serv_cod."' AND ente_id = ".$_SESSION['user']['ente_id']." AND rif_id = ".intval($rif_id)." AND anno = ".$_SESSION['user']['ente_anno']." ORDER BY grp_nome");

	while($row = $result->fetch_assoc())
		$array[$row['grp_id']] = $row['grp_nome'];

	return $array;
}
function get_sottogruppi($serv_cod){
	$array = array();
	$result = $GLOBALS['mysqli']->query("
	SELECT * FROM gruppi WHERE serv_cod = '".$serv_cod."' AND ente_id = ".$_SESSION['user']['ente_id']." AND anno = ".$_SESSION['user']['ente_anno']." ORDER BY grp_nome");
	while($row = $result->fetch_assoc()){
		if($row['rif_id'] == 0)
			$array[$row['grp_id']]['name'] = $row['grp_nome'];
		else
			$array[$row['rif_id']]['sub'][$row['grp_id']] = $row['grp_nome'];
	}

	return $array;
}
function get_grp_allowed($serv_cod,$allow_cod){
	$array = array();
	$result = $GLOBALS['mysqli']->query("
	SELECT * FROM gruppi_allowed 
	LEFT JOIN gruppi ON gruppi_allowed.grp_id = gruppi.grp_id
	WHERE serv_cod = '".$serv_cod."' AND ente_id = ".$_SESSION['user']['ente_id']." AND allow_cod = '".$allow_cod."' AND anno = ".$_SESSION['user']['ente_anno']);
	while($row = $result->fetch_assoc())
		$array[$row['grp_id']] = true;
	return $array;
}

function add_grp_allowed($serv_cod,$allow_cod,$ids,$allowed_grps = false){
	
	//controllo sia un mio gruppo
	$result = $GLOBALS['mysqli']->query("SELECT grp_id FROM gruppi WHERE serv_cod = '".$serv_cod."' AND ente_id = ".$_SESSION['user']['ente_id']);
	if($result->num_rows == 0)
		return;
	else
		while($row = $result->fetch_assoc())
			$my_grps[$row['grp_id']] = true;
		
	if(!$allowed_grps)
		$allowed_grps = get_grp_allowed($serv_cod,$allow_cod);
	
	$allowed_grps = array_keys($allowed_grps);

	if(!is_array($ids))
		$ids = array($ids);

	//gruppi da aggiungere / rimuovere
	$add = array_diff($ids,$allowed_grps);
	$remove = array_diff($allowed_grps,$ids);

	foreach($add as $id)
		if($my_grps[$id])
			$GLOBALS['mysqli']->query("INSERT INTO gruppi_allowed VALUES (NULL,".$id.",'".$allow_cod."')");
	foreach($remove as $id)
		if($my_grps[$id])
			$GLOBALS['mysqli']->query("DELETE FROM gruppi_allowed WHERE grp_id =".intval($id));
}
function get_servizio_id($serv_cod){
	$result = $GLOBALS['mysqli']->query("SELECT serv_id FROM servizi WHERE serv_cod = ".dbClean($serv_cod));
	return $result->fetch_object()->serv_id;
}
?>