* 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
35 lines
1.5 KiB
PHP
35 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Content\Services;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\View;
|
|
use Modules\Content\Models\PageContent;
|
|
use Throwable;
|
|
|
|
final class PageRenderer
|
|
{
|
|
public function hasComposition(string $pageKey): bool
|
|
{
|
|
return Schema::hasTable('page_content') && PageContent::where('page_key',$pageKey)->exists();
|
|
}
|
|
|
|
public function renderRegion(string $pageKey, string $region, bool $preview=false): string
|
|
{
|
|
if(!Schema::hasTable('page_content')) return '';
|
|
return PageContent::query()->with('document.type')->where('page_key',$pageKey)->where('region',$region)->where('is_enabled',true)->orderBy('sort_order')->get()
|
|
->filter(fn(PageContent $p)=>$preview || ($this->active($p) && $p->document && $this->active($p->document) && $p->document->status==='published'))
|
|
->map(function(PageContent $p) use($preview){
|
|
try { $view='stylist-theme-saasland::documents.'.$p->document->type->key; if(!View::exists($view)) return ''; $data=$p->document->data ?? []; return View::make($view, array_merge($data,['data'=>$data,'document'=>$p->document,'placement'=>$p,'preview'=>$preview]))->render(); }
|
|
catch(Throwable $e){ report($e); return ''; }
|
|
})->implode('');
|
|
}
|
|
|
|
private function active(object $model): bool
|
|
{
|
|
return (!$model->published_at || $model->published_at->lte(now())) && (!$model->expires_at || $model->expires_at->gt(now()));
|
|
}
|
|
}
|