Primer commit del sistema separado falta mejorar mucho
This commit is contained in:
93
telegram/api/templates/create.php
Executable file
93
telegram/api/templates/create.php
Executable file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* API de Plantillas de Telegram - Crear
|
||||
*/
|
||||
|
||||
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';
|
||||
|
||||
// Para depuración
|
||||
ini_set('display_errors', 0);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// Autenticación
|
||||
try {
|
||||
$userData = JWTAuth::requireAuth();
|
||||
} catch (Exception $e) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['success' => false, 'error' => 'No autenticado: ' . $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verificar permiso
|
||||
if (!hasPermission('manage_templates', 'telegram')) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'error' => 'No tienes permiso para crear plantillas de Telegram.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Solo permitir método POST
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Obtener datos
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$nombre = trim($data['nombre'] ?? '');
|
||||
$comando = trim($data['comando'] ?? '');
|
||||
$contenido = $data['contenido'] ?? '';
|
||||
|
||||
// Validación
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
|
||||
// Verificar si el comando ya existe (si se proporcionó)
|
||||
if ($comando) {
|
||||
$stmt = $db->prepare("SELECT id FROM plantillas_telegram WHERE comando = ?");
|
||||
$stmt->execute([$comando]);
|
||||
if ($stmt->fetch()) {
|
||||
http_response_code(409); // 409 Conflict
|
||||
echo json_encode(['success' => false, 'error' => 'Ya existe una plantilla con ese comando.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Insertar en la base de datos
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO plantillas_telegram (nombre, comando, contenido, usuario_id, fecha_creacion, fecha_modificacion)
|
||||
VALUES (?, ?, ?, ?, NOW(), NOW())
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
$nombre,
|
||||
empty($comando) ? null : $comando,
|
||||
$contenido,
|
||||
$userData->userId
|
||||
]);
|
||||
|
||||
$newTemplateId = $db->lastInsertId();
|
||||
|
||||
logToFile('telegram/templates.log', "Plantilla creada: {$nombre} (ID: {$newTemplateId}), Usuario: {$userData->username}");
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Plantilla creada correctamente.',
|
||||
'templateId' => $newTemplateId
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
error_log('Error en /telegram/api/templates/create.php: ' . $e->getMessage());
|
||||
echo json_encode(['success' => false, 'error' => 'Error del servidor al guardar la plantilla.']);
|
||||
}
|
||||
Reference in New Issue
Block a user