up
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* API para eliminar opción de menú
|
||||
* Fecha: 13 de enero de 2026
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
require_once '../classes/Database.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
throw new Exception('Método no permitido');
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$input) {
|
||||
throw new Exception('Datos inválidos');
|
||||
}
|
||||
|
||||
// Validar datos requeridos
|
||||
if (!isset($input['option_id'])) {
|
||||
throw new Exception('ID de opción requerido');
|
||||
}
|
||||
|
||||
$optionId = (int)$input['option_id'];
|
||||
$menuId = isset($input['menu_id']) ? (int)$input['menu_id'] : null;
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Verificar que la opción existe
|
||||
$option = $db->fetch("SELECT * FROM menu_options WHERE id = :id", ['id' => $optionId]);
|
||||
|
||||
if (!$option) {
|
||||
throw new Exception('Opción no encontrada');
|
||||
}
|
||||
|
||||
// Si se proporcionó menu_id, verificar que coincida
|
||||
if ($menuId !== null && $option['menu_id'] != $menuId) {
|
||||
throw new Exception('La opción no pertenece al menú especificado');
|
||||
}
|
||||
|
||||
// Eliminar la opción
|
||||
$result = $db->execute("DELETE FROM menu_options WHERE id = :id", ['id' => $optionId]);
|
||||
|
||||
if ($result > 0) {
|
||||
// Registrar en log
|
||||
error_log("Opción de menú eliminada: ID={$optionId}, Menú={$option['menu_id']}");
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Opción eliminada correctamente',
|
||||
'option_id' => $optionId,
|
||||
'menu_id' => $option['menu_id']
|
||||
]);
|
||||
} else {
|
||||
throw new Exception('No se pudo eliminar la opción');
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in delete_menu_option.php: " . $e->getMessage());
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user