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>
68 lines
1.9 KiB
PHP
68 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Config\Database;
|
|
use App\Support\Http;
|
|
use App\Support\Uploads;
|
|
|
|
class GalleryPhotoController
|
|
{
|
|
public static function store(): void
|
|
{
|
|
$body = Http::jsonBody();
|
|
$projectId = (int) ($body['gallery_project_id'] ?? 0);
|
|
$imagePath = trim($body['image_path'] ?? '');
|
|
|
|
if ($projectId <= 0 || $imagePath === '') {
|
|
Http::error('Projekt und Bild erforderlich', 422);
|
|
}
|
|
|
|
$db = Database::connection();
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO gallery_photos (gallery_project_id, image_path, caption, sort_order) VALUES (?, ?, ?, ?)'
|
|
);
|
|
$stmt->execute([
|
|
$projectId,
|
|
$imagePath,
|
|
trim($body['caption'] ?? '') ?: 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();
|
|
|
|
$stmt = Database::connection()->prepare(
|
|
'UPDATE gallery_photos SET caption = ?, sort_order = ? WHERE id = ?'
|
|
);
|
|
$stmt->execute([
|
|
trim($body['caption'] ?? '') ?: null,
|
|
(int) ($body['sort_order'] ?? 0),
|
|
$id,
|
|
]);
|
|
|
|
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 gallery_photos WHERE id = ?');
|
|
$oldStmt->execute([$id]);
|
|
$oldImagePath = $oldStmt->fetchColumn();
|
|
|
|
$stmt = $db->prepare('DELETE FROM gallery_photos WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
|
|
Uploads::deleteIfOrphaned($oldImagePath ?: null, null);
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
}
|