175 lines
6.5 KiB
PHP
175 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\ConsumptionMoments\StoreConsumptionMomentRequest;
|
|
use App\Http\Requests\Bo\ConsumptionMoments\UpdateConsumptionMomentRequest;
|
|
use App\Models\ConsumptionMoment;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ConsumptionMomentController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(ConsumptionMoment::class);
|
|
}
|
|
|
|
/**
|
|
* 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 $consumptionMomentModels */
|
|
$consumptionMomentModels = ConsumptionMoment::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$consumptionMomentModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.title'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($consumptionMomentModels);
|
|
|
|
/** Custom pagination */
|
|
$consumptionMomentModels = $this->paginate($consumptionMomentModels);
|
|
|
|
return view('bo.pages.consumption_moments.index', compact('consumptionMomentModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(ConsumptionMoment $consumptionMoment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.consumption_moments.show', ['consumptionMomentModel' => $consumptionMoment]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(ConsumptionMoment $consumptionMoment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.consumption_moments.create', ['consumptionMomentModel' => $consumptionMoment]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\ConsumptionMoments\StoreConsumptionMomentRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreConsumptionMomentRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$consumptionMoment = new ConsumptionMoment();
|
|
$consumptionMoment->fill($request->validated());
|
|
|
|
if ($consumptionMoment->saveOrFail()) {
|
|
return redirect()->route('bo.consumption_moments.edit', $consumptionMoment)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(ConsumptionMoment $consumptionMoment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.consumption_moments.edit', ['consumptionMomentModel' => $consumptionMoment]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\ConsumptionMoments\UpdateConsumptionMomentRequest $request
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateConsumptionMomentRequest $request,
|
|
ConsumptionMoment $consumptionMoment
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $consumptionMoment) {
|
|
$consumptionMoment->fill($request->validated());
|
|
|
|
if ($consumptionMoment->saveOrFail()) {
|
|
return redirect()->route('bo.consumption_moments.edit', $consumptionMoment)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.consumption_moments.edit', $consumptionMoment)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(ConsumptionMoment $consumptionMoment): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($consumptionMoment->deleteOrFail()) {
|
|
return redirect()->route('bo.consumption_moments.index', $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.consumption_moment', 1))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\ConsumptionMoment $consumptionMoment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(ConsumptionMoment $consumptionMoment): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($consumptionMoment->replicate());
|
|
}
|
|
}
|