menus
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Eliminar menú
|
||||
* Fecha: 12 de enero de 2026
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
|
||||
// Verificar autenticación
|
||||
requireAuthentication();
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Método no permitido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$input || !isset($input['menu_id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID del menú es requerido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$menuId = (int)$input['menu_id'];
|
||||
|
||||
if ($menuId <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID de menú inválido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Verificar que el menú existe
|
||||
$menu = $db->fetch("SELECT id, name FROM menus WHERE id = :id", ['id' => $menuId]);
|
||||
|
||||
if (!$menu) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Menú no encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Iniciar transacción
|
||||
$db->beginTransaction();
|
||||
|
||||
try {
|
||||
// Eliminar opciones del menú
|
||||
$db->execute("DELETE FROM menu_options WHERE menu_id = :menu_id", ['menu_id' => $menuId]);
|
||||
|
||||
// Eliminar el menú
|
||||
$result = $db->execute("DELETE FROM menus WHERE id = :id", ['id' => $menuId]);
|
||||
|
||||
if ($result) {
|
||||
$db->commit();
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => "Menú '{$menu['name']}' eliminado correctamente"
|
||||
]);
|
||||
} else {
|
||||
$db->rollback();
|
||||
echo json_encode(['error' => 'Error eliminando menú']);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$db->rollback();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in delete_menu.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'error' => 'Error interno del servidor',
|
||||
'debug' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user