personal-website/docs/www/blog/blogpostcomment.php

151 lines
4.3 KiB
PHP
Raw Normal View History

2024-07-23 23:18:32 +02:00
<?php
class BlogpostComment
{
public $comment_id;
public $blogpost_id;
public $blogpost_addr;
public $poster_id;
public $poster_name;
public $parent_id;
public $timestamp;
public $content;
public $children;
/**
* Display the comment, and recursively it's children
*/
public function display_comment() {
$body = sprintf("
<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++)
{
$child = $this->children[$i];
$body .= $child->display_comment();
}
}
return $body."</div></div>";
}
/**
* Load the child comments to this comment, and recursively their children.
*/
public function load_children($conn) {
// 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 AND
parent_id = :comment_id ORDER BY timestamp ASC;");
// Bind and execute the comment select
$stmt->bindParam(":blogpost_id", $this->blogpost_id);
$stmt->bindParam(":comment_id", $this->comment_id);
$stmt->execute();
// Fetch the comments
$results_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
$comments_arr = [];
// Prepare comment author selection statement
$stmt = $conn->prepare("SELECT username FROM users WHERE
user_id = :user_id;");
// Recursively fetch all the child comments
for($i = 0; $i < count($results_arr); $i++) {
$com = $results_arr[$i];
// If comment has a registered author, fetch their name
if($com["poster_id"]) {
$stmt->bindParam(":user_id", $com["poster_id"]);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// If user was erased from database, set name to [Deleted]
if(!$result) {
$username = "[Deleted]";
}
else {
$username = $result["username"];
}
}
else {
$username = "[Guest]";
}
$commentObj = new BlogpostComment($com["comment_id"],
$com["poster_id"], $username, $this->blogpost_id,
$this->blogpost_addr, $com["timestamp"], $com["content"],
$this->comment_id);
$comments_arr[] = $commentObj;
$commentObj->load_children($conn);
}
$this->children = $comments_arr;
}
/**
* Constructor for the BlogpostComment object.
* $comment_id GUID of the comment.
* $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, $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;
$this->content = $content;
$this->parent_id = $parent_id;
}
}
?>