<?php
$user_admin=$_COOKIE["user_admin"];
$jahrgang=$_COOKIE["jahrgang"];

if ($jahrgang == ""){ require("jahrgang.php"); exit;} //Wenn man kein Jahrgang ausgew&auml;hlt hat wird die Jahrgangsauswahlseite geladen
include("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php

// Rechte&uuml;berpr&uuml;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_puort'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende


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

switch($action){
default:


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>PU Standorte</title>
<style type='text/css'>
select{
	width:323px;
}
</style>
</head>

<body onload=\"if(document.anmschluss)document.anmschluss.so1.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=\"80%\" 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=\"98%\" background=\"../images/box_top.gif\" valign=\"top\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <form action="; echo $_SERVER['PHP_SELF'] . "?action=ubernahme"; echo" method=\"POST\" name=\"ubernahme\">
                      <font class=\"hd\">PU Standorte <input type=\"submit\" value=\"Übernahme Vorjahr\" name=\"B1\" size=\"20\"></form></font><br>
                    </form>
                    <font class=\"sh\">eintragen / löschen</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>
                    <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" id=\"table2\" name=\"anmschluss\">
                        <tr>
                            <td><br>
                            <table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">
                                <tr>
                                    <td width=\"1%\" background=\"../images/box_e1a.gif\">&nbsp;</td>
                                    <td align=\"left\" width=\"98%\" background=\"../images/box_top1.gif\" valign=\"top\">
                                    <p align=\"center\">&nbsp;</p>
                                    </td>
                                    <td align=\"right\" width=\"1%\" background=\"../images/box_e2a.gif\">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td width=\"1%\" background=\"../images/box_l.gif\">
                                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                                    <td>
                                    <form action="; echo $_SERVER['PHP_SELF'] . "?action=eintragen"; echo" method=\"POST\" name=\"anmschluss\">
                                    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-left-width:0; border-right-width:0; border-bottom-width:0\" bordercolor=\"#111111\" width=\"100%\">
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 1:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so1\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 2:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so2\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 3:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so3\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 4:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so4\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 5:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so5\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 6:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so6\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 7:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so7\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 8:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so8\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 9:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so9\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width=\"30%\">
                                            <p style=\"margin:10 4; \">PU Standort 10:
                                            </td>
                                            <td width=\"70%\" align=\"left\" >
                                            <p style=\"margin:10 4; \">
                                              <input type=\"text\" name=\"so10\" size=\"50\">
                                            </p>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                            <input type=\"submit\" value=\"Eintragen\" name=\"B1\" size=\"20\"></form>
                                            </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\">&nbsp;</td>
                                    <td align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\">
                                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">&nbsp;</td>
                                </tr>
                            </table>
                            <p>
                            <table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">
                                <tr>
                                    <td width=\"1%\" background=\"../images/box_e1a.gif\">&nbsp;</td>
                                    <td align=\"left\" width=\"98%\" background=\"../images/box_top1.gif\" valign=\"top\">
                                    <p align=\"center\">&nbsp;</p>
                                    </td>
                                    <td align=\"right\" width=\"1%\" background=\"../images/box_e2a.gif\">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td width=\"1%\" background=\"../images/box_l.gif\">
                                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                                    <td>
                                    <form action="; echo $_SERVER['PHP_SELF'] . "?action=del"; echo" method=\"POST\" name=\"anmschluss\">
                                    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-left-width:0; border-right-width:0; border-bottom-width:0\" bordercolor=\"#111111\" width=\"100%\">
                                        <tr>
                                            <td align='center'>
                                              <br>";
                                               $db = dbconnect();
                                               $query = "SELECT DISTINCT pusoid, bezeichnung
                                                           FROM stan_pu_standorte
                                                          WHERE jahrgang='$jahrgang'
                                                          ORDER BY bezeichnung ASC
                                                        ";
                                               $result = $db->query ($query)
                                                  or die ("Cannot execute query");
                                               echo "<p align=\"center\"><select size=\"5\" name=\"pu_delete\">";
                                               while ($row = $result->fetch_array()){
                                               $ort_row = "$row[testdatum]|$row[testzeit]|$row[raum]";
                                               echo "<option value='$row[pusoid]'>$row[bezeichnung]</option>\n";
                                               }
                                               echo "</select>
                                              <br>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                            <input type=\"submit\" value=\"Löschen\" name=\"B1\" size=\"20\"></form>
                                            </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\">&nbsp;</td>
                                    <td align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\">
                                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">&nbsp;</td>
                                </tr>
                            </table>
                            <br>
                            </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\">
                    <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>
            </p>
               
        </div>
        </td>
    </tr>
</table>

</body>

</html>";

break;
case "eintragen":



$db = dbconnect();

if ($_POST[so1] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so1]')");}
if ($_POST[so2] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so2]')");}
if ($_POST[so3] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so3]')");}
if ($_POST[so4] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so4]')");}
if ($_POST[so5] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so5]')");}
if ($_POST[so6] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so6]')");}
if ($_POST[so7] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so7]')");}
if ($_POST[so8] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so8]')");}
if ($_POST[so9] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so9]')");}
if ($_POST[so10] != ''){$sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$_POST[so10]')");}

$sql = $db->query("DELETE FROM stan_pu_standorte WHERE bezeichnung = ''");

echo "
<meta http-equiv=\"refresh\" content=\"1; URL=pu_standorte.php\">

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Datums&auml;nderung</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>
                    <span class=\"sh\">Datums&auml;nderung</span><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>Standorte erfasst!</b>
                    </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>";

break;
case "del":

$db = dbconnect();

$result = $db->query("SELECT count(*) anzahl 
                           FROM stan_pu_wunsch
                          WHERE ort = '$_POST[pu_delete]'
                       ");
$row = $result->fetch_array();

if($row[anzahl] < 1){
$sql = $db->query("DELETE FROM stan_pu_standorte WHERE pusoid = '$_POST[pu_delete]'");

echo "
<meta http-equiv=\"refresh\" content=\"1; URL=pu_standorte.php\">

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Datums&auml;nderung</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>
                    <span class=\"sh\">Löschung</span><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>Standort gelöscht!</b>
                    </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>";
}else{
echo "
<meta http-equiv=\"refresh\" content=\"5; URL=pu_standorte.php\">

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Datums&auml;nderung</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>
                    <span class=\"sh\">keine Löschung</span><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>Standort konnte nicht gelöscht werden<br> da er noch in Verwendung ist!</b>
                    </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>";
}

break;
case "ubernahme":

$jahr_neu = $jahrgang-1;

if($jahr_neu < 10){   // ggf. 0 anfügen, damit es DB konform wird
  $vorjahr = "0".$jahr_neu;
}else{
  $vorjahr = $jahr_neu;
}

$db = dbconnect();

$query = "SELECT bezeichnung
            FROM stan_pu_standorte
           WHERE jahrgang = '$vorjahr'
             AND bezeichnung not in (SELECT bezeichnung FROM stan_pu_standorte WHERE jahrgang = '$jahrgang')
         ";
$result = $db->query ($query)
          or die ("Cannot execute query");

while ($row = $result->fetch_array()){
  $sql = $db->query("INSERT INTO stan_pu_standorte (jahrgang, bezeichnung) VALUES ('$jahrgang', '$row[bezeichnung]')");
}

echo"<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF'] ."?action=default\">";

} // Ende Action
?>