create.php e edit.php tinham os mesmos campos de formulário duplicados lado a lado. Extrai o markup comum (nome, nascimento, área, estado, foto, título, aptidões, formações, descrição) para _form.php, seguindo o padrão já usado em events/ e memberships/. Cada view fica só com a lógica que lhe é própria (criar vs. carregar registo existente, upload/validação de foto, título da página, redirect). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
86 lines
3.5 KiB
PHP
86 lines
3.5 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
|
|
// 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_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'] ?? ''),
|
|
'title' => mb_substr(trim($_POST['title'] ?? ''), 0, 100),
|
|
'skills' => trim($_POST['skills'] ?? ''),
|
|
'certifications' => trim($_POST['certifications'] ?? ''),
|
|
];
|
|
if ($data['name'] === '') $errors[] = 'O nome é obrigatório.';
|
|
|
|
$photo_name = null;
|
|
if (!empty($_FILES['photo']['tmp_name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
|
|
// Tipo decidido pelo conteúdo, não pelo nome (config/functions_images.php).
|
|
$ext = upload_image_ext($_FILES['photo'], ['jpg','jpeg','png','gif','webp']);
|
|
if ($ext === null) $errors[] = 'Formato de foto inválido (JPG, PNG, GIF ou WebP).';
|
|
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() ?>
|
|
<?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'; ?>
|