Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website. Co-authored-by: Cursor <cursoragent@cursor.com>
102 lines
2.7 KiB
PHP
102 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\ContentBlock;
|
|
use App\Models\Photo;
|
|
use App\Tenancy\TenantContext;
|
|
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
|
|
{
|
|
$value = self::get($key);
|
|
|
|
return new HtmlString(filled($value) ? RichContentRenderer::make($value)->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::cacheKey());
|
|
}
|
|
|
|
/**
|
|
* @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::cacheKey(), fn (): array => ContentBlock::pluck('value', 'key')->all());
|
|
}
|
|
|
|
/**
|
|
* Stored texts are cached per landlord. Without a current landlord the key stays the historical one,
|
|
* which only happens before a request has chosen a website.
|
|
*/
|
|
private static function cacheKey(): string
|
|
{
|
|
$tenantId = app(TenantContext::class)->id();
|
|
|
|
return $tenantId === null ? self::CACHE_KEY : self::CACHE_KEY.'.'.$tenantId;
|
|
}
|
|
}
|