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

$db = dbconnect();



if (isset($_GET['action']))
   $action = $_GET['action'];
else
   $action = "";

switch($action){
default:
    # SOZ gibt es nicht mit Druckstatus=N. Dann hat man während der Ausführung abgebrochen. Damit SOZ erneut gedruckt werden kann darf es nicht in der printqueue stehen
    $sql_queue = $db->query("DELETE FROM stan_printqueue_antrag WHERE druckstatus='N' AND zuweisung='SOZ'");
echo "
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
  <link rel=\"stylesheet\" type=\"text/css\" href=\"../fancybox/jquery.fancybox-1.3.4.css\" media=\"screen\" />
  <script type=\"text/javascript\" src=\"../fancybox/jquery-1.5.1.min.js\"></script>
  <!-- DATEI GIBTS NICHT   <script>!window.jQuery && document.write('<script src=\"jquery-1.4.3.min.js\"><\/script>');</script>-->
  <script type=\"text/javascript\" src=\"../fancybox/jquery.mousewheel-3.0.4.pack.js\"></script>
  <script type=\"text/javascript\" src=\"../fancybox/jquery.fancybox-1.3.4.pack.js\"></script>
  <script type=\"text/javascript\">
    $(document).ready(function() {
        $(\".box\").fancybox({
            'width'                : '100%',
            'height'               : '100%',
            'autoScale'            : true,
            'transitionIn'         : 'elastic',
            'transitionOut'        : 'elastic',
            'type'                 : 'iframe'
        });
    });
  </script>
  <style type='text/css'>
.aformlink{
       text-decoration: none;
       border-bottom: 1px dotted red;
       background-image: none;
       padding-left: 0;
     }
  </style>
<title>Standortzuweisung drucken</title>
</head>
<body onload=\"if(document.einzel)document.einzel.user1.focus();return false;\">
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">Standortzuweisung drucken</font><br>
                    <font class=\"sh\">Treffen Sie unten eine Auswahl</font><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
			  <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-width: 0\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\" height=\"115\">
			    <tr>
			      <td align='center'>
			      <a href=".$_SERVER['PHP_SELF'] ."?action=pdf>Alle offenen Standortzuweisungen ausdrucken (Brief)</a>
			      
			      <br>
			      <br>
			              <a class='box' href=pdf_gen_ges_mail.php?sozonly=1 target='_blank'>Alle offenen Standortzuweisungen per Mail zustellen</a>
			      </td>
			    </tr>
			  </table>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";


break;
case "pdf":

// In der View stan_zuw_so sind nur DatensÃ¤tze drin, die nicht bereits gedruckt wurden. Abgleich mit 'stan_printqueue_antrag'
$queryb = $db->query ("SELECT count(*) Anz
                          FROM stan_zuw_so_neu
                         WHERE hs='$hs'");
$row_cur_uid_anz = $queryb->fetch_array();


if($row_cur_uid_anz['Anz'] > 0){

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

$pdf2= new utfFPDF();


#if($hs == 'K' or $hs == 'k'){
#  $pagecount = $pdf->setSourceFile("anschreiben_kehl/blanko_kehl.pdf");
#}else{
#  $pagecount = $pdf->setSourceFile("anschreiben/briefkopf_blanko_2023.pdf");
#}


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



$db = dbconnect();

$querya = "SELECT distinct UID, vorname, nachname
            FROM stan_zuw_so_neu
           WHERE hs='$hs'
             AND jahrgang='$jahrgang'
           ORDER BY uid
           ";
$resulta = $db->query ($querya)
   or die ("Cannot execute query1");

$datum = date("Y-m-d H:i:s", time());

while ($row_cur_uid2 = $resulta->fetch_array())
{
/* Testperson zurücksetzen
DELETE FROM `stan_printqueue_antrag` WHERE qid > 94541;
SELECT * from stan_printqueue_antrag WHERE qid > 94541;
*/
    # PDF erzeugen für jeden Student
    $dok_name2              = zuweisung_mail($hs, 'SOZ', NULL, $row_cur_uid2['UID'], NULL);
    $sql_queue = $db->query("UPDATE stan_printqueue_antrag
                                SET druckstatus='G'
                                  , datum = '$datum'
                                  , versandart = 'B'
                              WHERE dokument='$dok_name2'
                           ");
    
    
    # Sammeldokument erstellen für Druck
    $pagecount = $pdf2->setSourceFile("$dok_name2");
    $tplidx = $pdf2->ImportPage(1);
    $pdf2->addPage();
    $pdf2->useTemplate($tplidx,0,0,0);
    $pdf2->SetAutoPageBreak(true, 10);
    
}
  
  $pdf2->Output("zuweisung_standort.pdf","i");
  $pdf2->closeParsers();
  }else{ //($row_cur_uid_anz[Anz] = 0
  
  echo"
  <html>
  <head>
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
  <link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
  <title>Mailversand</title>
  </head>
  <body>
  <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">Keine aktuelle Zuweisungen</font><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br><p align=\"center\"><b>Es wurden alle Standortzuweisungen gedruckt!</b>
                    <p></p>
                    <p></p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
  </table>
  </body>
  </html>";
  
  } //Endif($row_cur_uid_anz[Anz] > 0){
}
?>
