Ekapp/skolehjem/app/Http/Controllers/WashingReservationControlle...

222 lines
7.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Location;
use App\WashingMachine;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\WashingReservation;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
date_default_timezone_set('Europe/Copenhagen');
class WashingReservationController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "lang" ]);
$this->middleware([ "check.auth:washing.machine.reservation.show" ])->only("show", "index", "appindex");
$this->middleware([ "check.auth:washing.machine.reservation.create" ])->only("create", "store");
$this->middleware([ "check.auth:washing.machine.reservation.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:washing.machine.reservation.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @param Request $request
* @return Application|Factory|View
*/
public function index(Request $request)
{
WashingReservation::query()->where('time', '<', date('Y-m-d H:i:s', strtotime('-1 hour')))->delete();
$reservations = WashingReservation::query()->orderBy('time', 'asc')->get();
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create()
{
$machines = WashingMachine::all();
$users = User::all();
return Response::detect("washing-reservations.create", [ 'machines' => $machines, 'users' => $users ]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Application|Factory|View
*/
public function store(Request $request)
{
$data = $request->validate([
"time" => "required",
"machine_id" => "required|unique:washing_reservations,machine_id,NULL,id,time,' . $request->time"
]);
$machineReservation = new WashingReservation($data);
$machineReservation->user_id = auth()->user()->id;
$allMachineReservations = WashingReservation::query()->where('time', '=', $request->time)->where('machine_id', '=', $request->machine_id)->get();
//Check if a reservation at that time on that machine already exists
if (count($allMachineReservations) > 0) {
return redirect()->route("washing-reservations.create", ["washing_reservation" => $machineReservation])->with('ReservationExists', '<p class="text-center"><b>Der findes allerede en reservation til denne tid, men denne vaskemaskine!</b></p>');
} else {
$machineReservation->save();
$reservations = WashingReservation::query()->get();
return redirect()->route('washing-reservations.appindex', ["reservations" => $reservations]);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function show($id)
{
$machineReservation = WashingReservation::find($id);
return Response::detect("washing-reservations.show", [
"machineReservation" => $machineReservation
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function edit($id)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return Application|Factory|View
*/
public function update(Request $request, $id)
{
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return RedirectResponse
*/
public function destroy($id)
{
$machineReservation = WashingReservation::find($id);
$machineReservation->delete();
$reservations = WashingReservation::query()->get();
if(app('router')->getRoutes()->match(app('request')->create(url()->previous()))->getName() == "washing-reservations.appindex")
return redirect()->route("washing-reservations.appindex");
else
return redirect()->route("washing-reservations.index", [ "reservations" => $reservations]);
}
//Used for creating the "create reservation" form in the app
public function api(Request $request){
if($request->ajax()){
$date = $request->date;
$datetext = $request->datetext;
if($request->location_id == 0)
$request->location_id = Location::all()->first()->id;
$machines = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->get();
if($request->machine_id == 0)
$request->machine_id = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->first()->id;
$reservations = WashingReservation::query()->where("machine_id", "=", $request->machine_id)->where("time", "LIKE", $datetext."%")->get();
$times = [];
foreach ($reservations as $reservation){
array_push($times, $reservation->time);
}
$locations = Location::query()->orderBy("name", "asc")->get();
$output = json_encode(['date' => $date, 'washingmachines' => $machines, 'unavailable_times' => $times, "locations" => $locations, "machine_id" => $request->machine_id, "location_id" => $request->location_id ]);
return Response($output);
}
}
//Returns the washingmachines to an ajax request
public function getMachines(Request $request){
if($request->ajax()){
if($request->location_id == 0)
$request->location_id = Location::all()->first()->id;
$machines = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->get();
$output = json_encode(["washingmachines" => $machines]);
return Response($output);
}
}
//Returns the unavailable reservation times to an ajax request
public function getTimes(Request $request){
if($request->ajax()){
if($request->location_id == 0)
$request->location_id = Location::all()->first()->id;
if($request->machine_id == 0)
$request->machine_id = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->first()->id;
$reservations = WashingReservation::query()->where("machine_id", "=", $request->machine_id)->where("time", "LIKE", $request->datetext."%")->get();
$times = [];
foreach ($reservations as $reservation){
array_push($times, $reservation->time);
}
$output = json_encode(["unavailable_times" => $times]);
return Response($output);
}
return null;
}
public function appindex(Request $request)
{
WashingReservation::query()->where('time', '<', date('Y-m-d H:i:s', strtotime('-1 hour')))->delete();
$reservations = WashingReservation::query()->where("user_id", "=", auth()->user()->id)->orderBY('time' , 'asc')->get();
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
}
}