2020-06-09 07:05:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-07-01 09:38:09 +00:00
|
|
|
use App\WashingMachine;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
2020-07-29 09:53:37 +00:00
|
|
|
use Illuminate\Foundation\Auth\User;
|
2020-07-01 09:38:09 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2020-06-09 07:05:07 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-06-24 06:23:15 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
|
2020-06-15 06:59:15 +00:00
|
|
|
use App\WashingReservation;
|
2020-07-29 13:27:07 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-07-01 09:38:09 +00:00
|
|
|
use Illuminate\View\View;
|
2020-06-09 07:05:07 +00:00
|
|
|
|
|
|
|
class WashingReservationController extends Controller
|
|
|
|
{
|
2020-06-30 08:04:06 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware([ "auth" ]);
|
|
|
|
|
|
|
|
$this->middleware([ "check.auth:washing.machine.reservation.list" ])->only("index");
|
|
|
|
$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");
|
|
|
|
}
|
|
|
|
|
2020-06-09 07:05:07 +00:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2020-06-15 06:59:15 +00:00
|
|
|
* @param Request $request
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
2020-06-15 06:59:15 +00:00
|
|
|
public function index(Request $request)
|
2020-06-09 07:05:07 +00:00
|
|
|
{
|
2020-08-03 09:17:36 +00:00
|
|
|
$reservations = WashingReservation::query()->orderBY('time' , 'asc')->paginate($request->query("limit", 20));
|
2020-06-15 06:59:15 +00:00
|
|
|
|
2020-06-25 11:28:51 +00:00
|
|
|
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2020-07-01 09:38:09 +00:00
|
|
|
$machines = WashingMachine::all();
|
2020-07-29 09:53:37 +00:00
|
|
|
$users = User::all();
|
|
|
|
return Response::detect("washing-reservations.create", [ 'machines' => $machines, 'users' => $users ]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2020-06-15 06:59:15 +00:00
|
|
|
$data = $request->validate([
|
2020-07-01 09:38:09 +00:00
|
|
|
"time" => "required",
|
2020-07-29 12:59:35 +00:00
|
|
|
"machine_id" => "required|unique:washing_reservations,machine_id,NULL,id,time,' . $request->time"
|
2020-06-15 06:59:15 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$machineReservation = new WashingReservation($data);
|
2020-07-29 12:59:35 +00:00
|
|
|
$machineReservation->user_id = auth()->user()->id;
|
2020-06-15 06:59:15 +00:00
|
|
|
$machineReservation->save();
|
|
|
|
|
2020-07-01 09:54:33 +00:00
|
|
|
$saved = $machineReservation->save();
|
|
|
|
|
2020-07-29 09:53:37 +00:00
|
|
|
if (!$saved) {
|
2020-07-01 09:54:33 +00:00
|
|
|
return Response::detect("washing-reservations.store", [
|
|
|
|
"washing_reservation" => $machineReservation
|
|
|
|
]);
|
2020-07-29 09:53:37 +00:00
|
|
|
} else {
|
2020-07-01 09:54:33 +00:00
|
|
|
$reservations = WashingReservation::query()->paginate($request->input("limit", 20));
|
2020-07-01 09:56:44 +00:00
|
|
|
|
2020-08-03 09:17:36 +00:00
|
|
|
return redirect()->route('washing-reservations.index', ["reservations" => $reservations]);
|
2020-07-01 09:54:33 +00:00
|
|
|
}
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2020-06-15 06:59:15 +00:00
|
|
|
$machineReservation = WashingReservation::find($id);
|
|
|
|
|
2020-06-25 11:28:51 +00:00
|
|
|
return Response::detect("washing-reservations.show", [
|
2020-06-15 06:59:15 +00:00
|
|
|
"machineReservation" => $machineReservation
|
|
|
|
]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2020-07-01 09:38:09 +00:00
|
|
|
$reservation = WashingReservation::query()->find($id);
|
|
|
|
$machines = WashingMachine::all();
|
|
|
|
|
|
|
|
return Response::detect("washing-reservations.edit", ['washing_reservation' => $reservation, 'machines' => $machines ]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2020-06-15 06:59:15 +00:00
|
|
|
$data = $request->validate([
|
2020-07-01 09:38:09 +00:00
|
|
|
"time" => "required",
|
|
|
|
"machine" => "required"
|
2020-06-15 06:59:15 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$machineReservation = WashingReservation::find($id);
|
|
|
|
|
|
|
|
$machineReservation->update($data);
|
|
|
|
|
2020-07-01 09:54:33 +00:00
|
|
|
$saved = $machineReservation->save();
|
2020-06-15 06:59:15 +00:00
|
|
|
|
2020-07-01 09:54:33 +00:00
|
|
|
if(!$saved){
|
|
|
|
return Response::detect("washing-reservations.update", [
|
|
|
|
"washing_reservation" => $machineReservation
|
|
|
|
]);
|
|
|
|
}else{
|
2020-07-02 08:06:17 +00:00
|
|
|
$reservations = WashingReservation::query()->paginate($request->query("limit", 20));
|
2020-07-01 09:56:44 +00:00
|
|
|
|
2020-07-02 08:06:17 +00:00
|
|
|
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
|
2020-07-01 09:54:33 +00:00
|
|
|
}
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
2020-07-01 09:38:09 +00:00
|
|
|
* @return RedirectResponse
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-06-15 06:59:15 +00:00
|
|
|
$machineReservation = WashingReservation::find($id);
|
|
|
|
$machineReservation->delete();
|
|
|
|
|
2020-07-01 09:38:09 +00:00
|
|
|
$reservations = WashingReservation::query()->paginate( 20);
|
|
|
|
|
|
|
|
return redirect()->route("washing-reservations.index", [ "reservations" => $reservations]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
2020-07-28 12:26:32 +00:00
|
|
|
|
|
|
|
public function api(Request $request){
|
|
|
|
if($request->ajax()){
|
|
|
|
$date = $request->date;
|
|
|
|
|
|
|
|
$machines = WashingMachine::all();
|
|
|
|
|
2020-07-29 09:53:37 +00:00
|
|
|
$reservations = WashingReservation::query()->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]);
|
2020-07-28 12:26:32 +00:00
|
|
|
return Response($output);
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 13:27:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/pencil.svg\" alt=\"Update\"></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
|
2020-07-30 07:17:27 +00:00
|
|
|
//Kan ikke søge på bruger
|
|
|
|
//Man kan søge på tidspunkt!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-29 13:27:07 +00:00
|
|
|
|
|
|
|
//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><a href="'. route("washing-reservations.edit", [ "washing_reservation" => $user->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-30 07:17:27 +00:00
|
|
|
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
2020-07-28 07:21:41 +00:00
|
|
|
|