<?php

define('_IN_JOHNCMS', 1);
require_once('../incfiles/core.php');
$headmod = 'news,'.$id;
$lng_news = core::load_lng('news');
function timer($var = '') {
	global $lng_news;
	if($var <= 0)
		return;
	if(86400 > $var) {
		if(3600 > $var) {
			if(60 > $var) {
				$time = $var;
				return $time . ' ' . $lng_news['sec'] . '.';
			} else if(60 <= $var && (60 * 2) > $var) {
				return $lng_news['one_minute'];
			}
			$hours = (60 - ceil((3600 - $var) / 60));
			return $hours . ' ' . $lng_news['minute'] . '.';
		} else if(3600 <= $var && (3600 * 2) > $var) {
			return $lng_news['one_hour'];
		}
		$days = (24 - ceil((86400 - $var) / 3600));
		return $days . ' ' . $lng_news['hour'] . '.';
	} else if(86400 <= $var && (86400 * 2) > $var) {
		return $lng_news['one_day'];
	}
	$days = ceil($var / 86400);
	return $days . ' дн.';
}
$mods = array (
'view',
'comments'
);
//Проверка выбора функции
if ($act && ($key = array_search($act, $mods)) !== false && file_exists('includes/' . $mods[$key] . '.php')) {
    require('includes/' . $mods[$key] . '.php');
} else {
	if($id) {
		$query = mysql_query("SELECT * FROM `cms_news_cat` WHERE `id`='$id' LIMIT 1;");
		if (mysql_num_rows($query)) {
			$req1 = mysql_fetch_assoc($query);
			
$textl =  htmlentities($req1['name'], ENT_QUOTES, 'UTF-8');
			require_once('../incfiles/head.php');
include ('nbolim.php');
echo '<div class="phdr"><center>' . htmlentities($req1['name'], ENT_QUOTES, 'UTF-8') . '</center></div>';
			
$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE `refid`='$id'".($rights < 6 ? " AND `time`<='" . time() . "'":"")), 0);
			if($total) {
$req = mysql_query("SELECT `id`, `name`, `text`, `time` FROM `cms_news` WHERE `refid`='$id'".($rights < 6 ? " AND `time`<='" . time() . "'":"")."
				ORDER BY `time` DESC LIMIT " . $start . "," . $kmess);
$i = 0;
echo '<div class="menu">';
				while (($row = mysql_fetch_assoc($req)) !== false) {
echo '<a href="index.php?act=view&amp;id=' . $row['id'] . '"><img src="/ico/feed.png"> ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a><hr>';
					echo functions::checkout($text, 2, 1);
					if($row['time'] > time())
						echo '<div class="sub func">' . $lng_news['begin_show_through'] . ': ' . timer($row['time'] - time()) . '</div>';
					++$i;
				}
					echo '</div>';
				if ($total > $kmess) {
echo '<div class="menu"><center>' . functions::display_pagination('index.php?id=' . $id . '&amp;', $start, $total, $kmess) . '</center></div>';
				}
			} else {
				echo '<div class="rmenu">' . $lng_news['news_no'] . '</div>';
			}
		} else {
			echo '<div class="rmenu">' . $lng_news['category_does_not_exist'] . '</div>';
		}
	} else {
		$textl = $lng['news'];
		require_once('../incfiles/head.php');
echo '<div class="header"><b>Bo\'limlar</b></div>';

		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_cat`"), 0);
		if($total) {
			if ($total > $kmess) 
echo '<div class="menu">' . functions::display_pagination('index.php?', $start, $total, $kmess) . '</div>';
			$req = mysql_query("SELECT `cms_news_cat`.`name`, `cms_news_cat`.`id`
			FROM `cms_news_cat`
			ORDER BY `cms_news_cat`.`realid` ASC LIMIT "  . $start . "," . $kmess);
			$i = 1;
			while (($row = mysql_fetch_assoc($req)) !== false) {
$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE ".($rights < 6 ? "`time`<='" . time() . "' AND ":"")."`refid`='{$row['id']}'"), 0);
echo $i % 2 ? '<div class="menu">' : '<div class="menu">';
				if(file_exists('../files/news/ico_cat_' . $row['id'] . '.png') !== false)
					echo '<img style="margin: 0 0 -3px 0;border: 0px;" src="../files/news/ico_cat_' . $row['id'] . '.png" alt="" width="16" height="16"/>&#160;';
echo '&raquo; <a href="index.php?id=' . $row['id'] . '">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a> (' . $count . ')';
				echo '</div>';
				++$i;
			}
			if ($total > $kmess) {
echo '<div class="menu"><center>' . functions::display_pagination('index.php?', $start, $total, $kmess) . '</center></div>';
			}
		} else {
			echo '<div class="rmenu">' . $lng_news['not_nor_one_categories'] . '</div>';
		}
	}
if($rights >= 6)
echo '<div class="menu"><div class="func"><a href="../panel/index.php?act=news&amp;mod=news">Yangilik qo\'shish</a><br />
</div></div>';
}
require_once("../incfiles/end.php");