ldc-agis/app/Http/Controllers/Bo/SeasonalSectionController.php

165 lines
5.9 KiB
PHP
Raw Normal View History

2023-12-19 15:09:12 +00:00
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\SeasonalSections\StoreSeasonalSectionRequest;
use App\Http\Requests\Bo\SeasonalSections\UpdateSeasonalSectionRequest;
use App\Models\SeasonalSection;
use App\Traits\Controllers\UpdateModelPublished;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SeasonalSectionController extends Controller
{
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(SeasonalSection::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 $seasonalSectionModels */
$seasonalSectionModels = SeasonalSection::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$seasonalSectionModels,
$search,
null,
'title',
'text',
);
}
$searchFields = \implode(', ', [
Str::ucfirst(trans('validation.attributes.title')),
trans('validation.attributes.text'),
]);
/** Sort columns with a query */
$this->sortQuery($seasonalSectionModels);
/** Custom pagination */
$seasonalSectionModels = $this->paginate($seasonalSectionModels);
return view('bo.pages.seasonal_sections.index', compact('seasonalSectionModels', 'search', 'searchFields'));
}
/**
* Show the form for creating a new resource.
*
* @param \App\Models\SeasonalSection $seasonalSection
* @return \Illuminate\Contracts\View\View
*/
public function create(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View
{
return view('bo.pages.seasonal_sections.create', ['seasonalSectionModel' => $seasonalSection]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\SeasonalSections\StoreSeasonalSectionRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreSeasonalSectionRequest $request): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request) {
$seasonalSection = new SeasonalSection();
if ($seasonalSection->fill($request->validated())->saveOrFail()) {
return redirect()->route('bo.seasonalsections.edit', $seasonalSection)
->with('success', __('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
});
}
/**
* Show the form for editing the specified resource.
*
* @param App\Models\SeasonalSection $seasonalSection
* @return \Illuminate\Contracts\View\View
*/
public function edit(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View
{
return view('bo.pages.seasonal_sections.edit', ['seasonalSectionModel' => $seasonalSection]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\SeasonalSections\UpdateSeasonalSectionRequest $request
* @param \App\Models\SeasonalSection $seasonalSection
* @return \Illuminate\Http\RedirectResponse
*/
public function update(
UpdateSeasonalSectionRequest $request,
SeasonalSection $seasonalSection
): \Illuminate\Http\RedirectResponse {
return DB::transaction(function () use ($request, $seasonalSection) {
if ($seasonalSection->fill($request->validated())->saveOrFail()) {
return redirect()->route('bo.seasonalsections.edit', $seasonalSection)
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
}
return redirect()->route('bo.seasonalsections.edit', $seasonalSection)
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
});
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\SeasonalSection $seasonalSection
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(SeasonalSection $seasonalSection): \Illuminate\Http\RedirectResponse
{
if ($seasonalSection->deleteOrFail()) {
return redirect()->route('bo.seasonalsections.index')
->with('success', __('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1))
]));
}
/**
* Duplicate the specified resource.
*
* @param \App\Models\SeasonalSection $seasonalSection
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View
{
return $this->create($seasonalSection->replicate());
}
}