93 lines
4.5 KiB
PHP
93 lines
4.5 KiB
PHP
<?php
|
|
// ─── Galleries & images CRUD helpers ───────────────────────────────────────────
|
|
// galleries — gallery names (id, name)
|
|
// gallery — images (i_id, i_file_name, i_file_type, i_project_id, i_created_at)
|
|
// Also uses `images` table for standalone uploaded images (img_id, img_file_name).
|
|
|
|
function db_gallery_list(PDO $pdo): array {
|
|
return $pdo->query("SELECT * FROM galleries WHERE deleted_at IS NULL ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
// id => name pairs for a <select> (gyms create/edit "gallery" picker).
|
|
function db_galleries_options(PDO $pdo): array {
|
|
return $pdo->query("SELECT id, name FROM galleries WHERE deleted_at IS NULL ORDER BY id")
|
|
->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
}
|
|
|
|
function db_gallery_get(PDO $pdo, int $id): ?array {
|
|
$stmt = $pdo->prepare("SELECT * FROM galleries WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
|
|
}
|
|
|
|
function db_gallery_create(PDO $pdo, string $name): int {
|
|
$stmt = $pdo->prepare("INSERT INTO galleries (name) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
$__id = (int) $pdo->lastInsertId();
|
|
audit_change($pdo, 'gallery', 'galleries', 'id', (int) $__id, 'create', null, 'name');
|
|
return $__id;
|
|
}
|
|
|
|
function db_gallery_update(PDO $pdo, int $id, string $name): bool {
|
|
$__before = audit_row($pdo, 'galleries', 'id', (int) $id);
|
|
$stmt = $pdo->prepare("UPDATE galleries SET name = ? WHERE id = ?");
|
|
$stmt->execute([$name, $id]);
|
|
$__r = $stmt->rowCount() > 0;
|
|
audit_change($pdo, 'gallery', 'galleries', 'id', (int) $id, 'update', $__before, 'name');
|
|
return $__r;
|
|
}
|
|
|
|
// Soft delete para a lixeira (config/functions_trash.php). As imagens ficam
|
|
// ligadas à galeria e só saem com a eliminação definitiva, 30 dias depois.
|
|
function db_gallery_delete(PDO $pdo, int $id): bool {
|
|
return trash_delete($pdo, 'gallery', $id);
|
|
}
|
|
|
|
// Gallery images
|
|
function db_gallery_images(PDO $pdo, int $gallery_id): array {
|
|
$stmt = $pdo->prepare("SELECT * FROM gallery WHERE i_project_id = ? AND deleted_at IS NULL ORDER BY i_created_at DESC");
|
|
$stmt->execute([$gallery_id]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
// Limite por ficheiro: o menor entre 16 MB e o upload_max_filesize do PHP.
|
|
function gallery_upload_max_bytes(): int {
|
|
$ini = trim((string) ini_get('upload_max_filesize'));
|
|
$n = (int) $ini;
|
|
$n *= match (strtolower(substr($ini, -1))) { 'g' => 1 << 30, 'm' => 1 << 20, 'k' => 1 << 10, default => 1 };
|
|
return $n > 0 ? min($n, 16 * 1024 * 1024) : 16 * 1024 * 1024;
|
|
}
|
|
|
|
// $error recebe o motivo (para a mensagem ao utilizador) quando devolve null.
|
|
function db_gallery_image_upload(PDO $pdo, int $gallery_id, array $file, ?string &$error = null): ?string {
|
|
$max_mb = round(gallery_upload_max_bytes() / 1048576);
|
|
if ($file['error'] === UPLOAD_ERR_INI_SIZE || $file['error'] === UPLOAD_ERR_FORM_SIZE) { $error = "maior que {$max_mb} MB"; return null; }
|
|
if ($file['error'] !== UPLOAD_ERR_OK) { $error = 'o envio falhou'; return null; }
|
|
|
|
if (($file['size'] ?? 0) > gallery_upload_max_bytes()) { $error = "maior que {$max_mb} MB"; return null; }
|
|
// Tipo decidido pelo conteúdo, não pelo nome (config/functions_images.php).
|
|
$ext = upload_image_ext($file, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'jxl']);
|
|
if ($ext === null) { $error = 'formato não suportado'; return null; }
|
|
|
|
$upload_dir = __DIR__ . '/../uploads/gallery/' . $gallery_id . '/';
|
|
if (!is_dir($upload_dir)) @mkdir($upload_dir, 0775, true);
|
|
|
|
$filename = bin2hex(random_bytes(16)) . '.' . $ext;
|
|
if (!move_uploaded_file($file['tmp_name'], $upload_dir . $filename)) { $error = 'não foi possível gravar'; return null; }
|
|
img_optimize($upload_dir . $filename, IMG_MAX_GALLERY);
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO gallery (i_file_name, i_file_type, i_project_id) VALUES (?, ?, ?)");
|
|
$stmt->execute([$filename, $ext === 'jxl' ? 'image/jxl' : ($file['type'] ?? 'image/jpeg'), $gallery_id]);
|
|
return $filename;
|
|
}
|
|
|
|
// A imagem tem de pertencer à galeria aberta. Vai para a lixeira: o ficheiro
|
|
// passa para uploads/gallery/<id>/.trash/ (sai do site, que lista a pasta) e
|
|
// só é apagado com a eliminação definitiva.
|
|
function db_gallery_image_delete(PDO $pdo, int $image_id, int $gallery_id): bool {
|
|
$own = $pdo->prepare("SELECT 1 FROM gallery WHERE i_id = ? AND i_project_id = ?");
|
|
$own->execute([$image_id, $gallery_id]);
|
|
if (!$own->fetchColumn()) return false;
|
|
return trash_delete($pdo, 'gallery_image', $image_id);
|
|
}
|