versao 11

This commit is contained in:
Flatlogic Bot 2025-10-29 16:16:48 +00:00
parent 5f577e0d7f
commit e84e983545
2 changed files with 132 additions and 169 deletions

112
macro_area_form.php Normal file
View File

@ -0,0 +1,112 @@
<?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]);
} 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]);
}
header("Location: macro_areas.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 text-primary">Detalhes da Macro Área</h6>
</div>
<div class="card-body">
<form id="macroAreaForm" method="POST" action="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="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'; ?>

View File

@ -3,52 +3,12 @@ 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) {
$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;
// 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.";
} 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.";
} else {
if ($id) {
$stmt = $pdo->prepare('UPDATE macro_areas SET nome = ?, slug = ?, descricao = ?, ativo = ? WHERE id = ?');
$stmt->execute([$nome, $slug, $descricao, $ativo, $id]);
} else {
$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;
}
header("Location: macro_areas.php");
exit;
}
}
}
// Handle deletion
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
// TODO: Add check to see if there are expenses associated before deleting
$stmt = $pdo->prepare('DELETE FROM macro_areas WHERE id = ?');
$stmt->execute([$id]);
header("Location: macro_areas.php");
@ -68,20 +28,13 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<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">
<a href="macro_area_form.php" class="btn btn-primary btn-icon-split">
<span class="icon text-white-50"><i data-lucide="plus" style="color: #FFFFFF;"></i></span>
<span class="text">Nova Macro Área</span>
</button>
</a>
</div>
</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 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">
@ -115,15 +68,9 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
<span class="badge <?php echo $badgeClass; ?>"><?php echo $statusText; ?></span>
</td>
<td>
<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">
<a href="macro_area_form.php?id=<?php echo $area['id']; ?>" class="btn btn-sm">
<i data-lucide="edit" style="color: #4C5958;"></i>
</button>
</a>
<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>
@ -142,119 +89,10 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="macroAreaModal" tabindex="-1" aria-labelledby="macroAreaModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="macroAreaModalLabel">Nova Macro Área</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form id="macroAreaForm" method="POST" action="macro_areas.php">
<div class="modal-body">
<input type="hidden" name="id" id="macroAreaId">
<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" 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"></textarea>
</div>
<div class="mb-3 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-bs-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 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;
var id = button ? button.getAttribute('data-id') : null;
modal.querySelector('.modal-title').textContent = 'Nova Macro Área';
modal.querySelector('#macroAreaForm').reset();
modal.querySelector('#macroAreaId').value = '';
modal.querySelector('#macroAreaAtivo').checked = true;
if (id) {
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;
}
});
<?php if ($error): ?>
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'] ?? '') ?>';
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; ?>
<?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');
@ -267,7 +105,8 @@ document.addEventListener('DOMContentLoaded', function () {
const row = tableRows[i];
const cells = row.getElementsByTagName('td');
let match = false;
for (let j = 0; j < cells.length; j++) {
// Start search from the first cell, and not the last one (actions)
for (let j = 0; j < cells.length - 1; j++) {
if (cells[j]) {
if (cells[j].textContent.toLowerCase().indexOf(searchTerm) > -1) {
match = true;
@ -297,4 +136,16 @@ document.addEventListener('DOMContentLoaded', function () {
width: 16px;
height: 16px;
}
/* Custom styles for status badges */
.badge-status-ativo {
background-color: #d1e7dd; /* Light green */
color: #0f5132; /* Dark green */
border: 1px solid #badbcc;
}
.badge-status-arquivado {
background-color: #f8d7da; /* Light red */
color: #842029; /* Dark red */
border: 1px solid #f5c2c7;
}
</style>