"; $body .= sprintf("

%s

Published on: %s
", $this->title, date("Y-m-d", strtotime($this->date_posted))); // Display tags for($i = 0; $i < count($this->tags); $i++) { $tag = $this->tags[$i]; $body .= sprintf(" %s ", $tag["color"], $tag["name"]); } // Display publish date and end metadata div $body .= sprintf(" Last edited on: %s
", date("Y-m-d", strtotime($this->date_edited))); // Display hrule, article content and end the article $body .= sprintf("
%s
", $this->content); return $body; } /** * Display the comments for this post and their children. */ public function display_comments(){ $body = "
"; for($i = 0; $i < count($this->comments); $i++){ $body .= $this->comments[$i]->display_comment(); } return $body."
"; } /** * 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. * $date_edited Timestamp at whioch the article was last edited. * $tags Array of the tags this article has. * $comments Array of Blogpostcomment objects, * the comments of this article. */ 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; $this->date_edited = $date_edited; $this->tags = $tags; $this->comments = $comments; } } 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("
By: %s On: %s
%s
", $this->comment_id, $this->poster_name, date("Y-m-d H:i", strtotime($this->timestamp)), $this->comment_id, $this->comment_id, $this->content, $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."
"; } /** * 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; } } /** * Load comments under a given blog. * Returns array of BlogpostComment objects. */ 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 AND parent_id IS NULL ORDER BY timestamp ASC;"); // 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 = []; // 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, $blogId, $blogAddress, $com["timestamp"], $com["content"], NULL); $commentObj->load_children($conn); $comments_arr[] = $commentObj; } return $comments_arr; } function get_blogpost_address($conn, $blogId) { // Prepare and bind the statement for selecting address $stmt = $conn->prepare("SELECT readable_address FROM blogposts WHERE blogpost_id = :blogpost_id"); $stmt->bindParam(":blogpost_id", $blogId); // Execute the statement $stmt->execute(); // Fetch the blogpost address $result = $stmt->fetch(PDO::FETCH_ASSOC); // If no blogpost with the given GUID was found, return null. if(!$result) { return null; } // Return the blogpost address. return $result["readable_address"]; } /** * 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 readable_address, title, content, date_posted, date_edited FROM blogposts WHERE blogpost_id = :blogpost_id;"); $stmt->bindParam(":blogpost_id", $blogId); // Execute the statement $stmt->execute(); // Fetch the blogpost $result = $stmt->fetch(PDO::FETCH_ASSOC); // If no post with given guid was found, // there is no information to gather, return. if(!$result){ return null; } // Prepare new statement for selecting the tags for a given blogpost $stmt = $conn->prepare("SELECT name, color FROM blogpost_tags INNER JOIN blogpost_has_tag ON blogpost_tags.tag_id = blogpost_has_tag.tag_id WHERE blogpost_id = :blogpost_id;"); // Bind and execute the tag select $stmt->bindParam(":blogpost_id", $blogId); $stmt->execute(); // Fetch the tags $tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC); // 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, $blogAddress); return new Blogpost($blogId, $blogAddress, $blogTitle, $blogContent, $datePosted, $dateEdited, $tags, $comments); } function generate_article($conn, $fp, $blogId) { // Attempt to load the blogpost $blogPost = load_blog($conn, $blogId); if(!$blogPost) { return false; } fprintf($fp, "

Comments:

", $blogPost->title, addslashes($blogPost->display_article()), $blogPost->blogpost_id, $blogPost->address, addslashes($blogPost->display_comments())); return true; } ?>