250 lines
11 KiB
PHP
250 lines
11 KiB
PHP
<?php
|
|
require_once 'includes/session.php';
|
|
require_once 'db/config.php';
|
|
include_once 'includes/header.php';
|
|
|
|
// Helper function to generate a slug from a string
|
|
function generateSlug($string) {
|
|
// Normalize to ASCII
|
|
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
|
|
// Convert to lowercase
|
|
$string = strtolower($string);
|
|
// Remove characters that are not letters, numbers, or hyphens
|
|
$string = preg_replace('/[^a-z0-9_\-]+/', '-', $string);
|
|
// Remove duplicate hyphens
|
|
$string = preg_replace('/-+/', '-', $string);
|
|
// Trim hyphens from the beginning and end
|
|
$string = trim($string, '-');
|
|
return $string;
|
|
}
|
|
|
|
|
|
$pdo = db();
|
|
$error = null;
|
|
|
|
// 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);
|
|
|
|
// Basic validation
|
|
if (empty($nome)) {
|
|
$error = "O campo Nome é obrigatório.";
|
|
} else {
|
|
// Check for duplicates
|
|
$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.";
|
|
} else {
|
|
if ($id) {
|
|
// Update
|
|
$stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, slug = ?, descricao = ?, ativo = ? WHERE id = ?');
|
|
$stmt->execute([$nome, $slug, $descricao, $ativo, $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 to avoid form resubmission
|
|
header("Location: macro_areas.php");
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle deletion
|
|
if (isset($_GET['delete'])) {
|
|
$id = $_GET['delete'];
|
|
// It's a good practice to check for dependencies before deleting
|
|
$stmt = $pdo->prepare('DELETE FROM macro_areas WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
header("Location: macro_areas.php");
|
|
exit;
|
|
}
|
|
|
|
|
|
// Fetch macro areas from the database
|
|
$stmt = $pdo->query('SELECT * FROM macro_areas ORDER BY nome ASC');
|
|
$macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<!-- Page Heading -->
|
|
<div class="d-sm-flex align-items-center justify-content-between mb-4">
|
|
<h1 class="h3 mb-0">Macro Áreas</h1>
|
|
<div>
|
|
<button class="btn btn-secondary btn-icon-split btn-sm">
|
|
<span class="icon text-white-50"><i class="fas fa-print"></i></span>
|
|
<span class="text">Imprimir Lista</span>
|
|
</button>
|
|
<button class="btn btn-primary btn-icon-split" data-toggle="modal" data-target="#macroAreaModal">
|
|
<span class="icon text-white-50"><i class="fas fa-plus"></i></span>
|
|
<span class="text">Nova Macro Área</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
|
<?php echo $error; ?>
|
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card shadow mb-4">
|
|
<div class="card-header py-3">
|
|
<h6 class="m-0 font-weight-bold">Registros</h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
|
|
<thead>
|
|
<tr>
|
|
<th>Nome</th>
|
|
<th>Descrição</th>
|
|
<th>Status</th>
|
|
<th style="width: 100px;">Ações</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($macro_areas as $area):
|
|
$badgeClass = $area['ativo'] ? 'badge-status-ativo' : 'badge-status-arquivado';
|
|
$statusText = $area['ativo'] ? 'Ativo' : 'Arquivado';
|
|
?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($area['nome']); ?></td>
|
|
<td><?php echo htmlspecialchars($area['descricao']); ?></td>
|
|
<td>
|
|
<span class="badge <?php echo $badgeClass; ?>"><?php echo $statusText; ?></span>
|
|
</td>
|
|
<td>
|
|
<button class="btn btn-info btn-circle btn-sm edit-btn"
|
|
data-id="<?php echo $area['id']; ?>"
|
|
data-nome="<?php echo htmlspecialchars($area['nome']); ?>"
|
|
data-descricao="<?php echo htmlspecialchars($area['descricao']); ?>"
|
|
data-ativo="<?php echo $area['ativo']; ?>"
|
|
data-toggle="modal"
|
|
data-target="#macroAreaModal">
|
|
<i class="fas fa-edit"></i>
|
|
</button>
|
|
<a href="macro_areas.php?delete=<?php echo $area['id']; ?>" class="btn btn-danger btn-circle btn-sm" onclick="return confirm('Tem certeza que deseja excluir este item?');">
|
|
<i class="fas fa-trash"></i>
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($macro_areas)): ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center">Nenhuma macro área encontrada.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="macroAreaModal" tabindex="-1" role="dialog" aria-labelledby="macroAreaModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog" role="document">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="macroAreaModalLabel">Nova Macro Área</h5>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<form id="macroAreaForm" method="POST" action="macro_areas.php">
|
|
<div class="modal-body">
|
|
<input type="hidden" name="id" id="macroAreaId">
|
|
<div class="form-group">
|
|
<label for="macroAreaNome">Nome *</label>
|
|
<input type="text" class="form-control" id="macroAreaNome" name="nome" placeholder="Ex: Moradia, Alimentação, Saúde" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="macroAreaDescricao">Descrição</label>
|
|
<textarea class="form-control" id="macroAreaDescricao" name="descricao" rows="3" placeholder="Descrição opcional da macro área"></textarea>
|
|
</div>
|
|
<div class="form-group form-check form-switch">
|
|
<input type="checkbox" class="form-check-input" id="macroAreaAtivo" name="ativo" value="1" checked>
|
|
<label class="form-check-label" for="macroAreaAtivo">Ativo</label>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
|
|
<button type="submit" class="btn btn-primary">Salvar</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include_once 'includes/footer.php'; ?>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var macroAreaModal = document.getElementById('macroAreaModal');
|
|
macroAreaModal.addEventListener('show.bs.modal', function (event) {
|
|
var button = event.relatedTarget;
|
|
var modal = this;
|
|
|
|
// The button that triggered the modal may not exist, e.g. if the modal is shown due to a server-side error.
|
|
var id = button ? button.getAttribute('data-id') : null;
|
|
|
|
// Reset form for new entry
|
|
modal.querySelector('.modal-title').textContent = 'Nova Macro Área';
|
|
modal.querySelector('#macroAreaForm').reset();
|
|
modal.querySelector('#macroAreaId').value = '';
|
|
modal.querySelector('#macroAreaAtivo').checked = true; // Default to active
|
|
|
|
if (id) {
|
|
// If editing, populate form
|
|
var nome = button.getAttribute('data-nome');
|
|
var descricao = button.getAttribute('data-descricao');
|
|
var ativo = button.getAttribute('data-ativo');
|
|
|
|
modal.querySelector('.modal-title').textContent = 'Editar Macro Área';
|
|
modal.querySelector('#macroAreaId').value = id;
|
|
modal.querySelector('#macroAreaNome').value = nome;
|
|
modal.querySelector('#macroAreaDescricao').value = descricao;
|
|
modal.querySelector('#macroAreaAtivo').checked = ativo == 1;
|
|
}
|
|
});
|
|
|
|
// If there was a server-side error, show the modal again and pre-fill the form
|
|
<?php if ($error): ?>
|
|
var modal = new bootstrap.Modal(document.getElementById('macroAreaModal'));
|
|
modal.show();
|
|
// Pre-fill form with submitted data
|
|
document.getElementById('macroAreaId').value = '<?php echo $_POST['id'] ?? '' ?>';
|
|
document.getElementById('macroAreaNome').value = '<?php echo htmlspecialchars($_POST['nome'] ?? '') ?>';
|
|
document.getElementById('macroAreaDescricao').value = '<?php echo htmlspecialchars($_POST['descricao'] ?? '') ?>';
|
|
document.getElementById('macroAreaAtivo').checked = <?php echo isset($_POST['ativo']) ? 'true' : 'false' ?>;
|
|
<?php if (!empty($_POST['id'])): ?>
|
|
document.querySelector('#macroAreaModal .modal-title').textContent = 'Editar Macro Área';
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
});
|
|
</script>
|
|
|
|
<style>
|
|
/* Custom styles for action buttons to ensure they are circular */
|
|
.btn-circle {
|
|
width: 30px;
|
|
height: 30px;
|
|
padding: 6px 0;
|
|
border-radius: 15px;
|
|
text-align: center;
|
|
font-size: 12px;
|
|
line-height: 1.42857;
|
|
}
|
|
</style>
|