Dodatkowe wersje językowe z AI

This commit is contained in:
2026-09-25 18:04:28 +02:00
parent a381ad09d8
commit 4fde92d323
23 changed files with 1231 additions and 148 deletions
+16 -132
View File
@@ -4,6 +4,8 @@ declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/menu_helpers.php';
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo json_encode([
@@ -29,138 +31,9 @@ if ($lang === '' || !isset($languages[$lang])) {
$langConfig = $languages[$lang];
$source = (string) ($langConfig['source'] ?? 'upstream');
$ttl = max(60, (int) ($config['ttl_seconds'] ?? 1800));
$ttl = max(60, (int) ($config['ttl_seconds'] ?? 86400));
$timeout = max(3, (int) ($config['timeout_seconds'] ?? 12));
$cacheDir = __DIR__ . '/cache';
$cacheFile = $cacheDir . '/menu_' . $lang . '.json';
if (!is_dir($cacheDir)) {
@mkdir($cacheDir, 0755, true);
}
function readMenuCache(string $cacheFile): ?array
{
if (!is_file($cacheFile)) {
return null;
}
$raw = file_get_contents($cacheFile);
if ($raw === false || $raw === '') {
return null;
}
$decoded = json_decode($raw, true);
return is_array($decoded) ? $decoded : null;
}
function writeMenuCache(string $cacheFile, array $payload): void
{
file_put_contents(
$cacheFile,
json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
LOCK_EX
);
}
function fetchUpstreamMenu(string $url, int $timeout): ?array
{
$body = false;
$httpCode = 0;
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_HTTPHEADER => ['Accept: application/json'],
]);
$body = curl_exec($ch);
$errno = curl_errno($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($errno !== 0) {
$body = false;
}
} else {
$context = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => $timeout,
'header' => "Accept: application/json\r\n",
'ignore_errors' => true,
],
]);
$body = @file_get_contents($url, false, $context);
if (isset($http_response_header[0]) && preg_match('/\s(\d{3})\s/', $http_response_header[0], $m)) {
$httpCode = (int) $m[1];
}
}
if ($body === false || ($httpCode > 0 && ($httpCode < 200 || $httpCode >= 300))) {
return null;
}
$decoded = json_decode($body, true);
return is_array($decoded) ? $decoded : null;
}
function mapUpstreamMenu(array $raw, string $lang, string $source): array
{
$categories = [];
foreach ($raw['categories'] ?? [] as $cat) {
if (!is_array($cat)) {
continue;
}
$id = trim((string) ($cat['id'] ?? ''));
$name = trim((string) ($cat['name'] ?? ''));
if ($id === '' || $name === '') {
continue;
}
$categories[] = ['id' => $id, 'name' => $name];
}
$sections = [];
foreach ($raw['tables'] ?? [] as $table) {
if (!is_array($table)) {
continue;
}
$sectionName = trim((string) ($table['name'] ?? ''));
if ($sectionName === '') {
continue;
}
$items = [];
foreach ($table['items'] ?? [] as $item) {
if (!is_array($item)) {
continue;
}
$items[] = [
'position' => (string) ($item['id'] ?? ''),
'categoryId' => (string) ($item['category'] ?? ''),
'tag' => '',
'image' => (string) ($item['image'] ?? ''),
'title' => trim((string) ($item['name'] ?? '')),
'description' => trim((string) ($item['name_extra'] ?? '')),
'price' => (string) ($item['price'] ?? ''),
];
}
$sections[] = [
'categoryName' => $sectionName,
'items' => $items,
];
}
return [
'status' => 'success',
'lang' => $lang,
'source' => $source,
'cachedAt' => gmdate('c'),
'categories' => $categories,
'sections' => $sections,
];
}
$cacheFile = menuCachePath($lang);
$cached = readMenuCache($cacheFile);
$now = time();
@@ -177,13 +50,24 @@ if ($isFresh) {
if ($source === 'ai') {
if ($cached) {
// Stary AI-cache jest OK dłużej niż TTL — cron i tak odświeża raz/dzień.
echo json_encode($cached, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$from = strtolower((string) ($langConfig['from'] ?? 'pl'));
$fallback = readMenuCache(menuCachePath($from));
if ($fallback) {
$fallback['lang'] = $lang;
$fallback['source'] = 'ai_fallback_pl';
echo json_encode($fallback, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
http_response_code(503);
echo json_encode([
'status' => 'error',
'message' => 'AI menu not available yet',
'message' => 'AI menu not available yet — run scripts/refresh_menu_cache.php',
'lang' => $lang,
], JSON_UNESCAPED_UNICODE);
exit;