mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
first commit
This commit is contained in:
95
app/Controllers/GameController.php
Normal file
95
app/Controllers/GameController.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use App\Models\Game;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
class GameController 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
|
||||
|
||||
// Get games with pagination and search
|
||||
$games = Game::getGroupedGamesWithPagination($this->pdo, $page, $perPage, $search);
|
||||
|
||||
// Get total count for pagination
|
||||
$totalCount = Game::getTotalCount($this->pdo, $search);
|
||||
|
||||
// Calculate pagination info
|
||||
$totalPages = ceil($totalCount / $perPage);
|
||||
$hasNextPage = $page < $totalPages;
|
||||
$hasPrevPage = $page > 1;
|
||||
|
||||
return $this->view->render($response, 'games/index.twig', [
|
||||
'title' => 'Games',
|
||||
'games' => $games,
|
||||
'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)
|
||||
{
|
||||
$gameKey = $args['game_key'];
|
||||
|
||||
// Find the main game entry (could be any platform version)
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT g.*, s.display_name as source_name
|
||||
FROM games g
|
||||
JOIN sources s ON g.source_id = s.id
|
||||
WHERE g.game_key = :game_key
|
||||
LIMIT 1
|
||||
");
|
||||
$stmt->execute(['game_key' => $gameKey]);
|
||||
$mainGame = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$mainGame) {
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
// Get all platform versions
|
||||
$gameModel = new Game($this->pdo);
|
||||
$gameModel->id = $mainGame['id'];
|
||||
$gameModel->game_key = $mainGame['game_key'];
|
||||
$platformVersions = $gameModel->getPlatformVersions();
|
||||
|
||||
return $this->view->render($response, 'games/show.twig', [
|
||||
'title' => $mainGame['title'],
|
||||
'main_game' => $mainGame,
|
||||
'platform_versions' => $platformVersions
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user