2020-06-09 07:05:07 +00:00
< ? php
namespace App\Http\Controllers ;
2020-08-06 08:32:34 +00:00
use App\Location ;
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
2020-08-25 10:26:48 +00:00
date_default_timezone_set ( 'Europe/Copenhagen' );
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 " ]);
2020-08-06 13:31:38 +00:00
$this -> middleware ([ " lang " ]);
2020-06-30 08:04:06 +00:00
2020-08-31 07:36:10 +00:00
$this -> middleware ([ " check.auth:washing.machine.reservation.show " ]) -> only ( " show " , " index " , " appindex " );
2020-06-30 08:04:06 +00:00
$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-25 10:26:48 +00:00
WashingReservation :: query () -> where ( 'time' , '<' , date ( 'Y-m-d H:i:s' , strtotime ( '-1 hour' ))) -> delete ();
2020-09-18 08:04:33 +00:00
$reservations = WashingReservation :: query () -> orderBy ( 'time' , 'asc' ) -> get ();
2020-09-03 06:31:31 +00:00
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
2020-08-04 13:09:10 +00:00
$allMachineReservations = WashingReservation :: query () -> where ( 'time' , '=' , $request -> time ) -> where ( 'machine_id' , '=' , $request -> machine_id ) -> get ();
2020-07-01 09:54:33 +00:00
2020-08-04 13:09:10 +00:00
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>' );
2020-07-29 09:53:37 +00:00
} else {
2020-08-04 13:09:10 +00:00
$machineReservation -> save ();
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 12:27:40 +00:00
return redirect () -> route ( 'washing-reservations.appindex' , [ " 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
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
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 );
2020-08-05 07:15:50 +00:00
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 ]);
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 ;
2020-08-04 13:29:21 +00:00
$datetext = $request -> datetext ;
2020-07-28 12:26:32 +00:00
2020-08-06 08:32:34 +00:00
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 ();
2020-07-28 12:26:32 +00:00
2020-08-03 10:57:14 +00:00
if ( $request -> machine_id == 0 )
2020-08-18 20:47:39 +00:00
$request -> machine_id = WashingMachine :: query () -> where ( " location_id " , " = " , $request -> location_id ) -> orderBy ( " name " , " asc " ) -> first () -> id ;
2020-08-03 10:57:14 +00:00
2020-08-04 13:29:21 +00:00
$reservations = WashingReservation :: query () -> where ( " machine_id " , " = " , $request -> machine_id ) -> where ( " time " , " LIKE " , $datetext . " % " ) -> get ();
2020-07-29 09:53:37 +00:00
$times = [];
foreach ( $reservations as $reservation ){
array_push ( $times , $reservation -> time );
}
2020-08-04 13:09:10 +00:00
2020-08-06 08:32:34 +00:00
$locations = Location :: query () -> orderBy ( " name " , " asc " ) -> get ();
2020-08-18 20:47:39 +00:00
$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 );
}
}
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 );
}
}
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 ]);
2020-07-28 12:26:32 +00:00
return Response ( $output );
}
}
2020-07-29 13:27:07 +00:00
2020-08-03 12:26:58 +00:00
public function appindex ( Request $request )
{
2020-08-25 10:26:48 +00:00
WashingReservation :: query () -> where ( 'time' , '<' , date ( 'Y-m-d H:i:s' , strtotime ( '-1 hour' ))) -> delete ();
2020-09-07 10:44:24 +00:00
$reservations = WashingReservation :: query () -> where ( " user_id " , " = " , auth () -> user () -> id ) -> orderBY ( 'time' , 'asc' ) -> paginate ( 10 );
2020-07-29 13:27:07 +00:00
2020-08-03 12:26:58 +00:00
return Response :: detect ( " washing-reservations.index " , [ " reservations " => $reservations ]);
}
2020-06-09 07:05:07 +00:00
}