186 lines
6.1 KiB
PHP
186 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Brands\StoreBrandRequest;
|
|
use App\Http\Requests\Bo\Brands\UpdateBrandRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Site;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class BrandController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $brandModels */
|
|
$brandModels = Brand::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$brandModels,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = trans('Nom');
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($brandModels);
|
|
|
|
/** Custom pagination */
|
|
$brandModels = $this->customPaginate($brandModels, $request->pagination);
|
|
|
|
return view('bo.pages.brands.index', compact('brandModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show model action.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.brands.show', ['brandModel' => $brand]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
$siteModels = Site::orderBy('name', 'ASC')->get();
|
|
|
|
return view('bo.pages.brands.create', [
|
|
'brandModel' => $brand,
|
|
'siteModels' => $siteModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\StoreBrandRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreBrandRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$brand = new Brand();
|
|
$brand->fill($request->safe()->except('sites'));
|
|
|
|
if ($brand->saveOrFail()) {
|
|
$brand->updateSites($brand);
|
|
return redirect()
|
|
->route('bo.brands.edit', $brand)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Brand $brand): \Illuminate\Contracts\View\View
|
|
{
|
|
$siteModels = Site::orderBy('name', 'ASC')->get();
|
|
|
|
return view('bo.pages.brands.edit', [
|
|
'brandModel' => $brand,
|
|
'siteModels' => $siteModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\UpdateBrandRequest $request
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateBrandRequest $request, Brand $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$brand->fill($request->safe()->except(['sites']));
|
|
$brand->updateSites($brand);
|
|
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.edit', $brand)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Brand $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($brand) {
|
|
if (count($brand->accounts)) {
|
|
return redirect()
|
|
->route('bo.brands.index')
|
|
->with('error', __(
|
|
':model n\'as pas pu être supprimé car elle est encore associée avec des Sites.',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
} else {
|
|
$brand->deleteSites($brand);
|
|
if ($brand->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
} //end if
|
|
});
|
|
}
|
|
}
|