<?php

require_once("../class/dB.class.php");
require_once("../tcpdf/tcpdf.php");

 
$mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');

$sql = "SELECT DISTINCT gruppe FROM AnmeldungPerson WHERE mitglied = 1";
$mydb->query($sql);
while ($row = $mydb->fetchRow())
{
    $gruppen_array[] = $row['gruppe']; // Inside while loop
}       
 
// 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);


$style= <<<EOF
<style>
    
    table {
        
    }
    td {
        height:28px;
        line-height:2px;
        text-align:left;
        border-bottom: 1px solid black;
    }
    
    .lfdnrfield{
        text-align:left;
        width: 50px;
    }
    
    .namefield{
        text-align:left;
        width: 150px;

    }
    
    .field{
        text-align:center;
        width: 200px;
    }
    
    th {
        text-align:center;
        border-bottom: 2px solid black;
    }
  
</style>
EOF;

 

foreach( $gruppen_array as $gruppe) 
 {
    $sql = "SELECT a.nachname, a.vorname, z.mitarbeiter, x.zelt  FROM AnmeldungAdressen as a JOIN AnmeldungPerson as p ON a.uid = p.uid JOIN AnmeldungZeltwunsch as z ON z.uid = a.uid JOIN AnmeldungVerwaltung as v ON v.uid=a.uid JOIN VerwaltungZelte as x ON x.zeltnummer=v.zeltnummer WHERE p.gruppe='". $gruppe ."' ORDER BY  a.nachname COLLATE latin1_german2_ci, a.vorname COLLATE latin1_german2_ci ";

    $mydb->query($sql);
    $amount = $mydb->count(); 
   
    
    if($amount > 0)
    {
        $lfdnr = 0;
         
        $pdf->AddPage();
      
        $pdf->SetFont('helvetica', 'B', 17);
        $pdf->Cell(190, 40, 'Jugendgruppe: '. $gruppe, '', 0, 'C', false, '', 0, false, 'T', 'M');
       
        
        $pdf->SetFont('helvetica', '', 11);
        $tbl = $style . "<table>";
        
        $tbl .= "<thead><tr><th class=\"lfdnrfield\">&nbsp;</th><th class=\"namefield\">Nachname</th><th class=\"namefield\">Vorname</th><th class=\"field\">Zelt</th></tr></thead></tbody>";
       
        while($row = $mydb->fetchRow())
        {
            $lfdnr++;
  
            if ($row['mitarbeiter'] == 1)
            {
                $tbl .= "<tr><td class=\"lfdnrfield\">$lfdnr</td><td class=\"namefield\"><strike>" . $row['nachname'] . "</strike></td><td class=\"namefield\"><strike>$row[vorname]</strike></td><td class=\"field\"><strike>$row[zelt]</strike></td></tr>";  
            }
            else
            {
                 $tbl .= "<tr><td class=\"lfdnrfield\">$lfdnr</td><td class=\"namefield\">$row[nachname]</td><td class=\"namefield\">$row[vorname]</td><td class=\"field\">$row[zelt]</td></tr>";  
            }

        }
        
       
        $tbl .= "</tbody></table>";

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


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


$mydb->disconnect();
?>
