Extrai os campos comuns (nome, cor, ícone/upload, ordem, descrição, e a biblioteca de ícones disponíveis) para _form.php, seguindo o padrão de events/ e memberships/. A diferença genuína entre criar e editar — o bloco de ícone mostra pré-visualização e checkbox "remover imagem atual" só quando já existe um registo — fica como um if ($item === null) explícito dentro do parcial, tal como o hero_image em events/. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
81 lines
3.1 KiB
PHP
81 lines
3.1 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; }
|
|
|
|
// Restaurar só por POST + CSRF (o formulário aponta para ?restore=1).
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['restore']) || 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() ?>
|
|
<?php include __DIR__ . '/_form.php'; ?>
|
|
<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'; ?>
|