2021-03-11 16:51:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__ .'/bootstrap.php';
|
|
|
|
|
|
|
|
use APnutI\Exceptions\NotFoundException;
|
|
|
|
use APnutI\Exceptions\HttpPnutForbiddenException;
|
|
|
|
use APnutI\Exceptions\NotSupportedPollException;
|
2021-03-27 10:46:31 +00:00
|
|
|
use APnutI\Exceptions\NotAuthorizedException;
|
|
|
|
use APnutI\Exceptions\PollAccessRestrictedException;
|
2021-03-11 16:51:25 +00:00
|
|
|
use APnutI\Entities\Poll;
|
2021-04-04 07:44:33 +00:00
|
|
|
use APnutI\Entities\User;
|
2021-03-11 16:51:25 +00:00
|
|
|
|
2021-04-04 07:44:33 +00:00
|
|
|
try {
|
|
|
|
echo get_page_header('Poll', true, ['poll']);
|
|
|
|
} catch (\Exception $e) {
|
2021-04-04 10:04:00 +00:00
|
|
|
die('Something went wrong :( "' . $e->getMessage() . '"' . get_page_footer());
|
2021-04-04 07:44:33 +00:00
|
|
|
}
|
2021-03-27 19:37:17 +00:00
|
|
|
|
2021-03-11 16:51:25 +00:00
|
|
|
if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) {
|
2021-04-04 10:04:00 +00:00
|
|
|
die('Invalid poll ID'.get_page_footer());
|
2021-03-11 16:51:25 +00:00
|
|
|
}
|
|
|
|
$poll_id = (int)$_GET['id'];
|
|
|
|
$poll = null;
|
2021-03-27 10:46:31 +00:00
|
|
|
|
2021-03-11 16:51:25 +00:00
|
|
|
try {
|
2021-03-27 10:46:31 +00:00
|
|
|
$poll_token = array_key_exists('polltoken', $_GET) ? $_GET['polltoken'] : null;
|
|
|
|
$poll = $api->getPoll($poll_id, $poll_token);
|
2021-03-11 16:51:25 +00:00
|
|
|
} catch (NotFoundException $nfe) {
|
2021-04-04 10:04:00 +00:00
|
|
|
die('Poll not found'.get_page_footer());
|
2021-03-11 16:51:25 +00:00
|
|
|
} catch (NotSupportedPollException $nspe) {
|
2021-04-04 10:04:00 +00:00
|
|
|
die('Sorry, this poll has a not yet supported type: ' . $nspe->getMessage() . get_page_footer());
|
2021-03-27 10:46:31 +00:00
|
|
|
} catch (PollAccessRestrictedException $are) {
|
|
|
|
$message = array_key_exists('polltoken', $_GET)
|
|
|
|
? 'Sorry, your poll token is invalid! Please enter a valid token: '
|
|
|
|
: ('Sorry, this poll is private! If you have found this poll on a post, '
|
|
|
|
. 'please enter a link to the post, the post ID or the access token for the poll: ');
|
|
|
|
die(
|
|
|
|
$message
|
|
|
|
. '<form><input type="text" name="polltoken">'
|
|
|
|
. '<input type="hidden" name="id" value="'.$poll_id.'"><button type="submit">Access poll</button></form>'
|
2021-04-04 10:04:00 +00:00
|
|
|
. get_page_footer()
|
2021-03-27 10:46:31 +00:00
|
|
|
);
|
2021-04-04 07:44:33 +00:00
|
|
|
} catch (\Exception $e) {
|
2021-04-04 10:04:00 +00:00
|
|
|
die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer());
|
2021-03-11 16:51:25 +00:00
|
|
|
}
|
2021-04-04 07:44:33 +00:00
|
|
|
|
2021-03-11 16:51:25 +00:00
|
|
|
$user_avatar_url = $poll->user->getAvatarUrl(50);
|
2021-04-04 07:44:33 +00:00
|
|
|
$user_avatar_url_srcset = get_source_set($poll->user, 50);
|
|
|
|
|
2021-03-27 10:46:31 +00:00
|
|
|
$username = '@' . $poll->user->username;
|
|
|
|
$disabled = $poll->canVote() ? '' : 'disabled';
|
2021-03-27 19:37:17 +00:00
|
|
|
$user_name = $poll->user->name ?? '';
|
|
|
|
$created_at = $poll->created_at;
|
|
|
|
$closed_at = $poll->closed_at;
|
2021-03-28 11:15:54 +00:00
|
|
|
$user_votes = $poll->getMyVotes();
|
|
|
|
|
|
|
|
$votes_remaining = $poll->max_options - count($user_votes);
|
|
|
|
$votes_remaining_plural = $votes_remaining === 1 ? '' : 's';
|
|
|
|
$votes_remaining_text = "$votes_remaining Vote$votes_remaining_plural remaining";
|
|
|
|
$votes_remaining_hidden = $poll->canVote() ? '' : ' hidden';
|
|
|
|
$data_can_vote = $poll->canVote() ? 'true' : 'false';
|
|
|
|
$disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled';
|
|
|
|
|
2021-03-28 19:19:04 +00:00
|
|
|
if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?>
|
2021-04-02 08:11:10 +00:00
|
|
|
<div class="banner-wrapper">
|
2021-04-04 10:04:00 +00:00
|
|
|
<div class="success-banner"><span>✓</span> Your vote has been saved, thank you!</div>
|
2021-03-28 19:19:04 +00:00
|
|
|
</div>
|
2021-04-04 19:23:20 +00:00
|
|
|
<?php }
|
|
|
|
if (array_key_exists('poll_created', $_GET) && $_GET['poll_created'] == 1) { ?>
|
|
|
|
<div class="banner-wrapper">
|
2021-04-05 08:56:15 +00:00
|
|
|
<div class="success-banner"><span>✓</span> Your poll and post have been created, thank you!</div>
|
2021-04-04 19:23:20 +00:00
|
|
|
</div>
|
2021-03-28 19:19:04 +00:00
|
|
|
<?php } ?>
|
2021-03-27 10:46:31 +00:00
|
|
|
<div class="poll">
|
|
|
|
<div class="header">
|
2021-03-27 19:37:17 +00:00
|
|
|
<div class="user">
|
2021-04-04 07:44:33 +00:00
|
|
|
<img src="<?= $user_avatar_url ?>" class="avatar" srcset="<?= $user_avatar_url_srcset ?>"/>
|
2021-03-27 19:37:17 +00:00
|
|
|
<div class="usernames">
|
|
|
|
<b><?= $user_name.'<br>' ?></b>
|
2021-04-04 07:44:33 +00:00
|
|
|
<span class="username"><?= User::getProfileLinkForUsername($username) ?></span>
|
2021-03-27 19:37:17 +00:00
|
|
|
</div>
|
|
|
|
<div class="spacer"></div>
|
|
|
|
<div class="datewrapper">
|
|
|
|
<span class="created_at">Created</span>
|
|
|
|
<span class="closed_at"><?= $poll->isClosed() ? 'Closed' : 'Closing' ?></span>
|
|
|
|
<time class="created_at" datetime="<?= $created_at->format(\DateTime::ISO8601) ?>">
|
|
|
|
<?= $created_at->format('Y-m-d, H:i:s e') ?>
|
|
|
|
</time>
|
|
|
|
<time class="closed_at" datetime="<?= $closed_at->format(\DateTime::ISO8601) ?>">
|
|
|
|
<?= $closed_at->format('Y-m-d, H:i:s e') ?>
|
|
|
|
</time>
|
|
|
|
</div>
|
2021-03-27 10:46:31 +00:00
|
|
|
</div>
|
2021-03-28 11:15:54 +00:00
|
|
|
<span class="prompt">
|
|
|
|
<em><?= $poll->prompt ?></em><br>
|
|
|
|
<span
|
|
|
|
class="votes-remaining <?= $votes_remaining_hidden ?>"
|
|
|
|
data-max-votes="<?= $poll->max_options ?>"><?= $votes_remaining_text ?>
|
|
|
|
</span>
|
|
|
|
</span>
|
2021-03-27 19:37:17 +00:00
|
|
|
</div>
|
|
|
|
<div class="options">
|
2021-03-28 19:19:04 +00:00
|
|
|
<form method="POST" action="vote_poll.php">
|
|
|
|
<input type="hidden" name="pollid" value="<?= $poll->id ?>"/>
|
|
|
|
<input type="hidden" name="polltoken" value="<?= $poll->token ?>"/>
|
2021-03-27 19:37:17 +00:00
|
|
|
<?php
|
2021-03-27 20:15:07 +00:00
|
|
|
$row = 1;
|
|
|
|
$user_args = [
|
|
|
|
'include_html' => false,
|
|
|
|
'include_counts' => false,
|
|
|
|
];
|
2021-04-04 10:04:00 +00:00
|
|
|
$input_type = $poll->max_options === 1 ? 'radio' : 'checkbox';
|
|
|
|
$input_name = $poll->max_options === 1 ? 'options' : 'options[]';
|
2021-03-27 19:37:17 +00:00
|
|
|
foreach ($poll->options as $option) {
|
2021-04-10 12:24:09 +00:00
|
|
|
$checked = $option->is_your_response ? 'checked' : '';
|
|
|
|
$num_respondents_text = $option->respondents > 0 ? ' (' . $option->respondents . ')' : '';
|
|
|
|
?>
|
2021-03-27 20:15:07 +00:00
|
|
|
<div class="option" style="grid-row: <?= $row ?>;">
|
2021-04-04 10:04:00 +00:00
|
|
|
<input
|
2021-04-04 19:23:20 +00:00
|
|
|
type="<?= $input_type ?>" <?= $checked.' '.$disabled ?>
|
2021-04-04 10:04:00 +00:00
|
|
|
value="<?= $option->position ?>"
|
2021-04-04 19:23:20 +00:00
|
|
|
name="<?= $input_name ?>"/>
|
2021-04-10 12:24:09 +00:00
|
|
|
<span class="option-text"><?= $option->text . $num_respondents_text?></span>
|
2021-03-28 11:15:54 +00:00
|
|
|
</div>
|
|
|
|
<div class="option-responses" style="grid-row: <?= $row++ ?>;grid-column: 2;">
|
2021-03-27 20:15:07 +00:00
|
|
|
<?php foreach ($option->respondent_ids as $res_id) {
|
|
|
|
$u = $api->getUser($res_id, $user_args); ?>
|
2021-04-04 07:44:33 +00:00
|
|
|
<a href="https://pnut.io/@<?= $u->username ?>">
|
|
|
|
<img
|
|
|
|
src="<?= $u->getAvatarUrl(20) ?>"
|
|
|
|
srcset="<?= get_source_set($u, 20) ?>"
|
|
|
|
class="avatar"
|
|
|
|
title="@<?= $u->username ?>">
|
|
|
|
</a>
|
2021-03-27 20:15:07 +00:00
|
|
|
<?php } ?>
|
2021-03-28 11:15:54 +00:00
|
|
|
</div>
|
2021-03-27 19:37:17 +00:00
|
|
|
<?php } ?>
|
2021-03-28 11:15:54 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
name="submit_vote"
|
|
|
|
value="submit" <?= $disabled_button?>
|
|
|
|
data-can-vote="<?= $data_can_vote ?>">
|
2021-04-10 12:28:38 +00:00
|
|
|
<?= count($user_votes) > 0 ? 'Update' : '' ?> Vote
|
2021-03-28 11:15:54 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
2021-03-27 10:46:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-04 10:04:00 +00:00
|
|
|
<?= get_page_footer(); ?>
|