157 lines
8.5 KiB
PHP
157 lines
8.5 KiB
PHP
<?php
|
|
require __DIR__ . '/../../../config/db.php';
|
|
require __DIR__ . '/../../../config/functions.php';
|
|
$user = require_admin();
|
|
|
|
$id = (int) ($_GET['id'] ?? 0);
|
|
if (!$id) { header('Location: index.php'); exit; }
|
|
|
|
$gallery = db_gallery_get($pdo, $id);
|
|
if (!$gallery) { header('Location: index.php'); exit; }
|
|
|
|
$errors = [];
|
|
$name = $gallery['name'];
|
|
$images = db_gallery_images($pdo, $id);
|
|
|
|
// Rename gallery
|
|
if (isset($_POST['rename'])) {
|
|
csrf_check();
|
|
$name = trim($_POST['name'] ?? '');
|
|
if ($name === '') $errors[] = 'O nome é obrigatório.';
|
|
elseif (db_gallery_update($pdo, $id, $name)) {
|
|
session()->setFlashdata('success', 'Galeria renomeada.');
|
|
header('Location: ' . base_url('views/admin/galerias/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Upload images (several at once: images[])
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload'])) {
|
|
csrf_check();
|
|
$files = [];
|
|
foreach ((array) ($_FILES['images']['name'] ?? []) as $i => $n) {
|
|
if ($_FILES['images']['error'][$i] === UPLOAD_ERR_NO_FILE) continue;
|
|
$files[] = ['name' => $n, 'type' => $_FILES['images']['type'][$i], 'tmp_name' => $_FILES['images']['tmp_name'][$i],
|
|
'error' => $_FILES['images']['error'][$i], 'size' => $_FILES['images']['size'][$i]];
|
|
}
|
|
$ok = 0; $failed = [];
|
|
foreach ($files as $file) {
|
|
if (db_gallery_image_upload($pdo, $id, $file, $why)) $ok++;
|
|
else $failed[] = $file['name'] . ' (' . $why . ')';
|
|
}
|
|
if ($ok) session()->setFlashdata('success', $ok === 1 ? 'Imagem carregada.' : "$ok imagens carregadas.");
|
|
if ($failed) session()->setFlashdata($ok ? 'warning' : 'error', 'Não carregadas: ' . implode(', ', $failed) . '.');
|
|
if (!$files) session()->setFlashdata('error', 'Escolha pelo menos uma imagem.');
|
|
header('Location: ' . base_url('views/admin/galerias/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
// Over post_max_size PHP drops the whole body ($_POST and $_FILES empty), so say so instead of doing nothing.
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$_POST && (int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > 0) {
|
|
session()->setFlashdata('error', 'O envio é maior do que o servidor aceita (' . ini_get('post_max_size') . ' no total). Carregue menos imagens de cada vez.');
|
|
header('Location: ' . base_url('views/admin/galerias/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
|
|
// Optimize existing images (resize/compress to the gallery cap)
|
|
if (isset($_POST['optimize'])) {
|
|
csrf_check();
|
|
$stats = img_optimize_dir(__DIR__ . '/../../../uploads/gallery/' . $id . '/', IMG_MAX_GALLERY);
|
|
session()->setFlashdata($stats['failed'] ? 'warning' : 'success', img_stats_message($stats));
|
|
header('Location: ' . base_url('views/admin/galerias/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
|
|
// Delete image
|
|
// POST + CSRF: um link GET podia ser disparado a partir de outro site.
|
|
if (isset($_POST['del_img']) && is_numeric($_POST['del_img'])) {
|
|
csrf_check();
|
|
db_gallery_image_delete($pdo, (int) $_POST['del_img'], $id);
|
|
session()->setFlashdata('success', 'Imagem removida — fica na lixeira 30 dias.');
|
|
header('Location: ' . base_url('views/admin/galerias/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
|
|
$page_title = 'Gerir Galeria';
|
|
include __DIR__ . '/../../../includes/header.php';
|
|
?>
|
|
<div class="container" style="max-width:900px;">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h3 mb-0"><?= htmlspecialchars($gallery['name']) ?></h1>
|
|
<a class="btn btn-outline-secondary btn-sm" href="index.php"><i class="bi bi-arrow-left me-1"></i>Voltar</a>
|
|
</div>
|
|
<?php foreach ($errors as $e): ?><div class="alert alert-danger"><?= htmlspecialchars($e) ?></div><?php endforeach; ?>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-md-4">
|
|
<div class="card shadow-sm"><div class="card-body">
|
|
<h2 class="h6 mb-3">Renomear</h2>
|
|
<form method="POST">
|
|
<?= csrf_field() ?>
|
|
<div class="mb-3">
|
|
<input class="form-control" name="name" value="<?= htmlspecialchars($name) ?>">
|
|
</div>
|
|
<button type="submit" name="rename" class="btn btn-primary btn-sm">Renomear</button>
|
|
</form>
|
|
</div></div>
|
|
|
|
<div class="card shadow-sm mt-3"><div class="card-body">
|
|
<h2 class="h6 mb-3">Adicionar imagens</h2>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<?= csrf_field() ?>
|
|
<div class="mb-3">
|
|
<input type="file" class="form-control" name="images[]" multiple accept=".jpg,.jpeg,.png,.gif,.webp,.jxl,image/jpeg,image/png,image/gif,image/webp,image/jxl">
|
|
<div class="form-text">Pode escolher várias. JPG, PNG, GIF, WebP ou JPEG XL, até <?= round(gallery_upload_max_bytes() / 1048576) ?> MB cada (máx. <?= (int) ini_get('max_file_uploads') ?> de cada vez, <?= htmlspecialchars((string) ini_get('post_max_size')) ?> no total).</div>
|
|
</div>
|
|
<button type="submit" name="upload" value="1" class="btn btn-primary btn-sm"><i class="bi bi-cloud-arrow-up me-1"></i>Carregar</button>
|
|
</form>
|
|
</div></div>
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
<div class="card shadow-sm"><div class="card-body">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h2 class="h6 mb-0">Imagens (<?= count($images) ?>)</h2>
|
|
<?php if ($images): ?>
|
|
<form method="POST" class="mb-0">
|
|
<?= csrf_field() ?>
|
|
<button type="submit" name="optimize" value="1" class="btn btn-sm btn-outline-success"
|
|
title="Redimensiona e comprime as imagens desta galeria para o máximo de <?= IMG_MAX_GALLERY ?>px"
|
|
onclick="return confirm('Otimizar todas as imagens desta galeria (máx. <?= IMG_MAX_GALLERY ?>px)? Os ficheiros originais serão substituídos.')">
|
|
<i class="bi bi-arrows-angle-contract me-1"></i>Otimizar imagens
|
|
</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php if (!$images): ?>
|
|
<p class="text-muted">Nenhuma imagem nesta galeria.</p>
|
|
<?php else: ?>
|
|
<div class="row g-2">
|
|
<?php foreach ($images as $img): ?>
|
|
<?php $rel = 'uploads/gallery/' . $id . '/' . $img['i_file_name']; ?>
|
|
<div class="col-6 col-lg-4 position-relative">
|
|
<?php if (str_ends_with(strtolower($rel), '.jxl') && img_fallback_rel($rel) === ''): ?>
|
|
<?php /* JPEG XL still without its WebP/JPG copy (no decoder on the server yet): most browsers
|
|
can't draw it, so show what is stored instead of a blank tile. */ ?>
|
|
<div class="img-thumbnail w-100 d-flex flex-column align-items-center justify-content-center text-center small text-body-secondary" style="height:140px;">
|
|
<i class="bi bi-file-earmark-image fs-3"></i>
|
|
<span class="badge text-bg-secondary my-1">JPEG XL</span>
|
|
<span class="text-truncate w-100 px-1" title="<?= htmlspecialchars($img['i_file_name']) ?>"><?= htmlspecialchars($img['i_file_name']) ?></span>
|
|
</div>
|
|
<?php else: ?>
|
|
<?= img_tag($rel, ['alt' => '', 'class' => 'img-thumbnail w-100', 'style' => 'height:140px;object-fit:cover;', 'loading' => 'lazy', 'width' => null]) ?>
|
|
<?php endif; ?>
|
|
<form method="POST" class="position-absolute top-0 end-0 m-1" onsubmit="return confirm('Remover esta imagem? Fica na lixeira durante 30 dias.')">
|
|
<?= csrf_field() ?>
|
|
<button class="btn btn-sm btn-danger rounded-circle" name="del_img" value="<?= (int) $img['i_id'] ?>" aria-label="Remover imagem"
|
|
style="width:28px;height:28px;display:flex;align-items:center;justify-content:center;font-size:12px">×</button>
|
|
</form>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php include __DIR__ . '/../../../includes/footer.php'; ?>
|