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

253 lines
8.4 KiB
PHP
Raw Permalink Normal View History

2022-09-28 07:38:08 +00:00
<?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\Product;
use App\Models\ProductCategory;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BrandController 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', '=', 'brands_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":
switch($search_compare){
case("="):
$brands = Brand::where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$brands = Brand::where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
}
}
else{
$brands = Brand::paginate($PerPagination);
}
return view('brands.index')
->with('search_types',$search_types)
->with('data',$brands)
->with('data_name','brand')
->with('data_names','brands')
;
}
public function deleted(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_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');
if($search_term != ""){
switch ($search_type){
case "name":
switch($search_compare){
case("="):
$brands = Brand::onlyTrashed()->where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$brands = Brand::onlyTrashed()->where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
}
}
else{
$brands = Brand::onlyTrashed()->paginate($PerPagination);
}
return view('brands.deleted')
->with('search_types',$search_types)
->with('data',$brands)
->with('data_name','brand')
->with('data_names','brands')
;
}
/**
* 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', '=', 'brands_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
return view('brands.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', '=', 'brands_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
$brand = new Brand();
$brand->name = $request->name;
$brand->save();
Logger::LogCreated($brand->id,get_class($brand));
return redirect()->route('brands.show',['brand' => $brand]);
}
/**
* Display the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function show($brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_view'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->first();
return view('brands.show')
->with('data',$object)
->with('data_name','brand')
;
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function edit($brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->first();
return view('brands.edit')
->with('data',$object)
->with('data_name','brand')
;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->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('brands.show',['brand' => $brand]);
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->first();
Logger::LogDeleted($object->id,get_class($object));
$object->delete();
return redirect()->route('brands.index');
}
/**
* Restore the specified resource from storage.
*
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->first();
$object->restore();
Logger::LogRestored($object->id,get_class($object));
return redirect()->route('brands.deleted');
}
/**
* Permanently emove the specified resource from storage.
*
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\RedirectResponse
*/
public function delete_force($brand)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'brands_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Brand::withTrashed()->where('id','=',$brand)->first();
Logger::LogForceDeleted($object->id,get_class($object));
$object->forceDelete();
return redirect()->route('brands.deleted');
}
}