Advertisement
alexander7567

class_blog.php

Mar 10th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.87 KB | None | 0 0
  1. <?php
  2.  
  3. class blog {
  4.  
  5.     function get_blog_posts($limit = 0) {
  6.         global $dbh;
  7.         if ($limit == 0) {
  8.             $limit_sql = "";
  9.         } else {
  10.             $limit_sql = " LIMIT ".$limit;
  11.         }
  12.         $sql = $dbh->prepare("SELECT * FROM blog_posts{$limit_sql}");
  13.         $sql->execute();
  14.         $blog_posts = $sql->fetchAll();
  15.         return $blog_posts;
  16.     }
  17.  
  18.     function get_blog_post_by_id($blog_post_id) {
  19.         global $dbh;
  20.         $sql = $dbh->prepare("SELECT * FROM blog_posts WHERE bpost_id = ?");
  21.         $sql->bindParam(1, $blog_post_id);
  22.         $sql->execute();
  23.         $blog_post = $sql->fetch();
  24.         return $blog_post;
  25.     }
  26.  
  27.     function get_short_text($blog_post_id, $limit = 200) {
  28.         global $dbh;
  29.         $blog_post = $this->get_blog_post_by_id($blog_post_id);
  30.         $text = strip_tags($blog_post['bpost_text']);
  31.         $post_link = $this->create_blog_link($blog_post_id);
  32.         return substr($text, 0, $limit)."... <a href='{$post_link}'>Read more</a>";
  33.     }
  34.  
  35.     function display_posts($short_text = 0, $limit = 0) {
  36.         $blog_posts = $this->get_blog_posts($limit);
  37.         $return = "";
  38.  
  39.         foreach ($blog_posts as $blog_post) {
  40.             if ($short_text) {
  41.                 $text = $this->get_short_text($blog_post['bpost_id']);
  42.             } else {
  43.                 $text = $blog_post['bpost_text'];
  44.             }
  45.             $blog_post['display_time'] = date("F j, Y, g:i a", $blog_post['bpost_time']);
  46.             $return .= "<div class=\"panel panel-primary\">
  47.             <div class=\"panel-heading\">{$blog_post['bpost_title']}</div>
  48.             <div class=\"panel-body\">
  49.             <small>Posted on: {$blog_post['display_time']}</small><br>
  50.             {$text}";
  51.             $return .= $this->display_comments($blog_post['bpost_id']);
  52.             $return .= "</div>
  53.             </div>";
  54.         }
  55.  
  56.         return $return;
  57.     }
  58.  
  59.     function create_blog_link($blog_post_id) {
  60.         global $dbh, $path_to_root;
  61.         return $path_to_root."blog.php?post_id=".$blog_post_id;
  62.     }
  63.  
  64.     function get_post_comments($blog_post_id, $limit = 0) {
  65.         global $dbh;
  66.         if ($limit == 0) {
  67.             $limit_sql = "";
  68.         } else {
  69.             $limit_sql = " LIMIT ".$limit;
  70.         }
  71.         $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_post = ? {$limit_sql}");
  72.         $sql->bindParam(1, $blog_post_id);
  73.         $sql->execute();
  74.         $blog_comments = $sql->fetchAll();
  75.         return $blog_comments;
  76.     }
  77.  
  78.     function get_comment_replies($comment_id) {
  79.         global $dbh;
  80.         $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_reply = ?");
  81.         $sql->bindParam(1, $comment_id);
  82.         $sql->execute();
  83.         $comment_replies = $sql->fetchAll();
  84.         return $comment_replies;
  85.     }
  86.  
  87.     function reply_count($comment_id) {
  88.         global $dbh;
  89.         $sql = $dbh->prepare("SELECT * FROM blog_comments WHERE comment_reply = ?");
  90.         $sql->bindParam(1, $comment_id);
  91.         $sql->execute();
  92.         $reply_count = $sql->rowCount();
  93.         return $reply_count;
  94.     }
  95.  
  96.     function display_comments($blog_post_id, $limit = 0) {
  97.         global $dbh, $user;
  98.         $return = "";
  99.         $comments = $this->get_post_comments($blog_post_id);
  100.         foreach ($comments as $comment) {
  101.             $comment_user = $user->get_userdata($comment['comment_userid']);
  102.             $return .= '<div class="media">
  103.      <a class="pull-left" href="#">
  104.        <img class="media-object" src="holder.js/64x64" alt="">
  105.      </a>
  106.      <div class="media-body">
  107.        <h4 class="media-heading">'.$comment_user['username'].'</h4>
  108.        '.$comment['comment_text'];
  109.             $replies = $this->get_comment_replies($comment['comment_id']);
  110.             foreach ($replies as $reply) {
  111.                 $comment_user = $user->get_userdata($comment['comment_userid']);
  112.                 $return .= '<div class="media">
  113.      <a class="pull-left" href="#">
  114.        <img class="media-object" src="holder.js/64x64" alt="">
  115.      </a>
  116.      <div class="media-body">
  117.        <h4 class="media-heading">'.$comment_user['username'].'</h4>
  118.        '.$comment['comment_text'];
  119.                 if ($this->reply_count($reply['comment_id'])) {
  120.                     $replies_level_1 = $this->get_comment_replies($reply['comment_id']);
  121.                     foreach ($replies_level_1 as $reply_level_1) {
  122.                         $comment_user = $user->get_userdata($comment['comment_userid']);
  123.                         $return .= '<div class="media">
  124.      <a class="pull-left" href="#">
  125.        <img class="media-object" src="holder.js/64x64" alt="">
  126.      </a>
  127.      <div class="media-body">
  128.        <h4 class="media-heading">'.$comment_user['username'].'</h4>
  129.        '.$reply_level_1['comment_text'];
  130.                     }
  131.                     $return .= '</div></div>';
  132.                 }
  133.             }
  134.             $return .= '</div></div>';
  135.         }
  136.         return $return;
  137.     }
  138.  
  139.     function insert_comment($type, $text, $userid, $id) {
  140.         global $dbh;
  141.         if ($type == "post") {
  142.             $comment_post = $id;
  143.             $comment_reply = 0;
  144.         } elseif ($type == "reply") {
  145.             $comment_reply = $id;
  146.             $comment_post = 0;
  147.         }
  148.  
  149.         $sql = $dbh->prepare("INSERT into blog_comments (comment_post, comment_reply, comment_userid, comment_text, comment_time) VALUES (?,?,?,?,?)");
  150.         $sql->bindParam(1, $comment_post);
  151.         $sql->bindParam(2, $comment_reply);
  152.         $sql->bindParam(3, $userid);
  153.         $sql->bindParam(4, $text);
  154.         $sql->bindValue(5, time());
  155.         $sql->execute();
  156.  
  157.         return true;
  158.     }
  159. }
  160.  
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement