Your IP : 216.73.217.78


Current Path : /home/seto/888mirror.pm/admin/
Upload File :
Current File : /home/seto/888mirror.pm/admin/generate.php

<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/openai.php';

auth_check();

header('Content-Type: application/json');

$brand_id = (int)($_POST['brand_id'] ?? 0);
$action   = $_POST['action'] ?? 'generate';

if (!$brand_id) {
    echo json_encode(['error' => 'Неверные параметры.']);
    exit();
}

$stmt = db()->prepare("SELECT name FROM brands WHERE id = ?");
$stmt->execute([$brand_id]);
$brand = $stmt->fetch();

if (!$brand) {
    echo json_encode(['error' => 'Бренд не найден.']);
    exit();
}

// Исследование бренда через web search
if ($action === 'research') {
    try {
        $facts = openai_research_brand($brand['name']);
        echo json_encode(['success' => true, 'facts' => $facts]);
    } catch (RuntimeException $e) {
        echo json_encode(['error' => $e->getMessage()]);
    }
    exit();
}

// Генерация переводов
$langs   = array_filter(array_map('trim', explode(',', $_POST['langs'] ?? '')));
$context = trim($_POST['context'] ?? '');

$active = db()->query("SELECT code FROM languages WHERE is_active = 1")->fetchAll(PDO::FETCH_COLUMN);
$langs  = array_values(array_intersect($langs, $active));

if (empty($langs)) {
    echo json_encode(['error' => 'Нет активных языков для генерации.']);
    exit();
}

try {
    $data = openai_generate($brand['name'], $langs, $context);
    openai_save_translations($brand_id, $data);
    echo json_encode(['success' => true, 'data' => $data]);
} catch (RuntimeException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}