193 lines
6.7 KiB
PHP
193 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* API - Obtener menús
|
|
* Fecha: 12 de enero de 2026 - Actualizado para diagnostico mejorado
|
|
*/
|
|
|
|
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: GET');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
try {
|
|
// Test inicial de conexión con diagnóstico detallado
|
|
error_log("=== INICIO DEBUG GET MENUS ===");
|
|
|
|
$db = Database::getInstance();
|
|
$connection = $db->getConnection();
|
|
|
|
if (!$connection) {
|
|
throw new Exception("No se pudo obtener la conexión a la base de datos");
|
|
}
|
|
|
|
error_log("Conexión DB establecida correctamente");
|
|
|
|
// Verificar base de datos actual
|
|
$currentDB = $db->fetch("SELECT DATABASE() as current_db");
|
|
error_log("Base de datos actual: " . json_encode($currentDB));
|
|
|
|
// Verificar si las tablas existen
|
|
try {
|
|
$showTablesQuery = "SHOW TABLES LIKE 'menus'";
|
|
$tablesExist = $db->fetchAll($showTablesQuery);
|
|
error_log("Query SHOW TABLES ejecutada. Resultado: " . json_encode($tablesExist));
|
|
|
|
if (empty($tablesExist)) {
|
|
error_log("Tabla 'menus' no existe, pero datos indican que ya existe. Continuando con consulta...");
|
|
} else {
|
|
error_log("Tabla 'menus' ya existe");
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("ERROR creando/verificando tablas: " . $e->getMessage());
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Error en estructura de base de datos',
|
|
'debug' => $e->getMessage(),
|
|
'step' => 'table_verification',
|
|
'data' => []
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
// Obtener menús con consulta simplificada
|
|
try {
|
|
error_log("Ejecutando consulta de menús...");
|
|
|
|
// Verificar estructura de tabla
|
|
$tableStructure = $db->fetchAll("DESCRIBE menus");
|
|
error_log("Estructura tabla menus: " . json_encode($tableStructure));
|
|
|
|
// Consulta adaptada a la estructura real de la tabla existente
|
|
// Incluir menu_type solo si la columna existe (migración puede no haberse ejecutado)
|
|
$hasMenuType = !empty($db->fetchAll("SHOW COLUMNS FROM menus LIKE 'menu_type'"));
|
|
if ($hasMenuType) {
|
|
$menusQuery = "SELECT
|
|
id,
|
|
name,
|
|
title,
|
|
description,
|
|
welcome_message,
|
|
parent_id,
|
|
is_root,
|
|
is_active,
|
|
order_position,
|
|
created_at,
|
|
updated_at,
|
|
menu_type
|
|
FROM menus ORDER BY order_position ASC, id ASC";
|
|
} else {
|
|
$menusQuery = "SELECT
|
|
id,
|
|
name,
|
|
title,
|
|
description,
|
|
welcome_message,
|
|
parent_id,
|
|
is_root,
|
|
is_active,
|
|
order_position,
|
|
created_at,
|
|
updated_at
|
|
FROM menus ORDER BY order_position ASC, id ASC";
|
|
}
|
|
$menus = $db->fetchAll($menusQuery);
|
|
|
|
error_log("Menús obtenidos: " . count($menus) . " registros");
|
|
|
|
// Transformar datos para compatibilidad con JavaScript
|
|
foreach ($menus as &$menu) {
|
|
// Mapear campos para compatibilidad
|
|
$menu['menu_name'] = $menu['title'] ?? $menu['name'];
|
|
$menu['menu_key'] = $menu['name'];
|
|
$menu['status'] = ($menu['is_active'] == 1) ? 'active' : 'inactive';
|
|
// welcome_message ya viene de la BD, solo asegurar que exista
|
|
if (!isset($menu['welcome_message'])) {
|
|
$menu['welcome_message'] = '';
|
|
}
|
|
// menu_type: garantizar que siempre exista el campo
|
|
if (!isset($menu['menu_type'])) {
|
|
$menu['menu_type'] = 'main';
|
|
}
|
|
}
|
|
|
|
error_log("Menús encontrados: " . count($menus));
|
|
|
|
if (!empty($menus)) {
|
|
error_log("Primer menú: " . json_encode($menus[0]));
|
|
}
|
|
|
|
// Procesar opciones con manejo de errores para estructura desconocida
|
|
foreach ($menus as &$menu) {
|
|
try {
|
|
// Primero verificar si la tabla menu_options existe
|
|
$optionsTableExists = $db->fetchAll("SHOW TABLES LIKE 'menu_options'");
|
|
|
|
if (!empty($optionsTableExists)) {
|
|
// Verificar estructura de la tabla
|
|
$optionsStructure = $db->fetchAll("DESCRIBE menu_options");
|
|
error_log("Estructura menu_options: " . json_encode($optionsStructure));
|
|
|
|
$optionsQuery = "SELECT * FROM menu_options WHERE menu_id = ? ORDER BY id ASC";
|
|
$options = $db->fetchAll($optionsQuery, [$menu['id']]);
|
|
} else {
|
|
error_log("Tabla menu_options no existe");
|
|
$options = [];
|
|
}
|
|
|
|
$menu['options'] = $options;
|
|
$menu['options_count'] = count($options);
|
|
} catch (Exception $e) {
|
|
error_log("Error opciones menú {$menu['id']}: " . $e->getMessage());
|
|
$menu['options'] = [];
|
|
$menu['options_count'] = 0;
|
|
}
|
|
}
|
|
|
|
error_log("=== FIN DEBUG GET MENUS SUCCESS ===");
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => $menus,
|
|
'total' => count($menus),
|
|
'message' => 'Menús cargados exitosamente',
|
|
'debug_info' => [
|
|
'db_name' => DB_NAME,
|
|
'total_found' => count($menus),
|
|
'timestamp' => date('Y-m-d H:i:s')
|
|
]
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("ERROR consulta menús: " . $e->getMessage());
|
|
error_log("Stack trace: " . $e->getTraceAsString());
|
|
error_log("=== FIN DEBUG GET MENUS ERROR ===");
|
|
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Error en consulta de menús',
|
|
'debug' => $e->getMessage(),
|
|
'code' => $e->getCode(),
|
|
'step' => 'menu_query',
|
|
'data' => []
|
|
]);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
error_log("ERROR GENERAL get_menus.php: " . $e->getMessage());
|
|
error_log("Stack trace: " . $e->getTraceAsString());
|
|
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Error general del sistema',
|
|
'debug' => $e->getMessage(),
|
|
'step' => 'initialization',
|
|
'data' => []
|
|
]);
|
|
}
|
|
?>
|