<?php

  include '../../system/core.php';
  $_title = 'Новости';
  include '../../system/header.php';

  if(!$user['id']){
  header('Location: /index.php/?m=guest'); exit;
  }


  if($user['menu'] == 1)
  {
  include '../../modules/menu/index.php';
  }else{

  }
	
  switch($_GET['m']){
  
  #Главная
  default:

  echo '<div class="tegi"><b>Новости ресурса</b></div></div>';
 echo '</div>'; 
 
 if( $user['status'] >= 3){echo '<span><div class="post1"><a href="?m=add">Добавить новость</a></span>';} 
  echo '</div></div>';
  
  
  $cop = $user['on_page'];
  $ca = mysql_result(mysql_query("SELECT COUNT(*) FROM `news`"),0);
  $cp = ceil($ca/$cop);
  if(isset($_GET['p']) && is_numeric($_GET['p'])){
  $p = $_GET['p'];
  if($_GET['p']>$cp) $p = $cp;
  if($_GET['p']<1)$p = 1;
  }else{
  $p = 1;
  }
  $start = $p*$cop-$cop;
  $news = mysql_query("SELECT * FROM `news` ORDER BY `time` DESC LIMIT $start,$cop");
  while($news2 = mysql_fetch_assoc($news)){
  echo '<div class="post1">Текст: '.smiles(bbcode($news2['text'])).'<br>
  Добавил: '.ustatus($news2['author']).' <a href="/us'.$news2['author'].'">'.uname($news2['author']).'</a> '.uaccess($news2['author']).' ('.times($news2['time']).')';
  
  if($user['status'] >= 2) echo ' [<a href="?m=del&amp;id='.$news2['id'].'">уд</a>] [<a href="?m=edit&amp;id='.$news2['id'].'">ред</a>]';
  
  echo '<br><span><a href="/news/com'.$news2['id'].'">Комментарии</a> ('.mysql_result(mysql_query('select count(`id`) from `news_com` where `news` = "'.$news2['id'].'"'),0).')</span></div>';

  }

  echo '<div class="levo">Стр. ';
  if($p-2>0) echo '<a href="/news?p=1"><<</a> ';
  if($p-1>0) echo '<a href="/news?p='.($p-1).'"><</a> ';

  if($p-3>0) echo '<a href="/news?p='.($p-3).'">'.($p-3).'</a> | ';
  if($p-2>0) echo '<a href="/news?p='.($p-2).'">'.($p-2).'</a> | ';  
  if($p-1>0) echo '<a href="/news?p='.($p-1).'">'.($p-1).'</a> | ';
  
  echo '<b>'.$p.'</b>';
  if($p+1<=$cp) echo ' | <a href="/news?p='.($p+1).'">'.($p+1).'</a>';
  if($p+2<=$cp) echo ' | <a href="/news?p='.($p+2).'">'.($p+2).'</a>';
  if($p+3<=$cp) echo ' | <a href="/news?p='.($p+3).'">'.($p+3).'</a>';

  if($p+1<=$cp) echo ' <a href="/news?p='.($p+1).'">></a>';
  if($p+2<=$cp) echo ' <a href="/news?p='.ceil($ca/$cop).'">>></a>';
  echo '</div></div>';
  
  break;
  
  case 'del':
  
  if($user['status'] < 2){
  header('Location: /news'); exit;
  }

  $id = TextGuard($_GET['id']);
  $news = mysql_fetch_array(mysql_query('select * from `news` where `id` = "'.$id.'" limit 1'));

  if(empty($id) or !is_numeric($id) or $news == 0){
  header('Location: /news'); exit;
  }

  $news_com = mysql_query('select * from `news_com` where `news` = "'.$id.'"');
  while($news_com2 = mysql_fetch_array($news_com)){
  mysql_query("DELETE FROM `news_com` WHERE `id` = '".$news_com2['id']."'");
  }

  mysql_query("DELETE FROM `news` WHERE `id` = '$id'");
  
  header('Location: /news'); exit;
   
  break;
  case 'add':
  if($user['status'] < 3){
  header('Location: /news'); exit;
  }

  

  echo '<div class="verh"><a href="/news"><b>Новости</a> | Добавить новость</b></div>';
  
 
  if(isset($_POST['submit'])){
  
  $text = TextGuard($_POST['text']);
  if(empty($text)) $err[] = 'Имя осталось пустым...';
  $name = TextGuard($_POST['name']);
  if(empty($name)) $err[] = 'Сообщение осталось пустым...'; 
  
  if(!$err){
  		mysql_query("INSERT INTO `news`(
			`author`,
		        `name`,	
			`text`,
      `time`
 			)VALUES(
			'".$user['id']."',
		        '".$name."',	
			'".$text."',
			'".time()."'
			)"); 

  header('Location: /news'); exit;
  }else{
  echo '<div class="pet">';
  foreach($err as $err_info){
  echo $err_info.'<br>';
  }
  echo '</div>';
  }
  }

  echo '<div class="news"><div class="newss">
  <form action="/news?m=add" method="post">
  Имя:<br><textarea style="width:98%" name="name" rows="3"></textarea><br> 
  Текст:<br><textarea style="width:98%" name="text" rows="3"></textarea><br>
  <input type="submit" value="Добавить" name="submit"/>
  </form></div></div>';
 
  break;
  case 'edit':
 
  if($user['status'] < 3){
  header('Location: /news'); exit;
  }

  $id = TextGuard($_GET['id']);
  $news = mysql_fetch_array(mysql_query('select * from `news` where `id` = "'.$id.'" limit 1'));

  if(empty($id) or !is_numeric($id) or $news == 0){
  header('Location: /news'); exit;
  }

  echo '<div class="verh"><a href="/news"><b>Новости</a> | Редактировать новость</b></div>';
include '../../system/nvg.php';
  if(isset($_POST['submit'])){
  
  $text = TextGuard($_POST['text']);
  if(empty($text)) $err[] = 'Сообщение осталось пустым...';
   $name = TextGuard($_POST['name']);
  if(empty($name)) $err[] = 'Сообщение осталось пустым...';
  if(!$err){
  mysql_query("UPDATE `news` SET `text`='".$text."' , `name`='".$name."' WHERE `id` = '".$id."' limit 1");
  header('Location: /news'); exit;
  }else{
  echo '<div class="pet">';
  foreach($err as $err_info){
  echo $err_info.'<br>';
  }
  echo '</div>';
  }
  }

  echo '<div class="mist">
  <form action="/news?m=edit&amp;id='.$id.'" method="post">
 Имя:<br><textarea style="width:98%" name="name" rows="3">'.$news['name'].'</textarea><br> 
  Текст:<br><textarea style="width:98%" name="text" rows="3">'.$news['text'].'</textarea><br>
  <input type="submit" value="Добавить" name="submit"/>
  </form></div>';

  break;
  case 'com':
  
  $id = TextGuard($_GET['id']);
  $news = mysql_fetch_array(mysql_query('select * from `news` where `id` = "'.$id.'" limit 1'));

  if(empty($id) or !is_numeric($id) or $news == 0){
  header('Location: /news'); exit;
  }

 
 echo '<div class="tegi">Новости</div>'; 
  echo '<div class="news"><div class="newss">Текст: '.smiles(bbcode($news['text'])).'<br>
  Добавил: '.ustatus($news['author']).' <a href="/us'.$news['author'].'">'.uname($news['author']).'</a> '.uaccess($news['author']).' ('.times($news['time']).')</div></div><div class="tegi"><font color="white">Комментарии:</font></div>
  <div class="glav">
  Сообщение:<br><form action="/news/com'.$id.'" method="post">
  <textarea style="width:98%" name="text" rows="3"></textarea><br>
  <input type="submit" value="Добавить" name="submit"/>
  </form></div>';

  if(isset($_POST['submit'])){
  
  $text = TextGuard($_POST['text']);
  if(empty($text)) $err[] = 'Сообщение осталось пустым...';
 
  $ncm = mysql_query("SELECT * FROM `news_com` WHERE `author`='".$user['id']."' ORDER BY `time` desc");
  while($ncm2 = mysql_fetch_assoc($ncm)){  
  $ncm_timeout = $ncm2['time'];
  
    if((time()-$ncm_timeout) < 5) $err[] = 'Писать можно только раз в 5 секунд...';

  } 
  if(mysql_result(mysql_query('select count(`id`) from `forum_m` where `author` = "'.$user['id'].'"'),0) < 10) $err[] = 'Наберите 10 постов на форуме, чтобы оставить свой коментарий';
         
    
  $ncm_rep = mysql_fetch_array(mysql_query('select * from `news_com` where `author` = "'.$user['id'].'" and `text` = "'.$text.'"'));
  
  if($ncm_rep != 0) $err[] = 'Вы уже писали подобное сообщение...';

  if(!$err){
  		mysql_query("INSERT INTO `news_com`(
			`news`,
			`author`,
			`text`,
      `time`
 			)VALUES(
			'".$id."',
			'".$user['id']."',
			'".$text."',
			'".time()."'
			)"); 

  header('Location: /news/com'.$id.''); exit;
  }else{
  echo '<div class="err">';
  foreach($err as $err_info){
  echo $err_info.'<br>';
  }
  echo '</div>';
  }
  }

  $cop = 10;
  $ca = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_com` where `news` = '".$id."'"),0);
  $cp = ceil($ca/$cop);
  if(isset($_GET['p']) && is_numeric($_GET['p'])){
  $p = $_GET['p'];
  if($_GET['p']>$cp) $p = $cp;
  if($_GET['p']<1)$p = 1;
  }else{
  $p = 1;
  }
  $start = $p*$cop-$cop;
  
  
  if(isset($_GET['dp'])){
  
  $pid = TextGuard($_GET['pid']);
  
  if(empty($pid) or !is_numeric($pid)){
  header('Location: ?'); exit;
  }

  $news_com = mysql_fetch_array(mysql_query('select * from `news_com` where `id` = "'.$pid.'"'));

  if($news_com == 0){
  header('Location: ?'); exit;
  }

  
  if($user['status'] < 1){
  header('Location: /news/com'.$id.''); exit;
  }

  mysql_query("DELETE FROM `news_com` WHERE `id` = '$pid'");
  }

  
  
  
  $news_com = mysql_query("SELECT * FROM `news_com` where `news` = '".$id."' ORDER BY `time` DESC LIMIT $start,$cop");
  while($news_com2 = mysql_fetch_assoc($news_com)){
  echo '<div class="post1">'.ustatus($news_com2['author']).' <a href="/us'.$news_com2['author'].'">'.uname($news_com2['author']).'</a> '.uaccess($news_com2['author']).' ('.times($news_com2['time']).')';
  
  if($user['status'] >= 2) echo ' [<a href="/news/com'.$id.'?dp&amp;pid='.$news_com2['id'].'">уд</a>]</br>';
  /////////////////////ВЫВОД ЗВЁЗД////////////////////////

 

  ////////////////////////////////////////////////////////
  
  echo ''.smiles(bbcode($news_com2['text'])).'</div>';
  }
  
 
    echo '<div class="levo">Стр. ';
  if($p-2>0) echo '<a href="/news?p=1"><<</a> ';
  if($p-1>0) echo '<a href="/news?p='.($p-1).'"><</a> ';

  if($p-3>0) echo '<a href="/news/com'.$id.'?p='.($p-3).'">'.($p-3).'</a> | ';
  if($p-2>0) echo '<a href="/news/com'.$id.'?p='.($p-2).'">'.($p-2).'</a> | ';  
  if($p-1>0) echo '<a href="/news/com'.$id.'?p='.($p-1).'">'.($p-1).'</a> | ';
  
  echo '<b>'.$p.'</b>';
  if($p+1<=$cp) echo ' | <a href="/news/com'.$id.'?p='.($p+1).'">'.($p+1).'</a>';
  if($p+2<=$cp) echo ' | <a href="/news/com'.$id.'?p='.($p+2).'">'.($p+2).'</a>';
  if($p+3<=$cp) echo ' | <a href="/news/com'.$id.'?p='.($p+3).'">'.($p+3).'</a>';

  if($p+1<=$cp) echo ' <a href="/news/com'.$id.'?p='.($p+1).'">></a>';
  if($p+2<=$cp) echo ' <a href="/news/com'.$id.'?p='.ceil($ca/$cop).'">>></a>';
  echo '</div>';
  echo '</div>';
  
  }

  

  include '../../system/footer.php';

?> 