You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.8 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php /*
Copyright © 2021-2022 Pk11
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/ ?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>停留所のNexTrip情報</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
main {
max-width: 768px;
}
h2 {
border-bottom: 1px solid lightgray;
}
</style>
</head>
<body>
<?php
// このページのURL
$self = "https://nextrip.xn--rdk.xn--tckwe/";
// formの変数
$stop = $_GET['stop'];
$favSet = $_POST['fav-set'];
$favRemove = $_POST['fav-remove'];
?>
<header>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="<?php echo $self; ?>">停留所のNexTrip情報</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="https://gist.github.com/Epicpkmn11/6e60adbf3475277b753b259ab02a53cd" target="_blank">ソース</a>
</li>
</ul>
</div>
</nav>
</header>
<main class="mt-3 container-fluid">
<?php
if(isset($stop)) { // 停留所情報を表示
$directions = [
"NB" => "北行",
"EB" => "東行",
"SB" => "南行",
"WB" => "西行"
];
// APIデータを取得
$content = file_get_contents("https://svc.metrotransit.org/NexTrip/$stop?format=json");
$json = json_decode($content);
if(isset($json)) {
// 情報をプリント
echo "<h2>停留所{$stop}のNexTrip情報</h2>";
echo '<div class="row row-cols-2 row-cols-sm-4 row-cols-md-6">';
foreach($json as $bus) {
echo '<div class="col">';
echo "<p><strong>$bus->Route{$bus->Terminal}{$directions[$bus->RouteDirection]}</strong><br>";
echo "$bus->DepartureText</p>";
echo '</div>';
}
echo '</div>';
// お気に入りフォームをプリント
$favDesc = json_decode($_COOKIE['favorites'], true)[$stop];
echo "<form method=\"post\" action=\"$self\">";
echo "<input type=\"hidden\" name=\"fav-set\" value=\"$stop\">";
echo '<div class="input-group">';
echo '<label for="desc" class="input-group-text">お気に入り</label>';
echo "<input type=\"text\" id=\"desc\" name=\"desc\" class=\"form-control\" placeholder=\"説明\" value=\"$favDesc\" required>";
echo '<input type="submit" class="btn btn-outline-secondary" value="' . (isset($favDesc) ? '更新' : '追加') . '">';
echo '</div>';
echo '</form>';
} else {
echo "<div class=\"alert alert-danger\">{$stop}は無効な停留所です。</div>";
}
} else { // HPを表示
// 停留所フォームをプリント
echo "<form method=\"get\" action=\"$self\">";
echo '<div class="input-group mb-3">';
echo '<label for="stop" class="input-group-text">停留所</label>';
echo '<input type="number" min="0" id="stop" name="stop" class="form-control" placeholder="12345" required>';
echo '<input type="submit" class="btn btn-outline-secondary" value="検索">';
echo '</div>';
echo '</form>';
// お気に入り一覧
$favorites = isset($_COOKIE['favorites']) ? json_decode($_COOKIE['favorites'], true) : Array();
if(isset($favSet)) { // お気に入りに追加
$desc = $_POST['desc'];
$favorites[$favSet] = $desc;
ksort($favorites);
setcookie("favorites", json_encode($favorites), 2147483647);
echo "<div class=\"alert alert-info\">{$favSet}({$desc})は設定しました。</div>";
} else if(isset($favRemove)) { // お気に入りから削除
if(array_key_exists($favRemove, $favorites)) {
$desc = $favorites[$favRemove];
unset($favorites[$favRemove]);
setcookie("favorites", json_encode($favorites), 2147483647);
echo "<div class=\"alert alert-info\">{$favRemove}({$desc})は削除しました。</div>";
} else {
echo "<div class=\"alert alert-warning\">{$favRemove}はお気に入りにありません。</div>";
}
}
// お気に入りがある場合、プリント
if(count($favorites) > 0) {
echo '<h2>お気に入り</h2>';
echo "<form action=\"$self\">";
echo '<ul class="list-unstyled">';
foreach($favorites as $favStop => $favDesc) {
echo '<li class="mb-1 d-flex">';
echo "<button type=\"submit\" formmethod=\"get\" name=\"stop\" value=\"$favStop\" class=\"btn btn-sm btn-secondary flex-fill me-1 text-start\">$favStop ($favDesc)</button>";
echo "<button type=\"submit\" formmethod=\"post\" name=\"fav-remove\" value=\"$favStop\" aria-label=\"{$favStop}を削除\" class=\"btn btn-sm btn-danger\">×</button>";
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
}
?>
</main>
</body>
</html>