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

80 lines
2.3 KiB
PHP

<?php
class Blogpost
{
public $blogpost_id;
public $address;
public $title;
public $content;
public $date_posted;
public $date_edited;
public $tags;
public $comments;
/**
* Display the article, or a warning message.
*/
public function display_article(){
// Display the blog title and metadata
$body = "<article><table class=\"noborder-table\" style=\"width: 100%;
margin-top: 16px;\">";
$body .= sprintf("<tr><td style=\"padding: 0px;\">
<h2 style=\"margin-top: 0px;\">%s</h2></td>
<td class=\"blog-publish-date\">Published on: %s</td></tr><tr>
<td class=\"blog-tags\">", $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("
<span class=\"blog-tag\" style=\"background-color: %s\">
%s
</span>", $tag["color"], $tag["name"]);
}
// Display publish date and end metadata div
$body .= sprintf("</td><td class=\"blog-publish-date\">
Last edited on: %s</td></tr></table>",
date("Y-m-d", strtotime($this->date_edited)));
// Display hrule, article content and end the article
$body .= sprintf("</article><hr><article>%s</article>", $this->content);
return $body;
}
/**
* Display the comments for this post and their children.
*/
public function display_comments(){
$body = "<article>";
for($i = 0; $i < count($this->comments); $i++){
$body .= $this->comments[$i]->display_comment();
}
return $body."</article>";
}
/**
* 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;
}
}
?>