<?php

defined('_IN_JOHNCMS') or die('Error: restricted access');
if(empty($_SESSION['error']))
	$_SESSION['error'] = '';
//Функция отображения рейтинга
function rating($id, $type = 0) {
	if(!$id)
		return '<img class="ico" src="../images/stars/stars_5.gif" alt="0" />';
	$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_rating` WHERE `news`='$id'"), 0); 
	if($total) {
		$query = mysql_query("SELECT `golos`, COUNT(*) as `count` FROM `cms_news_rating` WHERE `news`='$id' GROUP BY `golos`");
		$array['plus'] = 0;
		$array['minus'] = 0;
		while (($row = mysql_fetch_assoc($query)) !== false) {
			if(isset($row['golos']) && $row['golos'] == 1) $array['plus'] = $row['count'];
			else if(isset($row['golos']) && $row['golos'] == 2) $array['minus'] = $row['count'];
			else {
				$array['plus'] = 0;
				$array['minus'] = 0;
			}
		}
		if($array['plus'] > $array['minus']) {
			if($array['minus'] == 0) {
				if($array['plus'] == 1) $count = 60;
				else if($array['plus'] == 2) $count = 60;
				else if($array['plus'] == 3) $count = 60;
				else if($array['plus'] == 4) $count = 70;
				else if($array['plus'] == 5) $count = 70;
				else if($array['plus'] == 6) $count = 80;
				else if($array['plus'] == 7) $count = 80;
				else if($array['plus'] == 8) $count = 90;
				else if($array['plus'] == 9) $count = 90;
				else if($array['plus'] >= 10) $count = 100;
			} else {
				$count = round($array['minus'] / $array['plus'], 1) * 100;
				if($count == 0)	$count = 100;
				else $count = (100 - $count);
			}
		} else if($array['plus'] < $array['minus']) {
			if($array['plus'] == 0) {
				if($array['minus'] == 1) $count = 40;
				else if($array['minus'] == 2) $count = 40;
				else if($array['minus'] == 3) $count = 40;
				else if($array['minus'] == 4) $count = 30;
				else if($array['minus'] == 5) $count = 30;
				else if($array['minus'] == 6) $count = 20;
				else if($array['minus'] == 7) $count = 20;
				else if($array['minus'] == 8) $count = 10;
				else if($array['minus'] == 9) $count = 10;
				else if($array['minus'] >= 10) $count = 0;
			} else {
				$pr = (round($array['plus'] / $array['minus'], 1) * 100);
				$count = $pr;
			}
		} else
			$count = 50;
		$percent = $count;
		if($percent == 100)	$stars = 10;
		else if($percent < 100 && $percent >= 90) $stars = 9;
		else if($percent < 100 && $percent >= 90) $stars = 9;
		else if($percent < 90 && $percent >= 80) $stars = 8;
		else if($percent < 80 && $percent >= 70) $stars = 7;
		else if($percent < 70 && $percent >= 60) $stars = 6;
		else if($percent < 60 && $percent >= 50) $stars = 5;
		else if($percent < 50 && $percent >= 40) $stars = 4;
		else if($percent < 40 && $percent >= 30) $stars = 3;
		else if($percent < 30 && $percent >= 20) $stars = 2;
		else if($percent < 20 && $percent >= 10) $stars = 2;
		else if($percent < 10 && $percent > 0) $stars = 1;
		else if($percent == 0) $stars = 0;
		if($type == 0) return '<img class="ico" src="../images/stars/stars_'.$stars.'.gif" alt="&bull;" />';
		else return $percent;
	} else return '<img class="ico" src="../images/stars/stars_5.gif" alt="&bull;" />';
}
if($id) {
$query = mysql_query("SELECT `cms_news`.*, `cms_news_cat`.`name` as `catname`, `cms_news_cat`.`id` as `catid` FROM `cms_news` LEFT JOIN `cms_news_cat` ON `cms_news`.`refid`=`cms_news_cat`.`id` WHERE `cms_news`.`id`='$id'".($rights < 6 ? " AND `cms_news`.`time`<='" . time() . "'":"")." LIMIT 1;");
	if (mysql_num_rows($query)) {
		//Показываем новость
		$res1 = mysql_fetch_assoc($query);
$textl = htmlentities($res1['name'], ENT_QUOTES, 'UTF-8');
		require_once('../incfiles/head.php');

echo '<div class="menu"><a href="/"><img src="/ico/home.png"></a> » <a href="/news/news.php">Yangiliklar arxivi</a> » <a href="index.php?id=' . $res1['catid'] . '">' . htmlentities($res1['catname'], ENT_QUOTES, 'UTF-8') . '</a></div>';

echo '<div class="phdr"><center>' . htmlentities($res1['name'], ENT_QUOTES, 'UTF-8') . '</center></div>';
echo '<div class="menu"><center>';

// reklama
if (!empty($cms_ads[3])) {
echo $cms_ads[3];
}

$ncount = mysql_fetch_array(mysql_query("SELECT * FROM `cms_news` WHERE `id` ='$id'"));

if (!isset($_SESSION['cms_news']) || isset($_SESSION['cms_news'])&&$_SESSION['cms_news'] != $id) {
$_SESSION['cms_news'] = $id;
$newscount = intval($ncount['count']) + 1;
mysql_query("UPDATE `cms_news` SET `count` ='$newscount'WHERE `id` ='$id'");
}

		//Выводим автора новости
		$us = mysql_query("SELECT `id`, `name` FROM `users` WHERE `id` = '{$res1['user_id']}'");
		if (mysql_num_rows($us)) {
			$rowuse = mysql_fetch_assoc($us);
$name_use = $user_id ? '' . $rowuse['name'] . '' : $rowuse['name'];


		} else {
			$name_use = $lng['guest'];
		}

		//Выводим картинку
if(file_exists('../files/news/news_' . $id . '.jpg') !== false) echo '<center><img class="posts" src="/files/news/news_' . $id . '.jpg" width="300" height="200" style="background: #ffffff; padding: 3px;border: 1px solid #CCCCE5;border-radius: 2px;" alt=".">';

echo '<br><b>Joylandi: </b> <font color="red"> ' . date('d.m.o, H:i', $res1['time'] + (core::$system_set['timeshift'] + core::$user_set['timeshift']) * 3600) . '</font></center></center><hr>';

		$text = functions::checkout($res1['text'], 1, 1);
		if ($set_user['smileys'])
			$text = functions::smileys($text);
		echo $text;
echo '<div style="clear:both;"></div></div>';

		//Обрабатываем голосование
		if(isset($_POST['plus_x']) || isset($_POST['plus_y'])) {
			if($res1['user_id'] == $user_id) {
				$_SESSION['error'] = '<div class="list1 red">' . $lng_news['my_vote'] . '!</div>';
			} else {
				$plus = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_rating` WHERE `news`='$id' AND `user_id`='$user_id' LIMIT 1;"), 0);
				if($plus) {
					$_SESSION['error'] = '<div class="list1 red">' . $lng_news['already_voted'] . '!</div>';
				} else {
					mysql_query("INSERT INTO `cms_news_rating` SET
					`news`='$id',
					`user_id`='$user_id', `golos`='1';");
					$_SESSION['error'] = '<div class="list1 green">' . $lng_news['ok_vote'] . '!</div>';
				}
			}
			Header('Location: index.php?act=view&id=' . $id);
			exit;
		} else if(isset($_POST['minus_x']) || isset($_POST['minus_y'])) {
			if($res1['user_id'] == $user_id) {
				$_SESSION['error'] = '<div class="list1 red">' . $lng_news['my_vote'] . '!</div>';
			} else {
				$plus = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_rating` WHERE `news`='$id' AND `user_id`='$user_id' LIMIT 1;"), 0);
				if($plus) {
					$_SESSION['error'] = '<div class="list1 red">' . $lng_news['already_voted'] . '!</div>';
				} else {
					mysql_query("INSERT INTO `cms_news_rating` SET
					`news`='$id',
					`user_id`='$user_id', `golos`='2';");
					$_SESSION['error'] = '<div class="list1 green">' . $lng_news['ok_vote'] . '!</div>';
				}
			}
			Header('Location: index.php?act=view&id=' . $id);
			exit;
		}
		//Сообщение об оставленном голосе
		echo $_SESSION['error'];

echo '</div>';
echo '<div class="menu">
<div class="menu" align="center">
<!-- LikeBtn.com BEGIN -->
<span class="likebtn-wrapper" data-theme="custom" data-icon_l_c="#ffffff" data-icon_l_c_v="#ffffff" data-icon_d_c="#ffffff" data-icon_d_c_v="#ffffff" data-label_c="#ffffff" data-label_c_v="#ffffff" data-counter_l_c="#ffffff" data-counter_d_c="#ffffff" data-bg_c="#9f3089" data-bg_c_v="#9f3089" data-brdr_c="#db54f3" data-i18n_like="Yoqdi" data-identifier="nwEnt2362" data-show_dislike_label="true" data-share_enabled="false" data-share_size="small" data-loader_show="true"   data-i18n_dislike="Yoqmadi" data-i18n_after_like="Yoqdi" data-i18n_after_dislike="Yoqmadi" data-i18n_like_tooltip="Menga bu yoqdi" data-i18n_dislike_tooltip="Menga bu yoqmadi" data-i18n_unlike_tooltip="Menga endi yoqmaydi" data-i18n_undislike_tooltip="Rad etish"></span>
<script>(function(d,e,s){if(d.getElementById("likebtn_wjs"))return;a=d.createElement(e);m=d.getElementsByTagName(e)[0];a.async=1;a.id="likebtn_wjs";a.src=s;m.parentNode.insertBefore(a, m)})(document,"script","//w.likebtn.com/js/w/widget.js");</script>
<!-- LikeBtn.com END -->
</div>
<hr><center><b> Do\'stlaringizga ulashing </b></center><div class="it-share-93f60bacb3f5ffd3f41380e5a80d44fc horizontal center"></div><script type="text/javascript" async charset="utf-8" src="//share.itraffic.su/share.js?buttons=tg,wa,vi,ok,fb,vk,bm&size=normal&theme=1&align=center&direction=horizontal&add=0&key=93f60bacb3f5ffd3f41380e5a80d44fc"></script>
<hr><center>Ko\'rildi: <b>'. $ncount['count'] .'</b> | Joyladi: ' . $name_use . '</center>
		'.($res1['time'] > time()?'<div class="func">' . $lng_news['begin_show_through'] . ': ' . timer($res1['time'] - time() + (core::$system_set['timeshift'] * 3600)) . '</div>':'').' 
		
		</div>
<div class="phdr"><center>Bildirilgan izohlar</center></div><div class="menu">Barcha izohlar: <b>' . mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_comments` WHERE `refid`='$id'"), 0) . '</b><hr></div>';

include 'comments.php';
if ($id){
$query = mysql_query("SELECT `cms_news`.*, `cms_news_cat`.`name` as `catname`, `cms_news_cat`.`id` as `catid` FROM `cms_news` LEFT JOIN `cms_news_cat` ON `cms_news`.`refid`=`cms_news_cat`.`id` WHERE `cms_news`.`id`='$id'".($rights < 6 ? " AND `cms_news`.`time`<='" . time() . "'":"")." LIMIT 1;");
if (mysql_num_rows($query)) {
		$res1 = mysql_fetch_assoc($query);
$bolim= '' . $res1['catid'] . '';
$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE `refid`= '$bolim'"), 0);
echo '<div class="phdr"><center>Mavzuga oid yangiliklar</center></div>';
echo '<div class="menu">';
if ($total) {
$req = mysql_query("SELECT * FROM `cms_news` WHERE `refid`= '$bolim' ORDER BY `time` DESC LIMIT 5");
$i = 1;
while (($tayota = mysql_fetch_assoc($req)) !== false) {
$comm = mysql_result(mysql_query("select count(*) from `cms_news_comments` where `refid` = '".$tayota['id']."'"), 0);
echo '<a href="'.$home.'/news/index.php?act=view&amp;id='.$tayota['id'].'"><img src="/ico/link.png" alt="link"/> '.$tayota['name'].'</a><hr>';
++$i;
}
}
}
}
echo '</div>';

echo '
' . ($rights >= 6 ? '<div class="menu"><div class="func">
		<a href="../panel/index.php?act=news&amp;mod=newsedit&amp;id=' . $id . '">' . $lng['edit'] . '</a><br />
		<a href="../panel/index.php?act=news&amp;mod=delnews&amp;id=' . $id . '">' . $lng['delete'] . '</a><br />
		</div></div>':'') . '
';
	} else {
		$textl = $lng['news'];
		require_once('../incfiles/head.php');

		echo functions::display_error($lng_news['news_does_not_exist']);
	}
} else {
	$textl = $lng['news'];
	require_once('../incfiles/head.php');

	echo functions::display_error($lng_news['news_is_not_chose']);
}
unset($_SESSION['error']);