Files
WKVS/scripts/trainer/post-handler.php
T

251 lines
8.5 KiB
PHP

<?php
if (isset($_POST['apply_bulk_action'])) {
verify_csrf();
if (empty($_POST['turnerin_ids']) || !is_array($_POST['turnerin_ids'])) {
$_SESSION['form_message'] = 'Keine ' . $personMehrzahl . ' für die Aktion ausgewählt.';
$_SESSION['form_message_type'] = 0;
} elseif (!isset($_POST['bulk_action_programm']) && !isset($_POST['bulk_action_bezahlt'])) {
$_SESSION['form_message'] = 'Kein Programm für die Massenänderung ausgewählt.';
$_SESSION['form_message_type'] = 0;
} else {
$ids_to_update = array_map('intval', $_POST['turnerin_ids'] ?? []);
$new_programm = isset($_POST['bulk_action_programm']) ? trim($_POST['bulk_action_programm']) : '';
$bezahlt_update = $_POST['bulk_action_bezahlt'] ?? null;
if (empty($ids_to_update)) {
$_SESSION['form_message'] = 'Keine Einträge ausgewählt.';
$_SESSION['form_message_type'] = 0;
header("Location: " . $_SERVER['REQUEST_URI']);
exit;
}
$set_clauses = [];
$params = [];
$types = '';
if ($new_programm !== '') {
$set_clauses[] = 'programm = ?';
$params[] = $new_programm;
$types .= 's';
}
if (in_array($bezahlt_update, ['0', '3', '4', '5'], true)) {
$set_clauses[] = 'bezahltoverride = ?';
$params[] = (int) $bezahlt_update;
$types .= 'i';
}
if (empty($set_clauses)) {
$_SESSION['form_message'] = 'Keine gültigen Änderungen gewählt.';
$_SESSION['form_message_type'] = 0;
header("Location: " . $_SERVER['REQUEST_URI']);
exit;
}
if (strlen($types) !== count($params) || count($params) !== count($set_clauses)) {
die('Type/value mismatch: ' . strlen($types) . ' vs ' . count($params));
}
/* WHERE id IN (?, ?, ...) */
$placeholders = implode(',', array_fill(0, count($ids_to_update), '?'));
$sql = "UPDATE $tableTeilnehmende SET " . implode(', ', $set_clauses) . " WHERE id IN ($placeholders)";
$stmt = $mysqli->prepare($sql);
/* add ID params */
foreach ($ids_to_update as $id) {
$params[] = $id;
$types .= 'i';
}
$stmt->bind_param($types, ...$params);
if (!$stmt->execute()) {
throw new RuntimeException('DB error: ' . $stmt->error);
}
$updated_count = $stmt->affected_rows;
$stmt->close();
if ($updated_count === -1) {
$_SESSION['form_message'] = 'Ein Fehler ist bei der Aktualisierung aufgetreten.';
$_SESSION['form_message_type'] = 0;
} elseif ($updated_count > 0) {
$_SESSION['form_message'] = $updated_count . ' Einträge erfolgreich aktualisiert.';
$_SESSION['form_message_type'] = 1;
} else {
$_SESSION['form_message'] = 'Keine Änderungen vorgenommen.';
$_SESSION['form_message_type'] = 0;
}
}
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
if (isset($_POST['delete_id'])) {
verify_csrf();
$delete_id = intval($_POST['delete_id']);
$stmt = $mysqli->prepare("DELETE FROM $tableTeilnehmendee where id = ?");
$stmt->bind_param('i', $delete_id);
if ($stmt->execute()) {
$_SESSION['form_message'] = 'Eintrag erfolgreich gelöscht.';
$_SESSION['form_message_type'] = 1;
} else {
$_SESSION['form_message'] = 'Löschen fehlgeschlagen.';
$_SESSION['form_message_type'] = 0;
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit;
}
$edit_row = null;
if ($access_granted_trainer && isset($_GET['edit_id']) && is_numeric($_GET['edit_id']) && !isset($_POST['submit_turnerinnen_form'])) {
$edit_id = intval($_GET['edit_id']);
$edit_rows = db_select($mysqli, $tableTeilnehmende, "*", 'id = ?', [$edit_id]);
if (!isset($edit_rows) || !is_array($edit_rows) || count($edit_rows) !== 1) {
http_response_code(422);
exit;
}
$edit_row = $edit_rows[0];
if ($edit_row && ($edit_row['verein'] === $selectedverein || $isAdmin)) {
$_POST['nachname'] = $edit_row['name'] ?? '';
$_POST['vorname'] = $edit_row['vorname'] ?? '';
$_POST['geburtsdatum'] = $edit_row['geburtsdatum'] ?? '';
$_POST['programm'] = $edit_row['programm'] ?? '';
$_POST['edit_id'] = $edit_id;
if ($isAdmin) {
$_POST['verein'] = $edit_row['verein'] ?? '';
$_POST['bezahltOverride'] = $edit_row['bezahltoverride'] ?? 0;
}
} else {
$_SESSION['form_message'] = 'Ungültiger Eintrag zum Bearbeiten.';
$_SESSION['form_message_type'] = 0;
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
}
// === INSERT/UPDATE Handler ===
if ($access_granted_trainer && isset($_POST['submit_turnerinnen_form'])) {
verify_csrf();
$name = htmlspecialchars($_POST['nachname']);
$vorname = htmlspecialchars($_POST['vorname']);
$geburtsdatum = trim($_POST['geburtsdatum']);
$programm = htmlspecialchars($_POST['programm']);
if (!$isAdmin) {
$verein = $selectedverein;
} else {
$verein = htmlspecialchars($_POST['verein']);
$bezahlt = intval($_POST['bezahltOverride']);
}
if (empty($name) || empty($vorname) || empty($geburtsdatum) || empty($programm)) {
$_SESSION['form_message'] = 'Bitte füllen Sie alle erforderlichen Felder aus.';
$_SESSION['form_message_type'] = 0;
} else {
$data_to_insert = [
'name' => $name,
'vorname' => $vorname,
'geburtsdatum' => $geburtsdatum,
'programm' => $programm,
'verein' => $verein,
];
if ($isAdmin) {
$data_to_insert['bezahltoverride'] = $bezahlt;
}
// Check if we are editing an existing entry
$is_editing = isset($_POST['edit_id']) && is_numeric($_POST['edit_id']) && $_POST['edit_id'] > 0;
if ($is_editing) {
$edit_id = intval($_POST['edit_id']);
$entries = db_select($mysqli, $tableTeilnehmende, '*', 'id = ?', [$edit_id], 'rang ASC');
$entry = $entries[0]; // since you're fetching by ID, this should return exactly one row
$columns = array_keys($data_to_insert);
$set = implode(
', ',
array_map(fn($col) => "$col = ?", $columns)
);
$sql = "UPDATE $tableTeilnehmende SET $set WHERE id = ?";
var_dump($sql);
$stmt = $mysqli->prepare($sql);
$types = str_repeat('s', count($data_to_insert)) . 'i';
$values = array_values($data_to_insert);
$values[] = $edit_id;
$stmt->bind_param($types, ...$values);
$updated = $stmt->execute();
$stmt->close();
if ($updated === false) {
error_log('DB Update Error: ' . $wpdb->last_error);
$_SESSION['form_message'] = 'Fehler beim Aktualisieren der Personendaten.';
$_SESSION['form_message_type'] = 0;
} else if ($updated === 0) {
$_SESSION['form_message'] = 'Keine Änderungen vorgenommen.';
$_SESSION['form_message_type'] = 0;
} else {
$_SESSION['form_message'] = $personEinzahl . ' erfolgreich aktualisiert!';
$_SESSION['form_message_type'] = 1;
$_POST = [];
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?'));
exit;
}
} else {
$columns = array_keys($data_to_insert);
$set = implode(
', ',
array_map(fn($col) => "$col = ?", $columns)
);
$sql = "INSERT INTO $tableTeilnehmende SET $set";
$stmt = $mysqli->prepare($sql);
$types = str_repeat('s', count($data_to_insert));
$values = array_values($data_to_insert);
$stmt->bind_param($types, ...$values);
$inserted = $stmt->execute();
$stmt->close();
if ($inserted) {
$_SESSION['form_message'] = 'Daten erfolgreich gespeichert!';
$_SESSION['form_message_type'] = 1;
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?'));
exit;
} else {
$_SESSION['form_message'] = 'Fehler beim Speichern der Daten. Bitte versuchen Sie es später erneut.';
$_SESSION['form_message_type'] = 0;
}
}
}
}