100 lines
3.5 KiB
PHP
Executable File
100 lines
3.5 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* API de Plantillas de Discord - Crear
|
|
* REFRACTORIZADO para usar la tabla `comandos_discord`
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
require_once __DIR__ . '/../../../shared/utils/helpers.php';
|
|
require_once __DIR__ . '/../../../shared/auth/jwt.php';
|
|
require_once __DIR__ . '/../../../shared/database/connection.php';
|
|
|
|
ini_set('display_errors', 0);
|
|
error_reporting(E_ALL);
|
|
|
|
try {
|
|
$userData = JWTAuth::requireAuth();
|
|
} catch (Exception $e) {
|
|
http_response_code(401);
|
|
echo json_encode(['success' => false, 'error' => 'No autenticado: ' . $e->getMessage()]);
|
|
exit;
|
|
}
|
|
|
|
if (!hasPermission('manage_templates', 'discord')) {
|
|
http_response_code(403);
|
|
echo json_encode(['success' => false, 'error' => 'No tienes permiso para crear plantillas de Discord.']);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Método no permitido.']);
|
|
exit;
|
|
}
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$nombre = trim($data['nombre'] ?? '');
|
|
$comando = ltrim(trim($data['comando'] ?? ''), '#/');
|
|
$contenido = $data['contenido'] ?? '';
|
|
|
|
if (empty($nombre) || empty($contenido)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'El nombre y el contenido de la plantilla son obligatorios.']);
|
|
exit;
|
|
}
|
|
|
|
$db = getDB();
|
|
try {
|
|
$db->beginTransaction();
|
|
|
|
// 1. Verificar si el comando ya existe en la tabla `comandos_discord`
|
|
if (!empty($comando)) {
|
|
$stmt = $db->prepare("SELECT id FROM comandos_discord WHERE comando = ?");
|
|
$stmt->execute([$comando]);
|
|
if ($stmt->fetch()) {
|
|
$db->rollBack();
|
|
http_response_code(409); // Conflict
|
|
echo json_encode(['success' => false, 'error' => 'Ya existe un comando con ese nombre.']);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// 2. Insertar la plantilla (sin la columna `comando`)
|
|
$stmt = $db->prepare("
|
|
INSERT INTO plantillas_discord (nombre, contenido, usuario_id, fecha_creacion, fecha_modificacion)
|
|
VALUES (?, ?, ?, NOW(), NOW())
|
|
");
|
|
$stmt->execute([$nombre, $contenido, $userData->userId]);
|
|
$newTemplateId = $db->lastInsertId();
|
|
|
|
// 3. Si hay un comando, insertarlo en la tabla `comandos_discord`
|
|
if (!empty($comando)) {
|
|
logToFile('discord/templates.log', "Intentando insertar comando en comandos_discord. Comando: {$comando}, Plantilla ID: {$newTemplateId}", 'INFO');
|
|
$stmt = $db->prepare("
|
|
INSERT INTO comandos_discord (comando, descripcion, plantilla_id)
|
|
VALUES (?, ?, ?)
|
|
");
|
|
// Usamos el nombre de la plantilla como descripción por defecto
|
|
$stmt->execute([$comando, $nombre, $newTemplateId]);
|
|
logToFile('discord/templates.log', "Comando insertado en comandos_discord. Comando: {$comando}, Plantilla ID: {$newTemplateId}", 'INFO');
|
|
}
|
|
|
|
$db->commit();
|
|
|
|
logToFile('discord/templates.log', "Plantilla creada: {$nombre} (ID: {$newTemplateId}), por Usuario: {$userData->username}");
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Plantilla creada correctamente.',
|
|
'templateId' => $newTemplateId
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
if ($db->inTransaction()) {
|
|
$db->rollBack();
|
|
}
|
|
http_response_code(500);
|
|
logToFile('discord/errors.log', 'Error creando plantilla: ' . $e->getMessage(), 'ERROR');
|
|
echo json_encode(['success' => false, 'error' => 'Ocurrió un error en el servidor al crear la plantilla.']);
|
|
} |