HifiPlanet-Website/hifi/api/src/Controllers/SiteSettingsController.php
Maaxxs 54928d1411 Add package badges/visibility and a download area, speed up the site
Customer feedback round (4 items):

- Packages: one-click "Sichtbar/Ausgeblendet" toggle per package (saved
  immediately). Hidden packages disappear from the model page; the tier
  look of the remaining cards is ranked across all packages so it stays
  stable.
- Packages: badge per card (Empfohlen, Am beliebtesten, Tipp, Coming soon
  or custom text) plus an optional video link shown as "Zum Video". Legacy
  is_featured maps to "Empfohlen".
- New download area (/downloads + Admin -> Downloads): upload with
  progress, inline audio player, allow-listed file types, optional access
  password, new permission downloads.manage. Files live in
  api/uploads/downloads/ (gitignored, created at runtime).
- Performance:
  - SetupGate no longer refetches and remounts the whole app on every
    navigation.
  - Page loader waits only for pending API calls and above-the-fold images
    (fixes an 8 s hang on /leistungen caused by lazy images inside the
    hidden container).
  - Admin pages lazy-loaded; icon library moved out of the main bundle
    (initial JS 2.2 MB -> 0.48 MB).
  - Background video loads only near the viewport and was re-encoded to
    720p (32 MB -> 8.5 MB).
  - Visitors without a session cookie get no PHP session, so their parallel
    API requests no longer queue on the session lock.
  - index.html is served with Cache-Control: no-cache so browsers don't
    keep a stale build after deploys.
- Fonts (Barlow, Barlow Condensed, Michroma) are self-hosted instead of
  loaded from Google Fonts; the default privacy policy mentions this, lists
  all local storage uses and uses the new TDDDG name.
- A code update that adds a table no longer sends the site into the setup
  wizard while an admin exists.

Run "Datenbankstruktur aktualisieren" after deploying (new package
columns, downloads table, app_settings.downloads_password).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 23:15:48 +02:00

193 lines
9.1 KiB
PHP

<?php
namespace App\Controllers;
use App\Config\Database;
use App\Support\Fonts;
use App\Support\Http;
use App\Support\Uploads;
class SiteSettingsController
{
// Greifen, solange in den Einstellungen noch nichts eingetragen wurde -
// entsprechen dem, was bisher fest im Code stand (Adresse/Oeffnungszeiten waren vorher
// in Footer.jsx/ContactPage.jsx hart codiert, siehe DEFAULT_BUSINESS_HOURS unten).
private const DEFAULTS = [
'phone' => '09373 20 62 390',
'whatsapp' => null,
'contact_email' => 'info@hifi-planet-amorbach.de',
'address_street' => 'Boxbrunner Str. 20a',
'address_zip' => '63916',
'address_city' => 'Amorbach',
'logo_path' => null,
'logo_dark_path' => null,
'hero_image_path' => null,
'ga_measurement_id' => null,
'package_card_theme' => 'graphite',
'package_card_layout' => 'strip',
'package_wheel_hint_interval' => 8,
'package_card_width' => 210,
'package_card_height' => 480,
'vehicle_tile_width' => 240,
'vehicle_tile_height' => 180,
'gallery_card_text_x' => 0,
'gallery_card_text_y' => 100,
'site_font' => null,
'page_loader_enabled' => 1,
];
// Entspricht dem, was zuvor hart in Footer.jsx (isOpenNow() + die drei separaten
// Text-Repraesentationen der Oeffnungszeiten) stand: Mo-Fr 9-18, Sa 10-13, So zu.
private const DEFAULT_BUSINESS_HOURS = [
'mon' => ['open' => '09:00', 'close' => '18:00', 'closed' => false],
'tue' => ['open' => '09:00', 'close' => '18:00', 'closed' => false],
'wed' => ['open' => '09:00', 'close' => '18:00', 'closed' => false],
'thu' => ['open' => '09:00', 'close' => '18:00', 'closed' => false],
'fri' => ['open' => '09:00', 'close' => '18:00', 'closed' => false],
'sat' => ['open' => '10:00', 'close' => '13:00', 'closed' => false],
'sun' => ['open' => '', 'close' => '', 'closed' => true],
];
private const APP_SETTINGS_COLUMNS = 'phone, whatsapp, contact_email, address_street, address_zip, address_city,
logo_path, logo_dark_path, business_hours, hero_image_path, ga_measurement_id,
package_card_theme, package_card_layout, package_wheel_hint_interval,
package_card_width, package_card_height, vehicle_tile_width, vehicle_tile_height,
gallery_card_text_x, gallery_card_text_y, site_font, page_loader_enabled';
public static function show(): void
{
try {
$stmt = Database::connection()->query(
'SELECT ' . self::APP_SETTINGS_COLUMNS . ' FROM app_settings WHERE id = 1'
);
$row = $stmt->fetch();
} catch (\Throwable $e) {
$row = false;
}
$row = $row ?: [];
$result = [];
foreach (self::DEFAULTS as $key => $default) {
$result[$key] = ($row[$key] ?? null) !== null && $row[$key] !== '' ? $row[$key] : $default;
}
$decodedHours = is_string($row['business_hours'] ?? null) ? json_decode($row['business_hours'], true) : null;
$result['business_hours'] = is_array($decodedHours) ? $decodedHours : self::DEFAULT_BUSINESS_HOURS;
// Navbar/Footer zeigen "Downloads" nur, wenn dort auch etwas zu finden ist.
$result['has_downloads'] = DownloadController::hasVisible();
Http::send($result);
}
private const PACKAGE_CARD_THEMES = ['graphite', 'deep-blue', 'warm-bronze'];
private const PACKAGE_CARD_LAYOUTS = ['grid', 'strip', 'coverflow'];
public static function update(): void
{
$body = Http::jsonBody();
$db = Database::connection();
$packageCardTheme = trim($body['package_card_theme'] ?? '') ?: 'graphite';
if (!in_array($packageCardTheme, self::PACKAGE_CARD_THEMES, true)) {
$packageCardTheme = 'graphite';
}
$packageCardLayout = trim($body['package_card_layout'] ?? '') ?: 'strip';
if (!in_array($packageCardLayout, self::PACKAGE_CARD_LAYOUTS, true)) {
$packageCardLayout = 'strip';
}
// Wiederholungsabstand des Mausrad-Hinweises - auf einen sinnvollen Bereich
// geklammert, damit ein falscher/leerer Wert nicht zu einem Dauer-Flackern
// oder einem quasi-deaktivierten Hinweis fuehrt.
$wheelHintInterval = (int) ($body['package_wheel_hint_interval'] ?? 8);
$wheelHintInterval = max(2, min(120, $wheelHintInterval ?: 8));
// Kachelgroessen (px) - vom Admin frei eingegeben, nur auf einen Bereich geklammert,
// der weder verschwindend klein noch absurd riesig ist (kein Design-Fehler moeglich,
// aber die Wahl bleibt sonst dem Admin ueberlassen).
$packageCardWidth = max(80, min(600, (int) ($body['package_card_width'] ?? 210)));
$packageCardHeight = max(80, min(900, (int) ($body['package_card_height'] ?? 480)));
$vehicleTileWidth = max(80, min(600, (int) ($body['vehicle_tile_width'] ?? 240)));
$vehicleTileHeight = max(80, min(600, (int) ($body['vehicle_tile_height'] ?? 180)));
// Position des Marken-/Projekt-Namens auf den Bildergalerie-Kacheln in Prozent (X: 0 =
// ganz links, 100 = ganz rechts; Y: 0 = ganz oben, 100 = ganz unten), per Drag & Drop in
// der Live-Vorschau gesetzt (siehe WebsiteSettings.jsx) - auf den gueltigen Bereich
// geklammert, damit ein fehlerhafter Client-Wert nicht ausserhalb der Kachel landet.
$galleryCardTextX = max(0, min(100, (int) ($body['gallery_card_text_x'] ?? 0)));
$galleryCardTextY = max(0, min(100, (int) ($body['gallery_card_text_y'] ?? 100)));
// Globale Standard-Schriftart der Website - faellt auf Systemschrift (leerer String)
// zurueck, wenn ein unbekannter Wert ankommt, statt ihn ungeprueft zu speichern.
$siteFont = (string) ($body['site_font'] ?? '');
if (!Fonts::isValid($siteFont)) {
$siteFont = '';
}
$pageLoaderEnabled = !empty($body['page_loader_enabled']) ? 1 : 0;
$newHeroImagePath = trim($body['hero_image_path'] ?? '') ?: null;
$newLogoPath = trim($body['logo_path'] ?? '') ?: null;
$newLogoDarkPath = trim($body['logo_dark_path'] ?? '') ?: null;
$oldStmt = $db->prepare('SELECT hero_image_path, logo_path, logo_dark_path FROM app_settings WHERE id = 1');
$oldStmt->execute();
$old = $oldStmt->fetch() ?: [];
// Nur echte Wochentags-Schluessel mit den drei erwarteten Feldern uebernehmen - alles
// andere (fehlendes Feld, falscher Typ, unbekannter Tag) faellt auf den Standard zurueck,
// damit ein unvollstaendiger Client-Payload nicht ein kaputtes JSON abspeichert.
$businessHours = self::DEFAULT_BUSINESS_HOURS;
if (is_array($body['business_hours'] ?? null)) {
foreach (self::DEFAULT_BUSINESS_HOURS as $day => $default) {
$incoming = $body['business_hours'][$day] ?? null;
if (is_array($incoming)) {
$businessHours[$day] = [
'open' => trim((string) ($incoming['open'] ?? '')),
'close' => trim((string) ($incoming['close'] ?? '')),
'closed' => !empty($incoming['closed']),
];
}
}
}
$db->exec('INSERT IGNORE INTO app_settings (id) VALUES (1)');
$stmt = $db->prepare(
'UPDATE app_settings SET phone = ?, whatsapp = ?, contact_email = ?, address_street = ?, address_zip = ?,
address_city = ?, logo_path = ?, logo_dark_path = ?, business_hours = ?, hero_image_path = ?,
ga_measurement_id = ?, package_card_theme = ?, package_card_layout = ?, package_wheel_hint_interval = ?,
package_card_width = ?, package_card_height = ?, vehicle_tile_width = ?, vehicle_tile_height = ?,
gallery_card_text_x = ?, gallery_card_text_y = ?, site_font = ?, page_loader_enabled = ?
WHERE id = 1'
);
$stmt->execute([
trim($body['phone'] ?? '') ?: null,
trim($body['whatsapp'] ?? '') ?: null,
trim($body['contact_email'] ?? '') ?: null,
trim($body['address_street'] ?? '') ?: null,
trim($body['address_zip'] ?? '') ?: null,
trim($body['address_city'] ?? '') ?: null,
$newLogoPath,
$newLogoDarkPath,
json_encode($businessHours),
$newHeroImagePath,
trim($body['ga_measurement_id'] ?? '') ?: null,
$packageCardTheme,
$packageCardLayout,
$wheelHintInterval,
$packageCardWidth,
$packageCardHeight,
$vehicleTileWidth,
$vehicleTileHeight,
$galleryCardTextX,
$galleryCardTextY,
$siteFont ?: null,
$pageLoaderEnabled,
]);
Uploads::deleteIfOrphaned($old['hero_image_path'] ?? null, $newHeroImagePath);
Uploads::deleteIfOrphaned($old['logo_path'] ?? null, $newLogoPath);
Uploads::deleteIfOrphaned($old['logo_dark_path'] ?? null, $newLogoDarkPath);
self::show();
}
}