<?php
require_once("../phpexcel/Classes/PHPExcel.php");
require_once("../class/dB.class.php");

//error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


$objPHPExcel = new PHPExcel('My first Excel');

// Set properties
$objPHPExcel->getProperties()->setCreator("Michael Albrecht")
							 ->setLastModifiedBy("Michael Albrecht")
							 ->setTitle("Zeltwunschliste")
							 ->setSubject("www.zeltlageranmeldung.de")
							 ->setDescription("Liste der WÜnsche für die Zelteintielung")
							 ->setKeywords("Zelteinteilung")
							 ->setCategory("zeltlager");


// Add data
$objPHPExcel->setActiveSheetIndex(0);


$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Nachname');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Vorname');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Geschlecht');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Geburtsdatum');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Alter (heute)');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Wunsch 1');
$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Wunsch 2');
$objPHPExcel->getActiveSheet()->setCellValue('H1', 'Wunsch 3');
$objPHPExcel->getActiveSheet()->setCellValue('I1', 'Wunsch 4');

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

$sql = "SELECT a.nachname, a.vorname,  DATE_FORMAT(p.geburtstag, '%d.%m.%Y') as geburtsdatum, (YEAR(CURRENT_DATE) - YEAR(p.geburtstag)) - (RIGHT(CURRENT_DATE,5) < RIGHT(p.geburtstag,5)) AS age ,IF( p.geschlecht = 1, 'männlich', 'weiblich' ) AS geschlecht, z.insasse1, z.insasse2, z.insasse3, z.insasse4 FROM AnmeldungAdressen a LEFT JOIN AnmeldungPerson p ON a.uid = p.uid LEFT JOIN  AnmeldungZeltwunsch z ON a.uid = z.uid WHERE mitarbeiter = 0 ORDER BY a.nachname COLLATE latin1_german2_ci ASC, a.vorname COLLATE latin1_german2_ci ASC";
$mydb->query($sql);

$line= 1;

while ($row = $mydb->fetchRow())
{
  $line++;
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('0', $line, $row['nachname']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('1', $line, $row['vorname']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('2', $line, $row['geschlecht']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('3', $line, $row['geburtsdatum']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('4', $line, $row['age']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('5', $line, $row['insasse1']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('6', $line, $row['insasse2']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('7', $line, $row['insasse3']);
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('8', $line, $row['insasse4']); 
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Zeltwunsch');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

// Transmit File as Fileload
header("Content-Disposition: attachment; filename=zelteinteilung.xlsx");   
header("Content-Type: application/octet-stream");
$objWriter->save('php://output');


// Save Excel5 file
//echo date('H:i:s') , " Write to Excel5 format" , EOL;
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));


?>