<?php
	define('_CONSTANT_', 1); // Defend php injection;

	require_once '../../core/start.php'; // Conect classes and core;

	head('Новости', 'Новости');  // Title/Head;


$close = 1;

if ($close == 1) 
{
	error('Закрыто', '/');
}
/*
 * Only for admin;
 */ 
 if ($user['access'] == 2) 
 {
	 if (isset($_POST['newNews'])) 
	 { 
		 $data = $_POST;
		 $date = date('d-m-Y');
		 $time = time();
		 $title = $Filter->clearString($data['title']); // clearString = filter number
		 $text = $Filter->clearString($data['text']);

		 DB::$dbs->query("INSERT INTO " . NEWS . " (`uid`, `title`, `text`, `date`, `time`) VALUES (?, ?, ?, ?, ?)", [$user['id'], $data['title'], $data['text'], $date, $time]);
		 $User->addLog($user['id'], 'Добавил новость');
		 success('Новость добавлена', '/', 'На главную');
	 }
	 ?>
	 <form action="" method="POST"> 
	 <h3>Создание новости</h3>
	 <input type="text" placeholder="Title News" style="height:30px;width:60%;border-radius:50px;border-color:#ff0000;text-align:center;outline:none;" name="title">
	<script type="text/javascript" src='../ckeditor/ckeditor.js'></script>
	<textarea name="text" id="editor1" cols="45" rows="5"></textarea>
	<script type="text/javascript">
	CKEDITOR.replace( 'editor1');
	</script>
	 <button type="submit" name="newNews" style="height:30px;width:50%;border-radius:50px;border-color:#99d636;outline:none;">Создать</button>
	 </form>
	 <?php	 
 }

 /* 
  * News;
  */
  $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . NEWS . " ORDER BY `id` DESC");
  $n = new Navigator($all,10,'');
  $sql = DB::$dbs->query("SELECT * FROM " . NEWS . " ORDER BY `id` DESC LIMIT {$n->start()}, 10"); 

  if (empty($all)) 
  {
	  success('Новостей нет.', '/', 'На главную');
  }

  while ($news = $sql -> fetch()) 
  {
	  ?> 
	  <div>
			<div>
				<div style="margin-left: 30px;text-align: center;"><h1><?=$Filter->output($news['title'])?></h1></div><br>
				<div style="margin-left: 30px;"><?=$news['text']?></div><br>
				<div style="display:block;">
					<div style="">Автор: <?=userLink($news['uid'])?> <span style="float:right">Дата: <?=$news['date']?></span></div>
				</div>
			</div>
	  </div><br>
	  <?php
  }
   echo $n->navi(); // Navigation;

  require_once '../../core/foot.php'; // Footer;