<?php

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

switch($act)
{
default:
$str['mesto'] = 'Дневники';
$str['block'] = $set['site'].' / Дневники';
include '../style/'.$style.'/head.php';
$dn = mysql_result(mysql_query("select count(*) from `users` where `where` = 'Дневники' and time > ".(time()-300).""),0);
$myblog = mysql_result(mysql_query("select count(*) from `blogs` where `id_user` = ".$user['id'].""),0);
if($myblog == 0)
{
	echo '<div class="mess"><a href="index.php?act=add">Создать дневник</a></div>';
}
else
{
	echo '<div class="mess"><a href="user.php?id='.$user['id'].'">Мой дневник</a></div>';
}
echo '<div class="where">
<b>Дневников</b>: '.mysql_result(mysql_query("select count(*) from `blogs`"),0).'<br />
<b>Записей</b>: '.mysql_result(mysql_query("select count(*) from `blogs_records`"),0).' </div>';
$query = mysql_query("select * from `blogs` order by `id` desc limit ".$page.",".$set['str']."");
$total = mysql_result(mysql_query("select count(*) from `blogs`"),0);
if($total == 0)
{
	echo '<div class="on">Записей нет!</div>';
	break;
}
while($row = mysql_fetch_assoc($query))
{
	$i++;
	if($i%2)
	{
		echo '<div class="forum1">';
	}
	else
	{
		echo '<div class="forum2">';
	}
	$u = mysql_fetch_assoc(mysql_query("select `login` from `users` where `id` = ".$row['id_user'].""));
	$rate=mysql_result(mysql_query("SELECT SUM(`rate`) FROM `blogs_records` WHERE `id`=".$row['id'].""),0);
	echo '<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="user.php?id='.$row['id_user'].'">'.$row['title'].'</a> ('.clock($row['time']).')<br/>
	Автор: <a href="../p/'.$u['login'].'">'.$u['login'].'</a><br />
	<b>Рейтинг</b>: '.$rate.'</div>';
}
if($total > $set['str'])
{
	pages($page, $total, 'index.php?', $set['str']);
}
if ($user)
{
echo '<div class="where">в дневниках '.$dn.' человек</div>';
}
break;
case 'add':
reg();
$str['mesto'] = 'Создать дневник';
$str['block'] = $set['site'].' / Дневники / Создать дневник';
include '../style/'.$style.'/head.php';
if($myblog == 0)
{
	echo '<div class="mess"><a href="index.php?act=add">Создать дневник</a></div>';
}
else
{
	echo '<div class="mess"><a href="user.php?id='.$user['id'].'">Мой дневник</a></div>';
}
$myblog = mysql_result(mysql_query("select count(*) from `blogs` where `id_user` = ".$user['id'].""),0);
if($myblog != 0)
{
	echo '<div class="mess">Вы уже создали свой дневник!</div>';
	break;
}
if(isset($_POST['title']) and !empty($_POST['title']))
{
              mysql_query("update `users` set `balls`=`balls`+1 where `id` = ".$user['id']."");
	echo '<div class="mess">Дневник успешно создан!<br />';
	mysql_query("INSERT INTO `blogs` (id_user,title,time) VALUES (".$user['id'].",'".protect($_POST['title'])."',".time().")");
	$ins_id = mysql_insert_id();
	echo '<a href="view.php?id='.$ins_id.'">Перейти &raquo;</a></div>';

	break;
}

echo '<div class="mess">
<form method="post" action="index.php?act=add"><div>
<b>Название дневника</b>:<br/>
<input name="title" type="text" value=""/><br/>
<input value="Отправить" type="submit"/></div></form></div>';

break;
}



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