diff --git a/macro_area_form.php b/macro_area_form.php
new file mode 100644
index 0000000..786751c
--- /dev/null
+++ b/macro_area_form.php
@@ -0,0 +1,112 @@
+ '',
+ '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';
+?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/macro_areas.php b/macro_areas.php
index 70a5c57..34edd98 100644
--- a/macro_areas.php
+++ b/macro_areas.php
@@ -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);
Imprimir Lista
-
+
-
-
-
-
-
-
-