<?
@session_start();
if($_SESSION['ID_P'][1])
{

$limit_com=700;
$base_path="../";

require_once($base_path.'connect/connect.php');
include_once($base_path.'lib/lib.php');

if($_GET['id']) $id=intval($_GET['id']);
if($_GET['opt']) $opt=intval($_GET['opt']);
if($_GET['id_comment']) 
	{
	$id_comment=intval($_GET['id_comment']);
	$aa=mysql_query("SELECT * FROM comment WHERE id_comment='".$id_comment."'");
	$raa=mysql_fetch_array($aa);
	}

$r=mysql_query("SELECT * FROM board WHERE id='".$id."'");
$rr=mysql_fetch_array($r);


if(($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_P'][1] == $raa['id_akk']) && $opt)
	{
	if($opt==1 && $_SESSION['ID_P'][1] == $rr['id_akk'])
	mysql_query("UPDATE comment SET moder='0' WHERE id='".$id."' AND id_comment='".$id_comment."'");
	elseif($opt==2)
		{
		if(!$raa['typ_comment'])
		mysql_query("DELETE FROM comment WHERE id='".$id."' AND parent_id='".$id_comment."'");
		elseif($raa['typ_comment'])
		mysql_query("DELETE FROM comment WHERE id='".$id."' AND id_comment='".$id_comment."'");
		}
	}

if($_POST['comment'] && $_POST['com_a'])
	{
	$comment=_pre2($_POST['comment']);
	$comment=trim(obrez($comment[1], $limit_com));
	if($comment)
		{
		if($rr['comment']==2) $moder=1; else $moder=0;
					
		mysql_query("INSERT INTO comment (
			comment, 
			id, 
			id_akk, 
			contakt,
			moder,
			data
			) 
			VALUES (
			'".$comment."',
			'".$id."',
			'".$_SESSION['ID_P'][1]."',
			'".$_SESSION['ID_P'][3]."',
			'".$moder."',
			NOW()
			)");
		$parent_id=mysql_insert_id();
		mysql_query("UPDATE comment SET parent_id='".$parent_id."' WHERE id_comment='".$parent_id."'");
		
		$thm='Добавлен комментарий к Вашему объявлению';
		$c='К Вашему объявлению <a href="'.dir_top().'items/'.$rr['zag_url'].'_'.$rr['id'].'">'.$rr['zag'].'</a> добавлен комментарий';
		@mail_utf8($rr['email'], $thm, $c, $rru['email']);

		}
	}
		
		
if($_POST['comment'] && $_POST['com_b'])
	{
	$parent_id=intval($_POST['parent_id']);
	$comment=_pre2($_POST['comment']);
	$comment=trim(obrez($comment[1], $limit_com));
	if($comment)
		{
		mysql_query("INSERT INTO comment (
			comment, 
			id, 
			id_akk, 
			typ_comment,
			parent_id,
			data
			) 
			VALUES (
			'".$comment."',
			'".$id."',
			'".$_SESSION['ID_P'][1]."',
			'1',
			'".$parent_id."',
			NOW()
			)");
		}
	}
		
if(!$_SESSION['ID_P']) 
echo '<font color=green>Комментарии могут оставлять только зарегистрированные пользователи</font><br>';
elseif ($_SESSION['ID_P'][1] != $rr['id_akk'])
	{
	?>
	<form id="form_comment" method="post" >
	<textarea name="comment" rows="4" cols="70"></textarea>
	<input name="com_a" type="hidden" value="1">
	<input name="submit" class="submit" type="submit" onclick="comment_add(<?=$id?>); return false" value="Оставить комментарий">
	</form>
	<?
	}


$com=mysql_query("SELECT * FROM comment WHERE id='".$id."' ORDER BY parent_id, id_comment");
while($r_com=mysql_fetch_array($com))
	{
	if(!$r_com['moder'])
		{
		echo '<p>';
		if($r_com['contakt']) 
			{
			echo '<b>'.$r_com['contakt'].'</b> ('.date("d-m-Y H:i", strtotime($r_com['data'])).')';
			if(($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_Glob']) && !$r_com['typ_comment']) 
			echo ' ['.$r_com['id_akk'].']';
			echo '<br>';
			}
				
		if(!$r_com['typ_comment'])
		echo $r_com['comment'];
		else
		echo '<span style="color:#003366">'.$r_com['comment'].'</span>';

		if($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_P'][1] == $r_com['id_akk'])
			{
			echo '<br>';
			if($_SESSION['ID_P'][1] == $rr['id_akk'] && !$r_com['typ_comment'])
			echo ' <span id="otvet_'.$r_com['id_comment'].'"><font color=blue><b>&#8764;</b></font><a href="javascript:otvet('.$id.','.$r_com['id_comment'].')">Ответить на комментарий</a></span>';
			echo ' <font color=red><b>&#215;</b></font><a href="javascript:send_opt('.$id.', '.$r_com['id_comment'].', 2)">Удалить</a>';
			}
		}
	elseif($r_com['moder'] && ($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_P'][1] == $r_com['id_akk'] || $_SESSION['ID_Glob']))
		{
		echo '<p><span style="color:#CC9999">';
		if($r_com['contakt']) 
			{
			echo '<b>'.$r_com['contakt'].'</b> ('.date("d-m-Y H:i", strtotime($r_com['data'])).')';
			if($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_Glob'])
			echo ' ['.$r_com['id_akk'].'] ';
			echo '<br>';
			}
				
		if($_SESSION['ID_P'][1] == $rr['id_akk'])
		echo ' - <span style="color:#FF0000">Комментарий ожидает Вашего одобрения</span>';
		else
		echo ' - <span style="color:#FF0000">Комментарий ожидает одобрения владельца объявления</span>';
			
		echo '<br>';
		echo $r_com['comment'];
		echo '<span>';
				
		if($_SESSION['ID_P'][1] == $rr['id_akk'] || $_SESSION['ID_P'][1] == $r_com['id_akk'])
			{
			echo '<br>';
			if($_SESSION['ID_P'][1] == $rr['id_akk'])
			echo ' <font color=green><b>&#8744;</b></font><a href="javascript:send_opt('.$id.', '.$r_com['id_comment'].', 1)">Одобрить</a>';
			echo ' <font color=red><b>&#215;</b></font><a href="javascript:send_opt('.$id.', '.$r_com['id_comment'].', 2)">Удалить</a>';
			}
		}
	}
}
?>