Lager-v3/app/Http/Controllers/Product/ProductModelController.php

303 lines
11 KiB
PHP

<?php
namespace App\Http\Controllers\Product;
use App\Helpers\Logger;
use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\Permission;
use App\Models\ProductCategory;
use App\Models\ProductModel;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProductModelController 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', '=', 'models_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "brand", "name" => "brand"));
$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":
switch($search_compare){
case("="):
$models = ProductModel::where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$models = ProductModel::where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "brand":
switch($search_compare){
case("="):
$models = ProductModel::where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$models = ProductModel::where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
}
}
else{
$models = ProductModel::paginate($PerPagination);
}
return view('models.index')
->with('search_types',$search_types)
->with('data',$models)
->with('data_name','model')
->with('data_names','models')
;
}
/**
* 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', '=', 'models_deleted'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "brand", "name" => "brand"));
$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":
switch($search_compare){
case("="):
$models = ProductModel::onlyTrashed()->where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$models = ProductModel::onlyTrashed()->where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "brand":
switch($search_compare){
case("="):
$models = ProductModel::onlyTrashed()->where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$models = ProductModel::onlyTrashed()->where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
}
}
else{
$models = ProductModel::onlyTrashed()->paginate($PerPagination);
}
return view('models.deleted')
->with('search_types',$search_types)
->with('data',$models)
->with('data_name','model')
->with('data_names','models')
;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function create()
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
return view('models.create')
->with('brands',Brand::all())
;
}
/**
* 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', '=', 'models_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
$model = new ProductModel();
$model->name = $request->name;
$model->brand_id = $request->brand_id;
$model->save();
Logger::LogCreated($model->id,get_class($model));
return redirect()->route('models.show',['model' => $model]);
}
/**
* Display the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function show($model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_view'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
return view('models.show')
->with('data',$object)
;
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function edit($model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
return view('models.edit')
->with('data',$object)
->with('brands',Brand::withTrashed()->get())
->with('data_name','model')
;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request,$model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
if( $object->name != $request->name){
Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->name." til ".$request->name);
$object->name = $request->name;
}
if( $object->brand_id != $request->brand_id){
$brand = Brand::withTrashed()->where('id','=',$request->brand_id)->first();
Logger::LogEdited($object->id,get_class($object),"Fabrikant : ".$object->brand->name." til ".$brand->name);
$object->brand_id = $request->brand_id;
}
$object->save();
return redirect()->route('models.show',['model' => $model]);
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
Logger::LogDeleted($object->id,get_class($object));
$object->delete();
return redirect()->route('models.index');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete_force($model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
Logger::LogForceDeleted($object->id,get_class($object));
$object->forceDelete();
return redirect()->route('models.deleted');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($model)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'models_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = ProductModel::withTrashed()->where('id','=',$model)->first();
$object->restore();
Logger::LogRestored($object->id,get_class($object));
return redirect()->route('models.deleted');
}
}