Anhänger anlegen, aktivieren und löschen

This commit is contained in:
OpenCode 2026-11-13 01:19:05 +01:00
parent b50267e4f0
commit 5ecbd29d21
15 changed files with 265 additions and 55 deletions

View file

@ -332,55 +332,144 @@ function admin_employees(): void
function admin_trailers(): void
{
$errors = [];
$createErrors = [];
$nextSort = (int) db()->query('SELECT COALESCE(MAX(sort_order), 0) + 10 FROM trailers')->fetchColumn();
$createValues = [
'label' => '',
'color_name' => '',
'color_hex' => '#196abc',
'sort_order' => $nextSort,
'active' => 1,
];
if (is_post()) {
require_post();
$labels = post_array('label');
$colorNames = post_array('color_name');
$colors = post_array('color_hex');
$sorts = post_array('sort_order');
$action = post_string('action', 'update');
$existing = trailers_map();
$seen = [];
foreach ($existing as $id => $trailer) {
$label = trim((string) ($labels[$id] ?? ''));
$colorName = trim((string) ($colorNames[$id] ?? ''));
$color = strtolower(trim((string) ($colors[$id] ?? '')));
$sort = (int) ($sorts[$id] ?? $trailer['sort_order']);
if ($label === '' || mb_strlen($label) > 40) {
$errors['trailer_' . $id] = 'Bitte eine Bezeichnung angeben (max. 40 Zeichen).';
continue;
if ($action === 'create') {
$createValues = [
'label' => post_string('label'),
'color_name' => post_string('color_name'),
'color_hex' => strtolower(post_string('color_hex')),
'sort_order' => (int) post_string('sort_order'),
'active' => post_string('active') === '1' ? 1 : 0,
];
if ($createValues['label'] === '' || mb_strlen($createValues['label']) > 40) {
$createErrors['label'] = 'Bitte eine Bezeichnung angeben (max. 40 Zeichen).';
}
if (isset($seen[mb_strtolower($label)])) {
$errors['trailer_' . $id] = 'Die Bezeichnung muss eindeutig sein.';
continue;
if ($createValues['color_name'] === '' || mb_strlen($createValues['color_name']) > 40) {
$createErrors['color_name'] = 'Bitte einen Farbnamen angeben.';
}
$seen[mb_strtolower($label)] = true;
if ($colorName === '' || mb_strlen($colorName) > 40) {
$errors['trailer_' . $id] = 'Bitte einen Farbnamen angeben.';
continue;
if (!preg_match('/^#[0-9a-f]{6}$/', $createValues['color_hex'])) {
$createErrors['color_hex'] = 'Bitte eine gültige Farbe (#rrggbb) wählen.';
}
if (!preg_match('/^#[0-9a-f]{6}$/', $color)) {
$errors['trailer_' . $id] = 'Bitte eine gültige Farbe (#rrggbb) wählen.';
continue;
if ($createValues['label'] !== '') {
$stmt = db()->prepare('SELECT id FROM trailers WHERE label = ? LIMIT 1');
$stmt->execute([$createValues['label']]);
if ($stmt->fetchColumn()) {
$createErrors['label'] = 'Diese Bezeichnung ist bereits vergeben.';
}
}
db()->prepare('UPDATE trailers SET label = ?, color_name = ?, color_hex = ?, sort_order = ? WHERE id = ?')
->execute([$label, $colorName, $color, $sort, $id]);
}
if (!$errors) {
flash('success', 'Anhänger wurden gespeichert.');
if (!$createErrors) {
db()->prepare('INSERT INTO trailers (label, color_name, color_hex, sort_order, active) VALUES (?, ?, ?, ?, ?)')
->execute([
$createValues['label'], $createValues['color_name'], $createValues['color_hex'],
$createValues['sort_order'], $createValues['active'],
]);
flash('success', 'Anhänger ' . $createValues['label'] . ' wurde angelegt.');
redirect('/admin/trailers');
}
http_response_code(422);
} elseif ($action === 'delete') {
$id = (int) post_string('id');
$trailer = trailer_find($id);
if (!$trailer) {
abort_page(404, 'Anhänger nicht gefunden.');
}
$stmt = db()->prepare('SELECT COUNT(*) FROM bookings WHERE trailer_id = ?');
$stmt->execute([$id]);
if ((int) $stmt->fetchColumn() > 0) {
flash('error', 'Anhänger ' . $trailer['label'] . ' kann nicht gelöscht werden, weil Buchungen darauf verweisen. Der Anhänger kann stattdessen deaktiviert werden.');
redirect('/admin/trailers');
}
if ((int) $trailer['active'] === 1
&& (int) db()->query('SELECT COUNT(*) FROM trailers WHERE active = 1')->fetchColumn() <= 1
) {
flash('error', 'Mindestens ein Anhänger muss aktiv bleiben, damit neue Buchungen möglich sind.');
redirect('/admin/trailers');
}
db()->prepare('DELETE FROM trailers WHERE id = ?')->execute([$id]);
flash('success', 'Anhänger ' . $trailer['label'] . ' wurde gelöscht.');
redirect('/admin/trailers');
} else {
$labels = post_array('label');
$colorNames = post_array('color_name');
$colors = post_array('color_hex');
$sorts = post_array('sort_order');
$actives = post_array('active');
$existing = trailers_all(false);
$seen = [];
$updates = [];
foreach ($existing as $trailer) {
$id = (int) $trailer['id'];
$label = trim((string) ($labels[$id] ?? $trailer['label']));
$colorName = trim((string) ($colorNames[$id] ?? $trailer['color_name']));
$color = strtolower(trim((string) ($colors[$id] ?? $trailer['color_hex'])));
$sort = (int) ($sorts[$id] ?? $trailer['sort_order']);
$active = ((string) ($actives[$id] ?? '0')) === '1' ? 1 : 0;
if ($label === '' || mb_strlen($label) > 40) {
$errors['trailer_' . $id] = 'Bitte eine Bezeichnung angeben (max. 40 Zeichen).';
} elseif (isset($seen[mb_strtolower($label)])) {
$errors['trailer_' . $id] = 'Die Bezeichnung muss eindeutig sein.';
}
$seen[mb_strtolower($label)] = true;
if ($colorName === '' || mb_strlen($colorName) > 40) {
$errors['trailer_' . $id] = $errors['trailer_' . $id] ?? 'Bitte einen Farbnamen angeben.';
}
if (!preg_match('/^#[0-9a-f]{6}$/', $color)) {
$errors['trailer_' . $id] = $errors['trailer_' . $id] ?? 'Bitte eine gültige Farbe (#rrggbb) wählen.';
}
if ((int) $trailer['active'] === 1 && $active === 0) {
$stmt = db()->prepare("SELECT COUNT(*) FROM bookings WHERE trailer_id = ? AND status = 'active' AND end_date >= ?");
$stmt->execute([$id, today_iso()]);
if ((int) $stmt->fetchColumn() > 0) {
$errors['trailer_' . $id] = 'Der Anhänger hat noch aktuelle oder zukünftige Buchungen und kann nicht deaktiviert werden.';
}
}
$updates[$id] = [$label, $colorName, $color, $sort, $active];
}
if (!array_filter($updates, static fn (array $row): bool => (int) $row[4] === 1)) {
$errors['general'] = 'Mindestens ein Anhänger muss aktiv bleiben, damit neue Buchungen möglich sind.';
}
if (!$errors) {
foreach ($updates as $id => [$label, $colorName, $color, $sort, $active]) {
db()->prepare('UPDATE trailers SET label = ?, color_name = ?, color_hex = ?, sort_order = ?, active = ? WHERE id = ?')
->execute([$label, $colorName, $color, $sort, $active, $id]);
}
flash('success', 'Anhänger wurden gespeichert.');
redirect('/admin/trailers');
}
http_response_code(422);
}
http_response_code(422);
}
$rows = trailers_all(false);
// Zeilen mit Anzahl Buchungen für die Lösch-/Deaktivierungsprüfung.
$rows = db()->query(
'SELECT t.*, (SELECT COUNT(*) FROM bookings b WHERE b.trailer_id = t.id) AS booking_count
FROM trailers t ORDER BY t.sort_order, t.id'
)->fetchAll();
render_page('admin_trailers', [
'rows' => $rows,
'errors' => $errors,
'createErrors' => $createErrors,
'createValues' => $createValues,
'nextSort' => $nextSort,
], 'Anhänger');
}
function admin_reminders_send(): void
{
require_post();