159 lines
5.5 KiB
PHP
159 lines
5.5 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../includes/session.php';
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
// Check for user authentication
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Usuário não autenticado.']);
|
|
exit;
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$pdo = db();
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
handleGet($pdo);
|
|
break;
|
|
case 'POST':
|
|
handlePost($pdo);
|
|
break;
|
|
case 'PUT':
|
|
handlePut($pdo);
|
|
break;
|
|
case 'PATCH':
|
|
handlePatch($pdo);
|
|
break;
|
|
case 'DELETE':
|
|
handleDelete($pdo);
|
|
break;
|
|
default:
|
|
echo json_encode(['success' => false, 'error' => 'Método não suportado.']);
|
|
break;
|
|
}
|
|
|
|
function handleGet($pdo) {
|
|
try {
|
|
if (isset($_GET['action']) && $_GET['action'] == 'getActiveMacroAreas') {
|
|
$stmt = $pdo->prepare("SELECT id, name FROM macro_areas WHERE is_active = 1 AND user_id = :user_id ORDER BY name ASC");
|
|
$stmt->execute(['user_id' => $_SESSION['user_id']]);
|
|
$macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($macro_areas);
|
|
} elseif (isset($_GET['id'])) {
|
|
$stmt = $pdo->prepare("SELECT * FROM categories WHERE id = :id");
|
|
$stmt->execute(['id' => $_GET['id']]);
|
|
$category = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
echo json_encode($category);
|
|
} else {
|
|
$searchTerm = isset($_GET['search']) ? '%' . $_GET['search'] . '%' : '%';
|
|
$sql = "SELECT c.id, c.name, c.is_active, c.macro_area_id, ma.name as macro_area_name
|
|
FROM categories c
|
|
LEFT JOIN macro_areas ma ON c.macro_area_id = ma.id
|
|
WHERE c.name LIKE :searchTerm
|
|
ORDER BY c.name ASC";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute(['searchTerm' => $searchTerm]);
|
|
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($categories);
|
|
}
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => 'Erro ao buscar dados: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function handlePost($pdo) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (empty($data['name']) || empty($data['macro_area_id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Nome e Macro Área são obrigatórios.']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$sql = "INSERT INTO categories (name, macro_area_id, is_active) VALUES (:name, :macro_area_id, :is_active)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':name' => $data['name'],
|
|
':macro_area_id' => $data['macro_area_id'],
|
|
':is_active' => isset($data['is_active']) ? $data['is_active'] : 1
|
|
]);
|
|
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => 'Erro ao criar categoria: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function handlePut($pdo) {
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
echo json_encode(['success' => false, 'error' => 'ID da categoria não fornecido.']);
|
|
return;
|
|
}
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (empty($data['name']) || empty($data['macro_area_id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Nome e Macro Área são obrigatórios.']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$sql = "UPDATE categories SET name = :name, macro_area_id = :macro_area_id, is_active = :is_active WHERE id = :id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':id' => $id,
|
|
':name' => $data['name'],
|
|
':macro_area_id' => $data['macro_area_id'],
|
|
':is_active' => isset($data['is_active']) ? $data['is_active'] : 1
|
|
]);
|
|
echo json_encode(['success' => true]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => 'Erro ao atualizar categoria: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function handlePatch($pdo) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$id = $data['id'] ?? null;
|
|
$action = $data['action'] ?? null;
|
|
|
|
if (!$id || !$action) {
|
|
echo json_encode(['success' => false, 'error' => 'Dados inválidos.']);
|
|
return;
|
|
}
|
|
|
|
$newStatus = ($action === 'archive') ? 0 : 1;
|
|
|
|
try {
|
|
$sql = "UPDATE categories SET is_active = :is_active WHERE id = :id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':id' => $id,
|
|
':is_active' => $newStatus
|
|
]);
|
|
echo json_encode(['success' => true]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => 'Erro ao atualizar status: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function handleDelete($pdo) {
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
echo json_encode(['success' => false, 'error' => 'ID da categoria não fornecido.']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// Check for dependencies before deleting if necessary
|
|
// For example, check if any expenses are using this category
|
|
|
|
$sql = "DELETE FROM categories WHERE id = :id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute(['id' => $id]);
|
|
echo json_encode(['success' => true]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => 'Erro ao excluir categoria: ' . $e->getMessage()]);
|
|
}
|
|
} |