ldc-legaulois/app/Http/Controllers/Bo/NewsCategoryController.php

161 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2024-08-30 15:24:11 +02:00
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\NewsCategories\StoreNewsCategoryRequest;
use App\Http\Requests\Bo\NewsCategories\UpdateNewsCategoryRequest;
use App\Models\NewsCategory;
use App\Traits\Controllers\ChangesModelOrder;
2026-09-25 16:25:31 +02:00
use App\Traits\Controllers\UpdateModelPublished;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
2024-08-30 15:24:11 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class NewsCategoryController extends Controller
{
use ChangesModelOrder;
2026-09-25 16:25:31 +02:00
use UpdateModelPublished;
2024-08-30 15:24:11 +02:00
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(NewsCategory::class);
}
2024-08-30 15:24:11 +02:00
/**
* Display a listing of the resource.
*/
2026-09-25 16:25:31 +02:00
public function index(Request $request): View
2024-08-30 15:24:11 +02:00
{
/** @var \Illuminate\Database\Eloquent\Builder $newsCategoryModels */
$newsCategoryModels = NewsCategory::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$newsCategoryModels,
$search,
null,
'title',
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.title'),
]);
/** Sort columns with a query */
$this->sortQuery($newsCategoryModels);
/** Custom pagination */
$newsCategoryModels = $this->paginate($newsCategoryModels);
return view('bo.pages.news_categories.index', compact('newsCategoryModels', 'search', 'searchFields'));
}
/**
* Show the form for creating a new resource.
*/
2026-09-25 16:25:31 +02:00
public function create(NewsCategory $newsCategory): View
2024-08-30 15:24:11 +02:00
{
return view('bo.pages.news_categories.create', ['newsCategoryModel' => $newsCategory]);
}
/**
* Store a newly created resource in storage.
*/
2026-09-25 16:25:31 +02:00
public function store(StoreNewsCategoryRequest $request): RedirectResponse
2024-08-30 15:24:11 +02:00
{
return DB::transaction(function () use ($request) {
$newsCategory = new NewsCategory();
$newsCategory->fill($request->validated());
if ($newsCategory->saveOrFail()) {
return redirect()->route('bo.news_categories.edit', $newsCategory)
->with('success', trans('crud.messages.has_been_created', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-08-30 15:24:11 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_created', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
});
}
/**
* Display the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function show(NewsCategory $newsCategory): View
2024-08-30 15:24:11 +02:00
{
return view('bo.pages.news_categories.show', ['newsCategoryModel' => $newsCategory]);
}
/**
* Show the form for editing the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function edit(NewsCategory $newsCategory): View
2024-08-30 15:24:11 +02:00
{
return view('bo.pages.news_categories.edit', ['newsCategoryModel' => $newsCategory]);
}
/**
* Update the specified resource in storage.
*/
public function update(
UpdateNewsCategoryRequest $request,
NewsCategory $newsCategory
2026-09-25 16:25:31 +02:00
): RedirectResponse {
2024-08-30 15:24:11 +02:00
return DB::transaction(function () use ($request, $newsCategory) {
$newsCategory->fill($request->validated());
if ($newsCategory->saveOrFail()) {
return redirect()->route('bo.news_categories.edit', $newsCategory)
->with('success', trans('crud.messages.has_been_updated', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-08-30 15:24:11 +02:00
return redirect()->route('bo.news_categories.edit', $newsCategory)
->with('error', trans('crud.messages.cannot_be_updated', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
});
}
/**
* Remove the specified resource from storage.
*/
2026-09-25 16:25:31 +02:00
public function destroy(NewsCategory $newsCategory): RedirectResponse
2024-08-30 15:24:11 +02:00
{
/** @var array<string,string> */
$previousQueries = [];
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
if ($newsCategory->deleteOrFail()) {
return redirect()->route('bo.news_categories.index', $previousQueries)
->with('success', trans('crud.messages.has_been_deleted', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-08-30 15:24:11 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_deleted', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucfirst(trans_choice('models.classes.news_category', 1)),
2024-08-30 15:24:11 +02:00
]));
}
/**
* Duplicate the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function duplicate(NewsCategory $newsCategory): View
2024-08-30 15:24:11 +02:00
{
return $this->create($newsCategory->replicate());
}
}