mirror of
https://git.cant.at/Madeorsk/PollVerlaine
synced 2024-11-24 23:04:33 +01:00
Vote handling for form and JSON.
This commit is contained in:
parent
9c4add0217
commit
4ca767b730
42
index.php
42
index.php
@ -43,6 +43,48 @@ Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) {
|
|||||||
Flight::notFound();
|
Flight::notFound();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /polls/:id/vote
|
||||||
|
// Take an array of options and add a vote for each given option.
|
||||||
|
Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
|
||||||
|
$poll = Poll::load_poll($id);
|
||||||
|
if ($poll)
|
||||||
|
{
|
||||||
|
if (Flight::request()->type === "application/json")
|
||||||
|
{
|
||||||
|
if (isset(Flight::request()->data["options"]) && is_array(Flight::request()->data["options"]))
|
||||||
|
{ // Check that an options id array exists.
|
||||||
|
//TODO Check that only the authorized number of options are selected.
|
||||||
|
$poll->vote(Flight::request()->data["options"]); // Vote for the given options.
|
||||||
|
// Then save and show poll data.
|
||||||
|
$poll->save();
|
||||||
|
Flight::json(format_poll($poll));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (isset(Flight::request()->data["options"]))
|
||||||
|
{ // Check that any data has been sent.
|
||||||
|
$selected_options = Flight::request()->data["options"];
|
||||||
|
if (is_string($selected_options))
|
||||||
|
{ // If it is a string, input[type="radio"] were used so only one option is selected.
|
||||||
|
$poll->vote([intval($selected_options)]); // Vote for the selected option.
|
||||||
|
$poll->save();
|
||||||
|
Flight::redirect("/polls/$id/results"); // Redirect to the results.
|
||||||
|
} //TODO: Multiple options case.
|
||||||
|
else
|
||||||
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
||||||
|
//TODO Error code in query parameters?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Flight::notFound();
|
||||||
|
});
|
||||||
|
|
||||||
Flight::route("/", function () {
|
Flight::route("/", function () {
|
||||||
global $VERLAINE;
|
global $VERLAINE;
|
||||||
Flight::render("home", ["app_url" => $VERLAINE["app_url"]], "body_content");
|
Flight::render("home", ["app_url" => $VERLAINE["app_url"]], "body_content");
|
||||||
|
@ -14,15 +14,12 @@ class Poll
|
|||||||
$poll = new Poll();
|
$poll = new Poll();
|
||||||
$poll->title = $request_data->title;
|
$poll->title = $request_data->title;
|
||||||
$poll->creation_date = (new DateTime())->getTimestamp();
|
$poll->creation_date = (new DateTime())->getTimestamp();
|
||||||
$id = 0;
|
|
||||||
foreach ($request_data->options as $option)
|
foreach ($request_data->options as $option)
|
||||||
{
|
{
|
||||||
$poll->options[] = [
|
$poll->options[] = [
|
||||||
"id" => $id,
|
|
||||||
"label" => $option,
|
"label" => $option,
|
||||||
"votes" => 0,
|
"votes" => 0,
|
||||||
];
|
];
|
||||||
$id++;
|
|
||||||
}
|
}
|
||||||
$poll->gen_new_id();
|
$poll->gen_new_id();
|
||||||
$poll->save();
|
$poll->save();
|
||||||
@ -80,6 +77,18 @@ class Poll
|
|||||||
$this->id = $new_id;
|
$this->id = $new_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vote for a list of options.
|
||||||
|
* @param array $options - Array of integers containing voted options.
|
||||||
|
*/
|
||||||
|
public function vote(array $options)
|
||||||
|
{
|
||||||
|
// For each option in the list, add 1 to the vote number in the poll data.
|
||||||
|
foreach ($options as $option)
|
||||||
|
if (isset($this->options[intval($option)])) // Check invalid options id.
|
||||||
|
$this->options[intval($option)]->votes++;
|
||||||
|
}
|
||||||
|
|
||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
$db = dba_open(SAVE_PATH . "/polls.db", "wd");
|
$db = dba_open(SAVE_PATH . "/polls.db", "wd");
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<h1 class="poll"><?= $poll->title ?></h1>
|
<h1 class="poll"><?= $poll->title ?></h1>
|
||||||
<main>
|
<main>
|
||||||
<form action="#" id="poll">
|
<form action="/polls/<?= $poll->id ?>/vote" method="POST" id="poll">
|
||||||
<?php foreach ($poll->options as $option): ?>
|
<?php foreach ($poll->options as $id => $option): ?>
|
||||||
<div class="option">
|
<div class="option">
|
||||||
<input type="radio" name="options" value="<?= $option->id ?>" id="option-<?= $option->id ?>" />
|
<input type="radio" name="options" value="<?= $id ?>" id="option-<?= $id ?>" />
|
||||||
<label for="option-<?= $option->id ?>" class="check"></label>
|
<label for="option-<?= $id ?>" class="check"></label>
|
||||||
<label for="option-<?= $option->id ?>"><?= $option->label ?></label>
|
<label for="option-<?= $id ?>"><?= $option->label ?></label>
|
||||||
</div>
|
</div>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<input type="submit" value="Vote" />
|
<input type="submit" value="Vote" />
|
||||||
|
Loading…
Reference in New Issue
Block a user