mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
searcg revamp 😧
This commit is contained in:
@@ -20,44 +20,162 @@ class SearchController extends Controller
|
||||
{
|
||||
$queryParams = $request->getQueryParams();
|
||||
$search = trim($queryParams['q'] ?? '');
|
||||
$type = $queryParams['type'] ?? 'all';
|
||||
$sort = $queryParams['sort'] ?? 'relevance';
|
||||
$page = (int)($queryParams['page'] ?? 1);
|
||||
$perPage = 30;
|
||||
|
||||
if (empty($search)) {
|
||||
return $this->view->render($response, 'search/index.twig', [
|
||||
'title' => 'Search',
|
||||
'search' => $search,
|
||||
'results' => []
|
||||
]);
|
||||
$results = [];
|
||||
$totalResults = 0;
|
||||
|
||||
if (!empty($search)) {
|
||||
$searchTerm = $this->pdo->quote("%$search%");
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
// Build sort clause
|
||||
$sortClause = match($sort) {
|
||||
'title' => 'title ASC',
|
||||
'date' => 'created_at DESC',
|
||||
'rating' => 'rating DESC',
|
||||
default => 'title ASC'
|
||||
};
|
||||
|
||||
// Search movies
|
||||
if ($type === 'all' || $type === 'movies') {
|
||||
$movieStmt = $this->pdo->query("
|
||||
SELECT m.*, s.display_name as source_name, 'movie' as type,
|
||||
m.created_at as added_date
|
||||
FROM movies m
|
||||
JOIN sources s ON m.source_id = s.id
|
||||
WHERE (m.title LIKE $searchTerm OR m.overview LIKE $searchTerm)
|
||||
ORDER BY $sortClause
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['movies'] = $movieStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for movies
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM movies m
|
||||
WHERE (m.title LIKE $searchTerm OR m.overview LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
|
||||
// Search games
|
||||
if ($type === 'all' || $type === 'games') {
|
||||
$gameStmt = $this->pdo->query("
|
||||
SELECT g.*, 'game' as type, g.created_at as added_date
|
||||
FROM games g
|
||||
WHERE (g.title LIKE $searchTerm OR g.description LIKE $searchTerm)
|
||||
ORDER BY $sortClause
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['games'] = $gameStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for games
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM games g
|
||||
WHERE (g.title LIKE $searchTerm OR g.description LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
|
||||
// Search TV shows
|
||||
if ($type === 'all' || $type === 'tvshows') {
|
||||
$tvStmt = $this->pdo->query("
|
||||
SELECT t.*, s.display_name as source_name, 'tvshow' as type,
|
||||
t.created_at as added_date
|
||||
FROM tv_shows t
|
||||
JOIN sources s ON t.source_id = s.id
|
||||
WHERE (t.title LIKE $searchTerm OR t.overview LIKE $searchTerm)
|
||||
ORDER BY $sortClause
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['tvshows'] = $tvStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for TV shows
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM tv_shows t
|
||||
WHERE (t.title LIKE $searchTerm OR t.overview LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
|
||||
// Search music
|
||||
if ($type === 'all' || $type === 'music') {
|
||||
$musicStmt = $this->pdo->query("
|
||||
SELECT m.*, s.display_name as source_name, 'music' as type,
|
||||
m.created_at as added_date
|
||||
FROM music_albums m
|
||||
JOIN sources s ON m.source_id = s.id
|
||||
WHERE (m.title LIKE $searchTerm OR m.artist_name LIKE $searchTerm)
|
||||
ORDER BY $sortClause
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['music'] = $musicStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for music
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM music_albums m
|
||||
WHERE (m.title LIKE $searchTerm OR m.artist_name LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
|
||||
// Search adult videos
|
||||
if ($type === 'all' || $type === 'adult') {
|
||||
$adultStmt = $this->pdo->query("
|
||||
SELECT a.*, s.display_name as source_name, 'adult' as type,
|
||||
a.created_at as added_date
|
||||
FROM adult_videos a
|
||||
JOIN sources s ON a.source_id = s.id
|
||||
WHERE (a.title LIKE $searchTerm)
|
||||
ORDER BY $sortClause
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['adult'] = $adultStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for adult videos
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM adult_videos a
|
||||
WHERE (a.title LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
|
||||
// Search actors
|
||||
if ($type === 'all' || $type === 'actors') {
|
||||
$actorStmt = $this->pdo->query("
|
||||
SELECT a.*, 'actor' as type, a.created_at as added_date, a.thumbnail_path as thumbnail, a.name as title
|
||||
FROM actors a
|
||||
WHERE (a.name LIKE $searchTerm)
|
||||
|
||||
LIMIT $perPage OFFSET $offset
|
||||
");
|
||||
$results['actors'] = $actorStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Get total count for actors
|
||||
$countStmt = $this->pdo->query("
|
||||
SELECT COUNT(*) as count FROM actors a
|
||||
WHERE (a.name LIKE $searchTerm)
|
||||
");
|
||||
$totalResults += $countStmt->fetch(\PDO::FETCH_ASSOC)['count'];
|
||||
}
|
||||
}
|
||||
|
||||
// Search across different media types
|
||||
$results = [];
|
||||
// Calculate pagination
|
||||
$totalPages = ceil($totalResults / $perPage);
|
||||
|
||||
// Search movies (including adult videos)
|
||||
$searchTerm = $this->pdo->quote("%$search%");
|
||||
$movieStmt = $this->pdo->query("
|
||||
SELECT m.*, s.display_name as source_name, 'movie' as type
|
||||
FROM movies m
|
||||
JOIN sources s ON m.source_id = s.id
|
||||
WHERE (m.title LIKE $searchTerm OR m.overview LIKE $searchTerm)
|
||||
ORDER BY m.title
|
||||
LIMIT 20
|
||||
");
|
||||
$results['movies'] = $movieStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Search games
|
||||
$gameStmt = $this->pdo->query("
|
||||
SELECT g.*, 'game' as type
|
||||
FROM games g
|
||||
WHERE (g.title LIKE $searchTerm OR g.description LIKE $searchTerm)
|
||||
ORDER BY g.title
|
||||
LIMIT 20
|
||||
");
|
||||
$results['games'] = $gameStmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
return $this->view->render($response, 'search/index.twig', [
|
||||
'title' => 'Search Results',
|
||||
'title' => !empty($search) ? 'Search Results' : 'Search',
|
||||
'search' => $search,
|
||||
'results' => $results
|
||||
'type' => $type,
|
||||
'sort' => $sort,
|
||||
'page' => $page,
|
||||
'totalPages' => $totalPages,
|
||||
'totalResults' => $totalResults,
|
||||
'results' => $results,
|
||||
'hasResults' => !empty(array_filter($results))
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user