<?php
require_once 'DB.class.php';
require_once 'Pagination.class.php';
require_once 'Reply.class.php';
class Comments {
private $id;
private $level;
private $lap = 0;
private $supercomments_per_page;
private $moderation;
private $conn;
function __construct( $post_id ,$level, $supercomments, $moderation, $db_config ) {
$this->id = (isset($post_id)) ? $post_id : "0";
$this->level = (isset($level)) ? $level : "-1";
$this->supercomments_per_page = (isset($supercomments)) ? $supercomments : "20";
$this->moderation = (isset($moderation)) ? $moderation : false;
$db = new DB($db_config);
$this->conn = $db->connect();
}
private function get_gravatar( $email ) {
$default = "";
$size = "65";
return "thumb.png";
}
public function getComments() {
$reply = new Reply();
(isset($_POST['comment_post_ID'])) ? $validation = $reply->validation($this->moderation) : $validation = "";
$htmlComments = "";
$accepted = "";
if ($this->moderation)
$accepted = " AND accepted = true";
$pager = new Pagination($this->conn, "SELECT * FROM comments WHERE parent_id = 0 AND post_id = ".$this->id.$accepted, $this->supercomments_per_page, 5, "");
$rs = $pager->paginate();
if ($rs == true) {
while($row = mysql_fetch_assoc($rs))
$htmlComments .= $this->hierarchy(0,$row,$this->lap);
} else
$nofound = '<br/><div class="notification error"><div>No comments yet! Be the first:</div></div>';
return (isset($nofound) ? $nofound : '').$validation.$pager->renderFullNav().'
<div class="comments block">
<div class="comments">'
.$htmlComments.
'</div>
</div>
'.$pager->renderFullNav().$reply->form($this->id, (isset($_GET['replytocom'])) ? $_GET['replytocom'] : "");
}
/*
This private function checks whether there are any comment whose parent_id is the same as its.
If there's any such comment, it again executes the same function.
*/
private function hierarchy( $hl, $row, $laps ) {
$htmlComments = '
<div class="comment '.(($hl == 0) ? 'item' : 'reply').'" id="comment-'.$row['id'].'">
<div class="info">
<strong><a href="#" class="url">'.$row['author'].'</a></strong><em>'.date('F j, Y', strtotime($row['created_at'])).'</em>
'.(($this->level != $laps) ? '<a class="comment-reply" href="'.((isset($_GET['page'])) ? "?page=".$_GET['page']."&replytocom=".$row['id']."#respond" : "?replytocom=".$row['id']."#respond" ).'" onclick=\'return addComment.moveForm("comment-'.$row['id'].'", "'.$row['id'].'", "respond", "'.$this->id.'")\'>Reply</a>' : '').'
</div>
<div class="text">
<div class="r">
<div class="tl">
<div class="tr">
<div class="bl">
<div class="br">
<p>
'.nl2br($row['comment']).'
</p>
</div>
</div>
</div>
</div>
</div>
</div>
';
$laps++;
$accepted = "";
if ($this->moderation)
$accepted = " AND accepted = true";
$sql = "SELECT * FROM comments WHERE parent_id = ".$row['id']." AND post_id = ".$this->id.$accepted;
$rows = mysql_query($sql);
if (mysql_num_rows($rows) > 0) {
if ($hl == 0)
$htmlComments .= '<div class="children">';
else
$htmlComments .= '<div class="grandchildren">';
while ($row = mysql_fetch_assoc($rows)) {
if ($hl == 0)
$htmlComments .= $this->hierarchy(1,$row,$laps);
else
$htmlComments .= $this->hierarchy(2,$row,$laps).((($hl == 2) and ($laps < 3)) ? '</div>' : '');
}
if (($hl == 2) and ($laps >= 3))
$htmlComments .= "</div>";
if ($hl != 2)
$htmlComments .= "</div>";
}
$htmlComments .= "</div>";
return $htmlComments;
}
}
?>