<?php

include("../class/dB.class.php");
require_once("../tcpdf/tcpdf.php");
 
$mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');

$RowsOnPage = 28;
$RowsWritten = 0;
$counter = 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 *,(YEAR(CURRENT_DATE) - YEAR(p.geburtstag)) - ('12-31' < RIGHT(p.geburtstag,5)) AS age  FROM AnmeldungAdressen as a LEFT JOIN AnmeldungPerson as p on a.uid = p.uid LEFT JOIN AnmeldungVerwaltung as v on a.uid = v.uid LEFT Join VerwaltungGruppe as g on g.gruppe = v.gruppe LEFT Join VerwaltungZelte as z on z.zeltnummer = v.zeltnummer LEFT Join AnmeldungZeltwunsch as w on a.uid = w.uid WHERE w.abbau = 1 ORDER BY a.nachname COLLATE latin1_german2_ci ASC, a.vorname COLLATE latin1_german2_ci ASC ");
$NumberOfEntries =  $mydb->count();

$style= <<<EOF
<style>
    
   
    td {
        text-align:left;
    }
    .namefield{
        width: 250px;
    }
    
    .zelt{
        width: 150px;
         text-align:right;
    }
    .alter{
        width: 50px;
         text-align:right;
    }
    
    .lfd{
        width: 50px;
         text-align:left;
    }
    
    th {
        text-align:left;
        border-bottom: 2px solid black;
    }
    
    .dataline{
        border-bottom: 0.5px solid black;
        height:30px;
    }
  
</style>
EOF;

 

 While ($NumberOfEntries > 0)
 {
    $pdf->AddPage();
  
    $pdf->SetFont('helvetica', 'B', 17);
    $pdf->Cell(190, 40, 'Teilnehmer - Abbau', '', 0, 'C', false, '', 0, false, 'T', 'M');
   
    
    $pdf->SetFont('helvetica', '', 11);
    $tbl = $style . "<table cellpadding=\"5\">"; 
    $tbl .= "<thead><tr><th class=\"lfd\"> </th><th class=\"namefield\">Name</th><th class=\"alter\">Alter</th><th class=\"zelt\">Zelt</th></tr></thead></tbody>";
    
    do
    {
        $RowsWritten++;
        $NumberOfEntries--;
        $row = $mydb->fetchRow();
        $counter++;
        
        $tbl .= "<tr><th class=\" lfd dataline\">$counter</th><th class=\"namefield dataline\">$row[nachname], $row[vorname]</th><th class=\"alter dataline\">$row[age]</th><th class=\"zelt dataline\">$row[zelt]</th></tr>";
        
    }while(($RowsWritten <= $RowsOnPage) && ($NumberOfEntries > 0));
    

    
    $tbl .= "</tbody></table>";
    //$pdf->writeHTML($tbl, true, false, false, false, '');
    $pdf->writeHTMLCell	(0,0,15,30, $tbl, 0, 0, false, true,'',true);		
    $RowsWritten = 0;
 }


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

echo "...done";

$mydb->disconnect();
?>
