Tour Guide, mehrere Tour-Bilder und Guide-Verwaltungsrechte
This commit is contained in:
parent
322d232012
commit
b52155c5f0
12 changed files with 784 additions and 154 deletions
|
|
@ -83,7 +83,7 @@ class TourController
|
|||
$tour = new Tour();
|
||||
$tour->name = 'neue Tour';
|
||||
$tour->beschreibung = 'Tour Beschreibung';
|
||||
$tour->user_id = Auth::id();
|
||||
$tour->tour_guide_id = Auth::id();
|
||||
|
||||
view('tour.create', ['tour' => $tour]);
|
||||
}
|
||||
|
|
@ -95,13 +95,13 @@ class TourController
|
|||
'beschreibung' => 'required',
|
||||
'start' => 'required',
|
||||
'deadline' => 'required',
|
||||
'user_id' => 'required',
|
||||
'tour_guide_id' => 'required',
|
||||
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
||||
]);
|
||||
|
||||
$tour = Tour::create([
|
||||
'name' => (string) input('name'),
|
||||
'user_id' => (int) input('user_id'),
|
||||
'tour_guide_id' => (int) input('tour_guide_id'),
|
||||
'beschreibung' => (string) input('beschreibung'),
|
||||
'start' => db_date(input('start')),
|
||||
'deadline' => db_date(input('deadline')),
|
||||
|
|
@ -138,8 +138,9 @@ class TourController
|
|||
redirect('/tour');
|
||||
}
|
||||
|
||||
$creator = User::find((int) $tour->user_id);
|
||||
$canManage = $this->canManageBookings($tour);
|
||||
$guide = User::find((int) $tour->tour_guide_id);
|
||||
$canManage = $this->canManageTour($tour);
|
||||
$tourImages = $this->tourImages((int) $tour->id);
|
||||
|
||||
$users = [];
|
||||
foreach (Booking::forTour((int) $tour->id) as $booking) {
|
||||
|
|
@ -155,10 +156,11 @@ class TourController
|
|||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.show_old', [
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'creator' => $creator,
|
||||
'canManage' => $canManage,
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'guide' => $guide,
|
||||
'tourImages' => $tourImages,
|
||||
'canManage' => $canManage,
|
||||
]);
|
||||
|
||||
return;
|
||||
|
|
@ -170,35 +172,38 @@ class TourController
|
|||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.show', [
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'creator' => $creator,
|
||||
'rooms' => $rooms,
|
||||
'canManage' => $canManage,
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'guide' => $guide,
|
||||
'tourImages' => $tourImages,
|
||||
'rooms' => $rooms,
|
||||
'canManage' => $canManage,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $id): void
|
||||
{
|
||||
$current = Auth::user();
|
||||
|
||||
if (!$current->isAdmin()) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$tour = Tour::find((int) $id);
|
||||
|
||||
if ($tour === null) {
|
||||
if ($tour === null || !$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$guides = User::selectableGuides();
|
||||
$current = Auth::user();
|
||||
$guides = $current->isAdmin() ? User::selectableGuides() : [];
|
||||
$tourGuide = User::find((int) $tour->tour_guide_id);
|
||||
$tourImages = $this->tourImages((int) $tour->id);
|
||||
|
||||
if ($tour->start < today()) {
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit_old', ['tour' => $tour, 'guides' => $guides]);
|
||||
view('tour.edit_old', [
|
||||
'tour' => $tour,
|
||||
'guides' => $guides,
|
||||
'tourGuide' => $tourGuide,
|
||||
'tourImages' => $tourImages,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
@ -208,7 +213,13 @@ class TourController
|
|||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit', ['tour' => $tour, 'rooms' => $rooms, 'guides' => $guides]);
|
||||
view('tour.edit', [
|
||||
'tour' => $tour,
|
||||
'rooms' => $rooms,
|
||||
'guides' => $guides,
|
||||
'tourGuide' => $tourGuide,
|
||||
'tourImages' => $tourImages,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(string $id): void
|
||||
|
|
@ -220,43 +231,29 @@ class TourController
|
|||
redirect('/tour');
|
||||
}
|
||||
|
||||
if (!Auth::user()->isAdmin()) {
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
// Bild wird in Image::store() inhaltlich geprüft (Format/Größe);
|
||||
// die dortige Fehlermeldung wird als Flash-Meldung angezeigt.
|
||||
$current = Auth::user();
|
||||
|
||||
validate([
|
||||
'tour_id' => 'required',
|
||||
'name' => 'required|min:3',
|
||||
'user_id' => 'required',
|
||||
]);
|
||||
|
||||
$guide = User::find((int) input('user_id'));
|
||||
// Nur Admins dürfen den Tour Guide ändern.
|
||||
if ($current->isAdmin()) {
|
||||
$guide = User::find((int) input('tour_guide_id'));
|
||||
|
||||
if ($guide === null || $guide->role === 'GUEST') {
|
||||
flash_set('error', 'Der gewählte Ansprechpartner ist ungültig.');
|
||||
redirect('/tour/' . $tourId . '/edit');
|
||||
}
|
||||
|
||||
$imageError = null;
|
||||
|
||||
if (!empty($_FILES['bild']['name'])) {
|
||||
try {
|
||||
// Tour-Titelbilder liegen wie bisher unter public/img/tour/<id>.jpg
|
||||
Image::storePublic($_FILES['bild'], 'img/tour', (string) $tourId);
|
||||
} catch (\Throwable $e) {
|
||||
if (!$e instanceof \RuntimeException) {
|
||||
error_log('Tourbild-Upload: ' . $e);
|
||||
}
|
||||
|
||||
$imageError = $e instanceof \RuntimeException
|
||||
? $e->getMessage()
|
||||
: 'Das Bild konnte nicht verarbeitet werden.';
|
||||
if ($guide === null || $guide->role === 'GUEST') {
|
||||
flash_set('error', 'Der gewählte Tour Guide ist ungültig.');
|
||||
redirect('/tour/' . $tourId . '/edit');
|
||||
}
|
||||
|
||||
$tour->tour_guide_id = (int) $guide->id;
|
||||
}
|
||||
|
||||
$tour->user_id = (int) $guide->id;
|
||||
$tour->name = (string) input('name');
|
||||
if (input('beschreibung') !== null) {
|
||||
$tour->beschreibung = (string) input('beschreibung');
|
||||
|
|
@ -282,12 +279,7 @@ class TourController
|
|||
$tour->updated_at = now();
|
||||
$tour->save();
|
||||
|
||||
if ($imageError !== null) {
|
||||
flash_set('error', $imageError);
|
||||
redirect('/tour/' . $tourId . '/edit');
|
||||
}
|
||||
|
||||
redirect('/tour');
|
||||
redirect('/tour/' . $tourId . '/edit');
|
||||
}
|
||||
|
||||
public function destroy(string $id): void
|
||||
|
|
@ -300,6 +292,179 @@ class TourController
|
|||
redirect('/tour');
|
||||
}
|
||||
|
||||
/** Lädt mehrere Tour-Bilder hoch (Admin oder Tour Guide). */
|
||||
public function storeImages(string $tour_id): void
|
||||
{
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$files = $_FILES['bilder'] ?? null;
|
||||
|
||||
if (!is_array($files) || !isset($files['name'])) {
|
||||
flash_set('error', 'Bitte mindestens ein Bild auswählen.');
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
$names = (array) $files['name'];
|
||||
$tmpNames = (array) ($files['tmp_name'] ?? []);
|
||||
$errors = (array) ($files['error'] ?? []);
|
||||
$sizes = (array) ($files['size'] ?? []);
|
||||
|
||||
$stored = 0;
|
||||
$problems = [];
|
||||
|
||||
foreach (array_keys($names) as $i) {
|
||||
$upload = [
|
||||
'name' => (string) ($names[$i] ?? ''),
|
||||
'tmp_name' => (string) ($tmpNames[$i] ?? ''),
|
||||
'error' => (int) ($errors[$i] ?? UPLOAD_ERR_NO_FILE),
|
||||
'size' => (int) ($sizes[$i] ?? 0),
|
||||
];
|
||||
|
||||
if ($upload['error'] === UPLOAD_ERR_NO_FILE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$imageName = $this->uniqueTourImageName((int) $tour->id, $upload['name']);
|
||||
Image::storePublic($upload, 'img/tour/' . $tour->id, $imageName);
|
||||
$stored++;
|
||||
} catch (\Throwable $e) {
|
||||
if (!$e instanceof \RuntimeException) {
|
||||
error_log('Tourbild-Upload: ' . $e);
|
||||
$problems[] = 'Ein Bild konnte nicht verarbeitet werden.';
|
||||
} else {
|
||||
$problems[] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($stored > 0) {
|
||||
flash_set('status', $stored === 1 ? 'Bild wurde hinzugefügt.' : $stored . ' Bilder wurden hinzugefügt.');
|
||||
}
|
||||
|
||||
if ($problems !== []) {
|
||||
flash_set('error', implode(' ', array_unique($problems)));
|
||||
} elseif ($stored === 0) {
|
||||
flash_set('error', 'Bitte mindestens ein Bild auswählen.');
|
||||
}
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
/** Löscht ein Tour-Bild (Admin oder Tour Guide). */
|
||||
public function deleteImage(string $tour_id, string $imageName): void
|
||||
{
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$name = basename((string) $imageName);
|
||||
$name = preg_replace('/(?:_gross)?\.(?:jpe?g)$/i', '', $name) ?? $name;
|
||||
|
||||
$deleted = false;
|
||||
|
||||
if ($name === (string) $tour->id) {
|
||||
// Altes Einzelbild (public/img/tour/<id>.jpg)
|
||||
foreach (['.jpg', '_gross.jpg'] as $suffix) {
|
||||
$path = public_path('img/tour/' . $name . $suffix);
|
||||
if (is_file($path) && @unlink($path)) {
|
||||
$deleted = true;
|
||||
}
|
||||
}
|
||||
} elseif (preg_match('/^[a-z0-9][a-z0-9-]*$/', $name) === 1) {
|
||||
// Neues Bild im Ordner public/img/tour/<id>/
|
||||
foreach (['.jpg', '_gross.jpg'] as $suffix) {
|
||||
$path = public_path('img/tour/' . $tour->id . '/' . $name . $suffix);
|
||||
if (is_file($path) && @unlink($path)) {
|
||||
$deleted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flash_set($deleted ? 'status' : 'error', $deleted
|
||||
? 'Bild wurde gelöscht.'
|
||||
: 'Das Bild konnte nicht gelöscht werden.');
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert alle Tour-Bilder (altes Einzelbild und neuer Bilderordner),
|
||||
* neueste zuerst.
|
||||
*/
|
||||
private function tourImages(int $tourId): array
|
||||
{
|
||||
$images = [];
|
||||
|
||||
$legacy = public_path('img/tour/' . $tourId . '.jpg');
|
||||
if (is_file($legacy)) {
|
||||
$images[] = [
|
||||
'name' => (string) $tourId,
|
||||
'thumb' => '/img/tour/' . $tourId . '.jpg',
|
||||
'large' => '/img/tour/' . $tourId . '_gross.jpg',
|
||||
'version' => @filemtime($legacy) ?: 0,
|
||||
'mtime' => @filemtime($legacy) ?: 0,
|
||||
];
|
||||
}
|
||||
|
||||
$dir = public_path('img/tour/' . $tourId);
|
||||
if (is_dir($dir)) {
|
||||
foreach (glob($dir . '/*.jpg') ?: [] as $file) {
|
||||
$filename = basename($file);
|
||||
|
||||
if (str_ends_with($filename, '_gross.jpg')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = substr($filename, 0, -4);
|
||||
$gross = $dir . '/' . $name . '_gross.jpg';
|
||||
|
||||
$images[] = [
|
||||
'name' => $name,
|
||||
'thumb' => '/img/tour/' . $tourId . '/' . $filename,
|
||||
'large' => is_file($gross)
|
||||
? '/img/tour/' . $tourId . '/' . $name . '_gross.jpg'
|
||||
: '/img/tour/' . $tourId . '/' . $filename,
|
||||
'version' => @filemtime($file) ?: 0,
|
||||
'mtime' => @filemtime($file) ?: 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
usort($images, fn(array $a, array $b) => $b['mtime'] <=> $a['mtime']);
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
/** Erzeugt einen Dateinamen, der im Tour-Ordner noch nicht existiert. */
|
||||
private function uniqueTourImageName(int $tourId, string $originalName): string
|
||||
{
|
||||
$dir = public_path('img/tour/' . $tourId);
|
||||
$base = Image::slug(pathinfo($originalName, PATHINFO_FILENAME));
|
||||
|
||||
// Reine Zahlen wären mit dem alten Einzelbild (<id>.jpg) verwechselbar.
|
||||
if (ctype_digit($base)) {
|
||||
$base = 'bild-' . $base;
|
||||
}
|
||||
|
||||
$base = substr($base, 0, 60);
|
||||
$name = $base;
|
||||
$i = 2;
|
||||
|
||||
while (is_file($dir . '/' . $name . '.jpg') || is_file($dir . '/' . $name . '_gross.jpg')) {
|
||||
$name = $base . '-' . $i;
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
public function joinRoom(string $tour_id, string $room_id): void
|
||||
{
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
|
@ -313,7 +478,7 @@ class TourController
|
|||
|
||||
// Normale Nutzer dürfen sich nur selbst in einen echten, freien Raum
|
||||
// eintragen. room_id 0 (austragen) und Raumwechsel bleiben Admins
|
||||
// bzw. dem Tour-Ansprechpartner vorbehalten.
|
||||
// bzw. dem Tour Guide vorbehalten.
|
||||
if (Booking::forUser((int) $tour->id, (int) $user->id) !== null
|
||||
|| !TourRoom::isBookable((int) $tour->id, $roomId)
|
||||
|| !Tour::hasSpace((int) $tour->id)) {
|
||||
|
|
@ -326,10 +491,16 @@ class TourController
|
|||
|
||||
public function joinRoom4user(string $tour_id, string $room_id): void
|
||||
{
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$userId = (int) input('user_id', 0);
|
||||
|
||||
if ($userId > 0) {
|
||||
Booking::joinRoom((int) $tour_id, $userId, (int) $room_id);
|
||||
Booking::joinRoom((int) $tour->id, $userId, (int) $room_id);
|
||||
}
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
|
|
@ -337,7 +508,7 @@ class TourController
|
|||
|
||||
public function cancelBooking(string $tour_id): void
|
||||
{
|
||||
if (!$this->canManageBookings(Tour::find((int) $tour_id))) {
|
||||
if (!$this->canManageTour(Tour::find((int) $tour_id))) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
|
|
@ -347,7 +518,7 @@ class TourController
|
|||
|
||||
public function cancelBookingAdmin(string $tour_id, string $user_id): void
|
||||
{
|
||||
if (!$this->canManageBookings(Tour::find((int) $tour_id))) {
|
||||
if (!$this->canManageTour(Tour::find((int) $tour_id))) {
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
|
|
@ -357,24 +528,36 @@ class TourController
|
|||
|
||||
public function createRoom(string $tour_id, string $capacity): void
|
||||
{
|
||||
Tour::createRoom((int) $tour_id, (int) $capacity);
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
Tour::createRoom((int) $tour->id, (int) $capacity);
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
public function deleteRoom(string $tour_id, string $room_id): void
|
||||
{
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
|
||||
if (!$this->canManageTour($tour)) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
db()->execute('UPDATE `bookings` SET room_id = NULL WHERE room_id = ?', [(int) $room_id]);
|
||||
db()->execute('DELETE FROM `tour_rooms` WHERE tour_id = ? AND id = ?', [(int) $tour_id, (int) $room_id]);
|
||||
db()->execute('DELETE FROM `tour_rooms` WHERE tour_id = ? AND id = ?', [(int) $tour->id, (int) $room_id]);
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
private function canManageBookings(?Tour $tour): bool
|
||||
private function canManageTour(?Tour $tour): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
return $user !== null
|
||||
&& $tour !== null
|
||||
&& ($user->isAdmin() || (int) $user->id === (int) $tour->user_id);
|
||||
&& ($user->isAdmin() || (int) $user->id === (int) $tour->tour_guide_id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue