id] = Booking::countForTour((int) $tour->id); $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); } view('tour.index', ['tours' => $tours, 'participantCount' => $participantCount]); } public function indexOld(): void { $tours = Tour::past(); $participantCount = []; $imagecountPerTour = []; foreach ($tours as $tour) { $participantCount[$tour->id] = Booking::countForTour((int) $tour->id); $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); $files = glob(storage_path('img_storage/tour-' . $tour->id) . '/*') ?: []; $imagecountPerTour[$tour->id] = (int) (count($files) / 2); } view('tour.index_old', [ 'tours' => $tours, 'participantCount' => $participantCount, 'imagecountPerTour' => $imagecountPerTour, ]); } public function create(): void { $tour = new Tour(); $tour->name = 'neue Tour'; $tour->beschreibung = 'Tour Beschreibung'; $tour->user_id = Auth::id(); view('tour.create', ['tour' => $tour]); } public function store(): void { validate([ 'name' => 'required|min:3', 'beschreibung' => 'required', 'start' => 'required', 'deadline' => 'required', 'user_id' => 'required', 'bild' => 'mimes:jpg,jpeg,bmp,png,gif', ]); $tour = Tour::create([ 'name' => (string) input('name'), 'user_id' => (int) input('user_id'), 'beschreibung' => (string) input('beschreibung'), 'start' => db_date(input('start')), 'deadline' => db_date(input('deadline')), 'max_participants' => (int) (input('max_participants') ?: 5), 'is_cancelled' => (bool) input('is_cancelled', false), 'bericht' => null, 'created_at' => now(), 'updated_at' => now(), ]); redirect('/tour'); } public function show(string $id): void { $tour = Tour::find((int) $id); if ($tour === null) { redirect('/tour'); } $creator = User::find((int) $tour->user_id); $canManage = $this->canManageBookings($tour); $users = []; foreach (Booking::forTour((int) $tour->id) as $booking) { $user = User::find((int) $booking->user_id); if ($user !== null) { $users[] = $user; } } // Vergangene Tour if ($tour->start < today()) { $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); view('tour.show_old', [ 'tour' => $tour, 'users' => $users, 'creator' => $creator, 'canManage' => $canManage, ]); return; } $rooms = array_map(fn(array $row) => (object) $row, TourRoom::fetchRooms((int) $tour->id)); $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); view('tour.show', [ 'tour' => $tour, 'users' => $users, 'creator' => $creator, '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) { redirect('/tour'); } if ($tour->start < today()) { $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); view('tour.edit_old', ['tour' => $tour]); return; } $rooms = array_map(fn(array $row) => (object) $row, TourRoom::fetchRooms((int) $tour->id)); $tour->start = german_date($tour->start); $tour->deadline = german_date($tour->deadline); view('tour.edit', ['tour' => $tour, 'rooms' => $rooms]); } public function update(string $id): void { $tourId = (int) input('tour_id', $id); $tour = Tour::find($tourId); if ($tour === null) { redirect('/tour'); } if (!Auth::user()->isAdmin()) { redirect('/tour'); } // Bild wird in Image::store() inhaltlich geprüft (Format/Größe); // die dortige Fehlermeldung wird als Flash-Meldung angezeigt. validate([ 'tour_id' => 'required', 'name' => 'required|min:3', 'user_id' => 'required', ]); $imageError = null; if (!empty($_FILES['bild']['name'])) { try { Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name')); } catch (\Throwable $e) { if (!$e instanceof \RuntimeException) { error_log('Tourbild-Upload: ' . $e); } $imageError = $e instanceof \RuntimeException ? $e->getMessage() : 'Das Bild konnte nicht verarbeitet werden.'; } } $tour->name = (string) input('name'); if (input('beschreibung') !== null) { $tour->beschreibung = (string) input('beschreibung'); } if (input('start') !== null) { $tour->start = db_date(input('start')); } if (input('deadline') !== null) { $tour->deadline = db_date(input('deadline')); } if (input('max_participants') !== null) { $tour->max_participants = (int) input('max_participants'); } if (input('is_cancelled')) { $tour->is_cancelled = true; } else { $tour->is_cancelled = false; } if (input('bericht') !== null) { $tour->bericht = (string) input('bericht'); } $tour->updated_at = now(); $tour->save(); if ($imageError !== null) { flash_set('error', $imageError); redirect('/tour/' . $tourId . '/edit'); } redirect('/tour'); } public function destroy(string $id): void { // Im Original ohne Funktion. } public function uploadGallery(string $tour_id): void { redirect('/tour'); } public function joinRoom(string $tour_id, string $room_id): void { Booking::joinRoom((int) $tour_id, (int) Auth::id(), (int) $room_id); redirect('/tour/' . $tour_id); } public function joinRoom4user(string $tour_id, string $room_id): void { $userId = (int) input('user_id', 0); if ($userId > 0) { Booking::joinRoom((int) $tour_id, $userId, (int) $room_id); } redirect('/tour/' . $tour_id . '/edit'); } public function cancelBooking(string $tour_id): void { if (!$this->canManageBookings(Tour::find((int) $tour_id))) { redirect('/tour'); } Booking::deleteForUser((int) $tour_id, (int) Auth::id()); redirect('/tour'); } public function cancelBookingAdmin(string $tour_id, string $user_id): void { if (!$this->canManageBookings(Tour::find((int) $tour_id))) { redirect('/tour/' . $tour_id); } Booking::deleteForUser((int) $tour_id, (int) $user_id); redirect('/tour/' . $tour_id); } public function createRoom(string $tour_id, string $capacity): void { Tour::createRoom((int) $tour_id, (int) $capacity); redirect('/tour/' . $tour_id . '/edit'); } public function deleteRoom(string $tour_id, string $room_id): void { 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]); redirect('/tour/' . $tour_id . '/edit'); } private function canManageBookings(?Tour $tour): bool { $user = Auth::user(); return $user !== null && $tour !== null && ($user->isAdmin() || (int) $user->id === (int) $tour->user_id); } }