Lager-v3/app/Http/Controllers/Loan/NoteController.php

279 lines
9.8 KiB
PHP

<?php
namespace App\Http\Controllers\Loan;
use App\Helpers\Logger;
use App\Http\Controllers\Controller;
use App\Models\Note;
use App\Models\NoteType;
use App\Models\Permission;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class NoteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function index(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "username", "name" => "user"));
array_push($search_types,array("value" => "type", "name" => "type"));
array_push($search_types,array("value" => "note", "name" => "note"));
array_push($search_types,array("value" => "created", "name" => "created_at"));
$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 "username":
switch($search_compare){
case('='):
$notes = Note::where(function ($query) use ($search_term){
$query->whereHas('user',function ($query) use ($search_term){
$query->where('username','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$notes = Note::where(function ($query) use ($search_term){
$query->whereHas('user',function ($query) use ($search_term){
$query->where('username','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "note":
switch($search_compare){
case('='):
$notes = Note::where('note','=',$search_term)->paginate($PerPagination);
break;
default:
$notes = Note::where('note','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "type":
switch($search_compare){
case('='):
$notes = Note::where(function ($query) use ($search_term){
$query->whereHas('type',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$notes = Note::where(function ($query) use ($search_term){
$query->whereHas('type',function ($query) use ($search_term){
$query->where('name','like','%'.$search_term.'%');
});
})->paginate($PerPagination);
break;
}
break;
case "created":
switch($search_compare){
default:
$parts = explode('.',$search_term);
$d = $parts[0];
$m = $parts[1];
$y = $parts[2];
$constructed_date = $y."-".$m."-".$d;
$notes = Note::where('created_at','like','%'.$constructed_date.'%')->paginate($PerPagination);
break;
}
break;
}
}
else{
$notes = Note::Paginate($PerPagination);
}
return view('notes.index')
->with('search_types',$search_types)
->with('data',$notes)
->with('data_name','note')
->with('data_names','notes')
->with('without_create','true')
;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function deleted(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_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') ?? 10;
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
$search_compare = $request->input('search_compare');
$notes = Note::onlyTrashed()->Paginate($PerPagination);
return view('notes.deleted')
->with('search_types',$search_types)
->with('data',$notes)
->with('data_name','note')
->with('data_names','notes')
->with('without_create','true')
;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function show($note)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function edit($note)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$note_obj = Note::withTrashed()->where('id','=',$note)->first();
$note_types = NoteType::all();
return view('notes.edit')
->with('data',$note_obj)
->with('data_name','note')
->with('data_names','notes')
->with('types',$note_types)
;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $note)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$note_obj = Note::withTrashed()->where('id','=',$note)->first();
if($note_obj->note_type_id != $request->type){
$type = NoteType::where('id','=',$request->type)->first();
Logger::LogEdited($note_obj->id,get_class($note_obj),"Type : ".$note_obj->type->name." til ".$type->name);
$note_obj->note_type_id = $request->type;
}
if($note_obj->note != $request->note) {
Logger::LogEdited($note_obj->id,get_class($note_obj),"Note : ".$note_obj->note." til ".$request->note);
$note_obj->note = $request->note;
}
$note_obj->save();
return redirect()->route('notes.index');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete($note)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$note_obj = Note::where('id','=',$note)->first();
Logger::LogDeleted($note_obj->id,get_class($note_obj));
$note_obj->delete();
return redirect()->route('notes.index');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete_force($note)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$note_obj = Note::withTrashed()->where('id','=',$note)->first();
Logger::LogForceDeleted($note_obj->id,get_class($note_obj));
$note_obj->forceDelete();
return redirect()->route('notes.deleted');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($note)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'notes_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$note_obj = Note::withTrashed()->where('id','=',$note)->first();
$note_obj->restore();
Logger::LogRestored($note_obj->id,get_class($note_obj));
return redirect()->route('notes.deleted');
}
}