HifiPlanet-Website/hifi/api/src/Controllers/SiteSettingsController.php
Maaxxs 49e2700399 Make brand/model tile size and package card size admin-configurable
Adds four numeric app_settings fields (vehicle_tile_width/height,
package_card_width/height, all in px, clamped server-side to a sane
range but otherwise a free number as requested) with matching inputs
in WebsiteSettings.jsx.

Brand/model tiles (VehicleSelect.jsx, BrandPage.jsx) move from a
responsive CSS-grid with an implicit per-column width to a flex-wrap
layout where every tile has the admin's chosen fixed width/height -
applies to both the photo-background and plain-text card variants, so
mixed grids stay visually consistent.

Package cards (ModelPage.jsx) keep their three existing layout modes
(grid/strip/coverflow) but the actual card dimensions - previously
hardcoded Tailwind width/min-height classes with a separate mobile/sm
breakpoint value - are now driven by the same two settings. Grid mode's
column count is no longer fixed at 2/3 columns; it's now
repeat(auto-fill, minmax(width, 1fr)), so the chosen width determines how
many cards fit per row. Coverflow's center-scroll padding, previously
hardcoded against the old fixed 230px card width, now derives from the
configured width too.

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

187 lines
8.7 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,
];
// 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';
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;
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 = '';
}
$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 = ?
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,
]);
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();
}
}