200 lines
8.4 KiB
PHP
200 lines
8.4 KiB
PHP
<?php
|
|
require_once 'includes/session.php';
|
|
require_once 'db/config.php';
|
|
include_once 'includes/header.php';
|
|
|
|
// Helper function to generate a slug
|
|
function slugify($text) {
|
|
$text = preg_replace('~[\\pL\\d]+~u', '-', $text); // Added 'u' modifier for UTF-8
|
|
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
|
|
$text = preg_replace('~[^\\w]+~', '-', $text);
|
|
$text = trim($text, '-');
|
|
$text = preg_replace('~-~', '-', $text); // Simplified to just '-' as it's already trimmed
|
|
$text = strtolower($text);
|
|
return $text ?: 'n-a';
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle form submission (Create/Update)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'] ?? null;
|
|
$nome = $_POST['nome'] ?? '';
|
|
$descricao = $_POST['descricao'] ?? '';
|
|
$ativo = isset($_POST['ativo']) ? 1 : 0;
|
|
|
|
if ($nome) { // Basic validation
|
|
if ($id) {
|
|
// Update
|
|
$stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, descricao = ?, ativo = ? WHERE id = ?');
|
|
$stmt->execute([$nome, $descricao, $ativo, $id]);
|
|
} else {
|
|
// Create
|
|
$slug = slugify($nome);
|
|
$stmt = $pdo->prepare('INSERT INTO macro_areas (nome, descricao, slug, ativo, created_by) VALUES (?, ?, ?, ?, ?)');
|
|
$stmt->execute([$nome, $descricao, $slug, $ativo, $_SESSION['user_email'] ?? 'system']);
|
|
}
|
|
}
|
|
// Redirect to avoid form resubmission
|
|
header("Location: macro_areas.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle deletion
|
|
if (isset($_GET['delete'])) {
|
|
$id = $_GET['delete'];
|
|
$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>
|
|
|
|
<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):
|
|
// Added check for 'ativo' to determine badge class
|
|
$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']; ?>">
|
|
<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)):
|
|
// Added colspan to match the number of columns
|
|
?>
|
|
<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" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="macroAreaDescricao">Descrição</label>
|
|
<textarea class="form-control" id="macroAreaDescricao" name="descricao" rows="3"></textarea>
|
|
</div>
|
|
<div class="form-group form-check">
|
|
<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 () {
|
|
// Handle modal for "Nova Macro Área"
|
|
$('#macroAreaModal').on('show.bs.modal', function (event) {
|
|
var button = $(event.relatedTarget); // Button that triggered the modal
|
|
var modal = $(this);
|
|
|
|
// If button is the "edit" button, it will have data attributes
|
|
var id = button.data('id');
|
|
if (id) {
|
|
// Editing an existing record
|
|
var nome = button.data('nome');
|
|
var descricao = button.data('descricao');
|
|
var ativo = button.data('ativo');
|
|
|
|
modal.find('.modal-title').text('Editar Macro Área');
|
|
modal.find('#macroAreaId').val(id);
|
|
modal.find('#macroAreaNome').val(nome);
|
|
modal.find('#macroAreaDescricao').val(descricao);
|
|
if (ativo == 1) {
|
|
modal.find('#macroAreaAtivo').prop('checked', true);
|
|
} else {
|
|
modal.find('#macroAreaAtivo').prop('checked', false);
|
|
}
|
|
} else {
|
|
// Creating a new record
|
|
modal.find('.modal-title').text('Nova Macro Área');
|
|
modal.find('#macroAreaForm')[0].reset();
|
|
modal.find('#macroAreaId').val('');
|
|
modal.find('#macroAreaAtivo').prop('checked', true);
|
|
}
|
|
});
|
|
});
|
|
</script>
|