126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* API para guardar/actualizar opción de menú
|
|
* Fecha: 12 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['menu_id']) || !isset($input['option_key']) || !isset($input['option_text'])) {
|
|
throw new Exception('Datos incompletos: se requiere menu_id, option_key y option_text');
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Verificar que el menú existe
|
|
$menu = $db->fetch("SELECT id FROM menus WHERE id = :id", ['id' => $input['menu_id']]);
|
|
if (!$menu) {
|
|
throw new Exception('Menú no encontrado');
|
|
}
|
|
|
|
// Verificar estructura de tabla menu_options
|
|
$tableStructure = $db->fetchAll("DESCRIBE menu_options");
|
|
$availableColumns = array_column($tableStructure, 'Field');
|
|
|
|
// Preparar datos según estructura de tabla
|
|
$optionData = [];
|
|
|
|
if (in_array('menu_id', $availableColumns)) {
|
|
$optionData['menu_id'] = (int)$input['menu_id'];
|
|
}
|
|
|
|
if (in_array('option_number', $availableColumns)) {
|
|
$optionData['option_number'] = (int)$input['option_key'];
|
|
}
|
|
|
|
if (in_array('option_key', $availableColumns)) {
|
|
$optionData['option_key'] = $input['option_key'];
|
|
}
|
|
|
|
if (in_array('text', $availableColumns)) {
|
|
$optionData['text'] = $input['option_text'];
|
|
}
|
|
|
|
if (in_array('option_text', $availableColumns)) {
|
|
$optionData['option_text'] = $input['option_text'];
|
|
}
|
|
|
|
if (in_array('action_type', $availableColumns)) {
|
|
$optionData['action_type'] = $input['option_action'] ?? 'message';
|
|
}
|
|
|
|
if (in_array('option_action', $availableColumns)) {
|
|
$optionData['option_action'] = $input['option_action'] ?? 'message';
|
|
}
|
|
|
|
if (in_array('action_value', $availableColumns)) {
|
|
$optionData['action_value'] = $input['option_value'] ?? '';
|
|
}
|
|
|
|
if (in_array('option_value', $availableColumns)) {
|
|
$optionData['option_value'] = $input['option_value'] ?? '';
|
|
}
|
|
|
|
if (in_array('is_active', $availableColumns)) {
|
|
$optionData['is_active'] = ($input['is_active'] ?? true) ? 1 : 0;
|
|
}
|
|
|
|
if (in_array('order_index', $availableColumns)) {
|
|
// Obtener el siguiente índice de orden
|
|
$maxOrder = $db->fetch(
|
|
"SELECT COALESCE(MAX(order_index), 0) as max_order FROM menu_options WHERE menu_id = :menu_id",
|
|
['menu_id' => $input['menu_id']]
|
|
);
|
|
$optionData['order_index'] = ($maxOrder['max_order'] ?? 0) + 1;
|
|
}
|
|
|
|
if (in_array('created_at', $availableColumns)) {
|
|
$optionData['created_at'] = date('Y-m-d H:i:s');
|
|
}
|
|
|
|
// Insertar la opción
|
|
$optionId = $db->insert('menu_options', $optionData);
|
|
|
|
if (!$optionId) {
|
|
throw new Exception('Error insertando opción de menú');
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Opción de menú guardada correctamente',
|
|
'option_id' => $optionId,
|
|
'menu_id' => $input['menu_id']
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error in save_menu_option.php: " . $e->getMessage());
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|