Files
fitme3070.pt/views/admin/team/edit.php
T
2026-06-12 21:20:23 +01:00

105 lines
5.0 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; }
// Handle restore
if (isset($_GET['restore'])) {
csrf_check();
db_team_restore($pdo, $id);
session()->setFlashdata('success', 'Membro restaurado.');
header('Location: index.php');
exit;
}
$item = db_team_get($pdo, $id);
if (!$item) { header('Location: index.php'); exit; }
$errors = [];
$data = $item;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_check();
$data = [
'name' => trim($_POST['name'] ?? ''),
'birthday' => trim($_POST['birthday'] ?? ''),
'at' => trim($_POST['at'] ?? ''),
'status' => $_POST['status'] ?? 'ativo',
'description' => trim($_POST['description'] ?? ''),
];
if ($data['name'] === '') $errors[] = 'O nome é obrigatório.';
$photo_name = null;
if (!empty($_FILES['photo']['tmp_name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
$ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
if (!in_array($ext, ['jpg','jpeg','png','gif','webp'])) $errors[] = 'Formato de foto inválido.';
else {
$upload_dir = __DIR__ . '/../../../uploads/team/foto/';
if (!is_dir($upload_dir)) @mkdir($upload_dir, 0775, true);
$photo_name = bin2hex(random_bytes(16)) . '.' . $ext;
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $upload_dir . $photo_name)) {
$errors[] = 'Falha ao guardar a foto.';
$photo_name = null;
} else {
img_optimize($upload_dir . $photo_name, IMG_MAX_TEAM);
}
}
}
if (!$errors) {
$changed = db_team_update($pdo, $id, $data, $photo_name);
if ($changed) session()->setFlashdata('success', 'Membro atualizado.');
else session()->setFlashdata('warning', 'Nenhuma alteração.');
header('Location: ' . base_url('views/admin/team/edit.php?id=' . $id));
exit;
}
}
$page_title = 'Editar Membro';
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 Membro</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="name" required value="<?= htmlspecialchars($data['name'] ?? '') ?>"></div>
<div class="col-md-4"><label class="form-label">Nascimento</label>
<input type="date" class="form-control" name="birthday" value="<?= htmlspecialchars($data['birthday'] ?? '') ?>"></div>
<div class="col-md-4"><label class="form-label">Área</label>
<select class="form-select" name="at"><option value="">—</option>
<option value="gym" <?= ($data['at'] ?? '') === 'gym' ? 'selected' : '' ?>>Gym</option>
<option value="crossfit" <?= ($data['at'] ?? '') === 'crossfit' ? 'selected' : '' ?>>CrossFit</option>
</select></div>
<div class="col-md-4"><label class="form-label">Estado</label>
<select class="form-select" name="status">
<option value="ativo" <?= ($data['status'] ?? '') === 'ativo' ? 'selected' : '' ?>>Ativo</option>
<option value="inativo" <?= ($data['status'] ?? '') === 'inativo' ? 'selected' : '' ?>>Inativo</option>
</select></div>
<div class="col-md-4">
<label class="form-label">Foto</label>
<?php if (!empty($item['photo'])): ?>
<div class="mb-2"><img src="<?= base_url('uploads/team/foto/' . $item['photo']) ?>" alt="" style="max-height:80px;border-radius:4px;"></div>
<?php endif; ?>
<input type="file" class="form-control" name="photo" accept=".jpg,.jpeg,.png,.gif,.webp">
</div>
<div class="col-12"><label class="form-label">Descrição</label>
<textarea class="form-control js-wysiwyg" data-wysiwyg-height="200" name="description" rows="3"><?= htmlspecialchars($data['description'] ?? '') ?></textarea></div>
</div>
<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'; ?>