244 lines
8.0 KiB
PHP
244 lines
8.0 KiB
PHP
<?php
|
|
function get_kat_price_diff(array $old_data, string $new_kat): array {
|
|
global $preise_kats;
|
|
global $vereine_plus_array;
|
|
|
|
$return_array = [];
|
|
|
|
$old_kat = $old_data['programm'] ?? '';
|
|
$verein = $old_data['verein'] ?? '';
|
|
$bezahlt = (int) ($old_data['bezahlt'] ?? 1);
|
|
|
|
if ($old_kat === '' || $old_kat === $new_kat || !in_array($bezahlt, [2, 4], true) || !isset($preise_kats[$new_kat])) {
|
|
return $return_array;
|
|
}
|
|
|
|
$betrag_bezahlt = (float) ($old_data['betrag_bezahlt'] ?? 0.00);
|
|
$new_kat_preis = (float) $preise_kats[$new_kat];
|
|
|
|
if ($new_kat_preis > $betrag_bezahlt) {
|
|
$return_array['bezahlt'] = ($betrag_bezahlt === 0.00) ? 1 : 2;
|
|
} elseif ($new_kat_preis < $betrag_bezahlt) {
|
|
$return_array['bezahlt'] = 4;
|
|
$vereine_plus_array[$verein] = ((float) ($vereine_plus_array[$verein] ?? 0.00)) + $betrag_bezahlt - $new_kat_preis;
|
|
} else {
|
|
$return_array['bezahlt'] = 4;
|
|
}
|
|
|
|
return $return_array;
|
|
}
|
|
|
|
function update_verein_balances(array $vereine_plus_array_funct) {
|
|
global $mysqli;
|
|
global $db_tabelle_vereine;
|
|
|
|
if (empty($vereine_plus_array_funct)) {
|
|
return;
|
|
}
|
|
|
|
$stmt = $mysqli->prepare("UPDATE $db_tabelle_vereine SET `konto` = `konto` + ? WHERE `verein` = ?");
|
|
|
|
if ($stmt) {
|
|
foreach ($vereine_plus_array_funct as $verein => $betrag) {
|
|
if ($betrag <= 0.00) {
|
|
continue;
|
|
}
|
|
|
|
$stmt->bind_param("ds", $betrag, $verein);
|
|
$stmt->execute();
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
|
|
function remove_from_basket(array $ids_array = []) : bool {
|
|
global $mysqli;
|
|
global $db_tabelle_warenkorb_startgebueren;
|
|
|
|
$tplaceholders = implode(", ", array_fill(0, count($ids_array), "?"));
|
|
|
|
$stmt = $mysqli->prepare("DELETE FROM $db_tabelle_warenkorb_startgebueren WHERE `item_id` IN ($tplaceholders)");
|
|
$stmt->bind_param(str_repeat('i', count($ids_array)), ...$ids_array);
|
|
$ret = $stmt->execute();
|
|
$stmt->close();
|
|
|
|
return $ret;
|
|
}
|
|
|
|
$vereine_plus_array = [];
|
|
|
|
if (isset($_POST['delete_id'])) {
|
|
verify_csrf();
|
|
$delete_id = intval($_POST['delete_id']);
|
|
|
|
$stmt = $mysqli->prepare("DELETE FROM $db_tabelle_teilnehmendee 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, $db_tabelle_teilnehmende, "*", '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 || $is_admin)) {
|
|
$_POST['nachname'] = $edit_row['name'] ?? '';
|
|
$_POST['vorname'] = $edit_row['vorname'] ?? '';
|
|
$_POST['geburtsdatum'] = $edit_row['geburtsdatum'] ?? '';
|
|
$kat = $edit_row['programm'] ?? '';
|
|
$kat_id = db_get_var($mysqli, "SELECT `id` FROM $db_tabelle_kategorien WHERE `programm` = ?", [$kat]) ?: '';
|
|
$_POST['programm'] = $kat_id;
|
|
$_POST['edit_id'] = $edit_id;
|
|
if ($is_admin) {
|
|
$_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']);
|
|
$kat_id = (int) ($_POST['programm'] ?? 0);
|
|
|
|
if (!$is_admin) {
|
|
$verein = $selectedverein;
|
|
} else {
|
|
$verein = htmlspecialchars($_POST['verein']);
|
|
$bezahlt = intval($_POST['bezahltOverride']);
|
|
}
|
|
|
|
$kat = db_get_var($mysqli, "SELECT `programm` FROM $db_tabelle_kategorien WHERE `id` = ?", [$kat_id]) ?: '';
|
|
|
|
if (empty($name) || empty($vorname) || empty($geburtsdatum) || empty($kat_id) || empty($kat)) {
|
|
$_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' => $kat,
|
|
'verein' => $verein,
|
|
];
|
|
|
|
if ($is_admin) {
|
|
$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']);
|
|
|
|
$entry = db_select($mysqli, $db_tabelle_teilnehmende, '*', 'id = ?', [$edit_id], '', '1');
|
|
|
|
if (count($entry) === 1) {
|
|
$old_data = $entry[0];
|
|
$data_to_insert = array_merge($data_to_insert, get_kat_price_diff($old_data, $kat));
|
|
remove_from_basket([$edit_id]);
|
|
update_verein_balances($vereine_plus_array);
|
|
}
|
|
|
|
var_dump($data_to_insert);
|
|
|
|
$columns = array_keys($data_to_insert);
|
|
|
|
$set = implode(
|
|
', ',
|
|
array_map(fn($col) => "$col = ?", $columns)
|
|
);
|
|
|
|
$sql = "UPDATE $db_tabelle_teilnehmende 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 $db_tabelle_teilnehmende 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;
|
|
}
|
|
}
|
|
}
|
|
} |