<?php
require_once('../wu_init.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('3'); }
require('../wu-functions/wu_functions.php');

if(isset($_POST['id'])){
if(!empty($_POST['id'])){
$getid = intval($_POST['id']);
class Threaded_comments
{
public $parents  = array();
public $children = array();
function __construct($comments)
{
foreach ($comments as $comment)
{
if ($comment['t'] == 0)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['t']][] = $comment;
}
}
}
private function format_comment($comment, $depth)
{
if (empty($comment['av'])) { $av = 'def.png'; } else { $av = $comment['av']; }
for ($depth; $depth > 0; $depth--)
{
echo '<div class="lcom"></div>';
}
echo '<div class="media" onclick="reply(\''.$comment['id'].'\',\''.$comment['log'].'\');"><a class="pull-left" name="'.$comment['id'].'">
<img class="media-object" src="/wu-uploads/avatars/thumbs/'.$av.'" alt="'.$comment['log'].'">
</a>
<div class="media-body">
<font class="media-heading">'.$comment['log'].'</font>
<ul class="headline-info">
<li>'.wudate($comment['dt']).'</li>
</ul>
'.nl2br(htmlspecialchars($comment['c'])).'
</div>
</div>
';
}
private function print_parent($comment, $depth = 0)
{
foreach ($comment as $c)
{
$this->format_comment($c, $depth);
if (isset($this->children[$c['id']]))
{
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
$carr = mysqli_query($connect_db, "SELECT id,t,usr,c,dt,log,av FROM `".DB_PREFIX."_es_c` INNER JOIN ".DB_PREFIX."_users ON ".DB_PREFIX."_es_c.usr = ".DB_PREFIX."_users.uid WHERE m = '$getid' ORDER BY id ASC");
if (mysqli_num_rows($carr) > 0) {
while ($row = mysqli_fetch_array($carr)) {
$comm[] = $row;
}
$ecom = new Threaded_comments($comm);
$ecom->print_comments();
} else { echo '<div class="alert alert-info fade in"><i class="icon-info"></i> Комментариев нет</div>'; }
} else { echo '<div class="alert alert-error fade in"><i class="icon-warning"></i> Ошибка загрузки комментариев</div>'; }
} else { echo '<div class="alert alert-error fade in"><i class="icon-warning"></i> Ошибка загрузки комментариев</div>'; }
?>
<script type="text/javascript">
$(function() {
$(".modals_cl").on('click', function() {
var a_href = $(this).attr('href');
window.open(a_href, '_blank');
return false;
});
});
</script>