Files
folio/templates/search.php

79 lines
3.4 KiB
PHP

<?php ob_start(); ?>
<div class="search-page">
<form class="search-bar mb-5" action="/search" method="GET" role="search">
<div class="input-group">
<input type="search" class="form-control form-control-lg" name="q"
value="<?= htmlspecialchars($searchQuery) ?>"
placeholder="Rechercher…" autofocus autocomplete="off"
aria-label="Rechercher">
<button class="btn btn-primary" type="submit">Rechercher</button>
</div>
<div class="form-text mt-1">
Plusieurs mots = recherche ET · Fautes de frappe tolérées · Accents ignorés
</div>
</form>
<?php if ($searchQuery !== ''): ?>
<?php if (empty($searchResults)): ?>
<p class="text-muted">Aucun résultat pour <strong><?= htmlspecialchars($searchQuery) ?></strong>.</p>
<?php else: ?>
<p class="text-muted mb-4">
<?= count($searchResults) ?> résultat<?= count($searchResults) > 1 ? 's' : '' ?>
pour <strong><?= htmlspecialchars($searchQuery) ?></strong>
</p>
<?php
$tierLabels = [1 => 'Dans le titre', 2 => 'Dans le texte', 3 => 'À peu près'];
$byTier = [];
foreach ($searchResults as $r) {
$byTier[$r['tier'] ?? 3][] = $r;
}
$multiTier = count($byTier) > 1;
?>
<div class="search-results">
<?php foreach ($byTier as $tierNum => $tierResults): ?>
<?php if ($multiTier): ?>
<h3 class="h6 text-muted mt-4 mb-2 border-bottom pb-1"><?= $tierLabels[$tierNum] ?? '' ?></h3>
<?php endif; ?>
<?php foreach ($tierResults as $r):
$a = $r['article'];
$postUrl = '/post/' . rawurlencode($a['slug'] ?? '');
$cat = trim($a['category'] ?? '');
$date = $a['published_at'] ? date('d/m/Y', strtotime((string)$a['published_at'])) : '';
?>
<article class="search-result">
<div class="search-result-meta">
<?php if ($cat !== ''): ?>
<a class="search-result-cat" href="/categorie/<?= rawurlencode($cat) ?>"><?= htmlspecialchars($cat) ?></a>
<?php endif; ?>
<?php if ($date !== ''): ?>
<time class="search-result-date"><?= $date ?></time>
<?php endif; ?>
</div>
<h2 class="search-result-title">
<a href="<?= htmlspecialchars($postUrl) ?>"><?= htmlspecialchars($a['title'] ?? '') ?></a>
</h2>
<?php if ($r['snippet'] !== ''): ?>
<p class="search-result-snippet"><?= $r['snippet'] ?></p>
<?php endif; ?>
</article>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php
$content = ob_get_clean();
$title = $searchQuery !== '' ? 'Recherche : ' . $searchQuery : 'Recherche';
$metaRobots = 'noindex, follow';
$canonical = rtrim(APP_URL, '/') . '/';
include __DIR__ . '/layout.php';