Files
karczma-aplikacja-stoliki/api/get_table_name.php
2026-05-25 14:02:42 +02:00

41 lines
1.1 KiB
PHP

<?php
function getTableNameByHash($conn, $hash) {
if (empty($hash)) {
return '';
}
$cacheFile = __DIR__ . '/cache/tables_cache.json';
$cacheLifetime = 24 * 3600; // 24 godziny
$cacheData = [];
$cacheValid = false;
if (file_exists($cacheFile)) {
if (time() - filemtime($cacheFile) < $cacheLifetime) {
$json = file_get_contents($cacheFile);
$cacheData = json_decode($json, true);
if (is_array($cacheData)) {
$cacheValid = true;
}
}
}
if (!$cacheValid) {
$cacheData = [];
$tsql = "SELECT ID, Nazwa FROM dbo.NGastroStolik";
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt !== false) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$id = strtoupper($row['ID']);
$nazwa = trim($row['Nazwa']);
$cacheData[$id] = $nazwa;
}
file_put_contents($cacheFile, json_encode($cacheData, JSON_UNESCAPED_UNICODE));
}
}
$hashUpper = strtoupper($hash);
return $cacheData[$hashUpper] ?? '';
}