<?php
checkLogin('adm');
checkEnteService('men');
include("functions.inc.php");
dbConnect();

$zipname = "intolleranze.zip";
$zipfile = new zipfile();

$result = $mysqli->query("
SELECT 
attachments.att_id,
attachments.att_fname,
attachments_name.attach_shortname,
CONCAT_WS(' ',anagrafiche.anagr_cognome, anagrafiche.anagr_nome) AS anagrafica
FROM attachments 
LEFT JOIN attachments_name ON attachments.attach_id = attachments_name.attach_id
LEFT JOIN anagrafiche ON attachments.anagr_id = anagrafiche.anagr_id
WHERE attachments_name.serv_cod = 'men' AND anagrafiche.anno = ".$_SESSION['user']['ente_anno']);
if($result->num_rows > 0){
	while($row = $result->fetch_assoc()){
		$full_path = ROOT.'/userfiles/allegati/'.$row['att_fname'];
		if(is_file($full_path)){
			$name = urlFormat($row['anagrafica'].'_'.$row['attach_shortname']).'-'.$row['att_id'].'.'.pathinfo($full_path,PATHINFO_EXTENSION);
			$filedata = implode("", file($full_path));
			$zipfile->add_file($filedata, $name);
		}
	}
}else{
	$zipfile->add_file("", "nessun_modulo_presente");
}

header("Content-disposition: attachment; filename=intolleranze-".date("Y-m-d").".zip");
header("Content-type: application/octet-stream");
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).' GMT', true, 200);

echo $zipfile->file();
?>