feat: cover image fallback for home grid cards

- index.php: batch-load thesis_files covers for theses without banner_path
- Resolution order: banner_path → cover file → gradient placeholder
- Uses single IN() query to avoid N+1 problem
This commit is contained in:
Pontoporeia
2026-03-24 15:39:23 +01:00
parent 372abb5cd6
commit ed2b06a34c
2 changed files with 27 additions and 2 deletions

View File

@@ -28,6 +28,26 @@ try {
}
$totalPages = $isDefaultView ? 1 : (int)ceil($totalItems / $itemsPerPage);
// Batch-load cover images for theses that have no banner_path
$coverMap = [];
if (!empty($itemsToLoad)) {
$needCover = array_column(
array_filter($itemsToLoad, fn($t) => empty($t['banner_path'])),
'id'
);
if (!empty($needCover)) {
$ph = implode(',', array_fill(0, count($needCover), '?'));
$cStmt = $db->getConnection()->prepare("
SELECT thesis_id, file_path FROM thesis_files
WHERE file_type = 'cover' AND thesis_id IN ($ph)
");
$cStmt->execute($needCover);
foreach ($cStmt->fetchAll() as $row) {
$coverMap[$row['thesis_id']] = $row['file_path'];
}
}
}
} catch (Exception $e) {
error_log("Error loading theses: " . $e->getMessage());
$itemsToLoad = [];
@@ -36,6 +56,7 @@ try {
$totalItems = 0;
$latestYear = null;
$isDefaultView = false;
$coverMap = [];
}
$currentNav = '';
@@ -91,7 +112,11 @@ $currentNav = '';
$thumb = $item['banner_path'];
}
// 2. Cover image from thesis_files (not returned by view — skip for now)
// 2. Cover image from thesis_files (batch-loaded above)
if (!$thumb && isset($coverMap[$item['id']])) {
$thumb = $coverMap[$item['id']];
}
// 3. Fall through to gradient
?>
<?php if ($thumb): ?>