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