191 lines
6.4 KiB
PHP
191 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\Width;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Ranges\StoreRangeRequest;
|
|
use App\Http\Requests\Bo\Ranges\UpdateRangeRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Font;
|
|
use App\Models\Range;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RangeController extends Controller
|
|
{
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Range::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $query */
|
|
$query = Range::query()->with(['brand', 'font'])->withCount([
|
|
'products',
|
|
'products as published_products' => fn(Builder $q) => $q->where('products.status', true),
|
|
'brand as parent_published' => fn(Builder $q) => $q->where('status', true),
|
|
]);
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->input('search');
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
if ($request->input('brand_id')) {
|
|
$query->whereHas('brand', fn(Builder $q) => $q->where('id', $request->input('brand_id')));
|
|
}
|
|
|
|
/** Custom pagination */
|
|
$rangeModelList = $this->paginate($query);
|
|
$brandList = Brand::query()->pluck('name', 'id');
|
|
|
|
return view('bo.pages.ranges.index', compact(
|
|
'rangeModelList',
|
|
'brandList',
|
|
'search',
|
|
'searchFields'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(): \Illuminate\Contracts\View\View
|
|
{
|
|
$brandModelList = Brand::all();
|
|
$fontModelList = Font::all();
|
|
return view('bo.pages.ranges.create', [
|
|
'brandModelList' => $brandModelList,
|
|
'fontModelList' => $fontModelList,
|
|
'widths' => Width::toArray()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Ranges\StoreRangeRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreRangeRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$range = new Range();
|
|
$range->fill($request->validated());
|
|
|
|
if ($range->saveOrFail()) {
|
|
return redirect()->route('bo.ranges.edit', ['tenant' => tenant(), 'range' => $range])
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Range $range
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Range $range): View
|
|
{
|
|
$range->loadCount([
|
|
'products',
|
|
'products as published_products' => fn(Builder $q) => $q->where('products.status', true),
|
|
'brand as parent_published' => fn(Builder $q) => $q->where('status', true),
|
|
]);
|
|
$brandModelList = Brand::all();
|
|
$fontModelList = Font::all();
|
|
return view('bo.pages.ranges.edit', [
|
|
'brandModelList' => $brandModelList,
|
|
'fontModelList' => $fontModelList,
|
|
'rangeModel' => $range,
|
|
'widths' => Width::toArray()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Ranges\UpdateRangeRequest $request
|
|
* @param \App\Models\Range $range
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateRangeRequest $request, Range $range): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $range) {
|
|
$range->fill($request->validated());
|
|
|
|
if ($range->saveOrFail()) {
|
|
return redirect()->route('bo.ranges.edit', ['tenant' => tenant(), 'range' => $range])
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.ranges.edit', ['tenant' => tenant(), 'range' => $range])
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Range $range
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Range $range): RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($range->deleteOrFail()) {
|
|
return redirect()->route('bo.ranges.index', array_merge(['tenant' => \tenant()], $previousQueries))
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.range', 1))
|
|
]));
|
|
}
|
|
}
|