1
0
mirror of https://git.cant.at/Madeorsk/PollVerlaine synced 2024-11-22 08:04:32 +01:00

Subdirectory handling.

This commit is contained in:
Madeorsk 2018-08-26 12:04:29 +02:00
parent d9395a0eb2
commit 67efb77b0d
5 changed files with 18 additions and 13 deletions

View File

@ -4,6 +4,8 @@ require __DIR__ . "/src/models/Poll.php";
require __DIR__ . "/src/Format.php"; require __DIR__ . "/src/Format.php";
require __DIR__ . "/config/app.php"; require __DIR__ . "/config/app.php";
Flight::set("flight.base_url", $VERLAINE["app_url"]);
Flight::route("POST /polls", function () { Flight::route("POST /polls", function () {
$request = Flight::request(); $request = Flight::request();
if ($request->type === "application/json") if ($request->type === "application/json")
@ -20,6 +22,7 @@ Flight::route("POST /polls", function () {
}); });
Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) { Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) {
global $VERLAINE;
$poll = Poll::load_poll($id); $poll = Poll::load_poll($id);
if ($poll) if ($poll)
{ {
@ -32,7 +35,7 @@ Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) {
Flight::redirect("/polls/$id/results"); // A vote is already registered with this IP: redirect. Flight::redirect("/polls/$id/results"); // A vote is already registered with this IP: redirect.
else else
{ {
Flight::render("poll", ["poll" => $poll], "body_content"); Flight::render("poll", ["app_url" => $VERLAINE["app_url"], "poll" => $poll], "body_content");
Flight::render("layout"); Flight::render("layout");
} }
} }
@ -73,16 +76,16 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
if($poll->vote($selected_options)) // Vote for the selected option. if($poll->vote($selected_options)) // Vote for the selected option.
{ {
$poll->save(); $poll->save();
Flight::redirect("/polls/$id/results"); // Redirect to the results. Flight::redirect("/polls/$id/results", 301); // Redirect to the results.
} }
else else
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page.
} }
else else
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page.
} }
else else
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page.
//TODO Error code in query parameters? //TODO Error code in query parameters?
} }
} }
@ -125,7 +128,7 @@ Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function
if (Flight::request()->type === "application/json") if (Flight::request()->type === "application/json")
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>"); Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
else else
Flight::redirect('/'); Flight::redirect('/', 301);
} }
} }
else else

View File

@ -20,6 +20,7 @@ document.addEventListener("DOMContentLoaded", () => {
while(next_id < 4) create_choice(); while(next_id < 4) create_choice();
let form = document.getElementById("newpoll"); let form = document.getElementById("newpoll");
console.log(`${location.href}/polls`);
form.addEventListener("submit", (event) => { form.addEventListener("submit", (event) => {
event.preventDefault(); event.preventDefault();
@ -32,7 +33,7 @@ document.addEventListener("DOMContentLoaded", () => {
return choices; return choices;
} }
fetch("/polls", { fetch(`${location.href}${ location.href.endsWith("/") ? "" : "/" }polls`, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
title: form.querySelector(`input[name="title"]`).value, title: form.querySelector(`input[name="title"]`).value,

View File

@ -1,6 +1,6 @@
<body class="home"> <body class="home">
<script src="/static/js/fetch.min.js"></script> <script src="<?= $app_url ?>/static/js/fetch.min.js"></script>
<script src="/static/js/new.js"></script> <script src="<?= $app_url ?>/static/js/new.js"></script>
<h1>Poll Verlaine</h1> <h1>Poll Verlaine</h1>
<main> <main>
@ -28,7 +28,7 @@
<input type="text" id="pollurl" name="pollurl" value="<?= $app_url ?>:poll_url" /> <input type="text" id="pollurl" name="pollurl" value="<?= $app_url ?>:poll_url" />
<label for="deleteurl">Delete URL</label> <label for="deleteurl">Delete URL</label>
<input type="text" id="deleteurl" name="deleteurl" value="<?= $app_url ?>:delete_url" /> <input type="text" id="deleteurl" name="deleteurl" value="<?= $app_url ?>:delete_url" />
<a class="button" href=":poll_url">See the poll!</a> <a class="button" href="<?= $app_url ?>:poll_url">See the poll!</a>
</div> </div>
</main> </main>

View File

@ -1,10 +1,11 @@
<?php require __DIR__ . "/../config/app.php"; ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Poll Verlaine</title> <title>Poll Verlaine</title>
<link rel="stylesheet" type="text/css" href="/static/css/main.css" /> <link rel="stylesheet" type="text/css" href="<?= $VERLAINE["app_url"] ?>/static/css/main.css" />
</head> </head>
<body> <body>
<?= $body_content ?> <?= $body_content ?>

View File

@ -1,6 +1,6 @@
<h1 class="poll"><?= $poll->title ?></h1> <h1 class="poll"><?= $poll->title ?></h1>
<main> <main>
<form action="/polls/<?= $poll->id ?>/vote" method="POST" id="poll"> <form action="<?= $app_url ?>/polls/<?= $poll->id ?>/vote" method="POST" id="poll">
<?php foreach ($poll->options as $id => $option): ?> <?php foreach ($poll->options as $id => $option): ?>
<div class="option"> <div class="option">
<input type="<?= $poll->settings->multiple_choices ? "checkbox" : "radio" ?>" name="options[]" value="<?= $id ?>" id="option-<?= $id ?>" /> <input type="<?= $poll->settings->multiple_choices ? "checkbox" : "radio" ?>" name="options[]" value="<?= $id ?>" id="option-<?= $id ?>" />
@ -10,5 +10,5 @@
<?php endforeach; ?> <?php endforeach; ?>
<input type="submit" value="Vote" /> <input type="submit" value="Vote" />
</form> </form>
<a class="button margin" href="/polls/<?= $poll->id ?>/results">Jump to results</a> <a class="button margin" href="<?= $app_url ?>/polls/<?= $poll->id ?>/results">Jump to results</a>
</main> </main>