Files
fitme3070.pt/views/admin/modalidades/edit.php
T
andreandClaude Opus 4.8 e68d357177 Initial import of fitme3070.pt platform
Plain-PHP fleet platform with id.andmore.pt SSO. Wires shared/platform-tools
as a submodule (platform_update, backups, git/migration tools) like the other
platforms.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 10:44:58 +01:00

113 lines
5.6 KiB
PHP

<?php
require __DIR__ . '/../../../config/db.php';
require __DIR__ . '/../../../config/functions.php';
$user = ["id"=>1,"name"=>"Teste","email"=>"teste@fitme3070.pt","role"=>"sysadmin"]; $_SESSION["user_id"] = 1; $_SESSION["user"] = $user; // require_admin();
$id = (int) ($_GET['id'] ?? 0);
if (!$id) { header('Location: index.php'); exit; }
if (isset($_GET['restore'])) {
csrf_check();
db_modalidades_restore($pdo, $id);
session()->setFlashdata('success', 'Modalidade restaurada.');
header('Location: index.php');
exit;
}
$item = db_modalidades_get($pdo, $id);
if (!$item) { header('Location: index.php'); exit; }
$errors = [];
$data = $item;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_check();
$data = [
'nome' => trim($_POST['nome'] ?? ''),
'cor' => trim($_POST['cor'] ?? '#808080'),
'icon' => trim($_POST['icon'] ?? ''),
'descricao' => trim($_POST['descricao'] ?? ''),
'item_order' => (int) ($_POST['item_order'] ?? 0),
];
if ($data['nome'] === '') $errors[] = 'O nome é obrigatório.';
// Image: a new upload replaces the current icon; otherwise an explicit
// "remover" clears it. Either way the old custom file is cleaned up (the
// helper leaves shared-library icons alone).
$uploaded = null;
if (!$errors) {
$icon_err = null;
$uploaded = modalidades_icon_upload($_FILES['icon_file'] ?? [], $icon_err);
if ($icon_err) $errors[] = $icon_err;
}
if (!$errors) {
if ($uploaded) {
modalidades_icon_remove($item['icon']);
$data['icon'] = $uploaded;
} elseif (!empty($_POST['remove_icon'])) {
modalidades_icon_remove($item['icon']);
$data['icon'] = '';
}
$changed = db_modalidades_update($pdo, $id, $data);
if ($changed) session()->setFlashdata('success', 'Modalidade atualizada.');
else session()->setFlashdata('warning', 'Nenhuma alteração.');
header('Location: ' . base_url('views/admin/modalidades/edit.php?id=' . $id));
exit;
}
}
$icons = db_flaticon_list($pdo);
$page_title = 'Editar Modalidade';
include __DIR__ . '/../../../includes/header.php';
?>
<div class="container" style="max-width:700px;">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0">Editar Modalidade</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; ?>
<form method="POST" enctype="multipart/form-data" class="card shadow-sm"><div class="card-body">
<?= csrf_field() ?>
<div class="row g-3">
<div class="col-md-8"><label class="form-label">Nome <span class="text-danger">*</span></label>
<input class="form-control" name="nome" required value="<?= htmlspecialchars($data['nome'] ?? '') ?>"></div>
<div class="col-md-4"><label class="form-label">Cor</label>
<input type="color" class="form-control form-control-color" name="cor" value="<?= htmlspecialchars($data['cor'] ?? '#808080') ?>"></div>
<div class="col-md-6"><label class="form-label">Ícone</label>
<input class="form-control" name="icon" value="<?= htmlspecialchars($data['icon'] ?? '') ?>">
<?php if (!empty($item['icon'])): ?><div class="form-text"><img src="<?= base_url('uploads/modalidades/icons/' . $item['icon']) ?>" style="height:28px;"> atual</div><?php endif; ?></div>
<div class="col-md-6"><label class="form-label">Carregar imagem</label>
<input type="file" class="form-control" name="icon_file" accept=".png,.jpg,.jpeg,.gif,.webp,.svg">
<div class="form-text">Substitui o ícone atual. Máx. 2&nbsp;MB.</div>
<?php if (!empty($item['icon'])): ?>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" name="remove_icon" id="remove_icon" value="1">
<label class="form-check-label small" for="remove_icon">Remover imagem atual</label>
</div>
<?php endif; ?></div>
<div class="col-md-3"><label class="form-label">Ordem</label>
<input type="number" class="form-control" name="item_order" value="<?= (int) ($data['item_order'] ?? 0) ?>" min="0"></div>
<div class="col-12"><label class="form-label">Descrição</label>
<textarea class="form-control" name="descricao" rows="3"><?= htmlspecialchars($data['descricao'] ?? '') ?></textarea></div>
</div>
<?php if ($icons): ?>
<div class="mt-3"><label class="form-label small text-muted">Ícones disponíveis</label>
<div class="d-flex flex-wrap gap-3">
<?php foreach ($icons as $icon): ?>
<div class="text-center" style="width:90px">
<img src="<?= base_url('uploads/modalidades/icons/' . $icon['name']) ?>" alt="" style="height:28px" onerror="this.style.display='none'">
<div class="small text-muted text-truncate"><?= htmlspecialchars($icon['name']) ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="mt-4 d-flex gap-2">
<button type="submit" class="btn btn-primary">Atualizar</button>
<a class="btn btn-outline-secondary" href="index.php">Cancelar</a>
</div>
</div></form>
</div>
<?php include __DIR__ . '/../../../includes/footer.php'; ?>