Fixed #1 Added posting about new polls

This commit is contained in:
aymm 2021-04-05 10:56:15 +02:00
parent 67283a9474
commit f94ee6b5e1
Signed by: phlaym
GPG Key ID: A06651BAB6777237
6 changed files with 97 additions and 7 deletions

View File

@ -9,6 +9,10 @@
{ {
"type": "path", "type": "path",
"url": "../APnutI" "url": "../APnutI"
},
{
"type": "vcs",
"url": "git@phlaym.net:phlaym/APnutI.git"
} }
] ]
} }

4
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "225cc22652f4252bb94d4e0317ca2af6", "content-hash": "6a288e0d98c0c9d9a6ab51583c757345",
"packages": [ "packages": [
{ {
"name": "hutattedonmyarm/apnuti", "name": "hutattedonmyarm/apnuti",
@ -12,7 +12,7 @@
"dist": { "dist": {
"type": "path", "type": "path",
"url": "../APnutI", "url": "../APnutI",
"reference": "9f0c8ba3c6bda4f58dd6dcd488172cb354c15745" "reference": "2b45c9fdd0df6a6b6186c1afb1075ae5a0b69bbe"
}, },
"require": { "require": {
"ext-curl": "*", "ext-curl": "*",

View File

@ -32,7 +32,7 @@ if (!empty($_POST['submit'])) {
$duration_total_minutes = $duration_days*60*24 + $duration_hours * 60 + $duration_minutes; $duration_total_minutes = $duration_days*60*24 + $duration_hours * 60 + $duration_minutes;
try { try {
$poll = Poll::create($api, $prompt, $options, $max_options, $duration_total_minutes, $is_anonymous, $is_public); $poll = Poll::create($api, $prompt, $options, $max_options, $duration_total_minutes, $is_anonymous, $is_public);
redirect('view_poll.php?poll_created=1&id='.$poll->id); #TODO: Add posting about poll after creation redirect('post_poll.php?poll_token='.$poll->token.'&id='.$poll->id.'&prompt='.urlencode($prompt));
} catch (\Exception $e) { } catch (\Exception $e) {
die('Something went wrong creating the poll: "' . $e->getMessage() . '"' . get_page_footer()); die('Something went wrong creating the poll: "' . $e->getMessage() . '"' . get_page_footer());
} }

83
post_poll.php Normal file
View File

@ -0,0 +1,83 @@
<?php
require_once __DIR__ .'/bootstrap.php';
use APnutI\Entities\Poll;
try {
echo get_page_header('Post Poll', true, []);
} catch (\Exception $e) {
die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer());
}
if (!$api->isAuthenticated(false, true)) {
die('You need to be logged in to create a new post!' . get_page_footer());
}
if (!empty($_POST['submit'])) {
if (empty($_POST['poll_id']) || !is_numeric($_POST['poll_id']) || $_POST['poll_id'] <= 0) {
die('Invalid poll ID'.get_page_footer());
}
if (empty($_POST['poll_token'])) {
die('Invalid poll token'.get_page_footer());
}
if (empty($_POST['post_text'])) {
die('Invalid text'.get_page_footer());
}
try {
$params = [
'raw' => Poll::makePollNoticeRaw($_POST['poll_id'], $_POST['poll_token'])
];
$api->createPostWithParameters($_POST['post_text'], $params);
redirect('view_poll.php?poll_created=1&id=' . $_POST['poll_id']);
} catch (\Exception $e) {
die('Something went wrong creating your post: "' . $e->getMessage() . '"'.get_page_footer());
}
}
if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) {
die('Invalid poll ID'.get_page_footer());
}
if (empty($_GET['poll_token'])) {
die('Invalid poll token'.get_page_footer());
}
if (empty($_GET['prompt'])) {
die('Invalid prompt'.get_page_footer());
}
$poll_id = (int)$_GET['id'];
$poll_token = $_GET['poll_token'];
$prompt = $_GET['prompt'];
$dir_name = dirname($_SERVER['SCRIPT_NAME']);
if ($dir_name === '.' || $dir_name === '/') {
$dir_name = '';
}
$scheme = empty($_SERVER['REQUEST_SCHEME']) ? 'http' : $_SERVER['REQUEST_SCHEME'];
$url = $scheme
. '://'
. $_SERVER['HTTP_HOST']
. $dir_name
. '/view_poll.php?id='
. $poll_id;
?>
Do you want to post about your poll?
<form method="POST" class="post-poll">
<textarea rows="4" cols="50" name="post_text" maxlength="256">I created a new poll:
<?= $prompt ?>
<?= $url ?>
</textarea><br>
<input type="hidden" name="poll_id" value="<?= $poll_id ?>">
<input type="hidden" name="poll_token" value="<?= $poll_token ?>">
<button type="submit" name="submit" value="submit">Post to pnut</button>
</form>
<a href="/view_poll.php?id=<?= $poll_id ?>">Take me straight to the poll</a>
<p>
Note, that if your poll is set to private, you will either need to share your poll with a post,
or give the poll's access token to everyone who should be able to vote in your poll. Your access token is:
<pre><?= $poll_token ?></pre>
</p>
<?= get_page_footer() ?>

View File

@ -7,6 +7,7 @@
--disabled-color: gray; --disabled-color: gray;
--green: green; --green: green;
--default-shadow: 3px 3px 10px 1px var(--secondary-bg-color); --default-shadow: 3px 3px 10px 1px var(--secondary-bg-color);
--error-color: rgba(255, 0, 0, 0.3);
} }
@supports (color: color(display-p3 1 1 1)) { @supports (color: color(display-p3 1 1 1)) {
:root { :root {
@ -236,15 +237,18 @@ datewrapper time {
.create-poll button[type=submit] { .create-poll button[type=submit] {
grid-row: 8; grid-row: 8;
} }
.create-poll .error { .create-poll .error {
grid-column: 1/3; grid-column: 1/3;
grid-row: 7; grid-row: 7;
background-color: rgba(255, 0, 0, 0.3); background-color: var(--error-color);
} }
.create-poll .error:not(:empty) { .create-poll .error:not(:empty) {
margin: 8px; margin: 8px;
} }
.post-poll textarea {
background-color: var(--secondary-bg-color);
color: var(--main-text-color);
}
/* Footer */ /* Footer */
footer { footer {

View File

@ -43,7 +43,6 @@ try {
} catch (\Exception $e) { } catch (\Exception $e) {
die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer()); die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer());
} }
jslog($poll);
$user_avatar_url = $poll->user->getAvatarUrl(50); $user_avatar_url = $poll->user->getAvatarUrl(50);
$user_avatar_url_srcset = get_source_set($poll->user, 50); $user_avatar_url_srcset = get_source_set($poll->user, 50);
@ -69,7 +68,7 @@ if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?>
<?php } <?php }
if (array_key_exists('poll_created', $_GET) && $_GET['poll_created'] == 1) { ?> if (array_key_exists('poll_created', $_GET) && $_GET['poll_created'] == 1) { ?>
<div class="banner-wrapper"> <div class="banner-wrapper">
<div class="success-banner"><span></span> Your poll has been created, thank you!</div> <div class="success-banner"><span></span> Your poll and post have been created, thank you!</div>
</div> </div>
<?php } ?> <?php } ?>
<div class="poll"> <div class="poll">