Loheide-eu-mitarbeiterplatt.../app/Support/Theme/Theme.php
Friederich Loheide 800e267e1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m29s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 13m56s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 12m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 11m58s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 50s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m13s
Neue Seitenköpfe, global wählbar (0.48.0)
- PageHeader: Kopfband (Standard, kompakt), Kopfband mit Symbol, mit Symbol,
  kräftiger Titel – Einstellung page_header im Erscheinungsbild
- Symbol vom passenden Menüpunkt (aktiv oder per Adresse), Prop icon
- In Spalten (z. B. Downloads) schlichte Form statt Band
- Zurück-Links als breadcrumbs (IT-Service), Profil bearbeiten mit PageHeader
- Plugin-Assets: Fingerabdruck berücksichtigt Änderungszeiten der Dateien

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 20:05:37 +00:00

245 lines
10 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\Pagebuilder\Field;
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',
'nav_style' => 'bar',
'page_header' => 'band',
'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,
// Mehrere Titelbilder – je Seitenaufruf erscheint zufällig eines (profile_cover = das erste)
'profile_covers' => [],
'avatar_placeholder' => 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'];
/** Hauptmenü am PC: Leiste in der Kopfzeile oder – wie auf dem Handy – ein Burger-Menü */
public const NAV_STYLES = ['bar' => 'Leiste in der Kopfzeile', 'burger' => 'Burger-Menü (wie auf dem Handy)'];
/** Gestaltung der Seitenköpfe (Titel, Beschreibung, Aktionen oben auf jeder Seite) */
public const PAGE_HEADERS = [
'band' => 'Kopfband – weißes Band über die volle Breite',
'band_icon' => 'Kopfband mit Symbol des Bereichs',
'icon' => 'Mit Symbol des Bereichs',
'bold' => 'Kräftiger Titel mit Akzentstrich',
];
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]);
// Alle Bildfelder – jedes könnte eine SVG-Datei sein
foreach (['logo_light', 'logo_dark', 'logo_mark', 'app_icon', 'login_image', 'profile_cover', 'avatar_placeholder'] as $image) {
SvgSanitizer::sanitizeFile($theme[$image]);
}
// Große Fotos (Anmeldung, Titelbild) verkleinern – sie laden auf vielen Seiten mit
$theme = app(ThemePhotos::class)->optimizeAll($theme);
$this->settings->set(self::DRAFT, $theme);
}
/**
* Schon veröffentlichte und im Entwurf liegende Fotos nachträglich verkleinern (Migration).
*/
public function optimizePhotos(): void
{
$photos = app(ThemePhotos::class);
$published = $this->settings->get(self::PUBLISHED);
if (is_array($published)) {
$this->settings->set(self::PUBLISHED, $photos->optimizeAll($published));
}
$draft = $this->settings->get(self::DRAFT);
if (is_array($draft)) {
$this->settings->set(self::DRAFT, $photos->optimizeAll($draft));
}
}
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['nav_style'] = array_key_exists((string) ($theme['nav_style'] ?? ''), self::NAV_STYLES) ? $theme['nav_style'] : 'bar';
$theme['page_header'] = array_key_exists((string) ($theme['page_header'] ?? ''), self::PAGE_HEADERS) ? $theme['page_header'] : 'band';
$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(
// Nur sichere Adressen (kein javascript:, auch nicht aus importierten Themes)
fn ($link) => is_array($link) && filled($link['label'] ?? null) && ($url = Field::safeUrl(mb_substr(trim((string) ($link['url'] ?? '')), 0, 500))) !== ''
? ['label' => mb_substr(trim($link['label']), 0, 60), 'url' => $url]
: 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', 'avatar_placeholder', '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;
}
// Titelbilder: Liste gültiger Pfade; ein älteres Einzelbild wird übernommen
$validFile = fn ($path) => is_string($path) && preg_match('#^(theme|branding)/[A-Za-z0-9/_.-]+$#', $path) && ! str_contains($path, '..');
$covers = array_values(array_unique(array_filter((array) ($theme['profile_covers'] ?? []), $validFile)));
$theme['profile_covers'] = array_slice($covers !== [] ? $covers : array_filter([$theme['profile_cover']]), 0, 12);
$theme['profile_cover'] = $theme['profile_covers'][0] ?? 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'),
]);
}
}