Files
fitme3070.pt/views/admin/modalidades/index.php
T
andreandClaude Fable 5 116817e2de feat(backoffice): exportação CSV nas listagens, com audit
Novo helper csv_export() (config/functions.php): CSV Excel-friendly (BOM UTF-8
+ ';') e cada exportação fica no audit_log (RGPD: exportar dados pessoais tem
de ser rastreável). Botão "Exportar CSV" em todas as listagens do backoffice:
equipa, eventos, galerias, ginásios, horário, mensalidades, modalidades,
horário de funcionamento e lixeira (pedidos de inscrição e aulas experimentais
já tinham). Fecha a metade "exportação com permissões e audit" do item 6.9
(a única parte que o relatório considera ganho real à escala atual).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-25 23:43:41 +01:00

89 lines
4.7 KiB
PHP

<?php
require __DIR__ . '/../../../config/db.php';
require __DIR__ . '/../../../config/functions.php';
$user = require_admin();
// Handle delete BEFORE any output
// POST + CSRF: um link GET podia ser disparado a partir de outro site.
if (isset($_POST['delete']) && is_numeric($_POST['delete'])) {
csrf_check();
db_modalidades_delete($pdo, (int) $_POST['delete']);
session()->setFlashdata('success', 'Modalidade apagada — fica na lixeira 30 dias.');
header('Location: ' . base_url('views/admin/modalidades/index.php'));
exit;
}
// Optimize existing icons (resize/compress to the modalidades cap).
// Covers the whole icons dir, shared flaticon library included — resizing in
// place keeps the filenames, so nothing the picker or the site references breaks.
if (isset($_POST['optimize'])) {
csrf_check();
$stats = img_optimize_dir(modalidades_icon_dir(), IMG_MAX_MODALIDADE);
session()->setFlashdata($stats['failed'] ? 'warning' : 'success', img_stats_message($stats));
header('Location: ' . base_url('views/admin/modalidades/index.php'));
exit;
}
$items = db_modalidades_list($pdo);
if (isset($_GET['csv'])) {
$rows = [];
foreach ($items as $item) {
$rows[] = [$item['nome'], $item['cor'] ?? '', $item['item_order']];
}
csv_export('modalidade', 'modalidades', 'Exportação CSV de modalidades', ['Nome', 'Cor', 'Ordem'], $rows);
}
$page_title = 'Modalidades';
include __DIR__ . '/../../../includes/header.php';
?>
<div class="container">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0">Modalidades</h1>
<div class="d-flex gap-2">
<a class="btn btn-outline-secondary" href="?csv=1"><i class="bi bi-filetype-csv me-1"></i>Exportar CSV</a>
<form method="POST" class="mb-0">
<?= csrf_field() ?>
<button type="submit" name="optimize" value="1" class="btn btn-outline-success"
title="Redimensiona e comprime os ícones para o máximo de <?= IMG_MAX_MODALIDADE ?>px"
onclick="return confirm('Otimizar todos os ícones (máx. <?= IMG_MAX_MODALIDADE ?>px)? Os ficheiros originais serão substituídos.')">
<i class="bi bi-arrows-angle-contract me-1"></i>Otimizar ícones
</button>
</form>
<a class="btn btn-primary" href="<?= base_url('views/admin/modalidades/create.php') ?>"><i class="bi bi-plus-lg me-1"></i>Adicionar Modalidade</a>
</div>
</div>
<div class="card shadow-sm"><div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light"><tr>
<th>Ícone</th><th>Nome</th><th>Cor</th><th>Ordem</th><th style="width:120px">Ações</th>
</tr></thead>
<tbody>
<?php if (!$items): ?>
<tr><td colspan="5" class="text-center text-muted py-4">Nenhuma modalidade.</td></tr>
<?php else: ?>
<?php foreach ($items as $item): ?>
<tr>
<td><?php if ($item['icon']): ?><img src="<?= base_url('uploads/modalidades/icons/' . $item['icon']) ?>" alt="" style="height:32px;"><?php else: ?><span class="text-muted">—</span><?php endif; ?></td>
<td><strong><?= htmlspecialchars($item['nome']) ?></strong></td>
<td><span style="display:inline-block;width:24px;height:24px;border-radius:4px;background:<?= htmlspecialchars($item['cor']) ?>;border:1px solid rgba(0,0,0,.1)"></span> <?= htmlspecialchars($item['cor']) ?></td>
<td><?= (int) $item['item_order'] ?></td>
<td>
<?php if ($item['deleted_at']): ?>
<form method="POST" action="edit.php?id=<?= (int) $item['m_id'] ?>&amp;restore=1" class="d-inline"><?= csrf_field() ?><button class="btn btn-sm btn-outline-info" name="restore" value="1">Restaurar</button></form>
<?php else: ?>
<a class="btn btn-sm btn-outline-primary" href="edit.php?id=<?= $item['m_id'] ?>"><i class="bi bi-pencil"></i></a>
<form method="POST" class="d-inline" onsubmit="return confirm('Apagar esta modalidade?')"><?= csrf_field() ?><button class="btn btn-sm btn-outline-danger" name="delete" value="<?= (int) $item['m_id'] ?>" aria-label="Apagar"><i class="bi bi-trash"></i></button></form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
</div></div>
</div>
<?php include __DIR__ . '/../../../includes/footer.php';