* renderRegion() * draft and published-state handling * document and placement scheduling * disabled-placement filtering * ordered region rendering * safe fallback when composition tables or document views are missing * compatibility with common alternative column names
57 lines
2.5 KiB
PHP
57 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Content\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\View\View;
|
|
use Modules\Content\Models\ContentDocument;
|
|
use Modules\Content\Models\PageContent;
|
|
|
|
final class PageComposerController extends Controller
|
|
{
|
|
public function edit(string $pageKey): View
|
|
{
|
|
return view('content::admin.pages.edit', [
|
|
'pageKey'=>$pageKey,
|
|
'placements'=>PageContent::with('document.type')->where('page_key',$pageKey)->orderBy('region')->orderBy('sort_order')->get()->groupBy('region'),
|
|
'documents'=>ContentDocument::with('type')->orderBy('title')->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request, string $pageKey): RedirectResponse
|
|
{
|
|
$data=$request->validate(['content_document_id'=>['required','exists:content_documents,id'],'region'=>['required',Rule::in(['header','body','footer'])]]);
|
|
$data['page_key']=$pageKey; $data['is_enabled']=true;
|
|
$data['sort_order']=(int) PageContent::where('page_key',$pageKey)->where('region',$data['region'])->max('sort_order')+1;
|
|
PageContent::create($data);
|
|
return back()->with('status','Placement added.');
|
|
}
|
|
|
|
public function update(Request $request, string $pageKey, PageContent $placement): RedirectResponse
|
|
{
|
|
abort_unless($placement->page_key===$pageKey,404);
|
|
$placement->update($request->validate(['region'=>['sometimes',Rule::in(['header','body','footer'])],'is_enabled'=>['sometimes','boolean'],'published_at'=>['nullable','date'],'expires_at'=>['nullable','date','after:published_at']]));
|
|
return back()->with('status','Placement updated.');
|
|
}
|
|
|
|
public function destroy(string $pageKey, PageContent $placement): RedirectResponse
|
|
{
|
|
abort_unless($placement->page_key===$pageKey,404); $placement->delete();
|
|
return back()->with('status','Placement removed.');
|
|
}
|
|
|
|
public function reorder(Request $request, string $pageKey): JsonResponse
|
|
{
|
|
$data=$request->validate(['region'=>['required',Rule::in(['header','body','footer'])],'ids'=>['required','array'],'ids.*'=>['integer']]);
|
|
DB::transaction(function() use($data,$pageKey){ foreach($data['ids'] as $i=>$id){ PageContent::whereKey($id)->where('page_key',$pageKey)->where('region',$data['region'])->update(['sort_order'=>$i]); }});
|
|
return response()->json(['saved'=>true]);
|
|
}
|
|
}
|