117 lines
3.4 KiB
PHP
117 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* API - Guardar respuesta automática
|
|
* 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');
|
|
|
|
// Solo permitir POST
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
throw new Exception('Datos no válidos');
|
|
}
|
|
|
|
// Validar campos requeridos
|
|
$required_fields = ['trigger_type', 'response_text'];
|
|
foreach ($required_fields as $field) {
|
|
if (!isset($input[$field]) || trim($input[$field]) === '') {
|
|
throw new Exception("El campo '$field' es requerido");
|
|
}
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Preparar datos para insertar/actualizar
|
|
$data = [
|
|
'trigger_type' => $input['trigger_type'],
|
|
'response_text' => trim($input['response_text']),
|
|
'response_type' => $input['response_type'] ?? 'text',
|
|
'priority' => intval($input['priority'] ?? 0),
|
|
'is_active' => isset($input['is_active']) ? (bool)$input['is_active'] : true,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
// Campos opcionales
|
|
if (!empty($input['trigger_value'])) {
|
|
$data['trigger_value'] = trim($input['trigger_value']);
|
|
}
|
|
|
|
if (!empty($input['template_name'])) {
|
|
$data['template_name'] = trim($input['template_name']);
|
|
}
|
|
|
|
if (!empty($input['menu_id'])) {
|
|
$data['menu_id'] = intval($input['menu_id']);
|
|
}
|
|
|
|
// Verificar si es actualización o inserción
|
|
if (!empty($input['id'])) {
|
|
// Actualización
|
|
$id = intval($input['id']);
|
|
|
|
// Verificar que existe
|
|
$existing = $db->fetchAll("SELECT id FROM autoresponses WHERE id = ?", [$id]);
|
|
if (empty($existing)) {
|
|
throw new Exception('Respuesta automática no encontrada');
|
|
}
|
|
|
|
// Actualizar
|
|
$setClauses = [];
|
|
$values = [];
|
|
|
|
foreach ($data as $key => $value) {
|
|
$setClauses[] = "$key = ?";
|
|
$values[] = $value;
|
|
}
|
|
|
|
$values[] = $id;
|
|
|
|
$sql = "UPDATE autoresponses SET " . implode(', ', $setClauses) . " WHERE id = ?";
|
|
$db->execute($sql, $values);
|
|
|
|
$message = 'Respuesta automática actualizada correctamente';
|
|
|
|
} else {
|
|
// Inserción
|
|
$data['created_at'] = date('Y-m-d H:i:s');
|
|
$id = $db->insert('autoresponses', $data);
|
|
$message = 'Respuesta automática creada correctamente';
|
|
}
|
|
|
|
// Obtener el registro actualizado/creado
|
|
$autoresponse = $db->fetchAll("SELECT * FROM autoresponses WHERE id = ?", [$id]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => $autoresponse[0] ?? null,
|
|
'message' => $message,
|
|
'id' => $id
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error en save_autoresponse.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'debug' => $e->getTraceAsString()
|
|
]);
|
|
}
|
|
?>
|