flash.moe/public/collection/games.php
2021-07-31 04:39:10 +00:00

116 lines
3.6 KiB
PHP

<?php
require_once '_inc.php';
$sysId = filter_input(INPUT_GET, 'sys', FILTER_SANITIZE_NUMBER_INT);
$showAllGames = $sysId === null;
$getSystem = $pdo->prepare('
SELECT s.*, (
SELECT COUNT(`game_id`)
FROM `col_games`
WHERE `sys_id` = s.`sys_id`
) AS `sys_games`,
(
SELECT COUNT(`game_id`)
FROM `col_games_ownership`
WHERE `game_id` IN (
SELECT `game_id`
FROM `col_games`
WHERE `sys_id` = s.`sys_id`
)
) AS `sys_games_owned`
FROM `col_systems` AS s
WHERE `sys_id` = :sys
');
if(!$showAllGames) {
$getSystem->bindValue('sys', $sysId);
$getSystem->execute();
$system = $getSystem->fetch(PDO::FETCH_OBJ);
if(empty($system)) {
http_response_code(404);
die('No such system.');
}
}
$getGames = $pdo->prepare(sprintf(
'
SELECT g.*, (
SELECT COUNT(`game_id`) > 0
FROM `col_games_ownership`
WHERE `game_id` = g.`game_id`
) AS `game_owned`
FROM `col_games` AS g
%s
ORDER BY %s`game_owned` DESC, g.`sys_id`, COALESCE(`game_title_order`, `game_title`)
',
$showAllGames ? '' : 'WHERE `sys_id` = :sys',
$showAllGames ? '(
SELECT COUNT(`game_id`)
FROM `col_games`
WHERE `sys_id` = g.`sys_id`
) DESC, ' : ''
));
if(!$showAllGames)
$getGames->bindValue('sys', $sysId);
$getGames->execute();
$games = $getGames->fetchAll(PDO::FETCH_OBJ);
$title = $showAllGames
? 'All Games'
: $system->sys_name;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title><?=$title;?> - Game Collection</title>
<link href="assets/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="wrapper">
<h1><a href="./">Game Collection</a></h1>
<?php
foreach($games as $game):
if(empty($system->sys_id) || $system->sys_id !== $game->sys_id || (!$showAllGames && empty($headerPrinted))):
$headerPrinted = true;
if($showAllGames) {
$getSystem->bindValue('sys', $game->sys_id);
$getSystem->execute();
}
if($showAllGames) {
if(isset($system)):
?>
</div>
<?php
endif;
$system = $getSystem->fetch(PDO::FETCH_OBJ);
}
?>
<div class="syshead" id="<?=$system->sys_short;?>">
<div class="syshead-pic">
<img src="<?=sprintf('assets/systems/%s.jpg', $system->sys_short);?>" alt=""/>
</div>
<div class="syshead-cont">
<h2><a href="?sys=<?=$system->sys_id;?>"><?=$system->sys_name;?></a></h2>
<p><?=number_format($system->sys_games_owned);?> of <?=number_format($system->sys_games);?> game<?=($system->sys_games !== 1 ? 's' : '');?></p>
</div>
</div>
<div class="gamelist">
<?php endif; ?>
<div class="gamebox gamebox--<?=$system->sys_short;?><?php if(!$game->game_owned) { ?> gamebox--missing<?php } ?>">
<img src="<?=sprintf('assets/games/%s/%s.jpg', $system->sys_short, $game->game_short);?>" alt="" class="gamebox-art"/>
<a class="gamebox-link" href="<?=sprintf('game.php?id=%d', $game->game_id);?>"></a>
<div class="gamebox-cont">
<h3><?=$game->game_title;?></h3>
<p><?=$game->game_region;?></p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</body>
</html>