Files
karczma-aplikacja-stoliki/public/staff/generator_helpers.php
T
2026-09-24 17:57:45 +02:00

67 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
function formatQrDisplayName(string $nazwa): string
{
$nazwa = trim($nazwa);
if ($nazwa === '') {
return 'Stolik';
}
if (preg_match('/^stolik\s*(.*)$/iu', $nazwa, $matches)) {
$suffix = trim((string) $matches[1]);
return $suffix !== '' ? 'Stolik ' . $suffix : 'Stolik';
}
return 'Stolik ' . $nazwa;
}
function sanitizeZipFileName(string $name): string
{
$name = preg_replace('/[\\\\\\/:*?"<>|]+/u', '-', $name) ?? $name;
$name = preg_replace('/\\s+/u', ' ', $name) ?? $name;
$name = trim($name, " .-\t");
return $name !== '' ? $name : 'Stolik';
}
function buildUniqueQrFileNames(array $tables): array
{
$used = [];
foreach ($tables as &$table) {
$base = sanitizeZipFileName(formatQrDisplayName((string) ($table['nazwa'] ?? '')));
$candidate = $base;
$index = 2;
while (isset($used[$candidate])) {
$candidate = $base . ' (' . $index . ')';
$index++;
}
$used[$candidate] = true;
$table['fileName'] = $candidate . '.png';
}
unset($table);
return $tables;
}
function buildGuestAppBaseUrl(): array
{
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$scriptDir = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '/public/staff'));
$publicDir = str_replace('\\', '/', dirname($scriptDir));
$basePath = rtrim($publicDir, '/') . '/app.html?h=';
$baseUrl = "{$scheme}://{$host}{$basePath}";
return [$baseUrl, $basePath];
}
function appendPreviewParam(string $link, string $previewQuery): string
{
return str_contains($link, '?') ? $link . '&' . $previewQuery : $link . '?' . $previewQuery;
}