249 lines
8.4 KiB
PHP
249 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
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;
|
|
|
|
class WashingReservationController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware([ "auth" ]);
|
|
|
|
$this->middleware([ "check.auth:washing.machine.reservation.list" ])->only(["index", "appindex"]);
|
|
$this->middleware([ "check.auth:washing.machine.reservation.show" ])->only("show");
|
|
$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)
|
|
{
|
|
$reservations = WashingReservation::query()->orderBY('time' , 'asc')->paginate($request->query("limit", 20));
|
|
|
|
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;
|
|
$machineReservation->save();
|
|
|
|
$saved = $machineReservation->save();
|
|
|
|
if (!$saved) {
|
|
return Response::detect("washing-reservations.store", [
|
|
"washing_reservation" => $machineReservation
|
|
]);
|
|
} else {
|
|
$reservations = WashingReservation::query()->paginate($request->input("limit", 20));
|
|
|
|
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)
|
|
{
|
|
$reservation = WashingReservation::query()->find($id);
|
|
$machines = WashingMachine::all();
|
|
|
|
return Response::detect("washing-reservations.edit", ['washing_reservation' => $reservation, 'machines' => $machines ]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return Application|Factory|View
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$data = $request->validate([
|
|
"time" => "required",
|
|
"machine" => "required"
|
|
]);
|
|
|
|
$machineReservation = WashingReservation::find($id);
|
|
|
|
$machineReservation->update($data);
|
|
|
|
$saved = $machineReservation->save();
|
|
|
|
if(!$saved){
|
|
return Response::detect("washing-reservations.update", [
|
|
"washing_reservation" => $machineReservation
|
|
]);
|
|
}else{
|
|
$reservations = WashingReservation::query()->paginate($request->query("limit", 20));
|
|
|
|
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return RedirectResponse
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$machineReservation = WashingReservation::find($id);
|
|
$machineReservation->delete();
|
|
|
|
$reservations = WashingReservation::query()->paginate( 20);
|
|
|
|
return redirect()->route("washing-reservations.index", [ "reservations" => $reservations]);
|
|
}
|
|
|
|
public function api(Request $request){
|
|
if($request->ajax()){
|
|
$date = $request->date;
|
|
|
|
$machines = WashingMachine::all();
|
|
|
|
if($request->machine_id == 0)
|
|
$request->machine_id = WashingMachine::all()->first()->id;
|
|
|
|
$reservations = WashingReservation::query()->where("machine_id", "=", $request->machine_id)->where("time", "LIKE", substr($date, 1, strpos($date, "T"))."%")->get();
|
|
|
|
$times = [];
|
|
|
|
foreach ($reservations as $reservation){
|
|
array_push($times, $reservation->time);
|
|
}
|
|
//2020-07-28%
|
|
$output = json_encode(['date' => $date, 'washingmachines' => $machines, 'unavailable_times' => $times]);
|
|
return Response($output);
|
|
}
|
|
}
|
|
|
|
function search(Request $request){
|
|
if($request->ajax()){
|
|
$output = "<tr>".
|
|
"<th>Vaskemaskine</th>".
|
|
"<th>Tidspunkt</th>".
|
|
"<th>Bruger</th>".
|
|
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
|
|
"</tr>";
|
|
|
|
|
|
//Kan ikke søge på vaskemaskine da det er en foreign key
|
|
//Kan ikke søge på bruger
|
|
//Man kan søge på tidspunkt!
|
|
|
|
|
|
|
|
|
|
|
|
//filter search
|
|
if($request->isCheck === "vaskemaskine")
|
|
$users = WashingReservation::query()->where('machine_id', 'LIKE',$request->search.'%')->get();
|
|
elseif ($request->isCheck === "tidspunkt")
|
|
$users = WashingReservation::query()->where('time', 'LIKE',$request->search.'%')->get();
|
|
else
|
|
$users = WashingReservation::query()->where('time', 'LIKE',$request->search.'%')
|
|
->orWhere('machine','LIKE', $request->search.'%')
|
|
->get();
|
|
|
|
|
|
|
|
if(count($users) !== 0){
|
|
foreach ($users as $key => $user){
|
|
$output.='<tr>'.
|
|
'<td>' . WashingMachine::query()->find($user->machine_id)->name . '</td>'.
|
|
'<td>' . $user->time . '</td>'.
|
|
'<td>' . ucfirst(User::query()->find($user->user_id)->name_first) . ' ' . ucfirst(User::query()->find($user->user_id)->name_last) . '</td>'.
|
|
'<td><form method="post" action="' .route("washing-reservations.destroy", [ "washing_reservation" => $user->id ]). '" class="w-100 nostyle">'.
|
|
csrf_field().
|
|
method_field("delete").
|
|
|
|
'<button class="w-100 nostyle" onclick="return confirm(\'Are you sure you want to delete?\');" type="submit"><img class="w-100 cursor-pointer" src="'. asset('/images/icons/trashcan-dark.svg') . '" alt="Delete"></button>'.
|
|
'</form>'.
|
|
'</td>'.
|
|
'</tr>';
|
|
}
|
|
}else{
|
|
$output.='<tr>'.
|
|
'<td>Intet match</td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'</tr>';
|
|
}
|
|
return Response($output);
|
|
}
|
|
}
|
|
|
|
public function appindex(Request $request)
|
|
{
|
|
$reservations = WashingReservation::query()->where("user_id", "=", auth()->user()->id)->orderBY('time' , 'asc')->paginate($request->query("limit", 20));
|
|
|
|
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
|
|
}
|
|
}
|
|
|