Tabelle, Schluesselspalte, Berechtigung und je Feld, wie die Sprachen // gespeichert sind: 'columns' = eigene Spalten _de/_en, 'translation' = Deutsch // in der Spalte , Englisch in content_translations, 'single' = nur eine Spalte (gilt // fuer beide Sprachen). 'html' = Rich-Text, sonst Klartext (wird getrimmt). private const ENTITIES = [ 'home_sections' => [ 'table' => 'home_sections', 'key' => 'slug', 'permission' => 'content.manage', 'fields' => [ 'eyebrow' => ['lang' => 'columns'], 'title' => ['lang' => 'columns'], 'body_html' => ['lang' => 'columns', 'html' => true], 'button_label' => ['lang' => 'columns'], 'button_url' => ['lang' => 'single'], ], ], 'faqs' => [ 'table' => 'faqs', 'key' => 'id', 'permission' => 'content.manage', 'fields' => [ 'question' => ['lang' => 'columns', 'required' => true], 'answer' => ['lang' => 'columns', 'required' => true], ], ], 'partners' => [ 'table' => 'partners', 'key' => 'id', 'permission' => 'content.manage', 'fields' => ['name' => ['lang' => 'single', 'required' => true]], ], 'services' => [ 'table' => 'services', 'key' => 'id', 'permission' => 'services.manage', 'fields' => [ 'title' => ['lang' => 'translation', 'required' => true], 'description' => ['lang' => 'translation', 'required' => true], 'cta_label' => ['lang' => 'translation'], ], ], 'packages' => [ 'table' => 'packages', 'key' => 'id', 'permission' => 'packages.manage', 'fields' => [ 'name' => ['lang' => 'single', 'required' => true], 'description' => ['lang' => 'translation', 'multiline' => true], 'tagline' => ['lang' => 'translation'], 'price_text' => ['lang' => 'translation'], ], ], 'brands' => [ 'table' => 'brands', 'key' => 'id', 'permission' => 'brands.manage', 'fields' => ['name' => ['lang' => 'single', 'required' => true]], ], 'car_models' => [ 'table' => 'car_models', 'key' => 'id', 'permission' => 'models.manage', 'fields' => ['name' => ['lang' => 'single', 'required' => true]], ], 'gallery_brands' => [ 'table' => 'gallery_brands', 'key' => 'id', 'permission' => 'gallery.manage', 'fields' => ['name' => ['lang' => 'single', 'required' => true]], ], 'gallery_projects' => [ 'table' => 'gallery_projects', 'key' => 'id', 'permission' => 'gallery.manage', 'fields' => ['name' => ['lang' => 'single', 'required' => true]], ], 'gallery_photos' => [ 'table' => 'gallery_photos', 'key' => 'id', 'permission' => 'gallery.manage', 'fields' => ['caption' => ['lang' => 'single']], ], 'legal_pages' => [ 'table' => 'legal_pages', 'key' => 'slug', 'permission' => 'settings.manage', 'fields' => [ 'title' => ['lang' => 'single', 'required' => true], 'body_html' => ['lang' => 'single', 'html' => true, 'required' => true], ], ], ]; // Feste Texte (i18n-Schluessel wie "home.faqHeading") - eigene Berechtigung wie die // uebrigen Website-Inhalte. private const TEXT_PERMISSION = 'content.manage'; private const TEXT_KEY_PATTERN = '/^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+){1,5}$/'; private const TEXT_MAX_LENGTH = 2000; /** Oeffentlich (ueber /site-settings): alle Ueberschreibungen als ['de' => [key => text], 'en' => [...]]. */ public static function textOverrides(): array { $result = ['de' => new \stdClass(), 'en' => new \stdClass()]; try { $rows = Database::connection()->query('SELECT lang, text_key, value FROM text_overrides')->fetchAll(); } catch (\Throwable $e) { return $result; // Tabelle fehlt noch - es gelten die eingebauten Texte } $byLang = ['de' => [], 'en' => []]; foreach ($rows as $row) { if (isset($byLang[$row['lang']])) { $byLang[$row['lang']][$row['text_key']] = $row['value']; } } foreach ($byLang as $lang => $values) { if ($values) { $result[$lang] = $values; } } return $result; } public static function save(): void { $userId = AuthMiddleware::requireAdmin(); $body = Http::jsonBody(); $changes = $body['changes'] ?? null; if (!is_array($changes) || !$changes) { Http::error('Keine Änderungen übermittelt.', 422); } if (count($changes) > self::MAX_CHANGES) { Http::error('Zu viele Änderungen auf einmal - bitte zwischendurch speichern.', 422); } // Erst alles pruefen, dann schreiben - so landet nie ein halber Satz Aenderungen in der DB. $permissionCache = []; $can = function (string $permission) use ($userId, &$permissionCache): bool { return $permissionCache[$permission] ??= AuthMiddleware::userHasPermission($userId, $permission); }; $validated = []; foreach ($changes as $i => $change) { $validated[] = self::validate(is_array($change) ? $change : [], $can, $i + 1); } $db = Database::connection(); $db->beginTransaction(); try { foreach ($validated as $change) { self::apply($db, $change); } $db->commit(); } catch (\Throwable $e) { $db->rollBack(); Http::error('Speichern fehlgeschlagen: ' . $e->getMessage(), 500); } Http::send(['ok' => true, 'saved' => count($validated)]); } private static function validate(array $change, callable $can, int $position): array { $entity = (string) ($change['entity'] ?? ''); $lang = (string) ($change['lang'] ?? 'de'); if (!in_array($lang, ['de', 'en'], true)) { Http::error("Änderung $position: unbekannte Sprache.", 422); } $value = $change['value'] ?? ''; if (!is_string($value)) { Http::error("Änderung $position: ungültiger Wert.", 422); } if ($entity === 'text') { if (!$can(self::TEXT_PERMISSION)) { Http::error('Keine Berechtigung, feste Texte zu ändern.', 403); } $key = (string) ($change['id'] ?? ''); if (!preg_match(self::TEXT_KEY_PATTERN, $key) || strlen($key) > 190) { Http::error("Änderung $position: ungültiger Text-Schlüssel.", 422); } if (mb_strlen($value) > self::TEXT_MAX_LENGTH) { Http::error("Änderung $position: Text ist zu lang.", 422); } return ['entity' => 'text', 'id' => $key, 'lang' => $lang, 'value' => trim($value)]; } $config = self::ENTITIES[$entity] ?? null; if ($config === null) { Http::error("Änderung $position: dieser Bereich kann nicht live bearbeitet werden.", 422); } if (!$can($config['permission'])) { Http::error('Keine Berechtigung für einen Teil der Änderungen.', 403); } $field = (string) ($change['field'] ?? ''); $fieldConfig = $config['fields'][$field] ?? null; if ($fieldConfig === null) { Http::error("Änderung $position: Feld \"$field\" kann nicht live bearbeitet werden.", 422); } $id = $change['id'] ?? null; if ($config['key'] === 'id') { $id = (int) $id; if ($id <= 0) { Http::error("Änderung $position: ungültiger Eintrag.", 422); } } else { $id = (string) $id; $allowed = $entity === 'home_sections' ? HomeSectionController::ALLOWED_SLUGS : PageContentController::ALLOWED_SLUGS; if (!in_array($id, $allowed, true)) { Http::error("Änderung $position: unbekannter Abschnitt.", 422); } } if (empty($fieldConfig['html'])) { $value = trim(empty($fieldConfig['multiline']) ? preg_replace('/\s*\n\s*/', ' ', $value) : $value); } $isEmpty = trim(strip_tags($value)) === ''; // Pflichtfelder duerfen auf Deutsch nicht leer werden; ein leeres Englisch heisst // "deutschen Text verwenden" (bzw. bei faqs: leer lassen ist erlaubt). if (!empty($fieldConfig['required']) && $isEmpty && ($lang === 'de' || $fieldConfig['lang'] === 'single')) { Http::error("Änderung $position: dieses Feld darf nicht leer sein.", 422); } return [ 'entity' => $entity, 'config' => $config, 'field' => $field, 'fieldConfig' => $fieldConfig, 'id' => $id, 'lang' => $lang, 'value' => $isEmpty ? '' : $value, ]; } private static function apply(\PDO $db, array $change): void { if ($change['entity'] === 'text') { if ($change['value'] === '') { $db->prepare('DELETE FROM text_overrides WHERE lang = ? AND text_key = ?') ->execute([$change['lang'], $change['id']]); } else { $db->prepare( 'INSERT INTO text_overrides (lang, text_key, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)' )->execute([$change['lang'], $change['id'], $change['value']]); } return; } $config = $change['config']; $table = $config['table']; $keyColumn = $config['key']; $mode = $change['fieldConfig']['lang']; $value = $change['value']; if ($mode === 'translation' && $change['lang'] === 'en') { Translations::saveFromBody($change['entity'], (int) $change['id'], [$change['field'] . '_en' => $value]); return; } $column = $mode === 'columns' ? $change['field'] . '_' . $change['lang'] : $change['field']; // Startseiten-Abschnitte und Rechtstexte haben bis zum ersten Speichern evtl. noch keine // Zeile (dann zeigt die Seite die eingebauten Standardtexte) - erst damit anlegen. if ($keyColumn === 'slug') { self::ensureSlugRow($db, $change['entity'], $change['id']); } // Optionale Felder sind NULL-Spalten und bekommen bei leerem Text NULL (wie in den // Admin-Formularen), Pflichtfelder sind NOT NULL (leer kommt dort nur bei faqs-Englisch an). $stored = $value === '' && empty($change['fieldConfig']['required']) ? null : $value; $stmt = $db->prepare("UPDATE `$table` SET `$column` = ? WHERE `$keyColumn` = ?"); $stmt->execute([$stored, $change['id']]); } private static function ensureSlugRow(\PDO $db, string $entity, string $slug): void { $table = $entity === 'home_sections' ? 'home_sections' : 'legal_pages'; $exists = $db->prepare("SELECT 1 FROM `$table` WHERE slug = ?"); $exists->execute([$slug]); if ($exists->fetch()) { return; } $defaults = $entity === 'home_sections' ? HomeSectionController::DEFAULTS[$slug] : ['title' => PageContentController::DEFAULTS[$slug]['title'], 'body_html' => PageContentController::DEFAULTS[$slug]['body_html']]; $columns = array_keys($defaults); $db->prepare( "INSERT INTO `$table` (slug, " . implode(', ', $columns) . ') VALUES (?' . str_repeat(', ?', count($columns)) . ')' )->execute(array_merge([$slug], array_values($defaults))); } }