<?php
function get_users($user_id=false,$arrFilter=false){
	if(!empty($_REQUEST)){
		foreach($_REQUEST as $k=>$v)
			$arrFilter[$k] = $v;
	}

	//filtro id
	if($user_id !== false)
		$filter_where .= "users.user_id = ".intval($user_id)." AND ";
	
	//filtro cognome
	if($arrFilter['flt_cogn']){
		$filter_where .= "users.user_cognome LIKE ".dbClean($arrFilter['flt_cogn'])." AND ";
	}
	
	if(!$arrFilter['no_limit']){
		$l = 50;
		$limit = "LIMIT ". (intval($_GET['p']) * $l) .",".$l;
	}
	
	//query
	$result = $GLOBALS['mysqli']->query("
	SELECT SQL_CALC_FOUND_ROWS *, users.user_id AS user_id, users.provincia_id AS provincia_id, users.comune_id AS comune_id FROM users 
	LEFT JOIN province ON users.provincia_id = province.provincia_id
	LEFT JOIN comuni ON users.comune_id = comuni.comune_id
	WHERE ".$filter_where." users.ente_id = ".$_SESSION['user']['ente_id']." AND user_grp = 'usr'
	ORDER BY users.user_cognome, users.user_nome 
	".$limit);
	if($result->num_rows > 0){

		$list['pagination'] = pagination($l);
		while($row=$result->fetch_assoc())
			$list['list'][] = $row;
		
		//se ho richiesto un'anagrafica rimando solo i dati, e' piu' comodo
		return $user_id ? $list['list'][0] : $list;
	}
	return false;
}

function del_user($user_id){
	
	$result = $GLOBALS['mysqli']->query("DELETE FROM users WHERE user_id = ".$user_id." AND ente_id = ".$_SESSION['user']['ente_id']);
	if($GLOBALS['mysqli']->affected_rows == 1){
		//estraggo anagrafiche collegate e le elimino
		$result = $GLOBALS['mysqli']->query("SELECT anagr_id FROM anagrafiche WHERE user_id = ".$user_id." AND ente_id = ".$_SESSION['user']['ente_id']);
		while($row = $result->fetch_assoc()){
			del_anagr($row['anagr_id']);
		}
		
	}
	
}
?>