motorradig/motorradig/app/Http/Controllers/TourController.php

482 lines
16 KiB
PHP
Raw Normal View History

2020-09-30 10:51:06 +02:00
<?php
2023-01-12 13:50:48 +01:00
namespace App\Http\Controllers;
use App\Tour;
use App\Booking;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2023-01-12 13:50:48 +01:00
class TourController extends Controller
2020-09-30 10:51:06 +02:00
{
2023-01-12 13:50:48 +01:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
error_log("Tour:index");
2024-02-24 20:10:43 +01:00
2023-01-12 13:50:48 +01:00
$tours = Tour::where('start', '>', Carbon::now())->orderBy('start')->get();
$participantCount = array();
foreach ($tours as $key => $tour) {
$result = DB::select('select count(*) as cnt from bookings where tour_id=?', [$tour->id]);
$participantCount[$tour->id] = $result[0]->cnt;
$tours[$key]->start = Tour::toGermanDate($tour->start);
$tours[$key]->deadline = Tour::toGermanDate($tour->deadline);
}
return view('tour.index')->with('tours', $tours)->with('participantCount', $participantCount);
2020-10-06 08:54:14 +02:00
}
2023-01-12 13:50:48 +01:00
public function index_old()
{
2024-02-24 20:10:43 +01:00
Log::info("Tour:index_old");
2023-01-12 13:50:48 +01:00
$tours = Tour::where('start', '<', Carbon::now())->orderBy('start')->get();
$participantCount = array();
2024-02-24 20:10:43 +01:00
$imagecount_per_tour = array();
2023-01-12 13:50:48 +01:00
foreach ($tours as $key => $tour) {
$result = DB::select('select count(*) as cnt from bookings where tour_id=?', [$tour->id]);
$participantCount[$tour->id] = $result[0]->cnt;
$tours[$key]->start = Tour::toGermanDate($tour->start);
$tours[$key]->deadline = Tour::toGermanDate($tour->deadline);
2024-02-24 20:10:43 +01:00
$imagelist = Storage::disk('img_storage')->files("tour-$tour->id");
$len = sizeof($imagelist)/2;
$imagecount_per_tour[$tour->id]=$len;
Log::info("Tour $tour->id, ImageList size: $len");
2023-01-12 13:50:48 +01:00
}
2024-02-24 20:10:43 +01:00
return view('tour.index_old')->with('tours', $tours)->with('participantCount', $participantCount)->with('imagecount_per_tour', $imagecount_per_tour);
}
2023-01-12 13:50:48 +01:00
/*
2020-10-06 08:54:14 +02:00
2020-09-30 10:51:06 +02:00
2023-01-12 13:50:48 +01:00
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
error_log("Tour:create");
$tour = new Tour;
$tour->name = "neue Tour";
$tour->beschreibung = "Tour Beschreibung";
$tour->user_id = Auth::user()->id;
return view('tour.create')->with('tour', $tour);
}
2020-09-30 10:51:06 +02:00
2023-01-12 13:50:48 +01:00
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Log::info("TourController.store");
2023-01-12 13:50:48 +01:00
$request->validate(
[
'name' => 'required|min:3',
'beschreibung' => 'required',
'start' => 'required',
'deadline' => 'required',
'user_id' => 'required',
'bild' => 'mimes:jpg,jpeg,bmp,png,gif'
]
);
Log::info("Storage Test");
Log::info(Storage::allDirectories());
//dd($request);
//error_log("Test§§§");
//Log::info($request);
2023-01-12 13:50:48 +01:00
$tour = new Tour;
$tour->name = $request->input('name');
$tour->user_id = $request->input('user_id');
$tour->beschreibung = $request->input('beschreibung');
$tour->start = Tour::toDBDate($request->input('start'));
$tour->deadline = Tour::toDBDate($request->input('deadline'));
$tour->max_participants = $request->input('max_participants');
$tour->is_cancelled = $request->input('is_cancelled', false);
$tour->save();
Log::info("Stored deadline:" . $tour->deadline);
2023-01-12 13:50:48 +01:00
return redirect('/tour');
}
2023-01-12 13:50:48 +01:00
/**
* Display the specified resource.
*
* @param \App\Tour $tour
* @return \Illuminate\Http\Response
*/
public function show(Tour $tour)
{
2023-09-30 08:48:38 +02:00
Log::info("TourController::show triggered");
2023-01-12 13:50:48 +01:00
$creator = User::find($tour->user_id);
$bookings = Booking::where('tour_id', $tour->id)->get();
$users = array();
foreach ($bookings as $booking) {
$user = User::find($booking->user_id);
array_push($users, $user);
2023-09-30 08:48:38 +02:00
Log::info("Adding user with id $booking->user_id: $user");
2023-01-12 13:50:48 +01:00
}
// Vergangene Tour anzeigen
if ($tour->start < Carbon::now()) {
$tour->start = Tour::toGermanDate($tour->start);
$tour->deadline = Tour::toGermanDate($tour->deadline);
2023-09-30 08:48:38 +02:00
2023-01-12 13:50:48 +01:00
return view('tour.show_old')
->with('tour', $tour)
->with('users', $users)
->with('creator', $creator);
}
// Zukünftige Tour anzeigen
$rooms = $this->fetch_rooms($tour->id);
2021-03-06 09:53:31 +01:00
$tour->start = Tour::toGermanDate($tour->start);
$tour->deadline = Tour::toGermanDate($tour->deadline);
2023-09-30 08:48:38 +02:00
2023-01-12 13:50:48 +01:00
return view('tour.show')
2020-10-14 09:33:09 +02:00
->with('tour', $tour)
->with('users', $users)
2023-01-12 13:50:48 +01:00
->with('creator', $creator)
->with('rooms', $rooms);
2020-10-14 09:33:09 +02:00
}
2023-01-12 13:50:48 +01:00
/**
* Show the form for editing the specified resource.
*
* @param \App\Tour $tour
* @return \Illuminate\Http\Response
*/
private function fetch_rooms($tour_id)
{
// - all existing rooms with booked users (if any)
// - all users without room
$sql = "
2022-05-11 07:51:47 +02:00
SELECT
qry.tour_id,
qry.room_id,
2022-05-11 07:51:47 +02:00
u.id as user_id,
u.name as user_name,
qry.capacity,
2022-06-17 16:25:57 +02:00
qry.free_slots,
COALESCE(qry.room_name,'Ungebucht') as room_name,
2023-01-11 20:45:29 +01:00
COALESCE(qry.room_id,'ZZZZZZZZZ') as room_sort_order
2022-05-11 07:51:47 +02:00
FROM
(
SELECT
tr.tour_id,
tr.id as room_id,
tr.capacity,
tr.name as room_name,
2022-06-17 16:25:57 +02:00
b.user_id,
(SELECT tr.capacity - count(*) from bookings where tour_id=tr.tour_id and room_id=tr.id) as free_slots
2022-05-11 07:51:47 +02:00
FROM
2023-01-11 20:45:29 +01:00
tour_rooms tr LEFT JOIN bookings as b
ON b.tour_id = tr.tour_id AND b.room_id = tr.id
2022-05-11 07:51:47 +02:00
WHERE
2023-01-12 13:50:48 +01:00
tr.tour_id= " . $tour_id . "
2023-01-11 20:45:29 +01:00
2022-05-11 07:51:47 +02:00
) qry
LEFT JOIN users as u
ON qry.user_id = u.id
2023-01-11 20:45:29 +01:00
2022-05-11 07:51:47 +02:00
UNION
-- unbooked tour users
SELECT
b.tour_id,
b.room_id,
b.user_id,
u.name as user_name,
tr.capacity,
2022-06-17 16:25:57 +02:00
'0' as free_slots,
2022-05-11 07:51:47 +02:00
COALESCE(tr.name,'Ungebucht') as room_name,
COALESCE(tr.name,'ZZZZZZZZZ') as room_sort_order
FROM
users u,
bookings b
LEFT JOIN tour_rooms as tr
ON b.room_id = tr.id
WHERE
2023-01-12 13:50:48 +01:00
b.tour_id= " . $tour_id . "
2022-05-11 07:51:47 +02:00
AND
b.user_id = u.id
AND
capacity is null
2023-01-11 20:45:29 +01:00
ORDER BY room_sort_order, user_name
2022-05-11 07:51:47 +02:00
;
";
2023-01-12 13:50:48 +01:00
$rooms = DB::select($sql);
2022-06-14 09:34:39 +02:00
2023-01-12 13:50:48 +01:00
//Log::info("TourController.fetch_rooms, returning rooms result:");
//Log::info($rooms);
2023-01-12 13:50:48 +01:00
return ($rooms);
2020-10-14 09:33:09 +02:00
}
2021-03-06 09:53:31 +01:00
2023-01-12 13:50:48 +01:00
public function edit(Tour $tour)
{
2023-11-11 14:00:39 +01:00
Log::info("TourController::edit");
2023-01-12 13:50:48 +01:00
if (Auth::user()->role != 'ADMIN') {
return redirect('/tour');
}
2022-05-03 09:47:39 +02:00
2023-01-11 20:45:29 +01:00
2023-11-11 14:00:39 +01:00
Log::info("start: $tour->start , now: ". Carbon::now());
if ($tour->start < Carbon::now()) {
2023-11-11 14:00:39 +01:00
Log::info("TourController::showing tour.edit_old view");
$tour->start = Tour::toGermanDate($tour->start);
$tour->deadline = Tour::toGermanDate($tour->deadline);
return view('tour.edit_old')->with('tour', $tour);
}
2023-01-12 13:50:48 +01:00
$rooms = $this->fetch_rooms($tour->id);
2023-01-11 20:45:29 +01:00
2023-11-11 14:00:39 +01:00
$tour->start = Tour::toGermanDate($tour->start);
$tour->deadline = Tour::toGermanDate($tour->deadline);
Log::info("TourController::showing tour.edit view");
Log::info("TourController::edit =>");
Log::info("$tour");
2020-09-30 10:51:06 +02:00
2023-01-12 13:50:48 +01:00
return view('tour.edit')
->with('tour', $tour)
->with('rooms', $rooms);
}
2023-01-12 13:50:48 +01:00
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Tour $tour
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Tour $tour)
{
2024-02-03 21:33:13 +01:00
Log::info("Tour:update !");
2023-01-12 13:50:48 +01:00
$tour = Tour::find($request->input('tour_id'));
$request->validate(
[
'tour_id' => 'required',
'name' => 'required|min:3',
'user_id' => 'required',
'bild' => 'mimes:jpg,jpeg,bmp,png,gif'
]
);
if ($request->bild) {
$this->storeImage($request->bild, "tour_".$tour->id, $request->name);
/*
Log::info("Processing image");
2023-01-12 13:50:48 +01:00
$bild = Image::make($request->bild);
$breite = $bild->width();
$hoehe = $bild->height();
if ($breite > $hoehe) {
Image::make($request->bild)
->widen(1200)
->save(public_path() . '/img/tour/' . $tour->id . '_gross.jpg');
Image::make($request->bild)
->widen(64)
->save(public_path() . '/img/tour/' . $tour->id . '.jpg');
} else {
Image::make($request->bild)
->heighten(900)
->save(public_path() . '/img/tour/' . $tour->id . '_gross.jpg');
Image::make($request->bild)
->widen(96)
->save(public_path() . '/img/tour/' . $tour->id . '.jpg');
}
Log::info("Image saved as ". public_path() . '/img/tour/' . $tour->id . '_gross.jpg');
Log::info("Image saved as ". public_path() . '/img/tour/' . $tour->id . '.jpg');
*/
2020-10-04 10:41:08 +02:00
}
2024-02-03 21:33:13 +01:00
2023-01-12 13:50:48 +01:00
$tour->name = $request->input('name');
if ($request->beschreibung) $tour->beschreibung = $request->input('beschreibung');
if ($request->start) $tour->start = Tour::toDBDate($request->input('start'));
if ($request->deadline) $tour->deadline = Tour::toDBDate($request->input('deadline'));
if ($request->max_participants) $tour->max_participants = $request->input('max_participants');
2024-02-03 21:33:13 +01:00
if ($request->is_cancelled) {
$tour->is_cancelled = true;
$tour->status="STORNIERT";
} else {
$tour->is_cancelled=false;
$tour->status="FINDET STATT";
}
2023-01-12 13:50:48 +01:00
if ($request->bericht) $tour->bericht = $request->bericht;
2024-02-03 21:33:13 +01:00
2023-01-12 13:50:48 +01:00
$tour->save();
2020-10-04 10:41:08 +02:00
2023-01-12 13:50:48 +01:00
error_log("Is cancelled:" . $tour->is_cancelled);
2023-01-12 13:50:48 +01:00
return redirect('/tour');
2020-10-04 10:41:08 +02:00
2023-01-12 13:50:48 +01:00
}
2020-09-30 10:51:06 +02:00
2023-01-12 13:50:48 +01:00
/*
public function create_room($tour_id, $capacity) {
DB::delete("INSERT INTO booking_rooms")
}
*/
2022-05-07 08:27:50 +02:00
2023-01-12 13:50:48 +01:00
public function upload_gallery(Request $request, Tour $tour)
{
dd($request);
}
2023-01-12 13:50:48 +01:00
/**
* Remove the specified resource from storage.
*
* @param \App\Tour $tour
* @return \Illuminate\Http\Response
*/
public function destroy(Tour $tour)
{
//
}
2020-09-30 10:51:06 +02:00
2023-01-12 13:50:48 +01:00
public function join_room($tour_id, $room_id)
{
Log::info("TourController::join_room");
$user_id = Auth::user()->id;
Tour::join_room($tour_id, $user_id, $room_id);
return redirect('/tour/' . $tour_id);
}
2023-01-09 16:57:27 +01:00
2023-01-12 13:50:48 +01:00
public function join_room4user($tour_id, $room_id, $user_id)
{
Log::info("TourController::join_room");
Tour::join_room($tour_id, $user_id, $room_id);
2023-01-09 16:57:27 +01:00
2023-01-12 13:50:48 +01:00
$tour = Tour::find($tour_id);
$tour->start = Tour::toGermanDate($tour->start);
$tour->deadline = Tour::toGermanDate($tour->deadline);
2023-01-12 13:50:48 +01:00
$rooms = $this->fetch_rooms($tour->id);
2023-01-09 16:57:27 +01:00
2023-01-12 13:50:48 +01:00
return view('tour.edit')
->with('tour', $tour)
->with('rooms', $rooms);
2023-01-12 13:50:48 +01:00
return redirect('/tour/' . $tour_id);
}
2023-01-12 13:50:48 +01:00
public function cancelBooking($tour_id)
{
$user = Auth::user();
Booking::where('tour_id', $tour_id)->where('user_id', $user->id)->delete();
return redirect('/tour');
}
2023-01-12 13:50:48 +01:00
public function cancelBookingAdmin($tour_id, $user_id)
{
$user = Auth::user();
if (Auth::user()->role == "ADMIN") {
Booking::where('tour_id', $tour_id)->where('user_id', $user_id)->delete();
}
return redirect('/tour/' . $tour_id);
}
public function create_room($tour_id, $capacity)
{
Log::info("create_room Controller started");
$user = Auth::user();
Tour::create_room($tour_id, $capacity);
return redirect('tour/' . $tour_id . '/edit');
}
public function delete_room($tour_id, $room_id)
{
Log::info("create_room Controller started");
$user = Auth::user();
DB::update("UPDATE bookings SET room_id=0 WHERE room_id=" . $room_id);
DB::delete("DELETE FROM tour_rooms WHERE tour_id = ? AND id = ?", [$tour_id, $room_id]);
return redirect('tour/' . $tour_id . '/edit');
}
private function getImage($category, $imageName) {
Log::debug("File request for category: ".$category.", File: ".$imageName);
$user=Auth::user();
if ($user) {
if ($user->role == "ADMIN" || $user->role == "MEMBER") {
Log::info("User has proper role: ".$user->role.", serving content");
return response()->file(storage_path() . "/img_storage/".$category."/" . $imageName);
}
}
Log::debug("User $user->name is not authorized to access image $imageName in category $category");
return response()->file(public_path()."/img/placeholder/icon_unknown.png");
}
private function storeImage($bild, $category, $name) {
$cat=Str::slug($category);
$name=Str::slug($name);
$dirPath = storage_path() . "/img_storage/$cat";
if (!file_exists($dirPath)) {
mkdir($dirPath, 0777, true);
}
$bild = Image::make($bild);
$breite = $bild->width();
$hoehe = $bild->height();
if ($breite > $hoehe) {
Image::make($bild)
->widen(1200)
->save("$dirPath/$name"."_gross.jpg");
Image::make($bild)
->widen(64)
->save("$dirPath/$name".".jpg");
} else {
Image::make($bild)
->heighten(900)
->save("$dirPath/$name"."_gross.jpg");;
Image::make($bild)
->widen(96)
->save("$dirPath/$name".".jpg");
}
Log::info("Image saved as $dirPath/$name" . '_gross.jpg');
Log::info("Image saved as $dirPath/$name" . '.jpg');
2022-06-28 07:19:15 +02:00
}
2024-02-03 21:33:13 +01:00
}