<?php
if (!is_numeric($id)) $html = true;
$id = (int)$id;
if ($id < 1) $html = true;


if (empty($html)) {
	/* pages nav */
	$total = $this->DB->select($this->module . '_comments', DB_COUNT, array('cond' => array('entity_id' => $id)));
	$cntPages = ceil( $total / Config::read('comment_per_page', $this->module) );
	if ( isset($_GET['page']) ) {
		$page = (int)$_GET['page'];
		if ( $page < 1 ) $page = 1;
	} else {
		$page = 1;
	}
	if ( $page > $cntPages ) $page = $cntPages;
	if ( $cntPages > 1 ) {
		$pages = pagination( $page, $cntPages, '/' . $this->module . '/view/' . $id);
	} else {
		$pages = '&nbsp;';
	}


	$template = $this->Parser->getAndParse('viewcomment.html');
	$html = '';

	$res = $this->DB->select($this->module . '_comments', DB_ALL, array(
		'cond' => array(
			'entity_id' => $id
		),
		'page'  => $page,
		'limit' => Config::read('comment_per_page', $this->module),
	));



	foreach ($res as $comment) {
		$markets = array();
		$comment = $this->Parser->quoteTags($comment);
		
		/* COMMENT ADMIN BAR */
		$ip = (!empty($comment['ip'])) ? $comment['ip'] : 'Unknown';
		$moder_panel = '';
		$adm = false;
		if ($this->ACL->turn(array($this->module, 'edit_comments'), false)) {
			$moder_panel .= get_link(get_img('/sys/img/edit_16x16.png'), 
			'/' . $this->module . '/edit_comment_form/' . $comment['id']) . '&nbsp;';
			$adm = true;
		}
		if ($this->ACL->turn(array($this->module, 'delete_comments'), false)) {
			$moder_panel .= get_link(get_img('/sys/img/delete_16x16.png'), 
			'/' . $this->module . '/delete_comment/' . $comment['id'], array('onClick' => "return confirm('" . __('Are you sure') . "')")) . '&nbsp;';
			$adm = true;
		}
		if ($adm) {
			$moder_panel = get_img('/sys/img/ip.png', array('alt' => 'ip', 'title' => h($ip))) . $moder_panel;
		}
	
if ($this->DB->select('users', DB_FIRST, array('cond' => array('name' => $comment['name'])))) {
	$query = $this->DB->select('users', DB_FIRST, array('cond' => array('name' => $comment['name'])));
	$userid_comment = $query[0];

	if (file_exists('sys/avatars/' . $userid_comment['id'] . '.jpg')) {
		$markets['{COMMENT_AVATAR}'] = '<img src="/sys/avatars/' . $userid_comment['id'] . '.jpg" style="width: 100px; height: 100px;" />';
	} else {
		$markets['{COMMENT_AVATAR}'] = '<img src="/sys/img/noavatar.png" />';
	}

	$markets['{COMMENT_USER_URL}'] = '<a href="/users/info/'.$userid_comment['id'].'" target="_blank">'.$comment['name'].'</a>';

} else {

	$markets['{COMMENT_AVATAR}'] = '<img src="/sys/img/noavatar.png" />';
	$markets['{COMMENT_USER_URL}'] = $comment['name'];
}

		$markets['{COMMENT_NAME}'] = h($comment['name']);
		$markets['{COMMENT_DATE}'] = h($comment['date']);
		$markets['{COMMENT_MODER_PANEL}'] = $moder_panel;
		$markets['{COMMENT_MESSAGE}'] = $this->Textarier->print_page($comment['message']);
		$html .= $this->_replaceMarkets($markets, $template);
	}
} else {
	$html = '';
}

?>
