<?php
require_once '../../../config.php';
require_once $config ['root_path'] . '/core/functions.php';
include_once $config ['system_core'] . "/initEngine.php";


if(check_login_bool() && isset($_POST['add'])) {
	$s = $commentsClass->add();
	if($s === true) {
		refresh('/'.$languageURL.'items/'.$itemID, $langArray['complete_add_comment'], 'complete');
	}
	else {
		addErrorMessage($langArray['error_item_comment'], '', 'error');
	}
}
elseif(isset($_POST['add_reply'])) {
	if(!isset($_POST['comment_id'])) {
		$_POST['comment_id'] = 0;
	}
	$s = $commentsClass->add($_POST['comment_id']);
	if($s === true) {
		refresh('/'.$languageURL.'items/'.$itemID, $langArray['complete_add_reply'], 'complete');
	}
	else {
		addErrorMessage($langArray['error_item_comment'], '', 'error');
	}
}


$comments = $commentsClass->getAll(START, LIMIT, " `item_id` = '".intval($itemID)."' AND `reply_to` = '0' ", true, '`datetime` ASC');
if(is_array($comments)) {
	$users = $usersClass->getAll(0, 0, $commentsClass->usersQuery);
	abr('users', $users);

	$ordersClass = new orders();
	$buyFromUsers = $ordersClass->isItemBuyed($itemID, $commentsClass->usersQuery);
	abr('buyFromUsers', $buyFromUsers);
}
abr('comments', $comments);

abr('paging', paging('/'.$languageURL.'items/'.$itemID.'/?p=', '', PAGE, LIMIT, $commentsClass->foundRows));

die();
?>