187 lines
6.1 KiB
PHP
187 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Strips\StoreStripRequest;
|
|
use App\Http\Requests\Bo\Strips\UpdateStripRequest;
|
|
use App\Models\News;
|
|
use App\Models\Strip;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StripController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Strip::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<\App\Models\Strip> $stripModels */
|
|
$stripModels = Strip::query()->with('news');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->input('search');
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$stripModels,
|
|
$search,
|
|
null,
|
|
'text',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.attributes.content')),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($stripModels);
|
|
|
|
/** Custom pagination */
|
|
$stripModels = $this->paginate($stripModels);
|
|
|
|
return view('bo.pages.strips.index', compact('stripModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Strip $strip
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Strip $strip): \Illuminate\Contracts\View\View
|
|
{
|
|
$newsModels = News::query()->where('published', true)->get();
|
|
return view('bo.pages.strips.create', [
|
|
'stripModel' => $strip,
|
|
'newsModels' => $newsModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Strips\StoreStripRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreStripRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$strip = new Strip();
|
|
$strip->fill($request->validated());
|
|
if ($request->news_id === null) {
|
|
$strip->news()->dissociate();
|
|
}
|
|
|
|
if ($strip->fill($request->validated())->saveOrFail()) {
|
|
return redirect()->route('bo.strips.edit', $strip)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Strip $strip
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Strip $strip): \Illuminate\Contracts\View\View
|
|
{
|
|
$newsModels = News::query()->where('published', true)->get();
|
|
return view('bo.pages.strips.edit', [
|
|
'stripModel' => $strip,
|
|
'newsModels' => $newsModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Strips\UpdateStripRequest $request
|
|
* @param \App\Models\Strip $strip
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateStripRequest $request, Strip $strip): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $strip) {
|
|
$strip->fill($request->validated());
|
|
if ($request->news_id === null) {
|
|
$strip->news()->dissociate();
|
|
}
|
|
|
|
if (!Arr::exists($request->validated(), 'picture')) {
|
|
$strip->picture = null;
|
|
$strip->picture_alt = null;
|
|
$strip->picture_title = null;
|
|
} elseif (!Arr::exists($request->validated(), 'picto')) {
|
|
$strip->picto = null;
|
|
}
|
|
|
|
if ($strip->saveOrFail()) {
|
|
return redirect()->route('bo.strips.edit', $strip)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.strips.edit', $strip)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Strip $strip
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Strip $strip): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($strip->deleteOrFail()) {
|
|
return redirect()->route('bo.strips.index')
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.strip', 1))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Strip $strip
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Strip $strip): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($strip->replicate());
|
|
}
|
|
}
|