<?php

include '../system/start.php';
include '../system/functions.php';

$str['mesto'] = 'Комментарии к статусу';
$str['block'] = $set['site'].' / Комментарии к статусу';

include '../style/'.$style.'/head.php';
reg();

$id = abs(intval($_GET['id']));

$status = mysql_fetch_assoc(mysql_query("SELECT * FROM `statuses` WHERE `id` = '" . $id . "'"));

echo '<div class="where">Комментарии к "' . $status['text'] . '"</div>';

switch ($act) {
	case 'add':

			$text = protect($_POST['text']);
			if ($text) {
				$num = mysql_result(mysql_query("SELECT COUNT(*) FROM `statuses` WHERE `text` = '" . $text . "' AND `who` = '" . $user['login'] . "' AND `refid` = '" . $id . "' AND `type` = 'komm'"), 0);
				if ($num == 0) {
					mysql_query("INSERT INTO `statuses` SET `text` = '" . $text . "',
															`who` = '" . $user['login'] . "',
															`time` = '" . time() . "',
															`refid` = '" . $id . "',
															`type` = 'komm'");
					
					echo 'Комментарий успешно добавлен!<br /><a href="komm.php?id=' . $id . '">Назад</a>';
					header("Location: komm.php?id=" . $id ."");
				} else {
					echo 'Такой комментарий уже есть!<br /><a href="komm.php?id=' . $id . '">Назад</a>';
				}
			} else {
				echo 'Не введен комментарий!<br /><a href="komm.php?id=' . $id . '">Назад</a>';
			}

	break;

default :
	
	echo '<form action="?act=add&amp;id=' . $id . '" method="post">
			Комментарий:<br />
			<textarea name="text" cols="20" rows="4"></textarea><br />
			<input name="submit" type="submit" value="Добавить" /></form>';
	
	$r = mysql_query("SELECT * FROM `statuses` WHERE `type` = 'komm' AND `refid` = '" . $id . "' ORDER by `time` DESC LIMIT " . $page . "," . $set['str'] . "");
	$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `statuses` WHERE `type` = 'komm' AND `refid` = '" . $id . "'"), 0);
	
	if ($total == 0)
		echo 'Комментариев нет!';
		
	while ($e = mysql_fetch_assoc($r)) {
	
		echo ($i % 2) ? '<div class="forum1">' : '<div class="forum2">';
                           echo '<img src="../style/'.$style.'/ico/personal.gif" alt=""/> <a href="anketa.php?login=' . $e['who'] . '">' . $e['who'] . '</a> '; 
		echo statusl($e['who']); 
		echo '<br />
		<small>В ' . date("d.m.Y G:i", $e['time']) . '</small><br />
		' . text($e['text']) . '</div>';
		$i++;
	}
	
	if($total > $set['str'])
	{
		pages($page, $total, 'komm.php?id='.$status['id'].'&amp;', $set['str']);
	}

}
include '../style/'.$style.'/foot.php';
?>