Added footer, added radio buttons for single option polls, fixed voting bug

This commit is contained in:
aymm 2021-04-04 12:04:00 +02:00
parent 50750c6808
commit 44d0940e23
Signed by: phlaym
GPG Key ID: A06651BAB6777237
7 changed files with 61 additions and 16 deletions

View File

@ -1,4 +1,7 @@
{ {
"name": "hutattedonmyarm/dragonpolls",
"version": "0.1.0",
"description": "A polling client for pnut.io",
"require": { "require": {
"hutattedonmyarm/apnuti": "@dev" "hutattedonmyarm/apnuti": "@dev"
}, },

View File

@ -51,7 +51,18 @@ function get_page_header(
. $greeting . $greeting
. '<div class="spacer"></div>' . '<div class="spacer"></div>'
. $logout_link . $logout_link
. '</header>'; . '</header><main>';
}
function get_page_footer()
{
$version = json_decode(file_get_contents(__DIR__ . '/composer.json'), true)['version'];
return '</main><footer>'
. '<a href="https://phlaym.net/git/phlaym/Pfadlock/releases/tag/'.$version.'">Version ' . $version . '</a>'
. '<a href="https://phlaym.net/git/phlaym/Dragonpolls" title="Source"><div class="linkcontents">'
. file_get_contents(__DIR__.'/icons/home.svg')
. '<span class="linklabel">Source Code</span></div></a>'
. '</footer></body></html>';
} }
function redirect($to) function redirect($to)

View File

@ -3,3 +3,4 @@
require_once __DIR__ .'/bootstrap.php'; require_once __DIR__ .'/bootstrap.php';
echo get_page_header(); echo get_page_header();
echo get_page_footer();

View File

@ -3,7 +3,10 @@ window.addEventListener('DOMContentLoaded', () => {
for (const el of document.querySelectorAll('.option input[type=checkbox]')) { for (const el of document.querySelectorAll('.option input[type=checkbox]')) {
el.onclick = updateVotesRemaining; el.onclick = updateVotesRemaining;
} }
document.querySelector('.success-banner').onclick = hideBanner; const banner = document.querySelector('.success-banner');
if (banner) {
banner.onclick = hideBanner;
}
}); });
function displayLocalTimestamps() { function displayLocalTimestamps() {

View File

@ -29,6 +29,12 @@ body {
background: var(--main-bg-color); background: var(--main-bg-color);
color: var(--main-fg-color); color: var(--main-fg-color);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Cantarell, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Cantarell, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
display: flex;
flex-direction: column;
}
main {
flex-grow: 1;
} }
button { button {
@ -87,7 +93,7 @@ header {
align-items: center; align-items: center;
} }
header a { header a, footer a {
margin-right: 1em; margin-right: 1em;
} }
@ -174,4 +180,14 @@ datewrapper time {
.success-banner span { .success-banner span {
font-size: x-large; font-size: x-large;
}
/* Footer */
footer {
display: flex;
align-items: center;
padding: 8px;
background: var(--secondary-bg-color);
box-shadow: var(--default-shadow);
font-size: smaller;
} }

View File

@ -13,11 +13,11 @@ use APnutI\Entities\User;
try { try {
echo get_page_header('Poll', true, ['poll']); echo get_page_header('Poll', true, ['poll']);
} catch (\Exception $e) { } catch (\Exception $e) {
die('Something went wrong :( "'.$e->getMessage().'"'); die('Something went wrong :( "' . $e->getMessage() . '"' . get_page_footer());
} }
if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) { if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) {
die('Invalid poll ID'); die('Invalid poll ID'.get_page_footer());
} }
$poll_id = (int)$_GET['id']; $poll_id = (int)$_GET['id'];
$poll = null; $poll = null;
@ -26,9 +26,9 @@ try {
$poll_token = array_key_exists('polltoken', $_GET) ? $_GET['polltoken'] : null; $poll_token = array_key_exists('polltoken', $_GET) ? $_GET['polltoken'] : null;
$poll = $api->getPoll($poll_id, $poll_token); $poll = $api->getPoll($poll_id, $poll_token);
} catch (NotFoundException $nfe) { } catch (NotFoundException $nfe) {
die('Poll not found'); die('Poll not found'.get_page_footer());
} catch (NotSupportedPollException $nspe) { } catch (NotSupportedPollException $nspe) {
die('Sorry, this poll has a not yet supported type: ' . $nspe->getMessage()); die('Sorry, this poll has a not yet supported type: ' . $nspe->getMessage() . get_page_footer());
} catch (PollAccessRestrictedException $are) { } catch (PollAccessRestrictedException $are) {
$message = array_key_exists('polltoken', $_GET) $message = array_key_exists('polltoken', $_GET)
? 'Sorry, your poll token is invalid! Please enter a valid token: ' ? 'Sorry, your poll token is invalid! Please enter a valid token: '
@ -38,9 +38,10 @@ try {
$message $message
. '<form><input type="text" name="polltoken">' . '<form><input type="text" name="polltoken">'
. '<input type="hidden" name="id" value="'.$poll_id.'"><button type="submit">Access poll</button></form>' . '<input type="hidden" name="id" value="'.$poll_id.'"><button type="submit">Access poll</button></form>'
. get_page_footer()
); );
} catch (\Exception $e) { } catch (\Exception $e) {
die('Something went wrong :( "'.$e->getMessage().'"'); die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer());
} }
jslog($poll); jslog($poll);
@ -63,7 +64,7 @@ $disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled
if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?> if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?>
<div class="banner-wrapper"> <div class="banner-wrapper">
<div class="success-banner"><span></span> Your vote has been saved, thank you!</div> <div class="success-banner"><span></span> Your vote has been saved, thank you!</div>
</div> </div>
<?php } ?> <?php } ?>
<div class="poll"> <div class="poll">
@ -104,10 +105,15 @@ if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?>
'include_html' => false, 'include_html' => false,
'include_counts' => false, 'include_counts' => false,
]; ];
$input_type = $poll->max_options === 1 ? 'radio' : 'checkbox';
$input_name = $poll->max_options === 1 ? 'options' : 'options[]';
foreach ($poll->options as $option) { foreach ($poll->options as $option) {
$checked = $option->is_your_response ? 'checked' : ''; ?> $checked = $option->is_your_response ? 'checked' : ''; ?>
<div class="option" style="grid-row: <?= $row ?>;"> <div class="option" style="grid-row: <?= $row ?>;">
<input type="checkbox" <?= $checked.' '.$disabled ?> value="<?= $option->position ?>" name="options[]"/> <input
type="<?= $input_type ?>" <?= $checked.' '.$disabled ?>
value="<?= $option->position ?>"
name="<?= $input_name ?>"/>
<span class="option-text"><?= $option->text . ' (' . $option->respondents . ')'?></span> <span class="option-text"><?= $option->text . ' (' . $option->respondents . ')'?></span>
</div> </div>
<div class="option-responses" style="grid-row: <?= $row++ ?>;grid-column: 2;"> <div class="option-responses" style="grid-row: <?= $row++ ?>;grid-column: 2;">
@ -133,4 +139,4 @@ if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?>
</form> </form>
</div> </div>
</div> </div>
<?= get_page_footer(); ?>

View File

@ -2,16 +2,21 @@
require_once __DIR__ . '/bootstrap.php'; require_once __DIR__ . '/bootstrap.php';
$voted_options = []; $voted_options = [];
foreach ($_POST['options'] as $option) { if (is_array($_POST['options'])) {
$voted_options[] = (int)$option; foreach ($_POST['options'] as $option) {
$voted_options[] = (int)$option;
}
} else {
$voted_options[] = (int)$_POST['options'];
} }
try { try {
$res = $api->voteInPoll((int)$_POST['pollid'], $voted_options, $_POST['polltoken']); $res = $api->voteInPoll((int)$_POST['pollid'], $voted_options, $_POST['polltoken']);
} catch (\Exception $e) { } catch (\Exception $e) {
get_page_header('Voting error'); get_page_header('Voting error');
$str = 'Sorry, something went wrong while voting!' $str = 'Sorry, something went wrong while voting! "'
. 'Please yell at <a href="https://pnut.io/@hutattedonmyarm">@hutattedonmyarm></a><br>' . $e->getMessage()
. '<a href="view_poll.php?id="'.$_POST['pollid'].'>Go back to the poll</a>'; . '"<br>Please yell at <a href="https://pnut.io/@hutattedonmyarm">@hutattedonmyarm</a><br>'
. '<a href="view_poll.php?id='.$_POST['pollid'].'"s>Go back to the poll</a>';
die($str); die($str);
} }
redirect('view_poll.php?id='.$_POST['pollid'].'&success=1'); redirect('view_poll.php?id='.$_POST['pollid'].'&success=1');