HifiPlanet-Website/hifi/api/src/Support/Fonts.php
Maaxxs 326d124a60 Let admins upload their own custom fonts
Adds a new custom_fonts table (name, a server-generated collision-free
font_family identifier, file_path) with its own upload endpoint
(CustomFontController) separate from the generic image-only /uploads,
since it needs to accept WOFF2/WOFF/TTF/OTF instead. New management UI in
WebsiteSettings.jsx ("Eigene Schriftarten"): upload with a display name,
list with delete.

CustomFontsContext.jsx fetches the list once per layout (PublicLayout.jsx
for visitors, AdminLayout.jsx for the dashboard) and exposes it two ways:
useFontOptionsWithCustom() appends the uploaded fonts to the existing
static FONT_OPTIONS list for every font picker (RichTextEditor,
TextStyleWidget, the per-section and global site-font dropdowns), and a
new CustomFontFaces.jsx component renders the @font-face rules that
actually make an uploaded font usable - without it, picking a custom font
would just reference a name the browser has no file for. refresh() lets
WebsiteSettings.jsx update every picker immediately after an
upload/delete, not just on next page load.

Fonts::isValid() replaces the three places that validated a font_family
value against the fixed Fonts::VALUES list (site-wide default, per-section
override, per-field text style) - it now also accepts any uploaded
font's generated identifier, so those endpoints don't reject a custom
font as an unknown value.

Verified locally end-to-end: migration creates the table, a simulated
upload's font_family passes validation while a nonexistent one doesn't,
index() returns the expected shape, and destroy() removes both the row
and the file from uploads/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-10 03:28:57 +02:00

37 lines
1.2 KiB
PHP

<?php
namespace App\Support;
use App\Config\Database;
/**
* Muss zu hifi-src/src/utils/fontOptions.js (FONT_OPTIONS) passen - dieselbe Liste wird dort
* vom Rich-Text-Editor (Mail-Vorlagen/Rechtstexte), der globalen Website-Schriftart und der
* Schriftart pro Startseiten-Sektion gemeinsam verwendet.
*/
class Fonts
{
public const VALUES = [
'', 'Arial, Helvetica, sans-serif', 'Georgia, "Times New Roman", serif',
'"Times New Roman", Times, serif', 'Verdana, Geneva, sans-serif',
'"Courier New", Courier, monospace',
];
/**
* Gueltig ist entweder einer der festen Werte oben, oder der font_family-Bezeichner einer
* selbst hochgeladenen Schriftart (siehe CustomFontController) - jede Stelle, die bisher
* gegen VALUES geprueft hat, prueft stattdessen hierueber.
*/
public static function isValid(string $value): bool
{
if (in_array($value, self::VALUES, true)) {
return true;
}
if ($value === '') {
return true;
}
$stmt = Database::connection()->prepare('SELECT 1 FROM custom_fonts WHERE font_family = ?');
$stmt->execute([$value]);
return (bool) $stmt->fetchColumn();
}
}