Commenting uses address, iff available.

This commit is contained in:
Zdenek Borovec 2024-07-20 13:47:48 +02:00
parent b04d38c9e9
commit 08c2c31644

View file

@ -7,6 +7,7 @@ class BlogpostComment
{
public $comment_id;
public $blogpost_id;
public $blogpost_addr;
public $poster_id;
public $poster_name;
public $parent_id;
@ -18,6 +19,7 @@ class BlogpostComment
* Display the comment, and recursively it's children
*/
public function display_comment() {
if(is_null($this->blogpost_addr)) {
printf("
<div class=\"comment\" id=\"comment-%s\">
<div class=\"comment-own-wrapper\">
@ -34,17 +36,14 @@ class BlogpostComment
</div>
<div class=\"comment-response\">
<form method=\"post\" action=\"%s\">
<input type=\"hidden\" name=\"blogpost_id\"
value=\"%s\">
<input type=\"hidden\" name=\"comment_id\"
value=\"%s\">
<input type=\"hidden\" name=\"blogpost_id\" value=\"%s\">
<input type=\"hidden\" name=\"comment_id\" value=\"%s\">
<label for=\"comment_entry\">Write response:</label>
<div class=\"centered-container\">
<textarea name=\"comment_entry\"
class=\"comment-box\"></textarea>
</div>
<input name=\"submit\" type=\"submit\"
value=\"Send\">
<input name=\"submit\" type=\"submit\" value=\"Send\">
</form>
</div>
</div>
@ -59,6 +58,49 @@ class BlogpostComment
htmlspecialchars($_SERVER["PHP_SELF"]),
$this->blogpost_id,
$this->comment_id);
}
else {
printf("
<div class=\"comment\" id=\"comment-%s\">
<div class=\"comment-own-wrapper\">
<span class=\"comment-author\"> By: %s </span>
<span class=\"comment-date\"> On: %s </span>
<label for=\"reveal-response-%s\" class=\"checkbox-button\">
Respond
</label>
<input type=\"checkbox\" id=\"reveal-response-%s\"
style=\"display: none;\">
<hr>
<div class=\"comment-content\">
%s
</div>
<div class=\"comment-response\">
<form method=\"post\" action=\"%s\">
<input type=\"hidden\" name=\"blogpost_id\" value=\"%s\">
<input type=\"hidden\" name=\"address\" value=\"%s\">
<input type=\"hidden\" name=\"comment_id\" value=\"%s\">
<label for=\"comment_entry\">Write response:</label>
<div class=\"centered-container\">
<textarea name=\"comment_entry\"
class=\"comment-box\"></textarea>
</div>
<input name=\"submit\" type=\"submit\" value=\"Send\">
</form>
</div>
</div>
<div class=\"comment-child-wrapper\">
",
$this->comment_id,
$this->poster_name,
date("Y-m-d H:i", strtotime($this->timestamp)),
$this->comment_id,
$this->comment_id,
$this->content,
htmlspecialchars($_SERVER["PHP_SELF"]),
$this->blogpost_id,
$this->blogpost_addr,
$this->comment_id);
}
if($this->children != null) {
for($i = 0; $i < count($this->children); $i++)
@ -118,7 +160,8 @@ class BlogpostComment
$commentObj = new BlogpostComment($com["comment_id"],
$com["poster_id"], $username, $this->blogpost_id,
$com["timestamp"], $com["content"], $this->comment_id);
$this->blogpost_addr, $com["timestamp"], $com["content"],
$this->comment_id);
$comments_arr[] = $commentObj;
$commentObj->load_children($conn);
}
@ -132,14 +175,17 @@ class BlogpostComment
* $poster_id GUID of the comment author.
* $poster_name Name of the comment author.
* $blogpost_id GUID of the blogpost this comment is under.
* $blogpost_addr Human-readable address of the blogpost this
comment is under.
* $timestamp Timestamp at comment creation.
* $content Content of the comment.
* $parent_id GUID of the comment this is a reply to (or NULL).
*/
public function __construct($comment_id, $poster_id, $poster_name,
$blogpost_id, $timestamp, $content, $parent_id) {
$blogpost_id, $blogpost_address, $timestamp, $content, $parent_id) {
$this->comment_id = $comment_id;
$this->blogpost_id = $blogpost_id;
$this->blogpost_addr = $blogpost_address;
$this->poster_id = $poster_id;
$this->poster_name = $poster_name;
$this->timestamp = $timestamp;
@ -152,6 +198,7 @@ class BlogpostComment
class Blogpost
{
public $blogpost_id;
public $address;
public $title;
public $content;
public $date_posted;
@ -228,6 +275,7 @@ class Blogpost
/**
* Constructor for the blogpost.
* $blogpost_id GUID of the blogpost in the database.
* $address Readable address of the blogpost.
* $title Title of the blogpost.
* $content Content of the blogpost article.
* $date_posted Timestamp at publishing of article.
@ -236,9 +284,10 @@ class Blogpost
* $comments Array of Blogpostcomment objects,
* the comments of this article.
*/
public function __construct($blogpost_id, $title, $content,
$date_posted, $date_edited, $tags, $comments){
public function __construct($blogpost_id, $address, $title,
$content, $date_posted, $date_edited, $tags, $comments){
$this->blogpost_id = $blogpost_id;
$this->address = $address;
$this->title = $title;
$this->content = $content;
$this->date_posted = $date_posted;
@ -291,7 +340,7 @@ function send_comment($conn, $blogId, $posterId, $content, $parentId) {
* Load comments under a given blog.
* Returns array of BlogpostComment objects.
*/
function load_comments($conn, $blogId) {
function load_comments($conn, $blogId, $blogAddress) {
// Prepare new statement for selecting all the child comments.
$stmt = $conn->prepare("SELECT comment_id, poster_id, timestamp,
content FROM blogpost_comments WHERE blogpost_id = :blogpost_id
@ -334,7 +383,8 @@ function load_comments($conn, $blogId) {
}
$commentObj = new BlogpostComment($com["comment_id"], $com["poster_id"],
$username, $blogId, $com["timestamp"], $com["content"], NULL);
$username, $blogId, $blogAddress, $com["timestamp"],
$com["content"], NULL);
$commentObj->load_children($conn);
$comments_arr[] = $commentObj;
}
@ -348,8 +398,9 @@ function load_comments($conn, $blogId) {
*/
function load_blog($conn, $blogId){
// Prepare and bind statement for gathering blogpost info
$stmt = $conn->prepare("SELECT title, content, date_posted, date_edited
FROM blogposts WHERE blogpost_id = :blogpost_id;");
$stmt = $conn->prepare("SELECT readable_address, title, content,
date_posted, date_edited FROM blogposts WHERE
blogpost_id = :blogpost_id;");
$stmt->bindParam(":blogpost_id", $blogId);
// Execute the statement
@ -379,13 +430,14 @@ function load_blog($conn, $blogId){
// Set the variables
$blogTitle = $result["title"];
$blogAddress = $result["readable_address"];
$blogContent = $result["content"];
$datePosted = $result["date_posted"];
$dateEdited = $result["date_edited"];
$tags = $tags_arr;
$comments = load_comments($conn, $blogId);
$comments = load_comments($conn, $blogId, $blogAddress);
return new Blogpost($blogId, $blogTitle, $blogContent,
return new Blogpost($blogId, $blogAddress, $blogTitle, $blogContent,
$datePosted, $dateEdited, $tags, $comments);
}
@ -407,14 +459,22 @@ if(isset($_POST["submit"])) {
$parentId = isset($_POST["comment_id"]) ? $_POST["comment_id"] : "NULL";
$posterId = isset($_SESSION["current_user"]) ?
$_SESSION["current_user"]->user_id : "NULL";
$address = isset($_POST["address"]) ?
sanitize_input($_POST["address"]) : NULL;
// Try to send the comment
$commentId = send_comment($conn, $blogId, $posterId,
$commentContent, $parentId);
// Redirect to this page with GET
if(is_null($address)) {
header("Location: http://www.zdenekborovec-dev.cz/blog/".
"article?blogpost_id=".$blogId."#comment-".$commentId);
}
else {
header("Location: http://www.zdenekborovec-dev.cz/blog/".
"article?address=".$address."#comment-".$commentId);
}
die();
}
@ -462,6 +522,8 @@ $blogPost->display_article();
printf("<hr style=\"border-style: solid;\">");
// Display post comment form.
if(is_null($blogPost->address))
{
printf("
<article>
<h2> Comments: </h2>
@ -472,14 +534,37 @@ printf("
<textarea name=\"comment_entry\" class=\"comment-box\"
tabindex=\"1\"></textarea>
</div>
<input name=\"submit\" type=\"submit\" tabindex=\"2\" value=\"Send\">
<input name=\"submit\" type=\"submit\" tabindex=\"2\"
value=\"Send\">
</form>
</article>
",
htmlspecialchars($_SERVER["PHP_SELF"]), $blogId,
isset($_SESSION["current_user"]) ? $_SESSION["current_user"]->user_name :
"Guest");
isset($_SESSION["current_user"]) ?
$_SESSION["current_user"]->user_name : "Guest");
}
else
{
printf("
<article>
<h2> Comments: </h2>
<form method=\"post\" action=\"%s\">
<input type=\"hidden\" name=\"blogpost_id\" value=\"%s\">
<input type=\"hidden\" name=\"address\" value=\"%s\">
<label for=\"comment_entry\">Write a comment (%s):</label>
<div class=\"centered-container\">
<textarea name=\"comment_entry\" class=\"comment-box\"
tabindex=\"1\"></textarea>
</div>
<input name=\"submit\" type=\"submit\" tabindex=\"2\"
value=\"Send\">
</form>
</article>
",
htmlspecialchars($_SERVER["PHP_SELF"]), $blogId, $blogPost->address,
isset($_SESSION["current_user"]) ?
$_SESSION["current_user"]->user_name : "Guest");
}
// Display the blog comments
$blogPost->display_comments();