2020-06-09 07:05:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-08-06 06:37:16 +00:00
|
|
|
use App\Location;
|
2020-08-05 08:11:28 +00:00
|
|
|
use App\WashingReservation;
|
2020-07-01 08:30:28 +00:00
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
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-09 07:05:07 +00:00
|
|
|
|
2020-06-10 10:30:44 +00:00
|
|
|
use App\WashingMachine;
|
2020-08-25 07:25:42 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-07-01 08:30:28 +00:00
|
|
|
use Illuminate\View\View;
|
2020-06-10 10:30:44 +00:00
|
|
|
|
2020-06-09 07:05:07 +00:00
|
|
|
class WashingMachineController 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
|
|
|
|
|
|
|
$this->middleware([ "check.auth:washing.machine.list" ])->only("index");
|
|
|
|
$this->middleware([ "check.auth:washing.machine.show" ])->only("show");
|
|
|
|
$this->middleware([ "check.auth:washing.machine.create" ])->only("create", "store");
|
|
|
|
$this->middleware([ "check.auth:washing.machine.edit" ])->only("edit", "update");
|
|
|
|
$this->middleware([ "check.auth:washing.machine.delete" ])->only("delete");
|
|
|
|
}
|
|
|
|
|
2020-06-09 07:05:07 +00:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2020-06-10 10:30:44 +00:00
|
|
|
* @param Request $request
|
2020-07-01 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
2020-06-10 10:30:44 +00:00
|
|
|
public function index(Request $request)
|
2020-06-09 07:05:07 +00:00
|
|
|
{
|
2020-07-01 08:30:28 +00:00
|
|
|
$machines = WashingMachine::query()->paginate($request->query("limit", 20));
|
2020-06-10 10:30:44 +00:00
|
|
|
|
2020-06-24 06:53:07 +00:00
|
|
|
return Response::detect("washing-machines.index", [ "machines" => $machines ]);
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-07-01 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2020-08-06 06:37:16 +00:00
|
|
|
$locations = Location::all();
|
|
|
|
|
|
|
|
return Response::detect("washing-machines.create", ["locations" => $locations] );
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-07-01 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2020-06-10 10:30:44 +00:00
|
|
|
$data = $request->validate([
|
2020-08-06 06:37:16 +00:00
|
|
|
"name" => "required",
|
|
|
|
"location_id" => "required"
|
2020-06-10 10:30:44 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$machine = new WashingMachine($data);
|
|
|
|
|
2020-08-06 06:37:16 +00:00
|
|
|
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->get();
|
2020-08-04 06:31:14 +00:00
|
|
|
|
|
|
|
// If there already is a washing machine with that name, then don't add it
|
|
|
|
if (count($allMachines) > 0)
|
2020-08-17 09:07:26 +00:00
|
|
|
return redirect()->route("washing-machines.store");
|
2020-08-04 06:31:14 +00:00
|
|
|
else { // Else - Add it
|
|
|
|
$machine->save();
|
2020-07-01 09:54:33 +00:00
|
|
|
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
|
2020-08-04 06:31:14 +00:00
|
|
|
return redirect()->route("washing-machines.index", ['machines' => $machines]);
|
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 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2020-06-10 10:30:44 +00:00
|
|
|
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
2020-07-01 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2020-06-10 10:30:44 +00:00
|
|
|
$machine = WashingMachine::find($id);
|
2020-08-06 06:37:16 +00:00
|
|
|
$locations = Location::all();
|
2020-06-10 10:30:44 +00:00
|
|
|
|
2020-06-24 06:53:07 +00:00
|
|
|
return Response::detect("washing-machines.edit", [
|
2020-08-06 06:37:16 +00:00
|
|
|
"machine" => $machine,
|
|
|
|
"locations" => $locations
|
2020-06-10 10:30:44 +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 08:30:28 +00:00
|
|
|
* @return Application|Factory|View
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2020-06-10 10:30:44 +00:00
|
|
|
$data = $request->validate([
|
2020-08-06 06:37:16 +00:00
|
|
|
"name" => "required",
|
|
|
|
"location_id" => "required",
|
2020-06-10 10:30:44 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$machine = WashingMachine::find($id);
|
|
|
|
|
|
|
|
|
2020-08-06 06:37:16 +00:00
|
|
|
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->where('id', '!=', $id)->get();
|
2020-07-01 08:30:28 +00:00
|
|
|
|
2020-08-04 06:31:14 +00:00
|
|
|
// If there already is a washing machine with that name, then don't change it
|
|
|
|
if (count($allMachines) > 0)
|
2020-08-17 09:07:26 +00:00
|
|
|
return redirect()->route("washing-machines.store");
|
2020-08-04 06:31:14 +00:00
|
|
|
else { // Else - Change the name
|
|
|
|
$machine->update($data);
|
|
|
|
$machine->save();
|
2020-07-01 08:30:28 +00:00
|
|
|
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
|
2020-08-04 06:31:14 +00:00
|
|
|
return redirect()->route("washing-machines.index", ["machines" => $machines]);
|
2020-07-01 08:30:28 +00:00
|
|
|
}
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-07-01 08:30:28 +00:00
|
|
|
* @param $id
|
|
|
|
* @return Response
|
2020-06-09 07:05:07 +00:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-06-10 10:30:44 +00:00
|
|
|
$machine = WashingMachine::find($id);
|
2020-08-05 08:11:28 +00:00
|
|
|
|
|
|
|
$allReservations = WashingReservation::query()->where('machine_id', '=', $id);
|
|
|
|
|
|
|
|
$allReservations->delete();
|
2020-06-10 10:30:44 +00:00
|
|
|
$machine->delete();
|
|
|
|
|
2020-07-01 08:56:04 +00:00
|
|
|
return redirect()->route("washing-machines.index");
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|
2020-08-17 08:31:33 +00:00
|
|
|
|
|
|
|
public function nameCheck(Request $request){
|
2020-08-17 09:07:26 +00:00
|
|
|
$washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->get();
|
2020-08-17 08:31:33 +00:00
|
|
|
if(count($washing) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:07:26 +00:00
|
|
|
public function nameCheckUpdate(Request $request){
|
|
|
|
$washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->where('location_id', '=', $request->location)->where('id', '!=', $request->id)->get();
|
|
|
|
if(count($washing) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 07:25:42 +00:00
|
|
|
|
|
|
|
public function search(Request $request){
|
|
|
|
if($request->ajax()){
|
|
|
|
$output = "<tr>".
|
|
|
|
"<th>Navn</th>".
|
|
|
|
"<th>Lokation</th>".
|
|
|
|
"<th style=\"width: 1em;\"><img class=\"w-100\" src=".asset('/images/icons/pencil.svg')." alt=\"Update\"></th>".
|
|
|
|
"<th style=\"width: 1em;\"><img class=\"w-100\" src=".asset('/images/icons/trashcan.svg')." alt=\"Delete\"></th>".
|
|
|
|
"</tr>";
|
|
|
|
|
|
|
|
if($request->option !== 'all') {
|
|
|
|
$machines = DB::table('washing_machines')
|
|
|
|
->join('locations', 'washing_machines.location_id','=', 'locations.id')
|
|
|
|
->select(DB::raw('washing_machines.name as washing_name, locations.name as location_name, washing_machines.id as id'))
|
|
|
|
->where('locations.id', '=', $request->option)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
if(count($machines) !== 0){
|
|
|
|
foreach ($machines as $machine){
|
|
|
|
$output.='<tr>'.
|
|
|
|
'<td>' . $machine->washing_name . '</td>'.
|
|
|
|
'<td>' . $machine->location_name . '</td>'.
|
|
|
|
'<td><a href="'. route("washing-machines.edit", [ "washing_machine" => $machine->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
|
|
|
|
'<td><form method="post" action="' .route("washing-machines.destroy", [ "washing_machine" => $machine->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>'.
|
2020-08-25 12:07:20 +00:00
|
|
|
'<td>Det er ikke blivet oprettet nogen vaskmaskine på denne lokation endnu</td>'.
|
2020-08-25 07:25:42 +00:00
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'</tr>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif($request->option === "all"){
|
|
|
|
$machines = WashingMachine::all();
|
|
|
|
|
|
|
|
if(count($machines) !== 0){
|
|
|
|
foreach($machines as $machine) {
|
|
|
|
$output.= '<tr>'.
|
|
|
|
'<td>'.$machine->name.'</td>'.
|
|
|
|
'<td>'.Location::query()->where("id", "=", $machine->location_id)->first()->name.'</td>'.
|
|
|
|
'<td><a href="'. route("washing-machines.edit", [ "washing_machine" => $machine->id]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
|
|
|
|
'<td><form method="post" action="'. route('washing-machines.destroy', [ 'washing_machine' => $machine->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>'.
|
2020-08-25 12:07:20 +00:00
|
|
|
'<td>Det er ikke blivet oprettet nogen vaskmaskine på denne lokation endnu</td>'.
|
2020-08-25 07:25:42 +00:00
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'</tr>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Response($output);
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 07:05:07 +00:00
|
|
|
}
|