149 lines
4.9 KiB
PHP
149 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\StaticPages\StoreStaticPageRequest;
|
|
use App\Http\Requests\Bo\StaticPages\UpdateStaticPageRequest;
|
|
use App\Models\Site\StaticPage;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StaticPageController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $static_pages */
|
|
$static_pages = StaticPage::query()->where('type', '=', StaticPageType::crud());
|
|
|
|
if ($request->search) {
|
|
$this->searchQuery(
|
|
$static_pages,
|
|
$request->search,
|
|
null,
|
|
'title',
|
|
'value'
|
|
);
|
|
} //end if
|
|
|
|
$this->sortQuery($static_pages);
|
|
$searchFields = trans('Titre, contenu');
|
|
|
|
$static_pages->orderBy('updated_at', 'DESC');
|
|
$static_pages = $static_pages->paginate();
|
|
|
|
return view('back::pages.static_pages.index', compact('static_pages', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('back::pages.static_pages.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StaticPages\StoreStaticPageRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(StoreStaticPageRequest $request)
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$static_page = new StaticPage();
|
|
// * Only crud pages are allowed to be created.
|
|
$static_page->type = StaticPageType::crud();
|
|
$static_page->fill($request->validated());
|
|
|
|
if ($static_page->saveOrFail()) {
|
|
return redirect()
|
|
->to(
|
|
\App\Models\Site\StaticPage::getBoRouteForType($static_page->type, 'edit') ??
|
|
route('bo.static_pages.edit', $static_page->id)
|
|
)
|
|
->with('bo.success', trans(
|
|
':model a été créé',
|
|
['model' => Str::singular(trans('models.classes.static_pages'))]
|
|
));
|
|
}
|
|
return back();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Site\StaticPage $static_page
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(StaticPage $static_page)
|
|
{
|
|
return view('back::pages.static_pages.edit', ['static_pageModel' => $static_page]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StaticPages\UpdateStaticPageRequest $request
|
|
* @param \App\Models\Site\StaticPage $static_page
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(UpdateStaticPageRequest $request, StaticPage $static_page)
|
|
{
|
|
return DB::transaction(function () use ($request, $static_page) {
|
|
try {
|
|
$static_page->fill($request->validated());
|
|
} catch (\ErrorException $e) {
|
|
return back()
|
|
->with('bo.danger', trans(
|
|
':model n\'a pas pu être enregistré car l\'image n\'a pu être transférée.',
|
|
['model' => Str::singular(trans('models.classes.pages'))]
|
|
));
|
|
}
|
|
|
|
if ($static_page->saveOrFail()) {
|
|
return redirect()
|
|
->to(
|
|
\App\Models\Site\StaticPage::getBoRouteForType($static_page->type, 'edit') ??
|
|
route('bo.static_pages.edit', $static_page->id)
|
|
)
|
|
->with('bo.success', trans(
|
|
':model a été mis à jour',
|
|
['model' => trans('Page')]
|
|
));
|
|
}
|
|
return back();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Site\StaticPage $static_page
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(StaticPage $static_page)
|
|
{
|
|
if ($static_page->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.static_pages.index')
|
|
->with('bo.warning', trans(
|
|
':model a bien été supprimé',
|
|
['model' => Str::singular(trans('models.classes.static_pages'))]
|
|
));
|
|
}
|
|
return back();
|
|
}
|
|
}
|