Files
ABSDEV/Modules/Content/Services/PageRenderer.php
T
2026-07-27 08:19:38 +01:00

39 lines
1.5 KiB
PHP

<?php
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
{
try { return Schema::hasTable('page_content') && PageContent::query()->where('page_key',$pageKey)->where('is_enabled',true)->exists(); }
catch (Throwable) { return false; }
}
public function renderRegion(string $pageKey, string $region): string
{
if (! $this->hasComposition($pageKey)) return '';
$placements = PageContent::query()
->with('document.type')
->where('page_key', $pageKey)
->where('region', $region)
->where('is_enabled', true)
->where(fn($q) => $q->whereNull('starts_at')->orWhere('starts_at','<=',now()))
->where(fn($q) => $q->whereNull('ends_at')->orWhere('ends_at','>',now()))
->orderBy('sort_order')->get();
return $placements->map(function (PageContent $placement): string {
$document = $placement->document;
if (! $document || ! $document->type || $document->status !== 'published') return '';
$view = 'stylist-theme-saasland::documents.'.$document->type->renderer;
if (! View::exists($view)) $view = 'content::documents.unsupported';
return View::make($view, ['document'=>$document,'data'=>$document->data ?? [],'placement'=>$placement])->render();
})->implode("\n");
}
}