This commit is contained in:
Borgal
2025-11-16 21:13:04 +01:00
parent aeb2d87cf5
commit 1b9ba22bb5
11 changed files with 1104 additions and 234 deletions

View File

@@ -5,10 +5,10 @@ require_once 'inc/db.php';
$message = '';
$message_type = '';
$user_id = (int)$_SESSION['user_id']; // Sicherheitshalber als Integer
$user_id = (int)$_SESSION['user_id'];
// Aktuelle Benutzerdaten laden
$stmt_fetch = mysqli_prepare($conn, "SELECT username, email, role, birthday FROM users WHERE id = ?");
$stmt_fetch = mysqli_prepare($conn, "SELECT username, email, role, birthday, last_birthday_year FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt_fetch, "i", $user_id);
mysqli_stmt_execute($stmt_fetch);
$result = mysqli_stmt_get_result($stmt_fetch);
@@ -20,9 +20,10 @@ if (!$user_data) {
}
$current_username = $user_data['username'];
$current_email = $user_data['email'];
$current_email = $user_data['email'] ?? '';
$current_role = $user_data['role'];
$current_birthday = $user_data['birthday'] ?? '';
$current_last_bday_year = $user_data['last_birthday_year'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$new_username = trim($_POST['username'] ?? '');
@@ -37,35 +38,85 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
$message = "Ungültige E-Mail-Adresse.";
$message_type = 'danger';
} else {
// Standardwerte
$db_email = (!empty($new_email)) ? $new_email : null;
$db_birthday = (!empty($new_birthday)) ? $new_birthday : null;
// 🔹 GEBURTSTAGSLOGIK: Nur wenn Geburtstag neu/aktualisiert wird
$update_last_bday_year = false;
$new_last_bday_year = null;
if ($db_birthday !== null) {
$today = date('Y-m-d');
$current_year = (int)date('Y');
$birth_month = (int)date('m', strtotime($db_birthday));
$birth_day = (int)date('d', strtotime($db_birthday));
$birthday_this_year = "$current_year-$birth_month-$birth_day";
if (strtotime($birthday_this_year) < strtotime($today)) {
$new_last_bday_year = $current_year;
$update_last_bday_year = true;
}
}
// 🔹 Update in DB
mysqli_autocommit($conn, false);
$success = true;
// 1. Benutzerdaten aktualisieren
$stmt = mysqli_prepare($conn, "UPDATE users SET username = ?, email = ?, birthday = ? WHERE id = ?");
if ($stmt) {
$db_email = (!empty($new_email)) ? $new_email : null;
$db_birthday = (!empty($new_birthday)) ? $new_birthday : null;
mysqli_stmt_bind_param($stmt, "sssi", $new_username, $db_email, $db_birthday, $user_id);
if (mysqli_stmt_execute($stmt)) {
$_SESSION['username'] = $new_username;
$_SESSION['email'] = $new_email;
$result_reload = mysqli_query($conn, "SELECT username, email, role, birthday FROM users WHERE id = " . (int)$user_id);
if ($result_reload) {
$user_data = mysqli_fetch_assoc($result_reload);
$current_username = $user_data['username'];
$current_email = $user_data['email'];
$current_role = $user_data['role'];
$current_birthday = $user_data['birthday'] ?? '';
}
$message = "Profil erfolgreich aktualisiert!";
$message_type = 'success';
} else {
$message = "Fehler beim Speichern der Daten.";
$message_type = 'danger';
if (!mysqli_stmt_execute($stmt)) {
$success = false;
}
mysqli_stmt_close($stmt);
} else {
$message = "Datenbankfehler: Statement konnte nicht vorbereitet werden.";
$success = false;
}
// 2. Optional: last_birthday_year aktualisieren
if ($success && $update_last_bday_year) {
$stmt2 = mysqli_prepare($conn, "UPDATE users SET last_birthday_year = ? WHERE id = ?");
if ($stmt2) {
mysqli_stmt_bind_param($stmt2, "ii", $new_last_bday_year, $user_id);
if (!mysqli_stmt_execute($stmt2)) {
$success = false;
}
mysqli_stmt_close($stmt2);
} else {
$success = false;
}
}
if ($success) {
mysqli_commit($conn);
$_SESSION['username'] = $new_username;
$_SESSION['email'] = $new_email;
// Neu laden
$stmt_reload = mysqli_prepare($conn, "SELECT username, email, role, birthday, last_birthday_year FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt_reload, "i", $user_id);
mysqli_stmt_execute($stmt_reload);
$user_data = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt_reload));
mysqli_stmt_close($stmt_reload);
$current_username = $user_data['username'];
$current_email = $user_data['email'] ?? '';
$current_role = $user_data['role'];
$current_birthday = $user_data['birthday'] ?? '';
$current_last_bday_year = $user_data['last_birthday_year'];
$message = "Profil erfolgreich aktualisiert!";
$message_type = 'success';
} else {
mysqli_rollback($conn);
$message = "Fehler beim Speichern der Daten.";
$message_type = 'danger';
}
mysqli_autocommit($conn, true);
}
}
}
@@ -96,12 +147,20 @@ require_once 'inc/header.php';
</div>
<div class="mb-3">
<label for="email" class="form-label fw-bold">E-Mail-Adresse</label>
<input type="email" class="form-control" id="email" name="email" value="<?= htmlspecialchars($current_email) ?>">
<input type="email" class="form-control" id="email" name="email" value="<?= htmlspecialchars($current_email ?? '') ?>">
</div>
<div class="mb-3">
<label for="birthday" class="form-label fw-bold">Geburtstag</label>
<input type="date" class="form-control" id="birthday" name="birthday" value="<?= htmlspecialchars($current_birthday) ?>">
<small class="form-text text-muted">Für automatische Sonderzahlung.</small>
<input type="date" class="form-control" id="birthday" name="birthday" value="<?= htmlspecialchars($current_birthday ?? '') ?>">
<small class="form-text text-muted">
<?php if (!empty($current_birthday) && $current_last_bday_year == date('Y')): ?>
<span class="text-success">✓ In diesem Jahr bereits als Geburtstagszahler markiert.</span>
<?php elseif (!empty($current_birthday)): ?>
Geburtstag steht noch an du kannst als Sonderzahler vorgeschlagen werden.
<?php else: ?>
Für automatische Sonderzahlung.
<?php endif; ?>
</small>
</div>
<div class="mb-3">
<label for="role" class="form-label fw-bold">Rolle</label>