Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class blog {
- function get_blog_posts($limit = 0) {
- global $dbh;
- if ($limit == 0) {
- $limit_sql = "";
- } else {
- $limit_sql = " LIMIT ".$limit;
- }
- $sql = $dbh->prepare("SELECT * FROM blog_posts{$limit_sql}");
- $sql->execute();
- $blog_posts = $sql->fetchAll();
- return $blog_posts;
- }
- function get_blog_post_by_id($blog_post_id) {
- global $dbh;
- $sql = $dbh->prepare("SELECT * FROM blog_posts WHERE bpost_id = ?");
- $sql->bindParam(1, $blog_post_id);
- $sql->execute();
- $blog_post = $sql->fetch();
- return $blog_post;
- }
- function get_short_text($blog_post_id, $limit = 200) {
- global $dbh;
- $blog_post = $this->get_blog_post_by_id($blog_post_id);
- $text = strip_tags($blog_post['bpost_text']);
- $post_link = $this->create_blog_link($blog_post_id);
- return substr($text, 0, $limit)."... <a href='{$post_link}'>Read more</a>";
- }
- function display_posts($short_text = 0, $limit = 0) {
- $blog_posts = $this->get_blog_posts($limit);
- $return = "";
- foreach ($blog_posts as $blog_post) {
- if ($short_text) {
- $text = $this->get_short_text($blog_post['bpost_id']);
- } else {
- $text = $blog_post['bpost_text'];
- }
- $blog_post['display_time'] = date("F j, Y, g:i a", $blog_post['bpost_time']);
- $return .= "<div class=\"panel panel-primary\">
- <div class=\"panel-heading\">{$blog_post['bpost_title']}</div>
- <div class=\"panel-body\">
- <small>Posted on: {$blog_post['display_time']}</small><br>
- {$text}";
- $return .= $this->display_comments($blog_post['bpost_id']);
- $return .= "</div>
- </div>";
- }
- return $return;
- }
- function create_blog_link($blog_post_id) {
- global $dbh, $path_to_root;
- return $path_to_root."blog.php?post_id=".$blog_post_id;
- }
- function get_post_comments($blog_post_id, $limit = 0) {
- global $dbh;
- if ($limit == 0) {
- $limit_sql = "";
- } else {
- $limit_sql = " LIMIT ".$limit;
- }
- $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_post = ? {$limit_sql}");
- $sql->bindParam(1, $blog_post_id);
- $sql->execute();
- $blog_comments = $sql->fetchAll();
- return $blog_comments;
- }
- function get_comment_replies($comment_id) {
- global $dbh;
- $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_reply = ?");
- $sql->bindParam(1, $comment_id);
- $sql->execute();
- $comment_replies = $sql->fetchAll();
- return $comment_replies;
- }
- function reply_count($comment_id) {
- global $dbh;
- $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_reply = ?");
- $sql->bindParam(1, $comment_id);
- $sql->execute();
- $reply_count = $sql->rowCount();
- return $reply_count;
- }
- function display_comments($blog_post_id, $limit = 0) {
- global $dbh, $user;
- $return = "";
- $comments = $this->get_post_comments($blog_post_id);
- foreach ($comments as $comment) {
- $comment_user = $user->get_userdata($comment['comment_userid']);
- $return .= '<div class="media">
- <a class="pull-left" href="#">
- <img class="media-object" src="holder.js/64x64" alt="">
- </a>
- <div class="media-body">
- <h4 class="media-heading">'.$comment_user['username'].'</h4>
- '.$comment['comment_text'];
- $replies = $this->get_comment_replies($comment['comment_id']);
- foreach ($replies as $reply) {
- $comment_user = $user->get_userdata($comment['comment_userid']);
- $return .= '<div class="media">
- <a class="pull-left" href="#">
- <img class="media-object" src="holder.js/64x64" alt="">
- </a>
- <div class="media-body">
- <h4 class="media-heading">'.$comment_user['username'].'</h4>
- '.$comment['comment_text'];
- if ($this->reply_count($reply['comment_id'])) {
- $replies_level_1 = $this->get_comment_replies($reply['comment_id']);
- foreach ($replies_level_1 as $reply_level_1) {
- $comment_user = $user->get_userdata($comment['comment_userid']);
- $return .= '<div class="media">
- <a class="pull-left" href="#">
- <img class="media-object" src="holder.js/64x64" alt="">
- </a>
- <div class="media-body">
- <h4 class="media-heading">'.$comment_user['username'].'</h4>
- '.$reply_level_1['comment_text'];
- }
- $return .= '</div></div>';
- }
- }
- $return .= '</div></div>';
- }
- return $return;
- }
- function insert_comment($type, $text, $userid, $id) {
- global $dbh;
- if ($type == "post") {
- $comment_post = $id;
- $comment_reply = 0;
- } elseif ($type == "reply") {
- $comment_reply = $id;
- $comment_post = 0;
- }
- $sql = $dbh->prepare("INSERT into blog_comments (comment_post, comment_reply, comment_userid, comment_text, comment_time) VALUES (?,?,?,?,?)");
- $sql->bindParam(1, $comment_post);
- $sql->bindParam(2, $comment_reply);
- $sql->bindParam(3, $userid);
- $sql->bindParam(4, $text);
- $sql->bindValue(5, time());
- $sql->execute();
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement