488 lines
19 KiB
PHP
488 lines
19 KiB
PHP
<?php
|
||
session_start();
|
||
error_reporting(E_ALL);
|
||
ini_set('display_errors', 1);
|
||
require_once 'db_connection.php';
|
||
// Initialisierung
|
||
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
|
||
$row = [];
|
||
$bilder = [];
|
||
$zutaten_ingr = '';
|
||
$zubereitung_text = '';
|
||
// Daten laden, falls ID vorhanden
|
||
if ($id > 0) {
|
||
$sql = "SELECT * FROM Rezepte WHERE id = ?";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('i', $id);
|
||
$stmt->execute();
|
||
if ($stmt->error) {
|
||
die("Fehler beim Update: " . $stmt->error);
|
||
}
|
||
$result = $stmt->get_result();
|
||
$row = $result->fetch_assoc();
|
||
// Lade die Zutaten aus der Tabelle 'ingredients', falls Rezeptnummer vorhanden
|
||
if (!empty($row['Rezeptnummer'])) {
|
||
$rezeptnummer = 'R' . str_pad($row['Rezeptnummer'], 3, '0', STR_PAD_LEFT);
|
||
$sql_ingr = "SELECT ingr FROM ingredients WHERE rezeptnr = ?";
|
||
$stmt_ingr = $link->prepare($sql_ingr);
|
||
$stmt_ingr->bind_param('s', $rezeptnummer);
|
||
$stmt_ingr->execute();
|
||
$result_ingr = $stmt_ingr->get_result();
|
||
$ingr_row = $result_ingr->fetch_assoc();
|
||
$zutaten_ingr = $ingr_row['ingr'] ?? '';
|
||
}
|
||
// Lade die Zubereitungsschritte aus der Tabelle 'Zubereitung'
|
||
if (!empty($row['Rezeptnummer'])) {
|
||
$rezeptnr_zub = 'R' . str_pad($row['Rezeptnummer'], 3, '0', STR_PAD_LEFT);
|
||
$sql_zub = "SELECT text FROM Zubereitung WHERE rezeptnummer = ? ORDER BY schritt";
|
||
$stmt_zub = $link->prepare($sql_zub);
|
||
$stmt_zub->bind_param('s', $rezeptnr_zub);
|
||
$stmt_zub->execute();
|
||
$result_zub = $stmt_zub->get_result();
|
||
while ($zub_row = $result_zub->fetch_assoc()) {
|
||
$zubereitung_text .= $zub_row['text'] . "\n";
|
||
}
|
||
$zubereitung_text = trim($zubereitung_text);
|
||
}
|
||
// Bilder laden
|
||
$sql_bilder = "SELECT * FROM rezepte_bilder WHERE rezepte_id = ?";
|
||
$stmt_bilder = $link->prepare($sql_bilder);
|
||
$stmt_bilder->bind_param('i', $id);
|
||
$stmt_bilder->execute();
|
||
$result_bilder = $stmt_bilder->get_result();
|
||
$bilder = $result_bilder->fetch_all(MYSQLI_ASSOC);
|
||
}
|
||
// Einzelnes Bild speichern, wenn der "Bild speichern"-Button geklickt wurde
|
||
if (isset($_POST['save_single_image']) && $id > 0) {
|
||
if (!empty($_FILES['single_bild']['name'])) {
|
||
$rezeptnummer = isset($_POST['Rezeptnummer']) ? intval($_POST['Rezeptnummer']) : 0;
|
||
$r_nummer = 'R' . str_pad($rezeptnummer, 3, '0', STR_PAD_LEFT);
|
||
$upload_dir = 'uploads/' . $r_nummer . '/';
|
||
// Ordner erstellen, falls nicht vorhanden
|
||
if (!file_exists($upload_dir)) {
|
||
mkdir($upload_dir, 0777, true);
|
||
}
|
||
// Aktuelle Anzahl der Bilder in diesem Ordner zählen
|
||
$existing_files = glob($upload_dir . $r_nummer . '_*.jpg');
|
||
$next_index = count($existing_files);
|
||
$datei_name = $r_nummer . '_' . $next_index . '.jpg';
|
||
$datei_pfad = $upload_dir . $datei_name;
|
||
if (move_uploaded_file($_FILES['single_bild']['tmp_name'], $datei_pfad)) {
|
||
$sql_bild = "INSERT INTO rezepte_bilder (rezepte_id, datei_pfad) VALUES (?, ?)";
|
||
$stmt_bild = $link->prepare($sql_bild);
|
||
$stmt_bild->bind_param('is', $id, $datei_pfad);
|
||
$stmt_bild->execute();
|
||
}
|
||
}
|
||
header("Location: Edit.php?id=$id");
|
||
exit();
|
||
}
|
||
// Daten speichern, wenn das Formular gesendet wurde
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
|
||
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
|
||
$rezeptnummer = isset($_POST['Rezeptnummer']) ? intval($_POST['Rezeptnummer']) : 0;
|
||
$bezeichnung = $_POST['Bezeichnung'];
|
||
$beschreibung = $_POST['Beschreibung'];
|
||
$kategorie = $_POST['Kategorie'];
|
||
$vorbereitung = $_POST['Vorbereitung'];
|
||
$anzahl_personen = isset($_POST['Anzahl']) ? intval($_POST['Anzahl']) : 2; // Standardwert 2
|
||
$zutaten = $_POST['Zutaten'];
|
||
$zubereitung = $_POST['Zubereitung'];
|
||
$kommentar = $_POST['Kommentar'];
|
||
// Rezeptnummer für 'ingredients' und 'Zubereitung' formatieren (z. B. R050)
|
||
$rezeptnr_ingr = 'R' . str_pad($rezeptnummer, 3, '0', STR_PAD_LEFT);
|
||
$rezeptnr_zub = $rezeptnr_ingr;
|
||
// 1. Zutaten in 'ingredients' speichern/updaten
|
||
$stmt_ingr = $link->prepare("
|
||
INSERT INTO ingredients (rezeptnr, ingr)
|
||
VALUES (?, ?)
|
||
ON DUPLICATE KEY UPDATE ingr = ?
|
||
");
|
||
$stmt_ingr->bind_param("sss", $rezeptnr_ingr, $zutaten, $zutaten);
|
||
$stmt_ingr->execute();
|
||
// 2. Zubereitungsschritte in 'Zubereitung' speichern
|
||
// Alte Zubereitungsschritte löschen
|
||
$sql_del_zub = "DELETE FROM Zubereitung WHERE rezeptnummer = ?";
|
||
$stmt_del_zub = $link->prepare($sql_del_zub);
|
||
$stmt_del_zub->bind_param('s', $rezeptnr_zub);
|
||
$stmt_del_zub->execute();
|
||
// Neue Zubereitungsschritte speichern
|
||
$zubereitung_schritte = explode("\n", $zubereitung);
|
||
$schritt_nummer = 1;
|
||
foreach ($zubereitung_schritte as $schritt_text) {
|
||
$schritt_text = trim($schritt_text);
|
||
if (!empty($schritt_text)) {
|
||
$sql_ins_zub = "INSERT INTO Zubereitung (rezeptnummer, schritt, text) VALUES (?, ?, ?)";
|
||
$stmt_ins_zub = $link->prepare($sql_ins_zub);
|
||
$stmt_ins_zub->bind_param('sis', $rezeptnr_zub, $schritt_nummer, $schritt_text);
|
||
$stmt_ins_zub->execute();
|
||
$schritt_nummer++;
|
||
}
|
||
}
|
||
// 3. Rezept in 'Rezepte' speichern/updaten
|
||
if ($id > 0) {
|
||
$sql = "UPDATE Rezepte SET Rezeptnummer = ?, Bezeichnung = ?, Beschreibung = ?, Kategorie = ?, Vorbereitung = ?, Anzahl = ?, Zutaten = ?, Zubereitung = ?, Kommentar = ? WHERE id = ?";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('issssiissi', $rezeptnummer, $bezeichnung, $beschreibung, $kategorie, $vorbereitung, $anzahl_personen, $zutaten, $zubereitung, $kommentar, $id);
|
||
$stmt->execute();
|
||
} else {
|
||
$sql = "INSERT INTO Rezepte (Rezeptnummer, Bezeichnung, Beschreibung, Kategorie, Vorbereitung, Anzahl, Zutaten, Zubereitung, Kommentar) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('issssisss', $rezeptnummer, $bezeichnung, $beschreibung, $kategorie, $vorbereitung, $anzahl_personen, $zutaten, $zubereitung, $kommentar);
|
||
$stmt->execute();
|
||
$id = $stmt->insert_id;
|
||
}
|
||
header("Location: index.php");
|
||
exit();
|
||
}
|
||
// Bild löschen
|
||
if (isset($_GET['delete_bild'])) {
|
||
$bild_id = intval($_GET['delete_bild']);
|
||
$sql = "SELECT datei_pfad FROM rezepte_bilder WHERE id = ?";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('i', $bild_id);
|
||
$stmt->execute();
|
||
$result = $stmt->get_result();
|
||
$bild = $result->fetch_assoc();
|
||
if (!empty($bild['datei_pfad']) && file_exists($bild['datei_pfad'])) {
|
||
unlink($bild['datei_pfad']);
|
||
}
|
||
$sql = "DELETE FROM rezepte_bilder WHERE id = ?";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('i', $bild_id);
|
||
$stmt->execute();
|
||
header("Location: Edit.php?id=$id");
|
||
exit();
|
||
}
|
||
// Rezept löschen
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete'])) {
|
||
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
|
||
if ($id > 0) {
|
||
// Lösche den Eintrag in 'ingredients', falls vorhanden
|
||
$sql_rezept = "SELECT Rezeptnummer FROM Rezepte WHERE id = ?";
|
||
$stmt_rezept = $link->prepare($sql_rezept);
|
||
$stmt_rezept->bind_param('i', $id);
|
||
$stmt_rezept->execute();
|
||
$result_rezept = $stmt_rezept->get_result();
|
||
$rezept = $result_rezept->fetch_assoc();
|
||
if ($rezept) {
|
||
$rezeptnr_ingr = 'R' . str_pad($rezept['Rezeptnummer'], 3, '0', STR_PAD_LEFT);
|
||
$sql_del_ingr = "DELETE FROM ingredients WHERE rezeptnr = ?";
|
||
$stmt_del_ingr = $link->prepare($sql_del_ingr);
|
||
$stmt_del_ingr->bind_param('s', $rezeptnr_ingr);
|
||
$stmt_del_ingr->execute();
|
||
// Lösche die Zubereitungsschritte
|
||
$sql_del_zub = "DELETE FROM Zubereitung WHERE rezeptnummer = ?";
|
||
$stmt_del_zub = $link->prepare($sql_del_zub);
|
||
$stmt_del_zub->bind_param('s', $rezeptnr_ingr);
|
||
$stmt_del_zub->execute();
|
||
}
|
||
// Lösche das Rezept
|
||
$sql = "DELETE FROM Rezepte WHERE id = ?";
|
||
$stmt = $link->prepare($sql);
|
||
$stmt->bind_param('i', $id);
|
||
$stmt->execute();
|
||
}
|
||
header("Location: index.php");
|
||
exit();
|
||
}
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<title><?= $id > 0 ? 'Rezept bearbeiten' : 'Neues Rezept' ?></title>
|
||
<style>
|
||
body {
|
||
font-family: Arial, Helvetica, sans-serif;
|
||
background: #fcf6e3;
|
||
color: #8b5a2b;
|
||
font-size: 22px;
|
||
}
|
||
.back-link {
|
||
display: block;
|
||
margin-bottom: 20px;
|
||
color: #8b5a2b;
|
||
text-decoration: none;
|
||
font-weight: bold;
|
||
}
|
||
.back-link:hover {
|
||
text-decoration: underline;
|
||
}
|
||
.form-container {
|
||
width: 80%;
|
||
margin: 0 auto;
|
||
background: #fff;
|
||
padding: 20px;
|
||
border-radius: 8px;
|
||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||
}
|
||
table {
|
||
width: 100%;
|
||
border-collapse: collapse;
|
||
}
|
||
th, td {
|
||
padding: 10px;
|
||
text-align: left;
|
||
border-bottom: 1px solid #ddd;
|
||
color: #8b5a2b;
|
||
}
|
||
input[type="text"],
|
||
textarea,
|
||
select {
|
||
width: 100%;
|
||
padding: 8px;
|
||
font-size: 18px;
|
||
border: 1px solid #ccc;
|
||
border-radius: 4px;
|
||
color: #5b4636;
|
||
}
|
||
textarea {
|
||
height: 100px;
|
||
resize: vertical;
|
||
}
|
||
.submit-btn, .delete-btn {
|
||
padding: 10px 20px;
|
||
border: none;
|
||
border-radius: 4px;
|
||
font-size: 18px;
|
||
cursor: pointer;
|
||
margin-top: 10px;
|
||
margin-right: 10px;
|
||
}
|
||
.submit-btn {
|
||
background: #bb7e19;
|
||
color: #fff;
|
||
}
|
||
.submit-btn:hover {
|
||
background: #a06d15;
|
||
}
|
||
.delete-btn {
|
||
background: #d65c5c;
|
||
color: #fff;
|
||
}
|
||
.delete-btn:hover {
|
||
background: #b83d3d;
|
||
}
|
||
.button-container {
|
||
display: flex;
|
||
justify-content: flex-start;
|
||
margin-top: 20px;
|
||
}
|
||
.file-upload-row {
|
||
display: flex;
|
||
margin-top: 10px;
|
||
}
|
||
.file-upload-container {
|
||
position: relative;
|
||
overflow: hidden;
|
||
display: inline-block;
|
||
width: 48%;
|
||
}
|
||
.file-upload-container input[type="file"] {
|
||
position: absolute;
|
||
left: 0;
|
||
top: 0;
|
||
opacity: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
cursor: pointer;
|
||
}
|
||
.file-upload-btn, .save-single-image-btn {
|
||
background: #bb7e19;
|
||
color: white;
|
||
padding: 8px 12px;
|
||
border-radius: 4px;
|
||
border: none;
|
||
cursor: pointer;
|
||
font-size: 16px;
|
||
width: 100%;
|
||
text-align: center;
|
||
padding-left: 0;
|
||
}
|
||
.save-single-image-btn {
|
||
background: #8b5a2b;
|
||
}
|
||
.save-single-image-btn:hover {
|
||
background: #7a4b22;
|
||
}
|
||
.bilder-container {
|
||
margin-top: 15px;
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 10px;
|
||
}
|
||
.bild-vorschau {
|
||
position: relative;
|
||
width: 80px;
|
||
height: 80px;
|
||
}
|
||
.bild-vorschau img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
}
|
||
.bild-loeschen {
|
||
position: absolute;
|
||
top: 5px;
|
||
right: 5px;
|
||
background: rgba(255, 255, 255, 0.8);
|
||
border: none;
|
||
border-radius: 50%;
|
||
width: 20px;
|
||
height: 20px;
|
||
cursor: pointer;
|
||
font-size: 14px;
|
||
color: #d65c5c;
|
||
line-height: 20px;
|
||
text-align: center;
|
||
text-decoration: none;
|
||
display: block;
|
||
}
|
||
#lightbox {
|
||
display: none;
|
||
position: fixed;
|
||
z-index: 9999;
|
||
top: 0;
|
||
left: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
background-color: rgba(0, 0, 0, 0.8);
|
||
text-align: center;
|
||
padding-top: 50px;
|
||
}
|
||
#lightbox img {
|
||
max-width: 80%;
|
||
max-height: 80%;
|
||
margin: auto;
|
||
display: block;
|
||
}
|
||
#lightbox .close {
|
||
position: absolute;
|
||
top: 20px;
|
||
right: 30px;
|
||
color: #fff;
|
||
font-size: 35px;
|
||
font-weight: bold;
|
||
cursor: pointer;
|
||
}
|
||
.custom-button {
|
||
display: inline-block;
|
||
padding: 10px 20px;
|
||
background-color: #bb7e19;
|
||
color: white;
|
||
text-decoration: none;
|
||
border-radius: 5px;
|
||
font-family: Roboto, sans-serif;
|
||
font-size: 1em;
|
||
margin-right: 10px;
|
||
margin-bottom: 10px;
|
||
border: none;
|
||
cursor: pointer;
|
||
text-align: center;
|
||
}
|
||
.custom-button:hover {
|
||
background-color: #a06f15;
|
||
}
|
||
</style>
|
||
<script>
|
||
function openLightbox(src) {
|
||
const lightbox = document.getElementById('lightbox');
|
||
const lightboxImg = document.getElementById('lightbox-img');
|
||
lightboxImg.src = src;
|
||
lightbox.style.display = 'block';
|
||
}
|
||
function closeLightbox() {
|
||
document.getElementById('lightbox').style.display = 'none';
|
||
}
|
||
</script>
|
||
</head>
|
||
<body>
|
||
<div class="form-container">
|
||
<a href="./index.php" class="custom-button">Zurück zur Übersicht</a>
|
||
<a href="main.php?id=<?= htmlspecialchars($id) ?>" class="custom-button">Rezept anzeigen</a>
|
||
<h1><?= $id > 0 ? 'Rezept bearbeiten' : 'Neues Rezept' ?></h1>
|
||
<form method="post" action="Edit.php?id=<?= $id ?>" enctype="multipart/form-data">
|
||
<input type="hidden" name="id" value="<?= $id ?>">
|
||
<table>
|
||
<tr>
|
||
<th>Rezeptnummer:</th>
|
||
<td>
|
||
<input type="text" name="Rezeptnummer" value="<?= isset($row['Rezeptnummer']) ? htmlspecialchars($row['Rezeptnummer']) : '' ?>" required>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th>Bezeichnung:</th>
|
||
<td><input type="text" name="Bezeichnung" value="<?= isset($row['Bezeichnung']) ? htmlspecialchars($row['Bezeichnung']) : '' ?>"></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Beschreibung:</th>
|
||
<td><textarea name="Beschreibung"><?= isset($row['Beschreibung']) ? htmlspecialchars($row['Beschreibung']) : '' ?></textarea></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Kategorie:</th>
|
||
<td><input type="text" name="Kategorie" value="<?= isset($row['Kategorie']) ? htmlspecialchars($row['Kategorie']) : '' ?>"></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Vorbereitung:</th>
|
||
<td><textarea name="Vorbereitung"><?= isset($row['Vorbereitung']) ? htmlspecialchars($row['Vorbereitung']) : '' ?></textarea></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Anzahl der Personen:</th>
|
||
<td>
|
||
<input type="number" name="Anzahl" style="font-size: 18px;" value="<?= isset($row['Anzahl']) ? htmlspecialchars($row['Anzahl']) : '2' ?>" min="0" required>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th>Zutaten:</th>
|
||
<td><textarea name="Zutaten"><?= !empty($zutaten_ingr) ? htmlspecialchars($zutaten_ingr) : (isset($row['Zutaten']) ? htmlspecialchars($row['Zutaten']) : '') ?></textarea></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Zubereitung:</th>
|
||
<td><textarea name="Zubereitung"><?= isset($zubereitung_text) ? htmlspecialchars($zubereitung_text) : '' ?></textarea></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Kommentar:</th>
|
||
<td><textarea name="Kommentar"><?= isset($row['Kommentar']) ? htmlspecialchars($row['Kommentar']) : '' ?></textarea></td>
|
||
</tr>
|
||
<tr>
|
||
<th>Bilder hochladen:</th>
|
||
<td>
|
||
<div class="file-upload-row">
|
||
<div class="file-upload-container">
|
||
<button type="button" class="file-upload-btn">Bild auswählen</button>
|
||
<input type="file" name="single_bild" accept=".jpg,.jpeg,.png">
|
||
</div>
|
||
<button type="submit" name="save_single_image" class="save-single-image-btn">Bild speichern</button>
|
||
</div>
|
||
<?php if (!empty($bilder)): ?>
|
||
<div class="bilder-container">
|
||
<?php foreach ($bilder as $bild): ?>
|
||
<div class="bild-vorschau">
|
||
<img src="<?= htmlspecialchars($bild['datei_pfad']) ?>" alt="Bildvorschau" onclick="openLightbox('<?= htmlspecialchars($bild['datei_pfad']) ?>')">
|
||
<a href="Edit.php?id=<?= $id ?>&delete_bild=<?= $bild['id'] ?>" class="bild-loeschen" onclick="return confirm('Möchtest du dieses Bild wirklich löschen?')">×</a>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
<div class="button-container">
|
||
<button type="submit" name="save" class="submit-btn">Speichern</button>
|
||
<?php if ($id > 0): ?>
|
||
<button type="submit" name="delete" class="delete-btn" onclick="return confirm('Möchtest du dieses Rezept wirklich löschen?')">Rezept löschen</button>
|
||
<?php endif; ?>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<!-- Lightbox für Bildvergrößerung -->
|
||
<div id="lightbox" onclick="closeLightbox()">
|
||
<span class="close" onclick="closeLightbox()">×</span>
|
||
<img id="lightbox-img" src="" alt="Vergrößerte Ansicht">
|
||
</div>
|
||
</body>
|
||
</html>
|