home
/
u529748449
/
domains
/
borabilhete.com
/
public_html
/
admin
➕ New
📤 Upload
✎ Editing:
salvar_edicao_evento.php
← Back
<?php // admin/salvar_edicao_evento.php require_once __DIR__ . '/conexao.php'; require_once __DIR__ . '/auth.php'; error_reporting(E_ALL); ini_set('display_errors', 1); date_default_timezone_set('America/Sao_Paulo'); // ==== Permissão: somente admin e produtor ==== require_login(['admin','produtor']); if (($_SESSION['role'] ?? '') === 'agente') { header('Location: validar_ingresso.php'); exit; } $isAdmin = (($_SESSION['role'] ?? '') === 'admin'); $produtorId = (int)($_SESSION['produtor_id'] ?? 0); // ==== Helpers ==== function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function gerarSlug($nome) { $nome = trim($nome); $nome = mb_strtolower($nome, 'UTF-8'); $nome = strtr($nome, [ 'á'=>'a','à'=>'a','ã'=>'a','â'=>'a','ä'=>'a', 'é'=>'e','è'=>'e','ê'=>'e','ë'=>'e', 'í'=>'i','ì'=>'i','î'=>'i','ï'=>'i', 'ó'=>'o','ò'=>'o','õ'=>'o','ô'=>'o','ö'=>'o', 'ú'=>'u','ù'=>'u','û'=>'u','ü'=>'u', 'ñ'=>'n','ç'=>'c' ]); $nome = preg_replace('/[^a-z0-9]+/', '-', $nome); return trim($nome, '-'); } /** * Salva upload em ../public/imagens_eventos e retorna caminho relativo "imagens_eventos/arquivo.ext" * $prefix: "topo" ou "mapa" (por ex.) */ function uploadImagem(string $field, string $prefix): ?string { if (empty($_FILES[$field]) || $_FILES[$field]['error'] !== UPLOAD_ERR_OK) { return null; } $dirPublic = __DIR__ . '/../public/imagens_eventos/'; if (!is_dir($dirPublic)) mkdir($dirPublic, 0755, true); $ext = strtolower(pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION)); $name = $prefix . '_' . time() . '_' . bin2hex(random_bytes(3)) . '.' . $ext; $destAbs = $dirPublic . $name; if (!move_uploaded_file($_FILES[$field]['tmp_name'], $destAbs)) { return null; } // Caminho salvo no banco deve ser relativo à pasta /public return 'imagens_eventos/' . $name; } // ==== Entrada ==== $id_evento = (isset($_POST['id_evento']) && ctype_digit($_POST['id_evento'])) ? (int)$_POST['id_evento'] : 0; if ($id_evento <= 0) { http_response_code(400); exit('ID do evento não informado.'); } $nome = trim($_POST['nome'] ?? ''); $data = trim($_POST['data'] ?? ''); $local = trim($_POST['local'] ?? ''); $descricao = trim($_POST['descricao'] ?? ''); if ($nome === '' || $data === '' || $local === '') { http_response_code(400); exit('Preencha nome, data e local.'); } // ==== Carrega evento com ESCOPO (permissão) ==== if ($isAdmin) { $sql = "SELECT id, nome, slug, imagem_topo, imagem_mapa, produtor_id FROM eventos WHERE id = ? LIMIT 1"; $st = $conn->prepare($sql); $st->bind_param('i', $id_evento); } else { $sql = "SELECT id, nome, slug, imagem_topo, imagem_mapa, produtor_id FROM eventos WHERE id = ? AND produtor_id = ? LIMIT 1"; $st = $conn->prepare($sql); $st->bind_param('ii', $id_evento, $produtorId); } $st->execute(); $evento = $st->get_result()->fetch_assoc(); $st->close(); if (!$evento) { http_response_code(403); exit('Você não tem permissão para editar este evento ou ele não existe.'); } $slug_atual = (string)$evento['slug']; $slug_novo = gerarSlug($nome); $slug_final = ($slug_novo === $slug_atual) ? $slug_atual : $slug_novo; // ==== Uploads (mantém o atual se não enviar novo) ==== $imagem_topo = uploadImagem('imagem_topo', 'topo') ?: $evento['imagem_topo']; $imagem_mapa = uploadImagem('imagem_mapa', 'mapa') ?: $evento['imagem_mapa']; // ==== Transação ==== $conn->begin_transaction(); try { // Se o slug mudar, removemos a página antiga if ($slug_final !== $slug_atual && $slug_atual !== '') { $arqAntigo = __DIR__ . "/../public/evento/{$slug_atual}.php"; if (is_file($arqAntigo)) @unlink($arqAntigo); } // ---- Atualiza evento ---- $sqlUp = "UPDATE eventos SET nome = ?, slug = ?, data = ?, local = ?, descricao = ?, imagem_topo = ?, imagem_mapa = ? WHERE id = ?"; $stUp = $conn->prepare($sqlUp); $stUp->bind_param('sssssssi', $nome, $slug_final, $data, $local, $descricao, $imagem_topo, $imagem_mapa, $id_evento); if (!$stUp->execute()) { throw new RuntimeException($stUp->error); } $stUp->close(); // ---- Atualiza / remove setores existentes ---- if (!empty($_POST['setores']) && is_array($_POST['setores'])) { foreach ($_POST['setores'] as $s) { $id_setor = isset($s['id']) && ctype_digit((string)$s['id']) ? (int)$s['id'] : 0; if ($id_setor <= 0) continue; // Excluir setor if (isset($s['excluir'])) { $stDel = $conn->prepare("DELETE FROM setores WHERE id = ? AND id_evento = ?"); $stDel->bind_param('ii', $id_setor, $id_evento); if (!$stDel->execute()) { throw new RuntimeException($stDel->error); } $stDel->close(); continue; } // Atualizar setor $nome_setor = trim($s['nome'] ?? ''); $preco = (float)($s['preco'] ?? 0); $quantidade = isset($s['quantidade']) ? (int)$s['quantidade'] : 0; $desc_setor = trim($s['descricao'] ?? ''); $inicio_vendas = trim($s['inicio_vendas'] ?? ''); $fim_vendas = trim($s['fim_vendas'] ?? ''); if ($nome_setor === '') continue; $sqlSet = "UPDATE setores SET nome_setor = ?, preco = ?, quantidade_total = ?, descricao = ?, inicio_vendas = ?, fim_vendas = ? WHERE id = ? AND id_evento = ?"; $stSet = $conn->prepare($sqlSet); // tipos: s (nome) d (preco) i (quantidade) s (descricao) s (inicio) s (fim) i (id_setor) i (id_evento) $stSet->bind_param('sdisssii', $nome_setor, $preco, $quantidade, $desc_setor, $inicio_vendas, $fim_vendas, $id_setor, $id_evento); if (!$stSet->execute()) { throw new RuntimeException($stSet->error); } $stSet->close(); } } // ---- Novo setor (opcional) ---- if (!empty($_POST['novo_setor']['nome'])) { $ns_nome = trim($_POST['novo_setor']['nome']); $ns_preco = (float)($_POST['novo_setor']['preco'] ?? 0); $ns_qtd = isset($_POST['novo_setor']['quantidade']) ? (int)$_POST['novo_setor']['quantidade'] : 0; $ns_desc = trim($_POST['novo_setor']['descricao'] ?? ''); $ns_inicio = trim($_POST['novo_setor']['inicio_vendas'] ?? ''); $ns_fim = trim($_POST['novo_setor']['fim_vendas'] ?? ''); if ($ns_nome !== '') { $stIns = $conn->prepare("INSERT INTO setores (id_evento, nome_setor, preco, quantidade_total, descricao, inicio_vendas, fim_vendas) VALUES (?, ?, ?, ?, ?, ?, ?)"); // tipos: i s d i s s s $stIns->bind_param('isdisss', $id_evento, $ns_nome, $ns_preco, $ns_qtd, $ns_desc, $ns_inicio, $ns_fim); if (!$stIns->execute()) { throw new RuntimeException($stIns->error); } $stIns->close(); } } // ---- Recria a página do evento (com slug final) ---- $dirEvento = __DIR__ . '/../public/evento/'; if (!is_dir($dirEvento)) mkdir($dirEvento, 0755, true); $caminho_novo = $dirEvento . $slug_final . '.php'; $conteudo = "<?php\n\$id_evento = " . (int)$id_evento . ";\ninclude __DIR__ . '/../template_evento.php';\n"; if (file_put_contents($caminho_novo, $conteudo) === false) { throw new RuntimeException('Falha ao escrever a página do evento.'); } // Tudo certo $conn->commit(); header('Location: listar_eventos.php'); exit; } catch (Throwable $e) { $conn->rollback(); http_response_code(500); echo "<p style='color:red;'>Erro ao salvar alterações: ".h($e->getMessage())."</p>"; echo "<p><a href='listar_eventos.php'>← Voltar</a></p>"; exit; }
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel