<?

$tag = $this->NpjTranslit($this->tag);

$page = $this->LoadSingle("SELECT * FROM `".$this->GetConfigValue("mysql_database")."`.`".$this->GetConfigValue("table_prefix")."pages` WHERE `supertag` = '".$tag."'");
$comments = $this->LoadAll("SELECT * FROM `".$this->GetConfigValue("mysql_database")."`.`".$this->GetConfigValue("table_prefix")."comment` WHERE `pid` = '".$page['id']."' ORDER BY `date`  ASC ");

if(!$this->GetUser()){
	if ($_POST['name']){
		$user = $_POST['name'];
	}else{
		$user = 'Гость';
	}
} else {
	$user = $this->addspaces($this->GetUsername());
}

$srvComment = $this->srvFactory('Comment');
$sm = $srvComment->newSmarty();
$sm->assign('comments', $comments);
$sm->assign('user', $user);
echo $sm->fetch('comment.txt');


if (($_POST['delid']) and ($this->isadmin())){
	$this->_db->Query("DELETE FROM `".$this->GetConfigValue("mysql_database")."`.`".$this->GetConfigValue("table_prefix")."comment` WHERE `id` = '".$_POST['delid']."' LIMIT 1");
	$this->SetMessage("Удалено!");
	$this->Arefresh();
}

if(!$this->GetUser()) {
	echo '<table style="border: 0;"><tr><td>';
	echo 'Пожалуйста, введите число:</td><td>';
	echo $this->vfrmField("ccha", "kcaptcha", "");
	echo '</td><td><input type="submit" value="'.$this->GetResourceValue("AttachCommentButton").'" accesskey="s" />';
	echo '</td></tr></table>';
} else {
	echo '<input type="submit" value="'.$this->GetResourceValue("AttachCommentButton").'" accesskey="s" />';
}
echo $this->FormClose();


if ($_POST['comm']){
	$accept = $this->GetUser();
	if(!$accept) {
		$accept = $this->vfrmProcess("kcaptcha", "ccha");
	}

	if($accept) {
		if(strlen($_POST["text"]) != 0) {
			$un = htmlspecialchars($_POST["name"]);
			$text = htmlspecialchars($_POST["text"]);
			$text = str_replace("\n", "<br>", $text);
			$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
			$this->_db->Query("INSERT INTO `".$this->_db->config["mysql_database"]."`.`".$this->_db->config["table_prefix"]."comment` SET `pid`='".$page['id']."', `name`='".$un."', `text`='".$text."', `ip`='".$ip."';");
			$this->SetMessage("Запись добавлена!");
			$this->Arefresh();
		} else {
			$this->SetMessage('Вы не ввели текст сообщения. Попробуйте еще раз.');
			$this->Arefresh();
		}
	} else {
		$this->SetMessage('Вы некорректно ввели число. Попробуйте еще раз.');
		$this->Arefresh();
	}
}
?>