<?php

/*
////////////////////////////////////////////////////////////////////////////////
// JohnCMS                                                                    //
// Официальный сайт сайт проекта:      http://johncms.com                     //
// Дополнительный сайт поддержки:      http://gazenwagen.com                  //
////////////////////////////////////////////////////////////////////////////////
// JohnCMS core team:                                                         //
// Евгений Рябинин aka john77          john77@johncms.com                     //
// Олег Касьянов aka AlkatraZ          alkatraz@johncms.com                   //
//                                                                            //
// Информацию о версиях смотрите в прилагаемом файле version.txt              //
////////////////////////////////////////////////////////////////////////////////
*/

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;
				}
				
			}
			//echo $array['plus'] . $array['minus'];
			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;
			//echo $percent;
			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 * FROM `cms_news` WHERE `id`='$id'".($rights < 7 ? " AND `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="phdr"><h3>' . htmlentities($res1['name'], ENT_QUOTES, 'UTF-8') . '</h3></div>';
		echo '<div class="list1">';
		if(file_exists('../files/news/news_' . $id . '.jpg') !== false)
			echo '<img style="margin: 0 0 -3px 0;border: 0px;" src="../files/news/news_' . $id . '.jpg" alt=""/><div class="sub"></div>';
		$text = checkout($res1['text'], 1, 1);
		if ($set_user['smileys'])
			$text = smileys($text);
		echo $text;
		echo '</div>';
		
		if(isset($_POST['plus_x']) || isset($_POST['plus_y'])) {
			if($res1['user_id'] == $user_id) {
				$_SESSION['error'] = '<div class="list1 red">За свои новости голосовать нельзя!</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">Вы уже голосовали за данную новость!</div>';
				} else {
					mysql_query("INSERT INTO `cms_news_rating` SET
					`news`='$id',
					`user_id`='$user_id', `golos`='1';");
					$_SESSION['error'] = '<div class="list1 green">Ваш голос учьтен!</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">За свои новости голосовать нельзя!</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">Вы уже голосовали за данную новость!</div>';
				} else {
					mysql_query("INSERT INTO `cms_news_rating` SET
					`news`='$id',
					`user_id`='$user_id', `golos`='2';");
					$_SESSION['error'] = '<div class="list1 green">Ваш голос учьтен!</div>';
				}
			}
			Header('Location: index.php?act=view&id=' . $id);
			exit;
		}
		echo $_SESSION['error'];
		
		$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 = '<a href="../str/anketa.php?id=' . $rowuse['id'] . '">' . $rowuse['name'] . '</a>';
		} else {
			$name_use = 'Гость';
		}
		
		echo '<div class="list2"><span class="underline">Рейтинг:</span> ' . rating($id) . '<br />
		'.($user_id ? '<form action="index.php?act=view&amp;id=' . $id . '" method="post"><div>
		<input style="cursor:none; border: 0px; padding: 0;padding-top: 2px; margin: 0;" type="image" name="plus" value="15" src="../images/like_yes1.gif" alt="Нравится"/><input class="rate" style="cursor:none; border: 0px; margin: 0; padding: 0;padding-top: 2px;" type="image" src="../images/like_no1.gif" name="minus" alt="-"/>
		</div></form>':'<img class="ico" src="../images/like_yes1.gif" alt="+" /><img class="ico" src="../images/like_no1.gif" alt="-" /><br />').'
		</div><div class="gmenu">
		Добавил: ' . $name_use . '<br />
		Дата добавления: ' . date('d.m.o / H:i', $res1['time'] + $sdvigclock * 3600) . '<br />
		'.($res1['time'] > time()?'<div class="func">Начало показа через: ' . timer($res1['time'] - time()) . '</div>':'').'
		<a href="index.php?act=comments&amp;id=' . $id . '">Комментарии</a> (' . mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_comments` WHERE `refid`='$id'"), 0) . ')
		</div>
		' . ($rights >= 7 ? '<div class="menu func">
		<a href="../panel/index.php?act=mod_news&amp;mod=newsedit&amp;id=' . $id . '">Изменить</a><br />
		<a href="../panel/index.php?act=mod_news&amp;mod=delnews&amp;id=' . $id . '">Удалить</a><br />
		</div>':'') . '
		<div class="bmenu"><a href="index.php?id=' . $res1['refid'] . '">Назад</a></div>';
	} else {
		$textl = 'Новостная лента';
		require_once('../incfiles/head.php');
		echo display_error('Новость не существует!');
	}
} else {
	$textl = 'Новостная лента';
	require_once('../incfiles/head.php');
	echo display_error('Новость не выбрана!');
}

unset($_SESSION['error']);
