FSCash/html/edit_entry.php

232 lines
7.4 KiB
PHP

<?php
require 'config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
// Prüfen, ob die ID im GET-Parameter vorhanden ist
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header('Location: dashboard.php');
exit;
}
$entry_id = (int)$_GET['id'];
$entry = null;
$data_saved = "nix";
$loggedInUserId = "nix";
$debug = "";
$users = [];
$categories = [];
$pay_methods = [];
// Daten des Eintrags laden
try {
$stmtEntry = $pdo->prepare("
SELECT * FROM entries
WHERE id = :id
");
$stmtEntry->bindParam(':id', $entry_id, PDO::PARAM_INT);
$stmtEntry->execute();
$entry = $stmtEntry->fetch(PDO::FETCH_ASSOC);
if (!$entry) {
header('Location: dashboard.php');
exit;
}
} catch (PDOException $e) {
$data_saved = "Fehler: Eintrag konnte nicht geladen werden.";
}
// Lösch-Logik
if (isset($_POST['delete'])) {
try {
$stmt = $pdo->prepare("DELETE FROM entries WHERE id = :id");
$stmt->bindParam(':id', $entry_id, PDO::PARAM_INT);
$stmt->execute();
echo '<meta http-equiv="refresh" content="5;url=dashboard.php">Eintrag wurde gelöscht. Weiterleitung…'; exit;
// header('Location: dashboard.php');
exit;
} catch (PDOException $e) {
$data_saved = "Fehler: " . $e->getMessage();
}
}
// Speicher-Logik
if (!empty($_POST) && !isset($_POST['delete'])) {
$date = isset($_POST['date']) ? trim($_POST['date']) : '';
$user_id = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
$person = isset($_POST['person']) ? $_POST['person'] : [];
$category = isset($_POST['category']) ? trim($_POST['category']) : '';
$subject = isset($_POST['subject']) ? trim($_POST['subject']) : '';
$note = isset($_POST['note']) ? trim($_POST['note']) : '';
$amount = isset($_POST['amount']) ? (float)$_POST['amount'] : 0.0;
$payment_method = isset($_POST['payment_method']) ? trim($_POST['payment_method']) : '';
$jsonPersonen = json_encode($person);
try {
$stmt = $pdo->prepare("
UPDATE entries
SET
date = :date,
user_id = :user_id,
person = :person,
category = :category,
subject = :subject,
amount = :amount,
payment_method = :payment_method,
note = :note
WHERE id = :id
");
$stmt->bindParam(':id', $entry_id, PDO::PARAM_INT);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':person', $jsonPersonen);
$stmt->bindParam(':category', $category);
$stmt->bindParam(':subject', $subject);
$stmt->bindParam(':amount', $amount);
$stmt->bindParam(':payment_method', $payment_method);
$stmt->bindParam(':note', $note);
$stmt->execute();
$data_saved = "true";
} catch (PDOException $e) {
$data_saved = "Fehler: " . $e->getMessage();
}
}
// Benutzer, Kategorien und Zahlweisen laden
try {
$stmtUsers = $pdo->prepare("SELECT * FROM users ORDER BY id;");
$stmtUsers->execute();
$users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $user) {
if ($user['id'] == $_SESSION['user_id']) {
$loggedInUserId = $user['id'];
break;
}
}
$stmtCategories = $pdo->prepare("SELECT * FROM categories ORDER BY category_id;");
$stmtCategories->execute();
$categories = $stmtCategories->fetchAll(PDO::FETCH_ASSOC);
$stmtPayMethods = $pdo->prepare("SELECT payment_method_id, name FROM payment_methods ORDER BY name;");
$stmtPayMethods->execute();
$pay_methods = $stmtPayMethods->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$data_saved = "Fehler: ";
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=0.8">
<title>FSCash - Eintrag bearbeiten</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<?php echo "<!-- data_saved:" . $data_saved . " -->"; ?>
<?php if ($data_saved == "true"): ?>
<div id="data-notice" class="data-notice">
Daten gespeichert !
<a href="dashboard.php">Zurück zur Übersicht</a>
<?php elseif ($data_saved != "nix"): ?>
<div id="data-notice" class="data-notice error">
<?php echo $data_saved; ?>
<?php endif; ?>
</div>
<div class="container">
<!-- Navigation -->
<nav class="nav">
<button class="hamburger">☰</button>
<ul class="nav-list">
<li><a href="dashboard.php">Start</a></li>
<?php if ($is_admin === "true") echo '<li><a href="register.php">Neue User</a></li>'; ?>
<li><a href="rep-month.php">Report Monat</a></li>
<li><a href="rep-person.php">Report Personen</a></li>
<li><a href="menu5.php" style="display: none;">Menüpunkt 5</a></li>
</ul>
<a href="logout.php" class="logout">Logout</a>
</nav>
<form action="edit_entry.php?id=<?= $entry_id ?>" method="post" class="form-container">
<div class="form-group">
<input type="date" name="date" value="<?= htmlspecialchars($entry['date']) ?>" required>
</div>
<div class="form-group">
<select name="user_id" id="user_id">
<?php foreach ($users as $user): ?>
<option value="<?= htmlspecialchars($user['id']) ?>"
<?php if ($user['id'] == $entry['user_id']): ?> selected <?php endif; ?>
>
<?= htmlspecialchars($user['username']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Person (Multiselect) -->
<div class="form-group">
<label for="person">für wen</label>
<select name="person[]" id="person" multiple required size="4">
<?php
$entry_person = json_decode($entry['person'], true) ?? [];
foreach ($users as $user):
?>
<option value="<?= htmlspecialchars($user['id']) ?>"
<?php if (in_array($user['id'], $entry_person)): ?> selected <?php endif; ?>
>
<?= htmlspecialchars($user['username']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Category -->
<div class="form-group">
<select name="category" id="category" required>
<?php foreach ($categories as $category): ?>
<option value="<?= htmlspecialchars($category['category_id']) ?>"
<?php if ($category['category_id'] == $entry['category']): ?> selected <?php endif; ?>
>
<?= htmlspecialchars($category['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<input type="text" name="subject" value="<?= htmlspecialchars($entry['subject']) ?>" placeholder="Betreff" required>
</div>
<div class="form-group">
<input type="number" name="amount" step="0.01" value="<?= htmlspecialchars($entry['amount']) ?>" placeholder="Betrag" required>
</div>
<!-- Zahlweise -->
<div class="form-group">
<select name="payment_method" id="payment_method" required>
<?php foreach ($pay_methods as $method): ?>
<option value="<?= htmlspecialchars($method['payment_method_id']) ?>"
<?php if ($method['payment_method_id'] == $entry['payment_method']): ?> selected <?php endif; ?>
>
<?= htmlspecialchars($method['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Hinweis -->
<div class="form-group">
<input type="text" name="note" value="<?= htmlspecialchars($entry['note']) ?>" placeholder="Hinweis">
</div>
<div class="button-group">
<button type="submit" name="delete" class="btn-xr1l" style="background-color: #d9534f;">Löschen</button>
<button type="submit" class="btn-xr">Speichern</button>
</div>
</form>
</div>
<script src="assets/js/script.js"></script>
</body>
</html>