Files
MediaCollectorLibary/app/Controllers/TvShowController.php
Lars Behrends ca2d3a6960 ...
2025-10-18 22:03:30 +02:00

130 lines
4.1 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 TvShowController 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();
// Get pagination parameters
$page = max(1, (int)($queryParams['page'] ?? 1));
$perPage = max(12, min(100, (int)($queryParams['per_page'] ?? 24)));
// Get search parameters
$search = trim($queryParams['search'] ?? '');
// Get view mode
$viewMode = $queryParams['view'] ?? 'grid'; // grid, list, covers
// For now, return empty arrays since TV Shows aren't implemented yet
$tvshows = [];
$totalCount = 0;
// Calculate pagination info
$totalPages = 0;
$hasNextPage = false;
$hasPrevPage = false;
return $this->view->render($response, 'tvshows/index.twig', [
'title' => 'TV Shows',
'tvshows' => $tvshows,
'pagination' => [
'current_page' => $page,
'per_page' => $perPage,
'total_pages' => $totalPages,
'total_items' => $totalCount,
'has_next' => $hasNextPage,
'has_prev' => $hasPrevPage,
'next_page' => $page + 1,
'prev_page' => $page - 1
],
'search' => $search,
'view_mode' => $viewMode,
'view_modes' => ['grid', 'list', 'covers']
]);
}
public function show(Request $request, Response $response, $args)
{
$tvShowId = (int) $args['id'];
// Get TV show details
$stmt = $this->pdo->prepare("
SELECT t.*, s.display_name as source_name
FROM tv_shows t
JOIN sources s ON t.source_id = s.id
WHERE t.id = :id
");
$stmt->execute(['id' => $tvShowId]);
$tvShow = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$tvShow) {
return $response->withStatus(404);
}
// Decode metadata and other JSON fields
$metadata = json_decode($tvShow['metadata'] ?? '{}', true);
$cast = json_decode($tvShow['cast'] ?? '[]', true);
$genre = json_decode($tvShow['genre'] ?? '[]', true);
// Get actors for this TV show
$stmt = $this->pdo->prepare("
SELECT a.*
FROM actors a
JOIN actor_tv_show ats ON a.id = ats.actor_id
WHERE ats.tv_show_id = :tv_show_id
ORDER BY a.name ASC
");
$stmt->execute(['tv_show_id' => $tvShowId]);
$actors = $stmt->fetchAll(\PDO::FETCH_ASSOC);
/*
// Get seasons for this TV show
$stmt = $this->pdo->prepare("
SELECT * FROM tv_seasons
WHERE tv_show_id = :tv_show_id
ORDER BY season_number ASC
");
$stmt->execute(['tv_show_id' => $tvShowId]);
$seasons = $stmt->fetchAll(\PDO::FETCH_ASSOC);
*//*
// Get episodes for each season
foreach ($seasons as &$season) {
$stmt = $this->pdo->prepare("
SELECT * FROM tv_episodes
WHERE tv_show_id = :tv_show_id AND season_number = :season_number
ORDER BY episode_number ASC
");
$stmt->execute([
'tv_show_id' => $tvShowId,
'season_number' => $season['season_number']
]);
$season['episodes'] = $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
unset($season); // Unset reference
*/
return $this->view->render($response, 'tvshows/show.twig', [
'title' => $tvShow['title'],
'tvshow' => $tvShow,
'metadata' => $metadata,
'cast' => $cast,
'genre' => $genre,
'actors' => $actors,
'seasons' => $seasons
]);
}
}