Files
MediaCollectorLibary/app/Controllers/SearchController.php
Lars Behrends 04140786a7 Stuff i guess ?
2025-10-31 00:24:17 +01:00

64 lines
1.8 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'] ?? '');
if (empty($search)) {
return $this->view->render($response, 'search/index.twig', [
'title' => 'Search',
'search' => $search,
'results' => []
]);
}
// Search across different media types
$results = [];
// 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',
'search' => $search,
'results' => $results
]);
}
}