Phase 1 of the dashboard roadmap. ImageUploadField now has a clear button, and every controller with an image/cover_image column deletes the old file from hifi/api/uploads/ when it's replaced or the owning record is deleted (including cascade cleanup for gallery brand/project deletes) - previously no controller ever unlinked a physical file. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
126 lines
4.2 KiB
PHP
126 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Config\Database;
|
|
use App\Support\Http;
|
|
use App\Support\Slug;
|
|
use App\Support\Uploads;
|
|
|
|
class GalleryBrandController
|
|
{
|
|
public static function index(): void
|
|
{
|
|
$stmt = Database::connection()->query(
|
|
'SELECT id, name, slug, cover_image_path, sort_order FROM gallery_brands ORDER BY sort_order, name'
|
|
);
|
|
Http::send($stmt->fetchAll());
|
|
}
|
|
|
|
public static function store(): void
|
|
{
|
|
$body = Http::jsonBody();
|
|
$name = trim($body['name'] ?? '');
|
|
if ($name === '') {
|
|
Http::error('Name erforderlich', 422);
|
|
}
|
|
|
|
$slug = Slug::make($name);
|
|
$db = Database::connection();
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO gallery_brands (name, slug, cover_image_path, sort_order) VALUES (?, ?, ?, ?)'
|
|
);
|
|
$stmt->execute([
|
|
$name,
|
|
$slug,
|
|
trim($body['cover_image_path'] ?? '') ?: null,
|
|
(int) ($body['sort_order'] ?? 0),
|
|
]);
|
|
|
|
Http::send(['id' => (int) $db->lastInsertId(), 'name' => $name, 'slug' => $slug], 201);
|
|
}
|
|
|
|
public static function update(array $params): void
|
|
{
|
|
$id = (int) $params['id'];
|
|
$body = Http::jsonBody();
|
|
$name = trim($body['name'] ?? '');
|
|
if ($name === '') {
|
|
Http::error('Name erforderlich', 422);
|
|
}
|
|
|
|
$newCoverPath = trim($body['cover_image_path'] ?? '') ?: null;
|
|
$db = Database::connection();
|
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_brands WHERE id = ?');
|
|
$oldStmt->execute([$id]);
|
|
$oldCoverPath = $oldStmt->fetchColumn();
|
|
|
|
$stmt = $db->prepare(
|
|
'UPDATE gallery_brands SET name = ?, slug = ?, cover_image_path = ?, sort_order = ? WHERE id = ?'
|
|
);
|
|
$stmt->execute([
|
|
$name,
|
|
Slug::make($name),
|
|
$newCoverPath,
|
|
(int) ($body['sort_order'] ?? 0),
|
|
$id,
|
|
]);
|
|
|
|
Uploads::deleteIfOrphaned($oldCoverPath ?: null, $newCoverPath);
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function destroy(array $params): void
|
|
{
|
|
$id = (int) $params['id'];
|
|
$db = Database::connection();
|
|
|
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_brands WHERE id = ?');
|
|
$oldStmt->execute([$id]);
|
|
$oldCoverPath = $oldStmt->fetchColumn();
|
|
|
|
// ON DELETE CASCADE raeumt gallery_projects/gallery_photos in der DB mit auf, aber die
|
|
// dazugehoerigen Bilddateien muessen wir vorher selbst einsammeln - nach dem DELETE sind
|
|
// die Zeilen (und damit die Pfade) weg.
|
|
$projectImagesStmt = $db->prepare('SELECT cover_image_path FROM gallery_projects WHERE gallery_brand_id = ?');
|
|
$projectImagesStmt->execute([$id]);
|
|
$projectImages = $projectImagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
|
|
|
$photoImagesStmt = $db->prepare(
|
|
'SELECT p.image_path FROM gallery_photos p
|
|
JOIN gallery_projects gp ON gp.id = p.gallery_project_id
|
|
WHERE gp.gallery_brand_id = ?'
|
|
);
|
|
$photoImagesStmt->execute([$id]);
|
|
$photoImages = $photoImagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
|
|
|
$stmt = $db->prepare('DELETE FROM gallery_brands WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
|
|
foreach ([$oldCoverPath, ...$projectImages, ...$photoImages] as $path) {
|
|
Uploads::deleteIfOrphaned($path ?: null, null);
|
|
}
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function projectsForBrand(array $params): void
|
|
{
|
|
$db = Database::connection();
|
|
$brandStmt = $db->prepare('SELECT id, name, slug, cover_image_path FROM gallery_brands WHERE slug = ?');
|
|
$brandStmt->execute([$params['slug']]);
|
|
$brand = $brandStmt->fetch();
|
|
|
|
if (!$brand) {
|
|
Http::error('Marke nicht gefunden', 404);
|
|
}
|
|
|
|
$stmt = $db->prepare(
|
|
'SELECT id, name, slug, cover_image_path, sort_order FROM gallery_projects WHERE gallery_brand_id = ? ORDER BY sort_order, name'
|
|
);
|
|
$stmt->execute([$brand['id']]);
|
|
|
|
Http::send(['brand' => $brand, 'projects' => $stmt->fetchAll()]);
|
|
}
|
|
}
|