<?php
	/*
	*by Krite (http://upcoder.net)
	*/
	define('_IN_JOHNCMS', 1);
	$set_blogs = unserialize($set['blogs']);
	function date_n($timestamp)
	{
		global $realtime;
		$datestamp = date('d.m.o в H:i',$timestamp);
		$today = date("d.m.o",$realtime);
		$yestoday = date("d.m.o",$realtime-24*3600);
		$datestamp = str_replace($today,"Сегодня",$datestamp);
		$datestamp = str_replace($yestoday,"Вчера",$datestamp);
		return $datestamp;
	}
	$prefix = $set['prefblog'];
	$headmod = 'blogs';
	require_once('../incfiles/core.php');
	if (!$user_id)
	{
		require_once('../incfiles/head.php');
		echo display_error('Только для зарегистрированных посетителей');
		require_once('../incfiles/end.php');
		exit;
	}
	if ($id && $id != $user_id)
	{
		// Если был запрос на юзера, то получаем его данные
		$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
		if (mysql_num_rows($req))
		{
			$user = mysql_fetch_assoc($req);
			$textl = 'Блог: ' . $user['name'];
		}
		else
		{
			require_once('../incfiles/head.php');
			echo display_error('Такого пользователя не существует');
			require_once("../incfiles/end.php");
			exit;
		}
	}
	else
	{
		$id = false;
		$textl = 'Личный блог';
		//$user = $datauser;
	}
	require_once('../incfiles/head.php');
	echo '<div class="phdr"><b>' . ($id ? 'Блог пользователя: '.$user['name'] : 'Личный блог') . '</b></div>';
	
	if (empty($id))
		$id = $user_id;
	
	$count = mysql_result(mysql_query('SELECT COUNT(*) FROM `'.$set['prefblog'].'blogs` WHERE `uid`='.$id.''.($rights > 7?'':' AND `mod`=0').''), 0);
	if($count>0)
	{
		$req = mysql_query("SELECT * FROM `" . $set['prefblog'] . "blogs` WHERE `uid`='".$id."'".($rights > 7?'':' AND `mod`=0')." ORDER BY `time` DESC LIMIT " . $start . "," . $kmess);
		while ($row = mysql_fetch_assoc($req))
		{
			echo ($i % 2) ? '<div class="list2"><img src="img/b2.png" alt="&raquo;" width="14" height="14" /> ' : '<div class="list1"><img src="img/b1.png" alt="&raquo;" width="14" height="14" /> ';
			echo '<a href="?act=view&amp;id='.$row['id'].'">'.$row['name'].'</a>';
				
			if (mb_strlen($row['text']) > 150)
			{
				$text = mb_substr($row['text'], 0, 150);
				$text = checkout($text, 0, 2);
				echo '<div class="sub">'.$text.'...</div>';
			}
			else
			{
				echo '<div class="sub">'.checkout($row['text'], 0, 2).'</div>';
			}
			$golos = @ mysql_result(mysql_query('SELECT COUNT(*) FROM `' . $set['prefblog'] . 'blogs_rat` WHERE `ref`='.$row['id'].';'), 0);
			$count = @ mysql_result(mysql_query('SELECT SUM(`count`) FROM `' . $set['prefblog'] . 'blogs_rat` WHERE `ref`='.$row['id'].';'), 0);
			$rating = @ floor($count / $golos);
			echo '<div class="func">Автор: <b>' . $row['from'] . '</b><br />';
			echo ($golos!=0) ? 'Рейтинг: <img src="img/star'.$rating.'.png" alt="'.$rating.'" width="50" height="10" /> | Голосов: <a href="?act=golosa&amp;id='.$row['id'].'">'.$golos.'</a><br />':'';
			echo 'Статья написана: '.date_n($row['time']).'<br />';
			echo 'Прочтений: '.$row['count'].'</div>';
			echo '</div>';
			++$i;
		}
		if ($count > $kmess)
		{
			echo '<p class="menu">' . pagenav('index.php?id='.$id.'&amp;', $start, $count, $kmess) . '<br/>';
			echo '<form action="" method="POST">
			<input type="text" name="page" size="2"/><input type="submit" value="GO"/></form></p>';
		}
	}
	else
	{
		echo '<div class="menu">Статей нет</div>';
	}
	$countss = mysql_result(mysql_query('SELECT COUNT(*) FROM `'.$set['prefblog'].'blogs_cat`;'), 0);
	if($user_id and $countss>0)echo '<div class="menu"><a href="?act=add">Добавить статью</a></div>';
	echo '<div class="bmenu"><a href="./">Блоги</a></div>';
	require_once('../incfiles/end.php');
?>