versao 10

This commit is contained in:
Flatlogic Bot 2025-10-29 16:09:14 +00:00
parent 3b9a4503ac
commit 5f577e0d7f

View File

@ -5,20 +5,14 @@ include_once 'includes/header.php';
// Helper function to generate a slug from a string // Helper function to generate a slug from a string
function generateSlug($string) { function generateSlug($string) {
// Normalize to ASCII
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string); $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
// Convert to lowercase
$string = strtolower($string); $string = strtolower($string);
// Remove characters that are not letters, numbers, or hyphens
$string = preg_replace('/[^a-z0-9_\-]+/', '-', $string); $string = preg_replace('/[^a-z0-9_\-]+/', '-', $string);
// Remove duplicate hyphens
$string = preg_replace('/-+/', '-', $string); $string = preg_replace('/-+/', '-', $string);
// Trim hyphens from the beginning and end
$string = trim($string, '-'); $string = trim($string, '-');
return $string; return $string;
} }
$pdo = db(); $pdo = db();
$error = null; $error = null;
@ -30,26 +24,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$ativo = isset($_POST['ativo']) ? 1 : 0; $ativo = isset($_POST['ativo']) ? 1 : 0;
$slug = generateSlug($nome); $slug = generateSlug($nome);
// Basic validation
if (empty($nome)) { if (empty($nome)) {
$error = "O campo Nome é obrigatório."; $error = "O campo Nome é obrigatório.";
} else { } else {
// Check for duplicates
$stmt = $pdo->prepare('SELECT id FROM macro_areas WHERE (nome = ? OR slug = ?) AND id <> ?'); $stmt = $pdo->prepare('SELECT id FROM macro_areas WHERE (nome = ? OR slug = ?) AND id <> ?');
$stmt->execute([$nome, $slug, $id ?: 0]); $stmt->execute([$nome, $slug, $id ?: 0]);
if ($stmt->fetch()) { if ($stmt->fetch()) {
$error = "Já existe uma Macro Área com este nome."; $error = "Já existe uma Macro Área com este nome.";
} else { } else {
if ($id) { if ($id) {
// Update
$stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, slug = ?, descricao = ?, ativo = ? WHERE id = ?'); $stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, slug = ?, descricao = ?, ativo = ? WHERE id = ?');
$stmt->execute([$nome, $slug, $descricao, $ativo, $id]); $stmt->execute([$nome, $slug, $descricao, $ativo, $id]);
} else { } else {
// Create
$stmt = $pdo->prepare('INSERT INTO macro_areas (nome, slug, descricao, ativo, user_id) VALUES (?, ?, ?, ?, ?)'); $stmt = $pdo->prepare('INSERT INTO macro_areas (nome, slug, descricao, ativo, user_id) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([$nome, $slug, $descricao, $ativo, $_SESSION['user_id'] ?? 1]); $stmt->execute([$nome, $slug, $descricao, $ativo, $_SESSION['user_id'] ?? 1]);
$_SESSION['show_new_modal'] = true;
} }
// Redirect to avoid form resubmission
header("Location: macro_areas.php"); header("Location: macro_areas.php");
exit; exit;
} }
@ -59,31 +49,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Handle deletion // Handle deletion
if (isset($_GET['delete'])) { if (isset($_GET['delete'])) {
$id = $_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 = $pdo->prepare('DELETE FROM macro_areas WHERE id = ?');
$stmt->execute([$id]); $stmt->execute([$id]);
header("Location: macro_areas.php"); header("Location: macro_areas.php");
exit; exit;
} }
// Fetch macro areas from the database
$stmt = $pdo->query('SELECT * FROM macro_areas ORDER BY nome ASC'); $stmt = $pdo->query('SELECT * FROM macro_areas ORDER BY nome ASC');
$macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC); $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
?> ?>
<div class="container-fluid"> <div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4"> <div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0">Macro Áreas</h1> <h1 class="h3 mb-0">Macro Áreas</h1>
<div> <div>
<button class="btn btn-secondary btn-icon-split btn-sm"> <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="icon text-white-50"><i data-lucide="file-text" style="color: #FFFFFF;"></i></span>
<span class="text">Imprimir Lista</span> <span class="text">Imprimir Lista</span>
</button> </button>
<button class="btn btn-primary btn-icon-split" data-bs-toggle="modal" data-bs-target="#macroAreaModal"> <button class="btn btn-primary btn-icon-split" data-bs-toggle="modal" data-bs-target="#macroAreaModal">
<span class="icon text-white-50"><i class="fas fa-plus"></i></span> <span class="icon text-white-50"><i data-lucide="plus" style="color: #FFFFFF;"></i></span>
<span class="text">Nova Macro Área</span> <span class="text">Nova Macro Área</span>
</button> </button>
</div> </div>
@ -97,8 +83,14 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<?php endif; ?> <?php endif; ?>
<div class="card shadow mb-4"> <div class="card shadow mb-4">
<div class="card-header py-3"> <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold">Registros</h6> <h6 class="m-0 font-weight-bold text-primary">
<i data-lucide="layers" class="me-2" style="color: #005C53;"></i>Lista de Macro Áreas
</h6>
<div class="input-group" style="width: 250px;">
<span class="input-group-text text-slate-400"><i data-lucide="search"></i></span>
<input type="text" class="form-control" id="searchInput" placeholder="Buscar...">
</div>
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="table-responsive"> <div class="table-responsive">
@ -111,7 +103,7 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<th style="width: 100px;">Ações</th> <th style="width: 100px;">Ações</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="tableBody">
<?php foreach ($macro_areas as $area): <?php foreach ($macro_areas as $area):
$badgeClass = $area['ativo'] ? 'badge-status-ativo' : 'badge-status-arquivado'; $badgeClass = $area['ativo'] ? 'badge-status-ativo' : 'badge-status-arquivado';
$statusText = $area['ativo'] ? 'Ativo' : 'Arquivado'; $statusText = $area['ativo'] ? 'Ativo' : 'Arquivado';
@ -123,17 +115,17 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<span class="badge <?php echo $badgeClass; ?>"><?php echo $statusText; ?></span> <span class="badge <?php echo $badgeClass; ?>"><?php echo $statusText; ?></span>
</td> </td>
<td> <td>
<button class="btn btn-info btn-circle btn-sm edit-btn" <button class="btn btn-sm edit-btn"
data-id="<?php echo $area['id']; ?>" data-id="<?php echo $area['id']; ?>"
data-nome="<?php echo htmlspecialchars($area['nome']); ?>" data-nome="<?php echo htmlspecialchars($area['nome']); ?>"
data-descricao="<?php echo htmlspecialchars($area['descricao']); ?>" data-descricao="<?php echo htmlspecialchars($area['descricao']); ?>"
data-ativo="<?php echo $area['ativo']; ?>" data-ativo="<?php echo $area['ativo']; ?>"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#macroAreaModal"> data-bs-target="#macroAreaModal">
<i class="fas fa-edit"></i> <i data-lucide="edit" style="color: #4C5958;"></i>
</button> </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?');"> <a href="macro_areas.php?delete=<?php echo $area['id']; ?>" class="btn btn-sm" onclick="return confirm('Tem certeza que deseja excluir este item?');">
<i class="fas fa-trash"></i> <i data-lucide="trash-2" style="color: #4C5958;"></i>
</a> </a>
</td> </td>
</tr> </tr>
@ -187,22 +179,37 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<script> <script>
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
var macroAreaModal = document.getElementById('macroAreaModal'); var macroAreaModalEl = document.getElementById('macroAreaModal');
macroAreaModal.addEventListener('show.bs.modal', function (event) { var macroAreaModal = new bootstrap.Modal(macroAreaModalEl);
function showModalAlert(message, type = 'success') {
var modalBody = macroAreaModalEl.querySelector('.modal-body');
var existingAlert = modalBody.querySelector('.alert');
if (existingAlert) {
existingAlert.remove();
}
var alert = document.createElement('div');
alert.className = 'alert alert-' + type;
alert.textContent = message;
modalBody.insertBefore(alert, modalBody.firstChild);
setTimeout(function() {
if (alert) {
alert.remove();
}
}, 5000);
}
macroAreaModalEl.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget; var button = event.relatedTarget;
var modal = this; 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; var id = button ? button.getAttribute('data-id') : null;
// Reset form for new entry
modal.querySelector('.modal-title').textContent = 'Nova Macro Área'; modal.querySelector('.modal-title').textContent = 'Nova Macro Área';
modal.querySelector('#macroAreaForm').reset(); modal.querySelector('#macroAreaForm').reset();
modal.querySelector('#macroAreaId').value = ''; modal.querySelector('#macroAreaId').value = '';
modal.querySelector('#macroAreaAtivo').checked = true; // Default to active modal.querySelector('#macroAreaAtivo').checked = true;
if (id) { if (id) {
// If editing, populate form
var nome = button.getAttribute('data-nome'); var nome = button.getAttribute('data-nome');
var descricao = button.getAttribute('data-descricao'); var descricao = button.getAttribute('data-descricao');
var ativo = button.getAttribute('data-ativo'); var ativo = button.getAttribute('data-ativo');
@ -215,11 +222,8 @@ document.addEventListener('DOMContentLoaded', function () {
} }
}); });
// If there was a server-side error, show the modal again and pre-fill the form
<?php if ($error): ?> <?php if ($error): ?>
var modal = new bootstrap.Modal(document.getElementById('macroAreaModal')); macroAreaModal.show();
modal.show();
// Pre-fill form with submitted data
document.getElementById('macroAreaId').value = '<?php echo $_POST['id'] ?? '' ?>'; document.getElementById('macroAreaId').value = '<?php echo $_POST['id'] ?? '' ?>';
document.getElementById('macroAreaNome').value = '<?php echo htmlspecialchars($_POST['nome'] ?? '') ?>'; document.getElementById('macroAreaNome').value = '<?php echo htmlspecialchars($_POST['nome'] ?? '') ?>';
document.getElementById('macroAreaDescricao').value = '<?php echo htmlspecialchars($_POST['descricao'] ?? '') ?>'; document.getElementById('macroAreaDescricao').value = '<?php echo htmlspecialchars($_POST['descricao'] ?? '') ?>';
@ -228,18 +232,69 @@ document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#macroAreaModal .modal-title').textContent = 'Editar Macro Área'; document.querySelector('#macroAreaModal .modal-title').textContent = 'Editar Macro Área';
<?php endif; ?> <?php endif; ?>
<?php endif; ?> <?php endif; ?>
<?php
if (isset($_SESSION['show_new_modal']) && $_SESSION['show_new_modal']) {
unset($_SESSION['show_new_modal']);
echo "var showNewModalAfterSave = true;\n";
} else {
echo "var showNewModalAfterSave = false;\n";
}
?>
if (showNewModalAfterSave) {
var modalTitle = macroAreaModalEl.querySelector('.modal-title');
var form = macroAreaModalEl.querySelector('#macroAreaForm');
modalTitle.textContent = 'Nova Macro Área';
form.reset();
document.getElementById('macroAreaId').value = '';
document.getElementById('macroAreaAtivo').checked = true;
macroAreaModal.show();
showModalAlert('Salvo com sucesso! Adicione um novo registro.', 'success');
}
// Search functionality
const searchInput = document.getElementById('searchInput');
const tableBody = document.getElementById('tableBody');
const tableRows = tableBody.getElementsByTagName('tr');
searchInput.addEventListener('keyup', function() {
const searchTerm = searchInput.value.toLowerCase();
for (let i = 0; i < tableRows.length; i++) {
const row = tableRows[i];
const cells = row.getElementsByTagName('td');
let match = false;
for (let j = 0; j < cells.length; j++) {
if (cells[j]) {
if (cells[j].textContent.toLowerCase().indexOf(searchTerm) > -1) {
match = true;
break;
}
}
}
if (match) {
row.style.display = '';
} else {
row.style.display = 'none';
}
}
});
}); });
</script> </script>
<style> <style>
/* Custom styles for action buttons to ensure they are circular */ .input-group-text {
.btn-circle { background-color: #fff;
width: 30px; border-right: 0;
height: 30px; }
padding: 6px 0; #searchInput {
border-radius: 15px; border-left: 0;
text-align: center; }
font-size: 12px; .btn-sm i[data-lucide] {
line-height: 1.42857; width: 16px;
height: 16px;
} }
</style> </style>