<?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 = $lng['news'] . ' | ' . htmlentities($req1['name'], ENT_QUOTES, 'UTF-8');
			require_once('../incfiles/head.php');
			echo '<div class="phdr"><a href="./">' . $lng['news'] . '</a> | ' . htmlentities($req1['name'], ENT_QUOTES, 'UTF-8') . '</div>';
			
			$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE `refid`='$id'".($rights < 7 ? " AND `time`<='" . time() . "'":"")), 0);
			if($total) {
				if ($total > $kmess) 
					echo '<div class="topmenu">' . functions::display_pagination('index.php?id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
				$req = mysql_query("SELECT `id`, `img`, `reads` , `name`, `text`, `time` FROM `cms_news` WHERE `refid`='$id'".($rights < 7 ? " AND `time`<='" . time() . "'":"")."
				ORDER BY `time` DESC LIMIT $start,$kmess");				$i = 1;
				while (($row = mysql_fetch_assoc($req)) !== false) {
					echo '<div class="menu"><div style="float:left; width: 45px;">';
					if(empty($row['img'])) {
	echo'<img src="'.$home.'/images/news_empty.png" width="40" height="40" alt="FUTBOL.UZ" class="news_img">';
					   } else {
	echo'<img src="'.$row['img'].'" width="40" height="40" alt="FUTBOL.UZ" class="news_img">';					   
							  }
echo'</div><div style="margin-left:45px;"> <a href="' . $set['homeurl'] . '/news/index.php?act=view&amp;id=' . $row['id'] . '"> ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a>
<div style="color:gray"><i class="fa fa-eye"></i> '. $row['reads'] .'&nbsp;&nbsp;<i class="fa fa-comments-o"></i> ' . mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_comments` WHERE `refid`='" . $row['id'] . "'"), 0) . '  </div></div>';
echo '<div style="clear:both;"></div>';
echo '</div></div>';
					if($row['time'] > time())
						echo '<div class="sub func">' . $lng_news['begin_show_through'] . ': ' . timer($row['time'] - time()) . '</div>';
					echo '</div>';
					++$i;
				}
				echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
				if ($total > $kmess) {
					echo '<div class="topmenu">' . functions::display_pagination('index.php?id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
					echo '<p><form action="index.php" method="get">
					<input type="hidden" name="id" value="' . $id . '"/>
					<input type="text" name="page" size="2"/>
					<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
				}
			} 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="phdr">' . $lng['news'] . '</div>';

		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_cat`"), 0);
		if($total) {
			if ($total > $kmess) 
				echo '<div class="topmenu">' . 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 < 7 ? "`time`<='" . time() . "' AND ":"")."`refid`='{$row['id']}'"), 0);
				echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
				if(file_exists('../files/news/ico_cat_' . $row['id'] . '.png') !== false)
					echo '<img style="margin: 0 0 -3px 0;border: 0px;" src="../images/chelsea.png" alt="" width="16" height="16"/>&#160;';
				echo '<a href="index.php?id=' . $row['id'] . '">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a> (' . $count . ')';
				echo '</div>';
				++$i;
			}
			echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
			if ($total > $kmess) {
				echo '<div class="topmenu">' . functions::display_pagination('index.php?', $start, $total, $kmess) . '</div>';
				echo '<p><form action="index.php" method="get">
				<input type="text" name="page" size="2"/>
				<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
			}
		} else {
			echo '<div class="rmenu">' . $lng_news['not_nor_one_categories'] . '</div>';
		}
	}
	if($rights >= 7)
		echo '<div class="menu"><div class="func"><a href="../panel/index.php?act=news&amp;mod=news"> ' . $lng_news['add_news'] . '</a><br />
		<a href="../panel/index.php?act=news"> ' . $lng_news['touch_news'] . '</a></div></div>';
}
require_once("../incfiles/end.php");