versao 10
This commit is contained in:
parent
3b9a4503ac
commit
5f577e0d7f
145
macro_areas.php
145
macro_areas.php
@ -5,20 +5,14 @@ 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;
|
||||
|
||||
@ -30,26 +24,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$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]);
|
||||
$_SESSION['show_new_modal'] = true;
|
||||
}
|
||||
// Redirect to avoid form resubmission
|
||||
header("Location: macro_areas.php");
|
||||
exit;
|
||||
}
|
||||
@ -59,31 +49,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// 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="icon text-white-50"><i data-lucide="file-text" style="color: #FFFFFF;"></i></span>
|
||||
<span class="text">Imprimir Lista</span>
|
||||
</button>
|
||||
<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>
|
||||
</button>
|
||||
</div>
|
||||
@ -97,8 +83,14 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="card shadow mb-4">
|
||||
<div class="card-header py-3">
|
||||
<h6 class="m-0 font-weight-bold">Registros</h6>
|
||||
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
|
||||
<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 class="card-body">
|
||||
<div class="table-responsive">
|
||||
@ -111,7 +103,7 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
<th style="width: 100px;">Ações</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tbody id="tableBody">
|
||||
<?php foreach ($macro_areas as $area):
|
||||
$badgeClass = $area['ativo'] ? 'badge-status-ativo' : 'badge-status-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>
|
||||
</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-nome="<?php echo htmlspecialchars($area['nome']); ?>"
|
||||
data-descricao="<?php echo htmlspecialchars($area['descricao']); ?>"
|
||||
data-ativo="<?php echo $area['ativo']; ?>"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#macroAreaModal">
|
||||
<i class="fas fa-edit"></i>
|
||||
<i data-lucide="edit" style="color: #4C5958;"></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 href="macro_areas.php?delete=<?php echo $area['id']; ?>" class="btn btn-sm" onclick="return confirm('Tem certeza que deseja excluir este item?');">
|
||||
<i data-lucide="trash-2" style="color: #4C5958;"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
@ -187,22 +179,37 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var macroAreaModal = document.getElementById('macroAreaModal');
|
||||
macroAreaModal.addEventListener('show.bs.modal', function (event) {
|
||||
var macroAreaModalEl = document.getElementById('macroAreaModal');
|
||||
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 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
|
||||
modal.querySelector('#macroAreaAtivo').checked = true;
|
||||
|
||||
if (id) {
|
||||
// If editing, populate form
|
||||
var nome = button.getAttribute('data-nome');
|
||||
var descricao = button.getAttribute('data-descricao');
|
||||
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): ?>
|
||||
var modal = new bootstrap.Modal(document.getElementById('macroAreaModal'));
|
||||
modal.show();
|
||||
// Pre-fill form with submitted data
|
||||
macroAreaModal.show();
|
||||
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'] ?? '') ?>';
|
||||
@ -228,18 +232,69 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
document.querySelector('#macroAreaModal .modal-title').textContent = 'Editar Macro Área';
|
||||
<?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>
|
||||
|
||||
<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;
|
||||
.input-group-text {
|
||||
background-color: #fff;
|
||||
border-right: 0;
|
||||
}
|
||||
</style>
|
||||
#searchInput {
|
||||
border-left: 0;
|
||||
}
|
||||
.btn-sm i[data-lucide] {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
</style>
|
||||
Loading…
x
Reference in New Issue
Block a user