Versão inicial em produção
This commit is contained in:
+167
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
$titulo_pagina = 'Perfis';
|
||||
$pagina_ativa = 'perfis';
|
||||
|
||||
require_once __DIR__ . '/includes/config.php';
|
||||
require_once __DIR__ . '/includes/db.php';
|
||||
require_once __DIR__ . '/includes/auth.php';
|
||||
|
||||
ao_exigir_acesso_total();
|
||||
|
||||
$erro = null;
|
||||
$sucesso = null;
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['acao'] ?? '') === 'excluir') {
|
||||
$id = (int) ($_POST['id'] ?? 0);
|
||||
$stmt = $pdo->prepare('SELECT COUNT(*) FROM usuarios WHERE perfil_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if ((int) $stmt->fetchColumn() > 0) {
|
||||
$erro = 'Não é possível excluir: existem usuários com este perfil.';
|
||||
} else {
|
||||
$pdo->prepare('DELETE FROM perfis WHERE id = ?')->execute([$id]);
|
||||
header('Location: perfis.php?excluido=1');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['acao'] ?? '') === 'salvar') {
|
||||
$id = (int) ($_POST['id'] ?? 0);
|
||||
$nome = trim($_POST['nome'] ?? '');
|
||||
$acesso_total = isset($_POST['acesso_total']) ? 1 : 0;
|
||||
$ativo = isset($_POST['ativo']) ? 1 : 0;
|
||||
$paginas_sel = array_map('intval', $_POST['paginas'] ?? []);
|
||||
|
||||
if ($nome === '') {
|
||||
$erro = 'Informe o nome do perfil.';
|
||||
} else {
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
if ($id > 0) {
|
||||
$stmt = $pdo->prepare('UPDATE perfis SET nome=?, acesso_total=?, ativo=? WHERE id=?');
|
||||
$stmt->execute([$nome, $acesso_total, $ativo, $id]);
|
||||
} else {
|
||||
$stmt = $pdo->prepare('INSERT INTO perfis (nome, acesso_total, ativo) VALUES (?,?,?)');
|
||||
$stmt->execute([$nome, $acesso_total, $ativo]);
|
||||
$id = (int) $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
$pdo->prepare('DELETE FROM perfil_paginas WHERE perfil_id = ?')->execute([$id]);
|
||||
if (!$acesso_total && $paginas_sel) {
|
||||
$stmtIns = $pdo->prepare('INSERT INTO perfil_paginas (perfil_id, pagina_id) VALUES (?, ?)');
|
||||
foreach ($paginas_sel as $pid) {
|
||||
$stmtIns->execute([$id, $pid]);
|
||||
}
|
||||
}
|
||||
$pdo->commit();
|
||||
header('Location: perfis.php?salvo=1');
|
||||
exit;
|
||||
} catch (PDOException $e) {
|
||||
$pdo->rollBack();
|
||||
$erro = ($e->getCode() === '23000') ? 'Já existe um perfil com este nome.' : ('Erro ao salvar: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$editando = null;
|
||||
$paginas_do_perfil = [];
|
||||
if (isset($_GET['editar'])) {
|
||||
$stmt = $pdo->prepare('SELECT * FROM perfis WHERE id = ?');
|
||||
$stmt->execute([(int) $_GET['editar']]);
|
||||
$editando = $stmt->fetch() ?: null;
|
||||
if ($editando) {
|
||||
$stmt2 = $pdo->prepare('SELECT pagina_id FROM perfil_paginas WHERE perfil_id = ?');
|
||||
$stmt2->execute([$editando['id']]);
|
||||
$paginas_do_perfil = array_map('intval', array_column($stmt2->fetchAll(), 'pagina_id'));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_GET['salvo'])) $sucesso = 'Perfil salvo com sucesso.';
|
||||
if (isset($_GET['excluido'])) $sucesso = 'Perfil excluído.';
|
||||
|
||||
$todas_paginas = $pdo->query('SELECT id, chave, nome FROM paginas WHERE ativo = 1 ORDER BY ordem, nome')->fetchAll();
|
||||
$perfis = $pdo->query(
|
||||
'SELECT pf.*, (SELECT COUNT(*) FROM usuarios u WHERE u.perfil_id = pf.id) AS total_usuarios FROM perfis pf ORDER BY pf.nome'
|
||||
)->fetchAll();
|
||||
|
||||
require __DIR__ . '/includes/header.php';
|
||||
?>
|
||||
|
||||
<h1 class="ao-page-title">Perfis de acesso</h1>
|
||||
|
||||
<?php if ($erro): ?>
|
||||
<div class="ao-badge ao-badge-baixa" style="display:block; margin-bottom: var(--ao-space-4); padding: 10px 14px; font-size: var(--ao-fs-sm);"><?= htmlspecialchars($erro) ?></div>
|
||||
<?php endif; ?>
|
||||
<?php if ($sucesso): ?>
|
||||
<div class="ao-badge ao-badge-alta" style="display:block; margin-bottom: var(--ao-space-4); padding: 10px 14px; font-size: var(--ao-fs-sm);"><?= htmlspecialchars($sucesso) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="ao-card" style="max-width: 560px; margin-bottom: var(--ao-space-6);">
|
||||
<div class="ao-card-header"><strong><?= $editando ? 'Editar perfil' : 'Novo perfil' ?></strong></div>
|
||||
<form method="post" action="perfis.php">
|
||||
<input type="hidden" name="acao" value="salvar">
|
||||
<input type="hidden" name="id" value="<?= (int) ($editando['id'] ?? 0) ?>">
|
||||
<div class="ao-field" style="margin-bottom: var(--ao-space-4);">
|
||||
<label for="nome">Nome do perfil</label>
|
||||
<input type="text" id="nome" name="nome" required value="<?= htmlspecialchars($editando['nome'] ?? '') ?>">
|
||||
</div>
|
||||
|
||||
<label style="display:flex; align-items:center; gap:6px; font-size: var(--ao-fs-sm); margin-bottom: var(--ao-space-4);">
|
||||
<input type="checkbox" id="acesso_total" name="acesso_total" <?= (!empty($editando['acesso_total'])) ? 'checked' : '' ?>
|
||||
onchange="document.getElementById('bloco-paginas').style.display = this.checked ? 'none' : 'block';">
|
||||
Acesso total (administrador — libera todas as páginas automaticamente)
|
||||
</label>
|
||||
|
||||
<div id="bloco-paginas" style="display: <?= (!empty($editando['acesso_total'])) ? 'none' : 'block' ?>; margin-bottom: var(--ao-space-4); padding: var(--ao-space-3); background: var(--ao-bg-page); border-radius: 8px;">
|
||||
<label style="display:block; font-size: var(--ao-fs-xs); font-weight:600; color: var(--ao-text-muted); margin-bottom: 8px;">Páginas liberadas para este perfil</label>
|
||||
<?php if (!$todas_paginas): ?>
|
||||
<p style="font-size: var(--ao-fs-sm); color: var(--ao-text-muted); margin:0;">Nenhuma página cadastrada ainda — cadastre em <a href="paginas_admin.php">Páginas</a>.</p>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($todas_paginas as $p): ?>
|
||||
<label style="display:flex; align-items:center; gap:6px; font-size: var(--ao-fs-sm); padding: 4px 0;">
|
||||
<input type="checkbox" name="paginas[]" value="<?= $p['id'] ?>" <?= in_array((int) $p['id'], $paginas_do_perfil, true) ? 'checked' : '' ?>>
|
||||
<?= htmlspecialchars($p['nome']) ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<label style="display:flex; align-items:center; gap:6px; font-size: var(--ao-fs-sm); margin-bottom: var(--ao-space-5);">
|
||||
<input type="checkbox" name="ativo" <?= (!$editando || $editando['ativo']) ? 'checked' : '' ?>> Ativo
|
||||
</label>
|
||||
|
||||
<button type="submit" class="ao-btn ao-btn-primary"><?= $editando ? 'Salvar alterações' : 'Cadastrar perfil' ?></button>
|
||||
<?php if ($editando): ?>
|
||||
<a href="perfis.php" class="ao-btn ao-btn-secondary">Cancelar</a>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="ao-card">
|
||||
<div class="ao-card-header"><strong>Perfis cadastrados</strong></div>
|
||||
<table class="ao-table">
|
||||
<thead>
|
||||
<tr><th>Nome</th><th>Acesso</th><th>Usuários</th><th>Status</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($perfis as $p): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($p['nome']) ?></td>
|
||||
<td><?= $p['acesso_total'] ? '<span class="ao-badge ao-badge-alta">Total</span>' : '<span class="ao-badge ao-badge-neutro">Configurado</span>' ?></td>
|
||||
<td class="ao-numeric"><?= (int) $p['total_usuarios'] ?></td>
|
||||
<td><span class="ao-badge <?= $p['ativo'] ? 'ao-badge-alta' : 'ao-badge-neutro' ?>"><?= $p['ativo'] ? 'Ativo' : 'Inativo' ?></span></td>
|
||||
<td style="white-space: nowrap; text-align: right;">
|
||||
<a href="perfis.php?editar=<?= $p['id'] ?>" class="ao-btn ao-btn-secondary" style="padding: 4px 10px; font-size: var(--ao-fs-xs);">Editar</a>
|
||||
<?php if ((int) $p['total_usuarios'] === 0): ?>
|
||||
<form method="post" action="perfis.php" style="display:inline;" onsubmit="return confirm('Excluir este perfil?');">
|
||||
<input type="hidden" name="acao" value="excluir">
|
||||
<input type="hidden" name="id" value="<?= $p['id'] ?>">
|
||||
<button type="submit" class="ao-btn ao-btn-secondary" style="padding: 4px 10px; font-size: var(--ao-fs-xs);">Excluir</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<?php require __DIR__ . '/includes/footer.php'; ?>
|
||||
Reference in New Issue
Block a user