35330-vm/Backend/macro_area_form.php
Flatlogic Bot 20046a4844 Versao 17
2025-10-29 17:18:21 +00:00

115 lines
4.7 KiB
PHP

<?php
require_once 'includes/session.php';
require_once 'db/config.php';
// Helper function to generate a slug from a string
function generateSlug($string) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
$string = strtolower($string);
$string = preg_replace('/[^a-z0-9_\-]+/', '-', $string);
$string = preg_replace('/-+/', '-', $string);
$string = trim($string, '-');
return $string;
}
$pdo = db();
$error = null;
$macro_area = [
'id' => '',
'nome' => '',
'descricao' => '',
'ativo' => 1
];
$page_title = 'Nova Macro Área';
// Handle form submission (Create/Update)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'] ?? null;
$nome = trim($_POST['nome'] ?? '');
$descricao = trim($_POST['descricao'] ?? '');
$ativo = isset($_POST['ativo']) ? 1 : 0;
$slug = generateSlug($nome);
if (empty($nome)) {
$error = "O campo Nome é obrigatório.";
// Repopulate form data on error
$macro_area = $_POST;
} else {
$stmt = $pdo->prepare('SELECT id FROM macro_areas WHERE (nome = ? OR slug = ?) AND id <> ?');
$stmt->execute([$nome, $slug, $id ?: 0]);
if ($stmt->fetch()) {
$error = "Já existe uma Macro Área com este nome.";
// Repopulate form data on error
$macro_area = $_POST;
} else {
if ($id) {
// Update
$stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, slug = ?, descricao = ?, ativo = ? WHERE id = ?');
$stmt->execute([$nome, $slug, $descricao, $ativo, $id]);
$redirect_id = $id;
} else {
// Create
$stmt = $pdo->prepare('INSERT INTO macro_areas (nome, slug, descricao, ativo, user_id) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([$nome, $slug, $descricao, $ativo, $_SESSION['user_id'] ?? 1]);
$redirect_id = $pdo->lastInsertId();
}
header("Location: /Backend/macro_area_form.php");
exit;
}
}
} elseif (isset($_GET['id'])) {
// Handle edit mode (fetch data)
$id = $_GET['id'];
$stmt = $pdo->prepare('SELECT * FROM macro_areas WHERE id = ?');
$stmt->execute([$id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
if ($data) {
$macro_area = $data;
$page_title = 'Editar Macro Área';
}
}
include_once 'includes/header.php';
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0"><?php echo $page_title; ?></h1>
</div>
<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold" style="color: #005C53;">Detalhes da Macro Área</h6>
</div>
<div class="card-body">
<form id="macroAreaForm" method="POST" action="/Backend/macro_area_form.php<?php echo !empty($macro_area['id']) ? '?id='.$macro_area['id'] : ''; ?>">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($macro_area['id']); ?>">
<div class="mb-3">
<label for="macroAreaNome" class="form-label">Nome *</label>
<input type="text" class="form-control" id="macroAreaNome" name="nome" placeholder="Ex: Moradia, Alimentação, Saúde" value="<?php echo htmlspecialchars($macro_area['nome']); ?>" required>
</div>
<div class="mb-3">
<label for="macroAreaDescricao" class="form-label">Descrição</label>
<textarea class="form-control" id="macroAreaDescricao" name="descricao" rows="3" placeholder="Descrição opcional da macro área"><?php echo htmlspecialchars($macro_area['descricao']); ?></textarea>
</div>
<div class="mb-3 form-check form-switch">
<input type="checkbox" class="form-check-input" id="macroAreaAtivo" name="ativo" value="1" <?php echo !empty($macro_area['ativo']) && $macro_area['ativo'] ? 'checked' : ''; ?>>
<label class="form-check-label" for="macroAreaAtivo">Ativo</label>
</div>
<div class="mt-4">
<a href="/Backend/macro_areas.php" class="btn btn-secondary">Cancelar</a>
<button type="submit" class="btn btn-primary">Salvar</button>
</div>
</form>
</div>
</div>
</div>
<?php include_once 'includes/footer.php'; ?>