Optional unter KI-Modellbilder: das fertige Bild geht mit einem reinen Bearbeitungs- Prompt (eigener Endpunkt moeglich) noch einmal zu Higgsfield. Stapel und Modell-Panel laufen ueber HiggsfieldController::advance (car_models.ai_stage = cleanup). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ex4YsMRQ4GF2dkGWuX6Yio
205 lines
7.8 KiB
PHP
205 lines
7.8 KiB
PHP
<?php
|
||
|
||
namespace App\Controllers;
|
||
|
||
use App\Config\Database;
|
||
use App\Support\Http;
|
||
use App\Support\Translations;
|
||
use App\Support\Slug;
|
||
use App\Support\TextStyleCleanup;
|
||
use App\Support\Uploads;
|
||
|
||
class ModelController
|
||
{
|
||
public static function index(): void
|
||
{
|
||
$stmt = Database::connection()->query(
|
||
'SELECT m.id, m.name, m.slug, m.image_path, m.ai_state, m.ai_stage, m.ai_error, m.sort_order, m.brand_id, b.name AS brand_name, b.slug AS brand_slug
|
||
FROM car_models m JOIN brands b ON b.id = m.brand_id
|
||
ORDER BY b.name, m.sort_order, m.name'
|
||
);
|
||
Http::send($stmt->fetchAll());
|
||
}
|
||
|
||
public static function store(): void
|
||
{
|
||
$body = Http::jsonBody();
|
||
$name = trim($body['name'] ?? '');
|
||
$brandId = (int) ($body['brand_id'] ?? 0);
|
||
|
||
if ($name === '' || $brandId <= 0) {
|
||
Http::error('Name und Marke erforderlich', 422);
|
||
}
|
||
|
||
$db = Database::connection();
|
||
$stmt = $db->prepare(
|
||
'INSERT INTO car_models (brand_id, name, slug, image_path, sort_order) VALUES (?, ?, ?, ?, ?)'
|
||
);
|
||
$stmt->execute([
|
||
$brandId,
|
||
$name,
|
||
Slug::make($name),
|
||
trim($body['image_path'] ?? '') ?: null,
|
||
(int) ($body['sort_order'] ?? 0),
|
||
]);
|
||
|
||
Http::send(['id' => (int) $db->lastInsertId()], 201);
|
||
}
|
||
|
||
public static function update(array $params): void
|
||
{
|
||
$id = (int) $params['id'];
|
||
$body = Http::jsonBody();
|
||
$name = trim($body['name'] ?? '');
|
||
$brandId = (int) ($body['brand_id'] ?? 0);
|
||
|
||
if ($name === '' || $brandId <= 0) {
|
||
Http::error('Name und Marke erforderlich', 422);
|
||
}
|
||
|
||
$newImagePath = trim($body['image_path'] ?? '') ?: null;
|
||
$db = Database::connection();
|
||
$oldStmt = $db->prepare('SELECT image_path FROM car_models WHERE id = ?');
|
||
$oldStmt->execute([$id]);
|
||
$oldImagePath = $oldStmt->fetchColumn();
|
||
|
||
$stmt = $db->prepare(
|
||
'UPDATE car_models SET brand_id = ?, name = ?, slug = ?, image_path = ?, sort_order = ? WHERE id = ?'
|
||
);
|
||
$stmt->execute([
|
||
$brandId,
|
||
$name,
|
||
Slug::make($name),
|
||
$newImagePath,
|
||
(int) ($body['sort_order'] ?? 0),
|
||
$id,
|
||
]);
|
||
|
||
Uploads::deleteIfOrphaned($oldImagePath ?: null, $newImagePath);
|
||
|
||
Http::send(['ok' => true]);
|
||
}
|
||
|
||
public static function destroy(array $params): void
|
||
{
|
||
$id = (int) $params['id'];
|
||
$db = Database::connection();
|
||
|
||
$oldStmt = $db->prepare('SELECT image_path FROM car_models WHERE id = ?');
|
||
$oldStmt->execute([$id]);
|
||
$oldImagePath = $oldStmt->fetchColumn();
|
||
|
||
// ON DELETE CASCADE raeumt packages in der DB mit auf - deren text_styles-Zeilen
|
||
// vorher einsammeln, danach ist die Zuordnung weg.
|
||
$packagesStmt = $db->prepare('SELECT id FROM packages WHERE car_model_id = ?');
|
||
$packagesStmt->execute([$id]);
|
||
$packageIds = $packagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
||
|
||
$stmt = $db->prepare('DELETE FROM car_models WHERE id = ?');
|
||
$stmt->execute([$id]);
|
||
|
||
Uploads::deleteIfOrphaned($oldImagePath ?: null, null);
|
||
|
||
TextStyleCleanup::forgetEntity('car_models', (string) $id);
|
||
foreach ($packageIds as $packageId) {
|
||
TextStyleCleanup::forgetEntity('packages', (string) $packageId);
|
||
}
|
||
|
||
Http::send(['ok' => true]);
|
||
}
|
||
|
||
public static function packagesForModel(array $params): void
|
||
{
|
||
$db = Database::connection();
|
||
$modelStmt = $db->prepare(
|
||
'SELECT m.id, m.name, m.slug, b.name AS brand_name, b.slug AS brand_slug
|
||
FROM car_models m JOIN brands b ON b.id = m.brand_id
|
||
WHERE m.slug = ? AND b.slug = ?'
|
||
);
|
||
$modelStmt->execute([$params['model_slug'], $params['brand_slug']]);
|
||
$model = $modelStmt->fetch();
|
||
|
||
if (!$model) {
|
||
Http::error('Modell nicht gefunden', 404);
|
||
}
|
||
|
||
// SELECT * statt fester Spaltenliste: so bleibt die Seite auch auf einer Datenbank
|
||
// erreichbar, auf der "Datenbankstruktur aktualisieren" nach einem Update noch nicht
|
||
// gelaufen ist (dann fehlen z. B. is_hidden/badge - siehe die ?? unten).
|
||
$pkgStmt = $db->prepare('SELECT * FROM packages WHERE car_model_id = ? ORDER BY sort_order, name');
|
||
$pkgStmt->execute([$model['id']]);
|
||
$rows = $pkgStmt->fetchAll();
|
||
|
||
// Alle Produkte des Modells in einer Abfrage statt zwei Abfragen pro Paket.
|
||
$productsByPackage = [];
|
||
if ($rows) {
|
||
$ids = array_column($rows, 'id');
|
||
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||
$productStmt = $db->prepare(
|
||
"SELECT id, package_id, name_override, scraped_name, scraped_image_url, scrape_status, scraped_price, sort_order
|
||
FROM package_products WHERE package_id IN ($placeholders) ORDER BY sort_order, id"
|
||
);
|
||
$productStmt->execute($ids);
|
||
foreach ($productStmt->fetchAll() as $product) {
|
||
$productsByPackage[$product['package_id']][] = $product;
|
||
}
|
||
}
|
||
|
||
// Preise werden serverseitig aufsummiert und mit dem Aufschlag verrechnet,
|
||
// aber nie pro Bauteil an den Client zurückgegeben – der Kunde sieht nur den Gesamtpreis.
|
||
$totals = [];
|
||
foreach ($rows as $row) {
|
||
$subtotal = 0.0;
|
||
foreach ($productsByPackage[$row['id']] ?? [] as $product) {
|
||
$subtotal += (float) ($product['scraped_price'] ?? 0);
|
||
}
|
||
if ($row['markup_type'] === 'fixed') {
|
||
$total = $subtotal + (float) $row['markup_value'];
|
||
} elseif ($row['markup_type'] === 'percent') {
|
||
$total = $subtotal * (1 + (float) $row['markup_value'] / 100);
|
||
} else {
|
||
$total = $subtotal;
|
||
}
|
||
$totals[$row['id']] = round($total, 2);
|
||
}
|
||
|
||
// Preis-Rang ueber ALLE Pakete des Modells, auch die ausgeblendeten: ModelPage waehlt
|
||
// daraus Farbe/Strassenbild der Kachel. So behalten die sichtbaren Pakete ihr Aussehen,
|
||
// wenn eines dazwischen ausgeblendet wird (asort ist stabil - bei gleichem Preis
|
||
// entscheidet wie bisher die Sortierung).
|
||
$ranked = $totals;
|
||
asort($ranked);
|
||
$rankById = array_flip(array_keys($ranked));
|
||
|
||
$packages = [];
|
||
foreach ($rows as $row) {
|
||
if (!empty($row['is_hidden'])) {
|
||
continue;
|
||
}
|
||
$badge = PackageController::effectiveBadge($row);
|
||
$packages[] = [
|
||
'id' => $row['id'],
|
||
'name' => $row['name'],
|
||
'slug' => $row['slug'],
|
||
'description' => $row['description'],
|
||
'icon_name' => $row['icon_name'],
|
||
'tagline' => $row['tagline'],
|
||
'price_text' => $row['price_text'] ?? null,
|
||
'badge' => $badge,
|
||
'badge_text' => $badge === 'custom' ? ($row['badge_text'] ?? null) : null,
|
||
'video_url' => $row['video_url'] ?? null,
|
||
'is_featured' => $badge === 'recommended',
|
||
'sort_order' => $row['sort_order'],
|
||
'products' => array_map(static function (array $product): array {
|
||
unset($product['scraped_price']);
|
||
return $product;
|
||
}, $productsByPackage[$row['id']] ?? []),
|
||
'total_price' => $totals[$row['id']],
|
||
'tier_rank' => $rankById[$row['id']],
|
||
'tier_count' => count($rows),
|
||
];
|
||
}
|
||
|
||
Http::send(['model' => $model, 'packages' => Translations::attach('packages', $packages)]);
|
||
}
|
||
}
|