<?
include '../config.php';
include '../func.php';
head();
aut();
ban();

$id = num($_GET['id']);
if (empty($id)) {
header("Location: index.php?".SID);
exit;
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `microblog` WHERE `id` = '".$id."'"),0)==0) {
header("Location: /index.php?".SID);
exit;
}

if ($_GET['res'] == 'msg') echo 'Не введен коментарий';
if ($_GET['res'] == 'msg2') echo 'Повтор комента';
if ($_GET['res'] == 'msgyes') echo 'Коментарий добавлен';
if ($_GET['res'] == 'msgno') echo 'Возникла ошибка';
$blog = mysql_fetch_assoc(mysql_query("SELECT * FROM `microblog` WHERE `id` = '".$id."'"));
$view = $blog['view'] + 1;
mysql_query("UPDATE `microblog` SET `view` = '".$view."' WHERE `id` = '".$id."'");
echo head . 'Микроблог' . div . link1;

echo mat(smile(parsebb(adminbb(nl2br($blog['name'])))));

echo '</div>';

echo '<div class="katalog"><b>Автор:</b> '.user($blog['id_user']).' | <a href="blog_list.php?id='.$blog['id_user'].'"><b>Все записи</b></a> '.user($blog['id_user']).'<br/ >';

echo 'Просмотров: '.$blog['view'].' '.div. katalog . '<b>Коментарии:</b>' . div;

$all = mysql_result(mysql_query("SELECT count(*) FROM `microblog_kom` WHERE `id_dnev` = '".$id."'"), 0);
if($all){
$onpage = 10;
$allpage = ceil($kol_stena/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;


$result = mysql_query("SELECT * FROM `microblog_kom` WHERE `id_dnev` = '".$id."' order by id desc limit ".num($start).",".num($onpage)."");

while($kom = mysql_fetch_assoc($result)){
echo '<div class="'.($i++ % 2 ? 'link1' : 'link2').'">';
echo user($kom['id_user']) . ' ' . date2($kom['time']) . '<br/ >'; 

echo smile(parsebb(adminbb(nl2br($kom['msg'])))) . '<br/ >';

echo div;
}


echo link1 . '<b>Страницы:</b> ';
navigation($allpage,"blog.php?id=".$id."&p={p}",1,1);
echo div;
}

if ( isset($_POST['submit']) ) {
    
    $blog = num($_GET['id']);
    $info = mysql_fetch_assoc(mysql_query("SELECT * FROM `microblog` WHERE `id` = '".$blog."'"));
    $msg = html($_POST['msg']);
    
    if ( empty($msg) ) $err = 'Вы не ввели коментарий';
    $last = mysql_fetch_assoc(mysql_query("SELECT * FROM `microblog_kom` order by id desc limit 1"));
    if ($last['msg'] == $msg AND $last['id_user'] == $id_session) $err = 'такой коментарий уже есть';
    
    if (!empty($err)) {
        echo link1 . 'Ошибка, ' . $err . div;
    } else {
    
    if (mysql_query("INSERT INTO `microblog_kom` SET `msg` = '".$msg."', `id_user` = '".$id_session."',`id_dnev` = '".$id."', `time` = '".time()."'")) {
        if ($user['id'] != $info['id_user']) {
        $msg = 'Вашу запись откоментировали. Перейти к <b><a href="microblog/blog.php?id='.$id.'">Записи</b></a>. Откоментировал(а): '.user($id_session).'. Дата: '.date2(time()).'';
        mysql_query("INSERT INTO `uvedom` SET  `msg` = '".$msg."', `md` = 'microblog', `user_to` = '".$info['id_user']."', `user_ot` = '".$id_session."', `date` = '".time()."', `status` = '1'");
        }
        header('Location: blog.php?id='.$id.'&res=msgyes');
    } else {
        header('Location: blog.php?id='.$id.'&res=msgno');
    }
    
    }
    
}

echo div . link2 . 'Коментировать:<br/ ><form action="blog.php?id='.$id.'" method="post" name="form">';
echo '<textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/>';
echo '<input name="submit" type="submit" class="button" value="Написать" /></form>' . div;  



navig();
foot();
?>