<?php

include("../class/dB.class.php");
require_once("../tcpdf/tcpdf.php");
 
$mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');
         
$mydb->query("SELECT * FROM VerwaltungBeitraegePreise ORDER BY jahr DESC limit 1");
$row = $mydb->fetchRow(); 

$RowsOnPage = 9;
$RowsWritten = 0;
 
 
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

    //Page header
    public function Header()
    {
         //aktuelle Jahreszahl auslesen
        $mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');
         
        $mydb->query("SELECT * FROM VerwaltungLagerplatz ORDER BY jahr DESC limit 1");
        $row = $mydb->fetchRow();
        
        
        $this->SetFont('helvetica', 'B', 10);
        $this->Cell(170, 10, 'Evang. Gemeindejugend Memmingen - '. $row['platz'], 0, false, 'C', 0, '', 0, false, 'C', 'B');
        
       
        // Logo
        $image_file = '../../images/gemeindejugend_logo.jpg';
        $this->Image($image_file, 15, 5, '20', '', 'JPG', '', 'M', true, 600, '', false, false, 0, false, false, false);
        
        $mydb->disconnect();
    }

    // Page footer
    public function Footer() 
    {
       
        // Position at 10 mm from bottom
        $this->SetY(-10);
        // Set font
        $this->SetFont('helvetica', '', 8);
        // Status zeile
        $datum  = date("d.m.Y", time());
        $this->Cell(0, 10, 'created: ' . $datum . "  -   www.zeltlageranmeldung.de                                          Seite ".$this->PageNo(). " von " . $this->getAliasNbPages(), 0, false, 'L', 0, '', 0, false, 'T', 'M');
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator('www.fischerkoenig.de');
$pdf->SetAuthor('Team der Zeltlageranmeldung');
$pdf->SetTitle('Zeltlageranmeldung');
$pdf->SetSubject('Listen fürs Zeltlager');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH,'Zeltlageranmeldung', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
 
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetAutoPageBreak(TRUE, 10);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

//Datenbankabfrage
$mydb->query("SELECT * , IF( medikamentengabe = 1, 'ja', '<b>nein</b>' ) AS medabgabe FROM AnmeldungAdressen as a LEFT JOIN AnmeldungNotfallkontakt as b ON a.uid = b.uid LEFT JOIN AnmeldungZusatzangaben as c ON a.uid = c.uid LEFT JOIN AnmeldungVerwaltung as d ON a.uid=d.uid LEFT JOIN VerwaltungZelte as e ON d.zeltnummer=e.zeltnummer ORDER BY a.nachname COLLATE latin1_german2_ci ASC, a.vorname COLLATE latin1_german2_ci ASC ");
$NumberOfEntries =  $mydb->count();

$style= <<<EOF
<style>
    
   
    
</style>
EOF;

 

 While ($NumberOfEntries > 0)
 {
    $pdf->AddPage();
  
    $pdf->SetFont('helvetica', 'B', 17);
    $pdf->Cell(190, 40, 'Sanizelt-Liste', '', 0, 'C', false, '', 0, false, 'T', 'M');
   
    
    $pdf->SetFont('helvetica', '', 11);
    
    $tbl ="";
    do
    {
        $RowsWritten++;
        $NumberOfEntries--;
        $row = $mydb->fetchRow();
        
     
        $tbl .=  "<hr>";
        $tbl .=  "<table><tr><th><b>$row[nachname], $row[vorname]</b></th><th>Zelt: $row[zelt]</th><th>Medikamtengabe: $row[medabgabe]</th><th>Impfung: $row[impfung]</th></tr></table><br>";
        $tbl .=  "<b>Notfallkontakt</b>: $row[notfall_nachname] $row[notfall_vorname], $row[notfall_strasse], $row[notfall_plz] $row[notfall_wohnort], $row[notfall_telefon]<br>";
        $tbl .=  "<b>Allergien</b>: $row[allergien]<br>";
        $tbl .=  "<b>Medikamente</b>: $row[medikamente]<br>";

        
        
    }while(($RowsWritten <= $RowsOnPage) && ($NumberOfEntries > 0));
    

    $pdf->writeHTMLCell	(0,0,15,30, $tbl, 0, 0, false, true,'',true);		
    $RowsWritten = 0;
 }


        
$filepath = '../pdf_listen/Liste-Sanizelt.pdf';
$pdf->Output($filepath, 'F');

echo "...done";

$mydb->disconnect();
?>
