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

class PDF extends FPDF
{
	var $widths;
	var $aligns;
	var $line_h = 3.5;
	var $cell_padding = 1;
	
	function Header(){
		$this->SetFont('Arial','B',11);
		$this->MultiCell(0,6,entities_pdf(strtoupper($GLOBALS['grp_name'])).' - SERVIZO PRE / POST SCUOLA - DELEGATI AL RITIRO DEL MINORE',0,'C');		
		$this->Ln(5);
	}
	
	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}
	
	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}
	
	function Row($data,$fill)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb, $this->NbLines($this->widths[$i], $data[$i]));
		$h=$this->line_h*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		
		$fill_color = $fill ? 'DF' : '';
		
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x - $this->cell_padding, $y - $this->cell_padding, $w + $this->cell_padding, $h + $this->cell_padding*2,$fill_color);
			//Print the text
			$this->MultiCell($w, $this->line_h, $data[$i], 0, $a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w + $this->cell_padding, $y);
		}
		//Go to the next line
		$this->Ln($h+ $this->cell_padding*2);
	}
	
	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}
	
	function NbLines($w, $txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r", '', $txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
}

//delegati
$delegati = get_delegati_all();

//estraggo alunni iscritti al servizio pre / post scuola
$result = $mysqli->query("
SELECT 
CONCAT_WS(' ',anagrafiche.anagr_cognome,anagrafiche.anagr_nome) AS anagrafica,
anagrafiche.anagr_id,
CONCAT_WS(' ',users.user_cognome,users.user_nome) AS genitore,
user_cell,
servpps_pre,
servpps_post,
grp.grp_nome AS gruppo

FROM serv_pps
LEFT JOIN anagrafiche ON serv_pps.anagr_id = anagrafiche.anagr_id
LEFT JOIN users ON anagrafiche.user_id = users.user_id
LEFT JOIN gruppi grp ON serv_pps.scuola_id = grp.grp_id 

WHERE 
anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." AND anagrafiche.anno = ".$_SESSION['user']['ente_anno']." AND (serv_pps.servpps_pre = 1 OR serv_pps.servpps_post = 1) AND grp.grp_id IS NOT NULL
ORDER BY grp.grp_nome, anagrafiche.anagr_cognome, anagrafiche.anagr_nome
");

if($result->num_rows > 0){
	while($row=$result->fetch_assoc()){

		if($delegati[$row['anagr_id']]){
			
			$deleg = $delegati[$row['anagr_id']] ? implode(', ',$delegati[$row['anagr_id']]) : '';
	
			$anagrafiche[$row['gruppo']][] = array(
			entities_pdf(welltxt($row['anagrafica'])),
			($row['servpps_pre'] == 1 ? 'X' : ''),
			($row['servpps_post'] == 1 ? 'X' : ''),
			entities_pdf(($row['genitore'])),
			entities_pdf(welltxt($row['user_cell'])),
			entities_pdf(welltxt($deleg))
			);
		}
	}
	
}else{
	header("Location: ../adm/?noprintresult");	
	die();
}


$pdf = new PDF('L','mm','A4');
$pdf->SetMargins(15,10);
$pdf->SetWidths(array(50,10,10, 40, 20, 132));
$pdf->SetAligns(array('L','C','C', 'L', 'L', 'L'));

foreach($anagrafiche as $grp_name => $alunno){


	$pdf->AddPage();
	$pdf->SetFillColor(210,255,21);
	$pdf->SetFont('Arial','B',8);
	$pdf->Row(array('Alunno','PRE','POST','Genitore','Cellulare','Delegati al ritiro'),true);
	$pdf->SetFont('Arial','',8);
	$pdf->SetFillColor(224,235,255);
	$fill=false;

	foreach($alunno as $k=>$v){
		$pdf->Row($v,$fill);
		$fill = !$fill;
	}
}
$pdf->Output('prepost_delegati.pdf','D');
?>