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 // Get TV shows with pagination and search $tvshows = TvShow::getAllWithPagination($this->pdo, $page, $perPage, $search); // Get total count for pagination $totalCount = TvShow::getTotalCount($this->pdo, $search); // Calculate pagination info $totalPages = ceil($totalCount / $perPage); $hasNextPage = $page < $totalPages; $hasPrevPage = $page > 1; 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 and episodes for this TV show $tvShowModel = new TvShow($this->pdo, $tvShow); $seasons = $tvShowModel->getSeasonsWithEpisodes(); return $this->view->render($response, 'tvshows/show.twig', [ 'title' => $tvShow['title'], 'tvshow' => $tvShow, 'metadata' => $metadata, 'cast' => $cast, 'genre' => $genre, 'actors' => $actors, 'seasons' => $seasons ]); } }