Files
fewo-stommer/app/Support/Content.php
T
Lars StommerandClaude Opus 5.5 bb7eb62782 Inhaltsverwaltung: Bilder, Webseiten-Texte, Ausstattung, FAQ, Hinweis-Banner
- Bilder mit automatischen WebP-Webgrößen (640/1280/2048 px), Sortierung per Drag & Drop, Mehrfach-Upload, Import-Befehl photos:import
- Webseiten-Texte als feste, bearbeitbare Bausteine (config/content.php), Rich-Text bereinigt ausgegeben
- Ausstattung und FAQ aus der bisherigen Webseite übernommen
- Rolle Inhalte hat Zugriff auf alle Webseiten-Bereiche

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:19:51 +02:00

88 lines
2.3 KiB
PHP

<?php
namespace App\Support;
use App\Models\ContentBlock;
use App\Models\Photo;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\HtmlString;
use InvalidArgumentException;
/**
* Read access to the editable website texts defined in config/content.php.
*/
class Content
{
private const CACHE_KEY = 'content-blocks';
/**
* The stored value, or the configured default when nothing was saved.
*/
public static function get(string $key): ?string
{
$definition = self::definition($key);
$stored = self::stored()[$key] ?? null;
return filled($stored) ? $stored : ($definition['default'] ?? null);
}
/**
* Rich text as sanitised HTML, ready for {{ }} output in Blade.
*/
public static function html(string $key): HtmlString
{
return new HtmlString(RichContentRenderer::make(self::get($key) ?? '')->toHtml());
}
public static function photo(string $key): ?Photo
{
$id = self::get($key);
return $id ? Photo::find((int) $id) : null;
}
/**
* @param array<string, ?string> $values
*/
public static function save(array $values): void
{
foreach ($values as $key => $value) {
self::definition($key);
ContentBlock::updateOrCreate(['key' => $key], ['value' => $value]);
}
Cache::forget(self::CACHE_KEY);
}
/**
* @return array<string, array{label: string, fields: array<string, array{label: string, type: string, default?: ?string}>}>
*/
public static function sections(): array
{
return config('content');
}
/**
* @return array{label: string, type: string, default?: ?string}
*/
private static function definition(string $key): array
{
foreach (self::sections() as $section) {
if (isset($section['fields'][$key])) {
return $section['fields'][$key];
}
}
throw new InvalidArgumentException("Unknown content key [{$key}].");
}
/**
* @return array<string, ?string>
*/
private static function stored(): array
{
return Cache::rememberForever(self::CACHE_KEY, fn (): array => ContentBlock::pluck('value', 'key')->all());
}
}