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'; ?>