added comment rendering
This commit is contained in:
parent
0a931418b7
commit
14521fccd8
2 changed files with 183 additions and 49 deletions
|
@ -256,3 +256,31 @@ code {
|
|||
.blog-metadata .blog-publish-date {
|
||||
float: right;
|
||||
}
|
||||
|
||||
.comment .comment {
|
||||
margin-left: 0.5em;
|
||||
}
|
||||
|
||||
.comment .comment-own-wrapper{
|
||||
background-image: url("http://assets.zdenekborovec-dev.cz/common/planks_dark_tile.png");
|
||||
background-repeat: repeat;
|
||||
background-color: black;
|
||||
color: white;
|
||||
border: 1px solid black;
|
||||
}
|
||||
|
||||
.comment .comment-child-wrapper{
|
||||
border-left: 1px solid black;
|
||||
}
|
||||
|
||||
.comment .comment .comment-own-wrapper {
|
||||
border-top: 0px;
|
||||
}
|
||||
|
||||
.comment .comment-metadata, .comment .comment-content {
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
.comment-metadata .comment-date {
|
||||
float: right;
|
||||
}
|
||||
|
|
|
@ -7,11 +7,98 @@ include_once($COMMONS."/utils.php");
|
|||
class BlogpostComment
|
||||
{
|
||||
public $comment_id;
|
||||
public $parent_id;
|
||||
public $blogpost_id;
|
||||
public $poster_id;
|
||||
public $poster_name;
|
||||
public $timestamp;
|
||||
public $content;
|
||||
public $children;
|
||||
|
||||
/**
|
||||
* Display the comment, and recursively it's children
|
||||
*/
|
||||
public function display_comment() {
|
||||
printf("
|
||||
<div class=\"comment\">
|
||||
<div class=\"comment-own-wrapper\">
|
||||
<div class=\"comment-metadata\">
|
||||
<span>
|
||||
<bold>By: %s</bold>
|
||||
</span>
|
||||
<span class=\"comment-date\">
|
||||
On: %s
|
||||
</span>
|
||||
</div>
|
||||
<hr>
|
||||
<div class=\"comment-content\">
|
||||
%s
|
||||
</div>
|
||||
</div>
|
||||
<div class=\"comment-child-wrapper\">
|
||||
", $this->poster_name, date("Y-m-d", strtotime($this->timestamp)),
|
||||
$this->content);
|
||||
|
||||
if($this->children != null) {
|
||||
for($i = 0; $i < count($this->children); $i++)
|
||||
{
|
||||
$child = $this->children[$i];
|
||||
$child->display_comment();
|
||||
}
|
||||
}
|
||||
printf("</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, username FROM (blogpost_comments INNER JOIN users ON
|
||||
poster_id = user_id) WHERE blogpost_id = :blogpost_id
|
||||
AND parent_id = :comment_id;");
|
||||
|
||||
// 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 = [];
|
||||
|
||||
// Recursively fetch all the child comments
|
||||
for($i = 0; $i < count($results_arr); $i++) {
|
||||
$com = $results_arr[$i];
|
||||
$commentObj = new BlogpostComment($com["comment_id"],
|
||||
$com["poster_id"], $com["username"], $this->blogpost_id,
|
||||
$com["timestamp"], $com["content"]);
|
||||
$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.
|
||||
* $timestamp Timestamp at comment creation.
|
||||
* $content Content of the comment.
|
||||
*/
|
||||
public function __construct($comment_id, $poster_id, $poster_name,
|
||||
$blogpost_id, $timestamp, $content) {
|
||||
$this->comment_id = $comment_id;
|
||||
$this->blogpost_id = $blogpost_id;
|
||||
$this->poster_id = $poster_id;
|
||||
$this->poster_name = $poster_name;
|
||||
$this->timestamp = $timestamp;
|
||||
$this->content = $content;
|
||||
//printf("New comment %s<br> %s<br> %s<br> %s<br> %s<br> %s<br><hr>", $comment_id, $poster_id, $poster_name, $blogpost_id, $timestamp, $content);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -22,11 +109,12 @@ class Blogpost
|
|||
public $content;
|
||||
public $date_posted;
|
||||
public $tags;
|
||||
public $comments;
|
||||
|
||||
/**
|
||||
* Display the article, or a warning message.
|
||||
*/
|
||||
function display_article(){
|
||||
public function display_article(){
|
||||
// If a blog with given ID was not found display warning message.
|
||||
if(!$this->title){
|
||||
printf("
|
||||
|
@ -67,6 +155,17 @@ class Blogpost
|
|||
printf("</article><hr><article>%s</article>", $this->content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the comments for this post and their children.
|
||||
*/
|
||||
public function display_comments(){
|
||||
printf("<article><h2>Comments:</h2>");
|
||||
for($i = 0; $i < count($this->comments); $i++){
|
||||
$this->comments[$i]->display_comment();
|
||||
}
|
||||
printf("</article>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the blogpost.
|
||||
* $blogpost_id GUID of the blogpost in the database.
|
||||
|
@ -74,34 +173,56 @@ class Blogpost
|
|||
* $content Content of the blogpost article.
|
||||
* $date_posted Timestamp at publishing of article.
|
||||
* $tags Array of the tags this article has.
|
||||
* $comments Array of Blogpostcomment objects,
|
||||
* the comments of this article.
|
||||
*/
|
||||
public function __construct($blogpost_id, $title, $content,
|
||||
$date_posted, $tags){
|
||||
$date_posted, $tags, $comments){
|
||||
$this->blogpost_id = $blogpost_id;
|
||||
$this->title = $title;
|
||||
$this->content = $content;
|
||||
$this->date_posted = $date_posted;
|
||||
$this->tags = $tags;
|
||||
$this->comments = $comments;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to load info about the blog with guid in GET and set global
|
||||
* variables accordingly.
|
||||
* Load comments under a given blog.
|
||||
* Returns array of BlogpostComment objects.
|
||||
*/
|
||||
function get_blog_info($conn, $blogId){
|
||||
// Check DB connection
|
||||
if($conn == null){
|
||||
printf("
|
||||
<article>
|
||||
<h1>Failed DB connection, cannot proceed!</h1>
|
||||
If you see this error in production,
|
||||
please shoot me an email with helpful details.
|
||||
</article>");
|
||||
include_once($COMMONS."/footer.php");
|
||||
die();
|
||||
function load_comments($conn, $blogId) {
|
||||
// Prepare new statement for selecting all the "root" comments on the post.
|
||||
$stmt = $conn->prepare("SELECT comment_id, poster_id, timestamp, content,
|
||||
username FROM (blogpost_comments INNER JOIN users ON
|
||||
poster_id = user_id) WHERE blogpost_id = :blogpost_id AND
|
||||
parent_id IS NULL;");
|
||||
|
||||
// Bind and execute the comment select
|
||||
$stmt->bindParam(":blogpost_id", $blogId);
|
||||
$stmt->execute();
|
||||
|
||||
// Fetch the comments
|
||||
$results_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
|
||||
$comments_arr = [];
|
||||
|
||||
// Recursively fetch all the child comments
|
||||
for($i = 0; $i < count($results_arr); $i++) {
|
||||
$com = $results_arr[$i];
|
||||
$commentObj = new BlogpostComment($com["comment_id"], $com["poster_id"],
|
||||
$com["username"], $blogId, $com["timestamp"], $com["content"]);
|
||||
$commentObj->load_children($conn);
|
||||
$comments_arr[] = $commentObj;
|
||||
}
|
||||
|
||||
return $comments_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load info about the blog with a given guid and return corresponding
|
||||
* Blogpost object. NULL if blog couldn't be loaded.
|
||||
*/
|
||||
function load_blog($conn, $blogId){
|
||||
// Prepare and bind statement for gathering blogpost info
|
||||
$stmt = $conn->prepare("SELECT title, content, date_posted
|
||||
FROM blogposts WHERE blogpost_id = :blogpost_id;");
|
||||
|
@ -137,46 +258,30 @@ function get_blog_info($conn, $blogId){
|
|||
$blogContent = $result["content"];
|
||||
$datePosted = $result["date_posted"];
|
||||
$tags = $tags_arr;
|
||||
$comments = load_comments($conn, $blogId);
|
||||
|
||||
return new Blogpost($blogId, $blogTitle, $blogContent, $datePosted, $tags);
|
||||
return new Blogpost($blogId, $blogTitle, $blogContent,
|
||||
$datePosted, $tags, $comments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display all the comments responding to a given article.
|
||||
*/
|
||||
function display_comments($conn, $blogId){
|
||||
// Check DB connection
|
||||
if($conn == null){
|
||||
// Check DB connection
|
||||
if($conn == null){
|
||||
printf("
|
||||
<article>
|
||||
<h1>
|
||||
Failed to load comments due to database connection error!
|
||||
</h1>
|
||||
<h1>Failed DB connection, cannot proceed!</h1>
|
||||
If you see this error in production,
|
||||
please shoot me an email with helpful details.
|
||||
</article>");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare statement for selecting all coments replying to a given article.
|
||||
$stmt = $conn->prepare("SELECT;");
|
||||
|
||||
// Bind and execute the tag select
|
||||
$stmt->bindParam(":blogpost_id", $blogId);
|
||||
$stmt->execute();
|
||||
|
||||
// Fetch the tags
|
||||
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
|
||||
|
||||
include_once($COMMONS."/footer.php");
|
||||
die();
|
||||
}
|
||||
|
||||
// Get the blog id.
|
||||
$blogId = sanitize_input($_GET["guid"]);
|
||||
|
||||
$blogPost = null;
|
||||
// Try to get info about blog
|
||||
if($blogId) {
|
||||
$blogPost = get_blog_info($conn, $blogId);
|
||||
$blogPost = load_blog($conn, $blogId);
|
||||
}
|
||||
|
||||
// Display the header with title being the blog name, or not found message.
|
||||
|
@ -184,6 +289,7 @@ display_header($blogPost ? $blogPost->title : "Article not found");
|
|||
|
||||
// Display the blog
|
||||
$blogPost->display_article();
|
||||
$blogPost->display_comments();
|
||||
|
||||
include_once($COMMONS."/footer.php");
|
||||
?>
|
||||
|
|
Loading…
Reference in a new issue