196 lines
7 KiB
PHP
196 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\NewsBreedings\StoreNewsBreedingRequest;
|
|
use App\Http\Requests\Bo\NewsBreedings\UpdateNewsBreedingRequest;
|
|
use App\Models\NewsBreeding;
|
|
use App\Models\StaticPage;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class NewsBreedingController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(NewsBreeding::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 $newsBreedingModels */
|
|
$newsBreedingModels = NewsBreeding::query();
|
|
|
|
// Filter by associated page if requested.
|
|
if (
|
|
$request->associated_page &&
|
|
null !== StaticPage::where('type', \collect(StaticPageType::getBreedingPages())
|
|
->where('name', $request->associated_page)->first())->first()
|
|
) {
|
|
$newsBreedingModels = $newsBreedingModels->where(
|
|
'associated_page',
|
|
\collect(StaticPageType::getBreedingPages())->where('name', $request->associated_page)->first()
|
|
);
|
|
}
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$newsBreedingModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
'subtitle',
|
|
'content',
|
|
'associated_page'
|
|
);
|
|
}
|
|
$searchFields = str(\implode(', ', [
|
|
trans('validation.attributes.title'),
|
|
trans('validation.custom.subtitle'),
|
|
trans('validation.attributes.content'),
|
|
]))->ucfirst();
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($newsBreedingModels);
|
|
|
|
/** Custom pagination */
|
|
$newsBreedingModels = $this->paginate($newsBreedingModels);
|
|
|
|
return view('bo.pages.news_breedings.index', compact('newsBreedingModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(NewsBreeding $newsBreeding): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.news_breedings.create', ['newsBreedingModel' => $newsBreeding]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\NewsBreedings\StoreNewsBreedingRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreNewsBreedingRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$newsBreeding = new NewsBreeding();
|
|
$newsBreeding->fill($request->validated());
|
|
if ($newsBreeding->saveOrFail()) {
|
|
return redirect()->route('bo.news_breedings.edit', $newsBreeding)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(NewsBreeding $newsBreeding): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.news_breedings.show', ['newsBreedingModel' => $newsBreeding]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(NewsBreeding $newsBreeding): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.news_breedings.edit', ['newsBreedingModel' => $newsBreeding]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\NewsBreedings\UpdateNewsBreedingRequest $request
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateNewsBreedingRequest $request,
|
|
NewsBreeding $newsBreeding
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $newsBreeding) {
|
|
$newsBreeding->fill($request->validated());
|
|
if ($newsBreeding->saveOrFail()) {
|
|
return redirect()->route('bo.news_breedings.edit', $newsBreeding)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.news_breedings.edit', $newsBreeding)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(NewsBreeding $newsBreeding): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($newsBreeding->deleteOrFail()) {
|
|
return redirect()->route('bo.news_breedings.index', $previousQueries)
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.news_breeding', 1))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\NewsBreeding $newsBreeding
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(NewsBreeding $newsBreeding): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($newsBreeding->replicate());
|
|
}
|
|
}
|