Lager-v3/app/Http/Controllers/Rooms/BuildingController.php

243 lines
7.8 KiB
PHP

<?php
namespace App\Http\Controllers\Rooms;
use App\Helpers\Logger;
use App\Http\Controllers\Controller;
use App\Models\Building;
use App\Models\Permission;
use App\Models\Role;
use App\Models\Room;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BuildingController extends Controller
{
/**
* Display a listing of the resource.
*
*/
public function index(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
$PerPagination = $request->input('p') ?? 10;
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
$search_compare = $request->input('search_compare');
if($search_term != ""){
switch ($search_type){
case "name":
$buildings = Building::where(function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
})->orderBy('name', 'asc')->paginate($PerPagination);
break;
}
}
else{
$buildings = Building::orderBy('name', 'asc')->paginate($PerPagination);
}
return view('buildings.index')
->with('search_types',$search_types)
->with('data',$buildings)
->with('data_name','building')
->with('data_names','buildings')
;
}
/**
* Display a listing of the resource.
*
*/
public function deleted(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_viewAny_deleted'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
$PerPagination = $request->input('p');
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
if($search_term != ""){
switch ($search_type){
case "name":
$buildings = Building::where(function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
})->orderBy('name', 'asc')->paginate($PerPagination);
break;
}
}
else{
$buildings = Building::onlyTrashed()->orderBy('name', 'asc')->paginate($PerPagination);
}
return view('buildings.deleted')
->with('search_types',$search_types)
->with('data',$buildings)
->with('data_name','building')
->with('data_names','buildings')
;
}
/**
* Show the form for creating a new resource.
*
*/
public function create()
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
return view('buildings.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
$building = new Building();
$building->name = $request->name;
$building->save();
Logger::LogCreated($building->id,get_class($building));
return redirect()->route('buildings.show',['building' => $building]);
}
/**
* Display the specified resource.
*
* @return Application|Factory|View
*/
public function show($building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_view'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
return view('buildings.show')
->with('data',$object)
->with('data_name','building')
;
}
/**
* Show the form for editing the specified resource.
*
* @return Application|Factory|View
*/
public function edit($building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
return view('buildings.edit')
->with('data',$object)
->with('data_name','building')
;
}
/**
* Update the specified resource in storage.
*
*/
public function update(Request $request, $building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
if( $object->name != $request->name){
Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->name." til ".$request->name);
$object->name = $request->name;
}
$object->save();
return redirect()->route('buildings.show',['building' => $building]);
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
Logger::LogDeleted($object->id,get_class($object));
$object->delete();
return redirect()->route('buildings.index');
}
/**
* Permanently Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete_force($building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
Logger::LogForceDeleted($object->id,get_class($object));
$object->forceDelete();
return redirect()->route('buildings.deleted');
}
/**
* Permanently Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($building)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'buildings_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Building::withTrashed()->where('id','=',$building)->first();
$object->restore();
Logger::LogRestored($object->id,get_class($object));
return redirect()->route('buildings.deleted');
}
}