w
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Guardar menú
|
||||
* Fecha: 13 de noviembre de 2025
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
|
||||
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['name']) || !isset($input['title'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Nombre y título son requeridos']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Verificar si es menú raíz
|
||||
$isRoot = empty($input['parent_id']) ? 1 : 0;
|
||||
|
||||
// Obtener próximo order_position
|
||||
$maxOrder = $db->fetch(
|
||||
"SELECT COALESCE(MAX(order_position), 0) as max_order FROM menus"
|
||||
);
|
||||
|
||||
$menuData = [
|
||||
'name' => $input['name'],
|
||||
'title' => $input['title'],
|
||||
'description' => $input['description'] ?? '',
|
||||
'parent_id' => empty($input['parent_id']) ? null : $input['parent_id'],
|
||||
'is_root' => $isRoot,
|
||||
'is_active' => $input['is_active'] ?? 1,
|
||||
'order_position' => $maxOrder['max_order'] + 1,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
$menuId = $db->insert('menus', $menuData);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Menú guardado correctamente',
|
||||
'menu_id' => $menuId
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in save_menu.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor: ' . $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user