<?php
include("kurs/datenbankanbindung.php"); // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
$user_admin=$_COOKIE["user_admin"];
$jahrgang=$_COOKIE["jahrgang"];

// Rechteüberprüfung
$db = dbconnect();
if ($user_admin == ""){ require("index.php"); exit;} //Wenn man nicht angemeldet ist, darf man nicht auf die Seite
$result = $db->query("SELECT 1 FROM stan_admin_rechte, stan_admin_rechte_zuord , stan_admin where stan_admin_rechte.stan_admin_rolle = stan_admin_rechte_zuord.stan_admin_rolle AND stan_admin_rechte_zuord.said = stan_admin.said AND stan_admin.user = '$user_admin' AND stan_admin_rechte_zuord.stan_admin_rolle = 'a_pulistdr'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende

// Rechteüberprüfung ende




define('FPDF_FONTPATH','../../stud/prints/font/');
require('../../stud/prints/as_utf_class.php');
require_once("../../stud/prints/table_def.inc");

$pdf=new utfFPDF();


$pagecount = $pdf->setSourceFile("briefkopf_blanko.pdf");

$tplidx = $pdf->ImportPage(1);

$pdf->SetAutoPageBreak(true, 20);
$pdf->SetTopMargin(0);
$pdf->SetRightMargin(23);
$pdf->SetTopMargin(25);

$db = dbconnect();
$res_ansp = $db->query("SELECT wert1 name, wert2 mail FROM parameter WHERE pid='6'");
$row_ansp = $res_ansp->fetch_array();

$res_kontakt = $db->query("SELECT wert1 tel, wert2 fax FROM parameter WHERE pid='5'");
$row_kontakt = $res_kontakt->fetch_array();

$db = dbconnect();
$query = "SELECT distinct ort
            FROM stan_pu_wunsch
           WHERE uid IN (Select uid from stud WHERE jahrgang = '$jahrgang')
             AND prioritaet = 0
         ";
$result = $db->query ($query)
   or die ("Cannot execute query");
while ($row = $result->fetch_array()){


$pdf->addPage();
$pdf->useTemplate($tplidx,0,0,0);


$heute_format = date("d.m.Y", mktime(0, 0, 0, date("m"), date("d"), date("Y")));  // ISO-8601 Format wie in der Datenbank: Formatierung f&uuml;r Vergleiche erforderlich

$pdf->SetFont('Times', '', 10);
$pdf->SetY(56.7);
$pdf->SetX(117.9);
$pdf->MultiCell(47, 3, "Ludwigsburg,", 0, 'L');

$pdf->SetFont('Times', '', 10);
$pdf->SetY(56.7);
$pdf->SetX(136.1);
$pdf->MultiCell(47, 3, "$heute_format", 0, 'R');


$pdf->SetFont('Times', '', 10);
$pdf->SetY(60.9);
$pdf->SetX(117.9);
$pdf->MultiCell(47, 3, "Bearbeiter/in", 0, 'L');

$pdf->SetFont('Times', '', 10);
$pdf->SetY(60.9);
$pdf->SetX(136.1);
$pdf->MultiCell(47, 3, "$row_ansp[name]", 0, 'R');


$pdf->SetFont('Times', '', 10);
$pdf->SetY(65.1);
$pdf->SetX(117.9);
$pdf->MultiCell(47, 3, "Telefon", 0, 'L');

$pdf->SetFont('Times', '', 10);
$pdf->SetY(65.1);
$pdf->SetX(136.1);
$pdf->MultiCell(47, 3, "$row_kontakt[tel]", 0, 'R');


$pdf->SetFont('Times', '', 10);
$pdf->SetY(69.3);
$pdf->SetX(117.9);
$pdf->MultiCell(47, 3, "Fax", 0, 'L');

$pdf->SetFont('Times', '', 10);
$pdf->SetY(69.3);
$pdf->SetX(136.1);
$pdf->MultiCell(47, 3, "$row_kontakt[fax]", 0, 'R');


$pdf->SetFont('Times', '', 10);
$pdf->SetY(73.5);
$pdf->SetX(117.9);
$pdf->MultiCell(47, 3, "e-mail", 0, 'L');

$pdf->SetFont('Times', '', 10);
$pdf->SetY(73.5);
$pdf->SetX(136.1);
$pdf->MultiCell(47, 3, "$row_ansp[mail]", 0, 'R');



$db = dbconnect();

$res1 = $db->query("SELECT bezeichnung FROM stan_pu_standorte WHERE pusoid='$row[ort]'");
$row1 = $res1->fetch_array();



$pdf->SetFont('Times', 'B', 12);
$pdf->SetY(93);
$pdf->SetX(23);
$pdf->MultiCell(0, 4.5, "Praxisbegleitender Unterricht\nStandort: $row1[bezeichnung]", 0, 'L');

                               

$columns = 4; //five columns

#$pdf->SetX(30);
$pdf->SetY(110);
	//we initialize the table class
	$pdf->Table_Init($columns, true, true);

	$table_subtype = $table_default_table_type;
	$pdf->Set_Table_Type($table_subtype);
	
	//TABLE HEADER SETTINGS
	$header_subtype = $table_default_header_type;
	
	
	for($i=0; $i<$columns; $i++) $header_type[$i] = $table_default_header_type;

	$header_type[0]['WIDTH'] = 55;
	$header_type[1]['WIDTH'] = 25;
	$header_type[2]['WIDTH'] = 50;
	$header_type[3]['WIDTH'] = 25;

	
	$header_type[0]['TEXT'] = "Name";
	$header_type[1]['TEXT'] = "Geburtstag / -ort";
	$header_type[2]['TEXT'] = "Anschrift";
	$header_type[3]['TEXT'] = "AG";
	
	#$header_type[1]['COLSPAN'] = "2";

	//set the header type
	$pdf->Set_Header_Type($header_type);
	
	$pdf->Draw_Header();
	
	//TABLE DATA SETTINGS
	$data_subtype = $table_default_data_type;
	
	$data_type = Array();//reset the array
	for ($i=0; $i<$columns; $i++) $data_type[$i] = $data_subtype;

	$pdf->Set_Data_Type($data_type);
	


        $db = dbconnect();
        $query2 = "SELECT vorname, nachname, ag, jahrgang, poStrasse, poplz, poort, date_format(geburtstag, '%d.%m.%Y') geburtstag, geburtsort
                    FROM stud
                   WHERE uid IN (SELECT uid FROM stan_pu_wunsch WHERE ort='$row[ort]' AND prioritaet='0')
                     AND jahrgang='$jahrgang'
                   ORDER BY nachname";
        
        $result2 = $db->query ($query2)
           or die ("Cannot execute query2");
 
        while ($row2 = $result2->fetch_array())
	{
		$data = Array();
		

			$data[0]['TEXT'] = "$row2[nachname], $row2[vorname]";
			$data[0]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
			$data[0]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
		       #$data[0]['T_TYPE'] = "BI"; // B=BOLD, I=ITALIC
			
			$data[1]['TEXT'] = "$row2[geburtstag]\n$row2[geburtsort]";
			$data[1]['T_ALIGN'] = "L";
			$data[1]['V_ALIGN'] = "M";
			
			$data[2]['TEXT'] = "$row2[poStrasse]\n$row2[poplz] $row2[poort]";
			$data[2]['T_ALIGN'] = "L";
			$data[2]['V_ALIGN'] = "M";

			$data[3]['TEXT'] = "$row2[ag] / $row2[jahrgang]";
			$data[3]['T_ALIGN'] = "C";
			$data[3]['V_ALIGN'] = "M";


		$pdf->Draw_Data($data);
	}

	$pdf->Draw_Table_Border();




} // Ende While



$pdf->Output("Listendruck_PU_$jahrgang.pdf","I");
$pdf->closeParsers();
?>