Touren löschen und ungültige Datumsangaben verhindern
This commit is contained in:
parent
b1681f0e2b
commit
c6e848d30b
6 changed files with 157 additions and 4 deletions
|
|
@ -156,8 +156,8 @@ class TourController
|
|||
validate([
|
||||
'name' => 'required|min:3',
|
||||
'beschreibung' => 'required',
|
||||
'start' => 'required',
|
||||
'deadline' => 'required',
|
||||
'start' => 'required|date',
|
||||
'deadline' => 'required|date',
|
||||
'tour_guide_id' => 'required',
|
||||
]);
|
||||
|
||||
|
|
@ -361,6 +361,8 @@ class TourController
|
|||
validate([
|
||||
'tour_id' => 'required',
|
||||
'name' => 'required|min:3',
|
||||
'start' => 'required|date',
|
||||
'deadline' => 'date',
|
||||
]);
|
||||
|
||||
// Nur Admins dürfen den Tour Guide ändern.
|
||||
|
|
@ -409,7 +411,75 @@ class TourController
|
|||
|
||||
public function destroy(string $id): void
|
||||
{
|
||||
// Im Original ohne Funktion.
|
||||
$current = Auth::user();
|
||||
|
||||
if ($current === null || !$current->isAdmin()) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$tour = Tour::find((int) $id);
|
||||
|
||||
if ($tour === null) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$tourId = (int) $tour->id;
|
||||
$tourName = (string) $tour->name;
|
||||
|
||||
// Abhängige Daten zuerst entfernen.
|
||||
db()->execute('DELETE FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
||||
db()->execute('DELETE FROM `tour_rooms` WHERE tour_id = ?', [$tourId]);
|
||||
db()->execute('DELETE FROM `tours` WHERE id = ?', [$tourId]);
|
||||
|
||||
// Galerie und Tourbilder von der Festplatte entfernen.
|
||||
$this->deleteDirectory(storage_path('img_storage/tour-' . $tourId));
|
||||
$this->deleteDirectory(storage_path('img_storage/tour-' . $tourId . '-DELETED'));
|
||||
$this->deleteDirectory(public_path('img/tour/' . $tourId));
|
||||
|
||||
foreach ([
|
||||
public_path('img/tour/' . $tourId . '.jpg'),
|
||||
public_path('img/tour/' . $tourId . '_gross.jpg'),
|
||||
] as $file) {
|
||||
if (is_file($file)) {
|
||||
@unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
app_log('info', 'Tour gelöscht', [
|
||||
'tour_id' => $tourId,
|
||||
'name' => $tourName,
|
||||
'by' => (int) $current->id,
|
||||
]);
|
||||
|
||||
flash_set('status', 'Die Tour "' . $tourName . '" wurde gelöscht.');
|
||||
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
/** Löscht ein Verzeichnis mit Inhalt (nur für Tour-Dateien verwendet). */
|
||||
private function deleteDirectory(string $dir): void
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$items = scandir($dir) ?: [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item === '.' || $item === '..') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$path = $dir . '/' . $item;
|
||||
|
||||
if (is_dir($path) && !is_link($path)) {
|
||||
$this->deleteDirectory($path);
|
||||
} elseif (is_file($path) || is_link($path)) {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
@rmdir($dir);
|
||||
}
|
||||
|
||||
public function uploadGallery(string $tour_id): void
|
||||
|
|
|
|||
|
|
@ -476,6 +476,27 @@ function db_date(?string $deDate): string
|
|||
return $parts[2] . '-' . $parts[1] . '-' . $parts[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft ein Datum im Format TT.MM.JJJJ auf ein echtes Kalenderdatum.
|
||||
* Jahreszahlen vor 1900 (z. B. 00.00.0000 bzw. „Datum 0“) sind ungültig.
|
||||
*/
|
||||
function is_valid_german_date(?string $value, int $minYear = 1900): bool
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
if (!preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/', $value, $matches)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$day = (int) $matches[1];
|
||||
$month = (int) $matches[2];
|
||||
$year = (int) $matches[3];
|
||||
|
||||
return $year >= $minYear
|
||||
&& $year <= 2100
|
||||
&& checkdate($month, $day, $year);
|
||||
}
|
||||
|
||||
/** Einfache Validierung: validate(['name' => 'required|min:3', ...]). */
|
||||
function validate(array $rules): array
|
||||
{
|
||||
|
|
@ -501,6 +522,10 @@ function validate(array $rules): array
|
|||
$errors[$field][] = 'Bitte eine gültige E-Mail-Adresse angeben.';
|
||||
}
|
||||
|
||||
if ($rule === 'date' && $value !== null && trim((string) $value) !== '' && !is_valid_german_date((string) $value)) {
|
||||
$errors[$field][] = 'Bitte ein gültiges Datum im Format TT.MM.JJJJ angeben.';
|
||||
}
|
||||
|
||||
if (str_starts_with($rule, 'mimes:')) {
|
||||
$allowed = explode(',', substr($rule, 6));
|
||||
if (!empty($_FILES[$field]) && !empty($_FILES[$field]['name'])) {
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ $router->post('/tour', [TourController::class, 'store'], true);
|
|||
$router->get('/tour/{id}', [TourController::class, 'show'], true);
|
||||
$router->get('/tour/{id}/edit', [TourController::class, 'edit'], true);
|
||||
$router->put('/tour/{id}', [TourController::class, 'update'], true);
|
||||
$router->delete('/tour/{id}', [TourController::class, 'destroy'], true);
|
||||
$router->delete('/tour/{id}', [TourController::class, 'destroy'], 'ADMIN');
|
||||
$router->post('/tour/upload_gallery/{tour_id}', [TourController::class, 'uploadGallery']);
|
||||
|
||||
$router->post('/tour/{tour_id}/join/{room_id}', [TourController::class, 'joinRoom'], true);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
$current = auth_check() ? user() : null;
|
||||
$flashStatus = flash_pull('status');
|
||||
$flashError = flash_pull('error');
|
||||
$validationErrors = errors();
|
||||
unset($_SESSION['_errors']);
|
||||
$canChangeGuide = $current !== null && $current->isAdmin();
|
||||
$galleryLightbox = 'tour-bilder-' . $tour->id;
|
||||
|
||||
|
|
@ -40,6 +42,19 @@
|
|||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($validationErrors !== []): ?>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<strong>Bitte die Eingaben prüfen:</strong>
|
||||
<ul class="mb-0 mt-1">
|
||||
<?php foreach ($validationErrors as $messages): ?>
|
||||
<?php foreach ($messages as $message): ?>
|
||||
<li><?= e($message) ?></li>
|
||||
<?php endforeach; ?>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form style="background-color: #fff;" class="text-dark" method="post"
|
||||
action="/tour/<?= e($tour->id) ?>" enctype="multipart/form-data">
|
||||
<?= csrf_field() ?>
|
||||
|
|
@ -105,6 +120,15 @@
|
|||
<button type="submit" class="btn btn-danger col-2 mb-2 float-right mr-2">Speichern</button>
|
||||
</form>
|
||||
|
||||
<?php if ($current && $current->isAdmin()): ?>
|
||||
<form action="/tour/<?= e($tour->id) ?>" method="POST" style="clear: both;" class="mt-3 mb-4"
|
||||
onsubmit="return confirm('Diese Tour wirklich endgültig löschen? Anmeldungen, Zimmer und Bilder werden ebenfalls entfernt.');">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="_method" value="DELETE">
|
||||
<button type="submit" class="btn btn-outline-danger btn-sm">Tour löschen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<section class="tour-media-manager" aria-labelledby="tour-media-title">
|
||||
<div class="tour-media-head">
|
||||
<div>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
$current = auth_check() ? user() : null;
|
||||
$flashStatus = flash_pull('status');
|
||||
$flashError = flash_pull('error');
|
||||
$validationErrors = errors();
|
||||
unset($_SESSION['_errors']);
|
||||
$canChangeGuide = $current !== null && $current->isAdmin();
|
||||
$galleryLightbox = 'tour-bilder-' . $tour->id;
|
||||
?>
|
||||
|
|
@ -21,6 +23,19 @@
|
|||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($validationErrors !== []): ?>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<strong>Bitte die Eingaben prüfen:</strong>
|
||||
<ul class="mb-0 mt-1">
|
||||
<?php foreach ($validationErrors as $messages): ?>
|
||||
<?php foreach ($messages as $message): ?>
|
||||
<li><?= e($message) ?></li>
|
||||
<?php endforeach; ?>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form style="background-color: #fff;" class="text-dark" method="post" action="/tour/<?= e($tour->id) ?>">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="_method" value="PUT">
|
||||
|
|
@ -62,6 +77,15 @@
|
|||
<button type="submit" class="btn btn-danger col-2 mb-2 float-right mr-2">Speichern</button>
|
||||
</form>
|
||||
|
||||
<?php if ($current && $current->isAdmin()): ?>
|
||||
<form action="/tour/<?= e($tour->id) ?>" method="POST" style="clear: both;" class="mt-3 mb-4"
|
||||
onsubmit="return confirm('Diese Tour wirklich endgültig löschen? Anmeldungen, Zimmer und Bilder werden ebenfalls entfernt.');">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="_method" value="DELETE">
|
||||
<button type="submit" class="btn btn-outline-danger btn-sm">Tour löschen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<section class="tour-media-manager" aria-labelledby="tour-media-title">
|
||||
<div class="tour-media-head">
|
||||
<div>
|
||||
|
|
|
|||
|
|
@ -6,11 +6,21 @@
|
|||
$years = $years ?? [];
|
||||
$toursByYear = $toursByYear ?? [];
|
||||
$visibleYears = $visibleYears ?? ($selectedYear === null ? $years : [$selectedYear]);
|
||||
$flashStatus = flash_pull('status');
|
||||
$flashError = flash_pull('error');
|
||||
?>
|
||||
|
||||
<div class="container text-center text-light mt-5">
|
||||
<h1 class="text-center">Tourenplan: aktuelle Touren</h1>
|
||||
|
||||
<?php if ($flashStatus !== null): ?>
|
||||
<div class="alert alert-success" role="alert"><?= e($flashStatus) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($flashError !== null): ?>
|
||||
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="container text-primary mb-3">
|
||||
<span class="offset-1 col-4 float-left mb-3"><a class="btn btn-success" href="/tour/index_old" class="text-center text-light"><h3>Abgeschlossene Touren</h3></a></span>
|
||||
<span class="offset-1"><a href="/img_storage/pdf/Tourenplan.pdf" class="btn btn-success"><h3>Download</h3></a></span>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue