Files
MediaCollectorLibary/app/Controllers/SearchController.php
Lars Behrends 0f0fb3b410 searcg revamp 😧
2025-11-06 13:39:46 +01:00

182 lines
7.2 KiB
PHP

<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
class SearchController extends Controller
{
private \PDO $pdo;
public function __construct(\PDO $pdo, Twig $view)
{
parent::__construct($view);
$this->pdo = $pdo;
}
public function index(Request $request, Response $response, $args)
{
$queryParams = $request->getQueryParams();
$search = trim($queryParams['q'] ?? '');
$type = $queryParams['type'] ?? 'all';
$sort = $queryParams['sort'] ?? 'relevance';
$page = (int)($queryParams['page'] ?? 1);
$perPage = 30;
$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'];
}
}
// Calculate pagination
$totalPages = ceil($totalResults / $perPage);
return $this->view->render($response, 'search/index.twig', [
'title' => !empty($search) ? 'Search Results' : 'Search',
'search' => $search,
'type' => $type,
'sort' => $sort,
'page' => $page,
'totalPages' => $totalPages,
'totalResults' => $totalResults,
'results' => $results,
'hasResults' => !empty(array_filter($results))
]);
}
}