Loheide-eu-mitarbeiterplatt.../app/Support/Theme/Theme.php
Friederich Loheide 38f47f98e2
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 14m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 14m13s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 11m56s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 11m44s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 56m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1h2m6s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 10m30s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 47s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m13s
Oberfläche Mitarbeiter-App umschaltbar (0.52.0)
Einstellung interface (classic|app) im Erscheinungsbild; data-ui am Wurzelelement,
warme Tokens, Karten mit Schatten, Menüpunkte als Pillen; Startseite dreispaltig.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 19:17:23 +00:00

271 lines
12 KiB
PHP
Raw Permalink 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',
// Oberfläche: classic (Portal mit Menüleiste und Kacheln) oder app (Mitarbeiter-App: Feed, warme Flächen)
'interface' => 'classic',
'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',
];
/** Grundsätzliche Oberfläche – gilt für alle, die Administration legt sie fest */
public const INTERFACES = [
'classic' => 'Klassisch – Menüleiste, Startseite mit Kacheln',
'app' => 'Mitarbeiter-App – Neuigkeiten und Menschen im Mittelpunkt, warme Flächen, runde Formen',
];
/** Schrift und Rundung, die zur Oberfläche passen – beim Umschalten übernommen, solange unverändert */
public const INTERFACE_PRESETS = [
'classic' => ['font_body' => 'inter', 'radius' => 'medium'],
'app' => ['font_body' => 'figtree', 'radius' => 'round'],
];
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
{
$before = $this->working();
$theme = self::normalize([...$before, ...$values]);
// Oberfläche umgeschaltet: passende Schrift und Rundung mitnehmen – aber nur, wo noch
// die Voreinstellung der bisherigen Oberfläche steht (eigene Wahl bleibt erhalten)
if ($theme['interface'] !== $before['interface']) {
foreach (self::INTERFACE_PRESETS[$theme['interface']] as $key => $value) {
if ($theme[$key] === self::INTERFACE_PRESETS[$before['interface']][$key]) {
$theme[$key] = $value;
}
}
}
// 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['interface'] = array_key_exists((string) ($theme['interface'] ?? ''), self::INTERFACES) ? $theme['interface'] : 'classic';
$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'),
]);
}
}