resolveForMatch($matchId); if (!$templateRow) { $template = require __DIR__ . '/../../config/templates/ltv26_scoresheet.php'; } else { $template = json_decode($templateRow['config_json'], true); $template = is_array($template) ? $template : []; $template['image'] = $templateRow['image_path']; $template['width'] = (int) $templateRow['page_width']; $template['height'] = (int) $templateRow['page_height']; } $match = $this->match($db, $matchId); $sets = $this->rows($db, 'SELECT * FROM match_sets WHERE match_id = :id ORDER BY set_number', $matchId); $homePlayers = $this->players($db, (int) $match['home_team_id']); $awayPlayers = $this->players($db, (int) $match['away_team_id']); $signatures = $this->rows($db, 'SELECT * FROM referee_signatures WHERE match_id = :id ORDER BY signed_at DESC', $matchId); header('Content-Type: text/html; charset=utf-8'); header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); if ($templateRow) { $payload = json_encode(['match_id' => $matchId, 'template_id' => $templateRow['id'], 'time' => time()]); $db->prepare( 'INSERT INTO match_sheet_exports (match_id, sheet_template_id, export_type, export_hash) VALUES (:match_id, :sheet_template_id, "html", :export_hash)' )->execute([ 'match_id' => $matchId, 'sheet_template_id' => $templateRow['id'], 'export_hash' => hash('sha256', $payload), ]); } echo $this->html($template, $match, $sets, $homePlayers, $awayPlayers, $signatures); } private function html(array $template, array $match, array $sets, array $homePlayers, array $awayPlayers, array $signatures): string { $fields = [ 'tournament' => $match['tournament_name'], 'match_code' => '#' . $match['id'], 'date' => $match['scheduled_at'] ? date('d/m/Y', strtotime($match['scheduled_at'])) : '', 'time' => $match['scheduled_at'] ? date('H:i', strtotime($match['scheduled_at'])) : '', 'court' => $match['court_name'] ?? '', 'home_team' => $match['home_team'], 'away_team' => $match['away_team'], 'home_sets' => (string) $match['home_sets'], 'away_sets' => (string) $match['away_sets'], 'referee_signature' => $signatures[0]['signer_name'] ?? '', ]; $items = []; foreach ($fields as $key => $value) { if (isset($template['fields'][$key])) { $items[] = $this->box($template, $template['fields'][$key], $value); } } foreach ($sets as $index => $set) { if (!isset($template['result_rows'][$index], $template['result_columns'])) { continue; } $row = $template['result_rows'][$index]; $cols = $template['result_columns']; $items[] = $this->box($template, ['x' => $cols['home_pt']['x'], 'y' => $row['y'], 'w' => $cols['home_pt']['w'], 'size' => 11, 'align' => 'center'], (string) $set['home_points']); $items[] = $this->box($template, ['x' => $cols['set']['x'], 'y' => $row['y'], 'w' => $cols['set']['w'], 'size' => 11, 'align' => 'center'], $set['home_points'] . ' - ' . $set['away_points']); $items[] = $this->box($template, ['x' => $cols['away_pt']['x'], 'y' => $row['y'], 'w' => $cols['away_pt']['w'], 'size' => 11, 'align' => 'center'], (string) $set['away_points']); } if (!empty($template['home_players'])) { $items = array_merge($items, $this->playerBoxes($template, $template['home_players'], $homePlayers)); } if (!empty($template['away_players'])) { $items = array_merge($items, $this->playerBoxes($template, $template['away_players'], $awayPlayers)); } $image = htmlspecialchars($template['image'], ENT_QUOTES); return '