Loheide-eu-mitarbeiterplatt.../app/Support/Theme/Theme.php
Friederich Loheide 05b4b79cb3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m6s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m23s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m40s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Titelbild im Profil, durchsuchbare Baum-Auswahl im IT-Ticket (0.20.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 05:20:19 +00:00

196 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\Theme;
use App\Support\Settings;
use Illuminate\Support\Arr;
/**
* Erscheinungsbild der Installation: veröffentlichter Stand und Entwurf.
* Gespeichert in den Einstellungen (Datenbank) – überlebt jedes Update.
* Bilder und Schriften liegen im Speicher „uploads“ unter theme/.
*/
class Theme
{
public const DEFAULTS = [
'portal_name' => 'Inhaus',
'logo_light' => null,
'logo_dark' => null,
'logo_mark' => null,
'app_icon' => null,
'icons' => [],
'primary_color' => '#0F4C5C',
'secondary_color' => null,
'header_style' => 'light',
'font_body' => 'inter',
'font_heading' => 'same',
'custom_font' => null,
'radius' => 'medium',
'density' => 'normal',
'dark_mode' => 'user',
'login_layout' => 'split',
'login_title' => 'Alles Wichtige für Ihren Arbeitsalltag – an einem Ort.',
'login_text' => null,
'login_image' => null,
'profile_cover' => null,
'profile_cover_overlay' => 70,
'footer_text' => null,
'footer_links' => [],
'email_footer' => null,
'custom_css' => null,
'stylesheet' => null,
];
public const HEADER_STYLES = ['light' => 'Hell', 'brand' => 'In der Markenfarbe', 'dark' => 'Dunkel'];
public const RADII = ['none' => ['Eckig', '0rem'], 'small' => ['Leicht gerundet', '0.25rem'], 'medium' => ['Gerundet', '0.5rem'], 'large' => ['Stark gerundet', '0.75rem'], 'round' => ['Rund', '1rem']];
public const DENSITIES = ['compact' => ['Kompakt', '0.225rem'], 'normal' => ['Normal', '0.25rem'], 'comfortable' => ['Luftig', '0.275rem']];
public const DARK_MODES = [
'user' => 'Jede Person wählt selbst (Standard: wie im System)',
'light' => 'Immer hell',
'dark' => 'Immer dunkel',
];
public const COVER_OVERLAYS = [40 => 'Leicht (40 %)', 55 => 'Mittel (55 %)', 70 => 'Kräftig (70 %)', 85 => 'Sehr kräftig (85 %)'];
public const LOGIN_LAYOUTS = ['split' => 'Geteilt: Bild bzw. Markenfarbe links, Anmeldung rechts', 'centered' => 'Zentriert: Anmeldung vor dem Hintergrundbild'];
private const PUBLISHED = 'theme.published';
private const DRAFT = 'theme.draft';
public function __construct(private readonly Settings $settings) {}
/**
* @return array<string, mixed>
*/
public function published(): array
{
$stored = $this->settings->get(self::PUBLISHED);
return self::normalize(is_array($stored) ? $stored : $this->legacy());
}
/**
* @return array<string, mixed>|null
*/
public function draft(): ?array
{
$stored = $this->settings->get(self::DRAFT);
return is_array($stored) ? self::normalize($stored) : null;
}
public function hasDraft(): bool
{
return $this->draft() !== null;
}
/**
* Entwurf, sonst der veröffentlichte Stand – Ausgangspunkt für das Formular.
*
* @return array<string, mixed>
*/
public function working(): array
{
return $this->draft() ?? $this->published();
}
/**
* @param array<string, mixed> $values
*/
public function saveDraft(array $values): void
{
$theme = self::normalize([...$this->working(), ...$values]);
foreach (['logo_light', 'logo_dark', 'logo_mark'] as $logo) {
SvgSanitizer::sanitizeFile($theme[$logo]);
}
$this->settings->set(self::DRAFT, $theme);
}
public function discardDraft(): void
{
$this->settings->set(self::DRAFT, null);
}
/**
* Entwurf veröffentlichen: statische CSS-Datei erzeugen, Symbole berechnen.
*/
public function publish(ThemeCompiler $compiler, Icons $icons): void
{
$theme = $this->working();
$theme['icons'] = $icons->generate($theme);
$theme['stylesheet'] = $compiler->write($theme);
$this->settings->setMany([self::PUBLISHED => $theme, self::DRAFT => null]);
}
/**
* Auf die Standardwerte zurücksetzen – als Entwurf, damit man es erst ansehen kann.
*/
public function resetToDefaults(): void
{
$this->settings->set(self::DRAFT, self::normalize([...self::DEFAULTS, 'portal_name' => $this->published()['portal_name']]));
}
/**
* Nur bekannte Schlüssel, gültige Werte.
*
* @param array<string, mixed> $theme
* @return array<string, mixed>
*/
public static function normalize(array $theme): array
{
$theme = [...self::DEFAULTS, ...Arr::only($theme, array_keys(self::DEFAULTS))];
$hex = fn ($value) => is_string($value) && preg_match('/^#[0-9a-fA-F]{6}$/', $value) ? strtoupper($value) : null;
$theme['portal_name'] = mb_substr(trim((string) $theme['portal_name']), 0, 60) ?: self::DEFAULTS['portal_name'];
$theme['primary_color'] = $hex($theme['primary_color']) ?? self::DEFAULTS['primary_color'];
$theme['secondary_color'] = $hex($theme['secondary_color']);
$theme['header_style'] = array_key_exists($theme['header_style'], self::HEADER_STYLES) ? $theme['header_style'] : 'light';
$theme['font_body'] = array_key_exists($theme['font_body'], Fonts::FONTS) || ($theme['font_body'] === 'custom' && $theme['custom_font']) ? $theme['font_body'] : 'inter';
$theme['font_heading'] = $theme['font_heading'] === 'same' || array_key_exists($theme['font_heading'], Fonts::FONTS) || ($theme['font_heading'] === 'custom' && $theme['custom_font']) ? $theme['font_heading'] : 'same';
$theme['radius'] = array_key_exists($theme['radius'], self::RADII) ? $theme['radius'] : 'medium';
$theme['density'] = array_key_exists($theme['density'], self::DENSITIES) ? $theme['density'] : 'normal';
$theme['dark_mode'] = array_key_exists($theme['dark_mode'], self::DARK_MODES) ? $theme['dark_mode'] : 'user';
$theme['profile_cover_overlay'] = array_key_exists((int) $theme['profile_cover_overlay'], self::COVER_OVERLAYS) ? (int) $theme['profile_cover_overlay'] : 70;
$theme['login_layout'] = array_key_exists($theme['login_layout'], self::LOGIN_LAYOUTS) ? $theme['login_layout'] : 'split';
$theme['footer_links'] = array_values(array_filter(array_map(
fn ($link) => is_array($link) && filled($link['label'] ?? null) && filled($link['url'] ?? null)
? ['label' => mb_substr(trim($link['label']), 0, 60), 'url' => mb_substr(trim($link['url']), 0, 500)]
: null,
(array) $theme['footer_links'],
)));
$theme['icons'] = is_array($theme['icons']) ? $theme['icons'] : [];
foreach (['login_title', 'login_text', 'footer_text', 'email_footer'] as $text) {
$theme[$text] = filled($theme[$text]) ? mb_substr(trim((string) $theme[$text]), 0, 500) : null;
}
foreach (['logo_light', 'logo_dark', 'logo_mark', 'app_icon', 'login_image', 'profile_cover', 'custom_font'] as $file) {
$theme[$file] = is_string($theme[$file]) && preg_match('#^(theme|branding)/[A-Za-z0-9/_.-]+$#', $theme[$file]) && ! str_contains($theme[$file], '..') ? $theme[$file] : null;
}
$theme['custom_css'] = filled($theme['custom_css']) ? mb_substr((string) $theme['custom_css'], 0, 50000) : null;
return $theme;
}
/**
* Einstellungen aus Phase 0 (Portalname, Logo, Markenfarbe) übernehmen.
*
* @return array<string, mixed>
*/
private function legacy(): array
{
return array_filter([
'portal_name' => $this->settings->get('branding.portal_name'),
'primary_color' => $this->settings->get('branding.primary_color'),
'logo_light' => $this->settings->get('branding.logo_path'),
]);
}
}