271 lines
7.7 KiB
PHP
271 lines
7.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Controllers;
|
||
|
|
|
||
|
|
use App\Core\Auth;
|
||
|
|
use App\Core\Image;
|
||
|
|
use App\Models\Booking;
|
||
|
|
use App\Models\Tour;
|
||
|
|
use App\Models\TourRoom;
|
||
|
|
use App\Models\User;
|
||
|
|
|
||
|
|
class TourController
|
||
|
|
{
|
||
|
|
public function index(): void
|
||
|
|
{
|
||
|
|
$tours = Tour::upcoming();
|
||
|
|
$participantCount = [];
|
||
|
|
|
||
|
|
foreach ($tours as $tour) {
|
||
|
|
$participantCount[$tour->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);
|
||
|
|
|
||
|
|
$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,
|
||
|
|
]);
|
||
|
|
|
||
|
|
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,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
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');
|
||
|
|
}
|
||
|
|
|
||
|
|
validate([
|
||
|
|
'tour_id' => 'required',
|
||
|
|
'name' => 'required|min:3',
|
||
|
|
'user_id' => 'required',
|
||
|
|
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!empty($_FILES['bild']['name'])) {
|
||
|
|
Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$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();
|
||
|
|
|
||
|
|
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, string $user_id): void
|
||
|
|
{
|
||
|
|
Booking::joinRoom((int) $tour_id, (int) $user_id, (int) $room_id);
|
||
|
|
|
||
|
|
$tour = Tour::find((int) $tour_id);
|
||
|
|
$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 cancelBooking(string $tour_id): void
|
||
|
|
{
|
||
|
|
Booking::deleteForUser((int) $tour_id, (int) Auth::id());
|
||
|
|
redirect('/tour');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function cancelBookingAdmin(string $tour_id, string $user_id): void
|
||
|
|
{
|
||
|
|
if (Auth::user()->isAdmin()) {
|
||
|
|
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');
|
||
|
|
}
|
||
|
|
}
|