<?php
define('_IN_JOHNCMS', 1);
require_once('../incfiles/core.php');
$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'];
function nbs($id)
{
	global $user_id,$prefix,$realtime;
	$out = false;
	$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . $prefix . "blogs` WHERE `time`>'" . ($realtime - (3 * 24 * 3600)) . "'".($rights > 7?'':' AND `mod`=0').";"), 0);
	$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . $prefix . "blogs` LEFT JOIN `" . $prefix . "blogs_rdm` ON `" . $prefix . "blogs`.`id`=`" . $prefix . "blogs_rdm`.`topic_id` AND `" . $prefix . "blogs_rdm`.`user_id`='" . $id . "' WHERE `" . $prefix . "blogs_rdm`.`topic_id` Is Null OR `" . $prefix . "blogs`.`time`>`" . $prefix . "blogs_rdm`.`time`"), 0);
	$out .= '<div class="menu"><a href="?act=new">Новые статьи</a> ['.$count.']</div>';
	$out .= $user_id ? '<div class="menu"><a href="?act=ns">Непрочитанные</a>&nbsp;' . ($total ? '[' . $total . ']' : '') . '</div>':'';
	return $out;
}
////////////////////////////////////////////////////////////
// Ограничиваем доступ к блогам                          //
////////////////////////////////////////////////////////////
$error = '';
if (!$set['mod_blogs'] && $rights < 7)
    $error = 'Блоги закрыты';
elseif ($set['mod_blogs'] == 1 && !$user_id)
    $error = 'Доступ к блогам открыт только <a href="../login.php">авторизованным</a> посетителям';
if ($error)
{
    require_once('../incfiles/head.php');
    echo '<div class="rmenu">' . $error . '</div>';
    require_once('../incfiles/end.php');
    exit;
}

$headmod = $id ? 'blogs,' . $id : 'blogs';

// Заголовки блогов
if (empty($id))
{
    $textl = 'Блоги';
}
else
{
    $req = mysql_query('SELECT `name` FROM `'.$set['prefblog'].'blogs_cat` WHERE `id`= ' . $id . ' LIMIT 1;');
    $res = mysql_fetch_assoc($req);
    $hdr = strtr($res['name'], array (
        '&quot;' => '',
        '&amp;' => '',
        '&lt;' => '',
        '&gt;' => '',
        '&#039;' => ''
    ));
    $hdr = mb_substr($hdr, 0, 60);
    $hdr = checkout($hdr);
    $textl = mb_strlen($res['name']) > 60 ? $hdr . '...' : $hdr;
}
$array = array (
	'add',
	'view',
    'new',
	'golosa',
	'atach',
	'komm',
	'file',
	'edit',
	'del',
	'ns',
	'cleans',
	'alf',
	'top'
);
if (in_array($act, $array) && file_exists($act . '.php'))
{
    require_once($act . '.php');
}
else
{
    require_once('../incfiles/head.php');
    // Если блоги закрыты, то для Админов выводим напоминание
    if (!$set['mod_blogs'])
        echo '<p class="rmenu"><b>Блоги закрыты!</b></p>';
    if ($id)
	{
		echo '<div class="phdr">'.$textl.'</div>';
		$count = mysql_result(mysql_query('SELECT COUNT(*) FROM `'.$set['prefblog'].'blogs` WHERE `ref`='.$id.''.($rights > 7?'':' AND `mod`=0').''), 0);
		if($count>0)
		{
			$req = mysql_query("SELECT * FROM `" . $set['prefblog'] . "blogs` WHERE `ref`='".$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>';
		}
		echo '<div class="gmenu"><a href="./">Назад</a></div>';
    }
	else
	{
        echo '<div class="phdr">Блоги | <a href="?act=top">Топ статей</a></div>';
		echo nbs($user_id);
		$count = mysql_result(mysql_query('SELECT COUNT(*) FROM `'.$set['prefblog'].'blogs_cat`;'), 0);
		if($count>0)
		{
			$res = mysql_query("SELECT * FROM `" . $set['prefblog'] . "blogs_cat` ORDER BY `realid` LIMIT " . $start . "," . $kmess);
			while ($row = mysql_fetch_assoc($res))
			{
				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="?id='.$row['id'].'">'.$row['name'].'</a> [' . mysql_result(mysql_query("SELECT COUNT(`id`) FROM `" . $set['prefblog'] . "blogs` WHERE `ref`='" . $row['id'] . "'".($rights > 7?'':' AND `mod`=0').";"), 0) . ']';
				if($row['text'])echo '<div class="sub">'.$row['text'].'</div>';
				echo '</div>';
				++$i;
			}
			if ($count > $kmess)
			{
				echo '<p class="menu">' . pagenav('index.php?', $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>';
		}
		if($user_id and $count>0)echo '<div class="menu"><a href="?act=add">Добавить статью</a></div>';
		$cfile = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . $prefix . "blogs_files`;"), 0);
		echo '<div class="gmenu"><a href="?act=file">Файлы блогов</a> ['.$cfile.']</div>';
		echo '<div class="list1"><form action="search.php" method="post">';
		echo '<input type="text" value="' . ($search ? checkout($search) : '') . '" name="search" /> ';
		echo '<input type="submit" value="Поиск" name="submit" /><br />';
		echo '</form></div>';
    }
}
if($rights > 7){echo '<div class="bmenu"><a href="../panel/index.php?act=mod_blogs">В админку</a></div>';}

require_once("../incfiles/end.php");
?>