diff --git a/macro_areas.php b/macro_areas.php index dd94044..70a5c57 100644 --- a/macro_areas.php +++ b/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); ?>