285 lines
10 KiB
PHP
285 lines
10 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\Rooms;
|
||
|
|
||
|
use App\Helpers\Logger;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Models\Building;
|
||
|
use App\Models\Loan;
|
||
|
use App\Models\LoanType;
|
||
|
use App\Models\Permission;
|
||
|
use App\Models\Room;
|
||
|
use App\Models\User;
|
||
|
use Illuminate\Auth\Access\Response;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
|
||
|
class RoomController extends Controller
|
||
|
{
|
||
|
/**
|
||
|
* Display a listing of the resource.
|
||
|
*
|
||
|
*/
|
||
|
public function index(Request $request)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_viewAny'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$search_types = [];
|
||
|
array_push($search_types,array("value" => "building", "name" => "building"));
|
||
|
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 "building":
|
||
|
$rooms = Room::where(function ($query) use ($search_term){
|
||
|
$query->whereHas('building',function ($query) use ($search_term){
|
||
|
$query->where('name','like','%' . $search_term . '%');
|
||
|
});
|
||
|
})->join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->paginate($PerPagination);
|
||
|
break;
|
||
|
case "name":
|
||
|
$rooms = Room::where('rooms.name','like','%' . $search_term . '%')->join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->paginate($PerPagination);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else{
|
||
|
$rooms = Room::join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->paginate($PerPagination);
|
||
|
}
|
||
|
|
||
|
|
||
|
return view('rooms.index')
|
||
|
->with('search_types',$search_types)
|
||
|
->with('data',$rooms)
|
||
|
->with('data_name','room')
|
||
|
->with('data_names','rooms')
|
||
|
;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Display a listing of the resource.
|
||
|
*
|
||
|
*/
|
||
|
public function deleted(Request $request)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_viewAny_deleted'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$search_types = [];
|
||
|
array_push($search_types,array("value" => "building", "name" => "building"));
|
||
|
array_push($search_types,array("value" => "room", "name" => "room"));
|
||
|
|
||
|
$PerPagination = $request->input('p') ?? '10';
|
||
|
$search_term = $request->input('search_term');
|
||
|
$search_type = $request->input('search_type');
|
||
|
|
||
|
if($search_term != ""){
|
||
|
switch ($search_type){
|
||
|
case "building":
|
||
|
$rooms = Room::where(function ($query) use ($search_term){
|
||
|
$query->whereHas('building',function ($query) use ($search_term){
|
||
|
$query->where('name','like','%' . $search_term . '%');
|
||
|
});
|
||
|
})->join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->paginate($PerPagination);
|
||
|
break;
|
||
|
break;
|
||
|
case "room":
|
||
|
$rooms = Room::where('rooms.name','like','%' . $search_term . '%')->join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->paginate($PerPagination);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else{
|
||
|
$rooms = Room::onlyTrashed()->join('buildings', 'rooms.building_id', '=', 'buildings.id')->select('rooms.*')->orderBy('buildings.name','asc')->onlyTrashed()->Paginate($PerPagination);
|
||
|
}
|
||
|
|
||
|
|
||
|
return view('rooms.deleted')
|
||
|
->with('search_types',$search_types)
|
||
|
->with('data',$rooms)
|
||
|
->with('data_name','room')
|
||
|
;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for creating a new resource.
|
||
|
*
|
||
|
*/
|
||
|
public function create()
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_create'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
return view('rooms.create')
|
||
|
->with('buildings',Building::all()->sortBy(['name','asc']))
|
||
|
;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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', '=', 'rooms_create'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$room = new Room();
|
||
|
$room->building_id = $request->building_id;
|
||
|
$room->name = $request->name;
|
||
|
$room->save();
|
||
|
Logger::LogCreated($room->id,get_class($room));
|
||
|
|
||
|
return redirect()->route('rooms.show',['room' => $room]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Display the specified resource.
|
||
|
*
|
||
|
* @param \App\Models\Room $room
|
||
|
*/
|
||
|
public function show($room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_view'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
|
||
|
$reservations = Loan::where('loan_type_id','!=',LoanType::where('name','=','Loan')->first()->id)
|
||
|
->where('room_id','=',$object->id)
|
||
|
->select('*',DB::raw('count(loanable_id) as amount'))
|
||
|
->groupBy('loanable_type','loanable_id','loan_type_id','room_id')
|
||
|
->orderBy('loan_type_id')
|
||
|
->orderBy('user_id')
|
||
|
->orderBy('date_start')
|
||
|
->orderBy('date_end')
|
||
|
->orderBy('loanable_type')
|
||
|
->orderBy('loanable_id')
|
||
|
->get()
|
||
|
;
|
||
|
|
||
|
return view('rooms.show')
|
||
|
->with('data',$object)
|
||
|
->with('data_name','room')
|
||
|
->with('reservations',$reservations)
|
||
|
;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for editing the specified resource.
|
||
|
*
|
||
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||
|
*/
|
||
|
public function edit($room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_edit'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
|
||
|
return view('rooms.edit')
|
||
|
->with('data',$object)
|
||
|
->with('buildings',Building::all()->sortBy([['name','asc']]))
|
||
|
->with('data_name','room')
|
||
|
;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Update the specified resource in storage.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
*/
|
||
|
public function update(Request $request, $room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_edit'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
|
||
|
$object->building_id = $request->building_id;
|
||
|
if( $object->name != $request->name){
|
||
|
Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->name." til ".$request->name);
|
||
|
$object->name = $request->name;
|
||
|
}
|
||
|
if( $object->building_id != $request->building_id){
|
||
|
$building = Building::withTrashed()->where('id','=',$request->building_id)->first();
|
||
|
Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->building->name." til ".$building->name);
|
||
|
$object->building_id = $request->building_id;
|
||
|
}
|
||
|
$object->save();
|
||
|
|
||
|
return redirect()->route('rooms.show',['room' => $room]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove the specified resource from storage.
|
||
|
*
|
||
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
*/
|
||
|
public function destroy($room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_delete'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
Logger::LogDeleted($object->id,get_class($object));
|
||
|
$object->delete();
|
||
|
|
||
|
return redirect()->route('rooms.index');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Permanently Remove the specified resource from storage.
|
||
|
*
|
||
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
*/
|
||
|
public function delete_force($room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_delete_force'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
Logger::LogForceDeleted($object->id,get_class($object));
|
||
|
$object->forceDelete();
|
||
|
|
||
|
return redirect()->route('rooms.deleted');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Restore the specified resource from storage.
|
||
|
*
|
||
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
*/
|
||
|
public function restore($room)
|
||
|
{
|
||
|
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'rooms_restore'))
|
||
|
? Response::allow()
|
||
|
: Response::deny('you are not the chosen one');
|
||
|
|
||
|
$object = Room::where('id','=',$room)->withTrashed()->first();
|
||
|
$object->restore();
|
||
|
Logger::LogRestored($object->id,get_class($object));
|
||
|
|
||
|
return redirect()->route('rooms.deleted');
|
||
|
}
|
||
|
}
|