This commit is contained in:
2025-12-30 01:13:06 +01:00
parent 6303da2633
commit ef94ad8038
5 changed files with 109 additions and 74 deletions

47
src/App/Search.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App;
final class Search
{
public function __construct(private ?\PDO $pdo) {}
public function searchEvents(string $query, int $limit = 100): array
{
if (!$this->pdo) return [];
$q = trim($query);
if ($q === '') return [];
$tokens = array_filter(preg_split('/\s+/', $q) ?: [], fn($t) => $t !== '');
if (!$tokens) {
$tokens = [$q];
}
$conditions = [];
$params = [];
$i = 0;
foreach ($tokens as $tok) {
$ph = ':kw' . $i++;
$conditions[] = "(title LIKE $ph OR teaser_public LIKE $ph OR description LIKE $ph OR city LIKE $ph OR region LIKE $ph OR zip LIKE $ph)";
$params[$ph] = '%' . $tok . '%';
}
$where = $conditions ? ('AND ' . implode(' AND ', $conditions)) : '';
$sql = "SELECT id, title, teaser_public, description, city, region, starts_at, visibility, allow_kids, location_label
FROM events
WHERE starts_at >= NOW()
AND status != 'cancelled'
$where
ORDER BY starts_at ASC
LIMIT :lim";
$stmt = $this->pdo->prepare($sql);
foreach ($params as $k => $v) {
$stmt->bindValue($k, $v, \PDO::PARAM_STR);
}
$stmt->bindValue(':lim', $limit, \PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
}
}