2020-08-06 06:37:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Location;
|
2020-08-18 06:30:20 +00:00
|
|
|
use App\WashingMachine;
|
2020-08-18 10:25:32 +00:00
|
|
|
use App\WashingReservation;
|
2020-08-06 06:37:16 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2020-08-21 08:54:00 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use function GuzzleHttp\Promise\all;
|
2020-08-06 06:37:16 +00:00
|
|
|
|
|
|
|
class LocationController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware([ "auth" ]);
|
2020-08-06 13:31:38 +00:00
|
|
|
$this->middleware([ "lang" ]);
|
2020-08-06 06:37:16 +00:00
|
|
|
|
2020-08-31 07:36:10 +00:00
|
|
|
$this->middleware([ "check.auth:locations.show" ])->only("show", "index");
|
2020-08-06 06:37:16 +00:00
|
|
|
$this->middleware([ "check.auth:locations.create" ])->only("create", "store");
|
|
|
|
$this->middleware([ "check.auth:locations.edit" ])->only("edit", "update");
|
|
|
|
$this->middleware([ "check.auth:locations.delete" ])->only("delete");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$locations = Location::query()->paginate($request->input("limit", 20));
|
|
|
|
|
|
|
|
return Response::detect("locations.index", [ "locations" => $locations ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Response::detect("locations.create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$data = $request->validate([
|
|
|
|
"name" => "required",
|
|
|
|
]);
|
|
|
|
|
|
|
|
$location = new Location($data);
|
|
|
|
|
|
|
|
$locations = Location::query()->where('name', '=', $request->name)->get();
|
|
|
|
|
|
|
|
// If there already is a washing machine with that name, then don't add it
|
|
|
|
if (count($locations) > 0)
|
2020-08-17 08:43:52 +00:00
|
|
|
return redirect()->route("locations.index");
|
2020-08-06 06:37:16 +00:00
|
|
|
else { // Else - Add it
|
|
|
|
$location->save();
|
|
|
|
$locations = Location::query()->paginate($request->input("limit", 20));
|
|
|
|
return redirect()->route("locations.index", ['locations' => $locations]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Location $location
|
2020-08-06 08:32:34 +00:00
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2020-08-06 06:37:16 +00:00
|
|
|
*/
|
|
|
|
public function show(Location $location)
|
|
|
|
{
|
2020-08-06 08:32:34 +00:00
|
|
|
return view("admin.locations.show", [ "location" => $location]);
|
2020-08-06 06:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Location $location
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit(Location $location)
|
|
|
|
{
|
|
|
|
return Response::detect("locations.edit", [ "location" => $location] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \App\Location $location
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$data = $request->validate([
|
|
|
|
"name" => "required",
|
|
|
|
]);
|
|
|
|
|
|
|
|
$location = Location::find($id);
|
|
|
|
|
|
|
|
|
|
|
|
$allMachines = Location::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get();
|
|
|
|
|
|
|
|
// If there already is a washing machine with that name, then don't change it
|
|
|
|
if (count($allMachines) > 0)
|
2020-08-17 08:43:52 +00:00
|
|
|
return redirect()->route("locations.index");
|
2020-08-06 06:37:16 +00:00
|
|
|
else { // Else - Change the name
|
|
|
|
$location->update($data);
|
|
|
|
$location->save();
|
|
|
|
|
|
|
|
$locations = Location::query()->paginate($request->input("limit", 20));
|
|
|
|
return redirect()->route("locations.index", ["locations" => $locations]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-08-17 08:43:52 +00:00
|
|
|
* @param int $id
|
2020-08-06 06:37:16 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-08-17 08:43:52 +00:00
|
|
|
public function destroy($id)
|
2020-08-06 06:37:16 +00:00
|
|
|
{
|
2020-08-18 10:25:32 +00:00
|
|
|
$location = Location::find($id);
|
|
|
|
$washingMachines = WashingMachine::query()->where('location_id', '=', $id)->get();
|
|
|
|
|
|
|
|
foreach ($washingMachines as $machine) {
|
|
|
|
$washingReservations = WashingReservation::query()->where('machine_id', '=', $machine->id)->get();
|
2020-08-18 06:30:20 +00:00
|
|
|
|
2020-08-18 10:25:32 +00:00
|
|
|
foreach ($washingReservations as $reservation)
|
|
|
|
{
|
|
|
|
$reservation->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$machine->delete();
|
|
|
|
}
|
2020-08-18 06:30:20 +00:00
|
|
|
|
2020-08-18 10:25:32 +00:00
|
|
|
$location->delete();
|
2020-08-06 06:37:16 +00:00
|
|
|
|
2020-08-17 08:43:52 +00:00
|
|
|
return redirect()->route("locations.index");
|
2020-08-06 06:37:16 +00:00
|
|
|
}
|
2020-08-17 08:31:33 +00:00
|
|
|
|
|
|
|
public function nameCheck(Request $request){
|
|
|
|
$locations = Location::query()->where('name', 'LIKE',$request->nameCheck)->get();
|
|
|
|
if(count($locations) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 08:43:52 +00:00
|
|
|
public function nameCheckUpdate(Request $request){
|
|
|
|
$locations = Location::query()->where('name', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
|
|
|
|
if(count($locations) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 06:37:16 +00:00
|
|
|
}
|