<?php
define('MTOP', 1);

require_once('../system/connect.php');
require_once('../system/core.php');
require_once('../system/function.php');

switch($act){
default:
$title = 'Новости';
require_once('../head.php');
echo '<div class="title2">'.$title.'</div>';

if($user_data['level'] == 3) { echo '<div class="main"><a class="link-buttoms" href="'.$HOME.'/news/create">Создать новую новость</a></div>';}

$page_top = $set['page_top'];
$all = mysql_num_rows(mysql_query("SELECT * FROM `news`"));
if($all != 0){

$total=intval(($all-1)/$page_top)+1;
$page=abs(intval($_GET['page']));
if(empty($page) OR $page < 0) $page = 1;
if($page > $total) $page = $total;
$past=intval($all/$page_top);
$start=$page*$page_top-$page_top;
$top = mysql_query("SELECT * FROM `news` ORDER BY `time` DESC LIMIT ".$start.",".$page_top."");
while($row = mysql_fetch_array($top)){
echo '<div class="main"><b><a href="'.$HOME.'/news/'.$row['id'].'">'.$row['name'].'</a></b> <span class="tb" style="float: right;">'.data($row['time']).'</span><br />
'.$row['text'].'<br />
Автор: '.nick($row['user']).'</div>';
}

}else{
	echo '<div class="info">Пока новостей нет.</div>';
}


break;

case 'create':


$title = 'Создать новую новость';
require_once('../head.php');
echo '<div class="title2"><a class="link-buttoms" href="'.$HOME.'/news">Новости</a>  <span class="tb">'.$title.'</span></div>';

level(3);

if(isset($_REQUEST['create'])) {
	
	 $name = filter($_POST['name']);
	 $text = filter($_POST['text']);

if(empty($name)) { $err = 'Введите название новости!'; }	
if(mb_strlen($name) < 5) { $err = 'Введите название новости от 5 символов!'; }	
if(empty($text)) { $err = 'Введите текст новости!'; }	
if(mb_strlen($text) < 5) { $err = 'Введите текст новости от 5 символов!'; }	

if($err){
echo '<div class="error">'.$err.'</div>';	
}else{
mysql_query("INSERT INTO `news` SET `name` = '".$name."', `text` = '".$text."', `user` = '".$user_data['id']."', `time` = '".time()."'");
$lastID = mysql_insert_id();
header('location: '.$HOME.'/news/'.$lastID.'');
}
	 
} 

echo '<div class="main">
<form action="" method="POST">
<input type="text" name="name" placeholder="Название новости"><br />
<textarea name="text" placeholder="Текст новости" style="margin: 5px 0px; width: 451px; height: 95px;"></textarea><br />
<input type="submit" name="create" value="Создать новость">
</form></div>';


break;

case 'view':
$new = mysql_fetch_assoc(mysql_query("SELECT * FROM `news` WHERE `id` = '".$id."'"));
$title = $new['name'];
require_once('../head.php');

if($user_data){
$views = mysql_fetch_assoc(mysql_query("SELECT `user`, `news` FROM `newsView` WHERE `user` = '".$user_data['id']."' and `news` = '".$new['id']."' "));
if($views == 0) {
mysql_query("INSERT INTO `newsView` SET `news` = '".$id."', `user` = '".$user_data['id']."', `time` = '".time()."'");
}else{
mysql_query("UPDATE `newsView` SET `time` = '".time()."' WHERE `user` = '".$user_data['id']."' LIMIT 1");
}
}
echo '<div class="title2"><a class="link-buttoms" href="'.$HOME.'/news">Новости</a> <span class="tb">'.$title.'</span></div>';
echo '<div class="main">
'.$new['text'].'</div>
<div class="main"><b>Создана:</b> '.data($new['time']).'<br />
<b>Автор:</b> '.nick($new['user']).'<br />
<b>Просмотров:</b> <a href="'.$HOME.'/news/'.$id.'/views">'.mysql_result(mysql_query("SELECT COUNT(*) FROM `newsView` WHERE `news` = '".$id."' "),0).'</a></div>';
break;

case 'views':
$new = mysql_fetch_assoc(mysql_query("SELECT * FROM `news` WHERE `id` = '".$id."'"));
$title = $new['name'];
require_once('../head.php');


echo '<div class="title2"><a class="link-buttoms" href="'.$HOME.'/news/'.$id.'">'.$new['name'].'</a> <span class="tb">Просмотров</span></div>';


$rows = mysql_query("SELECT * FROM `newsView` WHERE `news` = '".$id."' ORDER BY `time` DESC LIMIT 50");
while($a = mysql_fetch_assoc($rows))
{
echo '<div class="main">'.nick($a['user']).' '.data($a['time']).'</div>';
}

break;

}


require_once('../foot.php');
?>