<?php
$t = "Новости";
if ($VarIO->DefS("module_news_enabled", 1) == 0){
	twig("/news.lm", ["block"	=>	"news_disabled"]);
}
if ($user->auth() && !$acl->isNewsRead){
	twig("/news.lm", ["block"	=>	"no_access"]);
} elseif (!$user->auth() && $VarIO->DefS("module_news_guest_view", 1) == 0){
	twig("/news.lm", ["block"	=>	"no_access"]);
} elseif ($user->auth() && $act == "create" && $acl->isNewsWrite){
	if (isset($_POST["send"])){
		$title 		= isset($_POST["title"]) && !empty($_POST["title"])?$core->FilterString($_POST["title"]):"";
		$message 	= isset($_POST["message"]) && !empty($_POST["message"])?$core->FilterString($_POST["message"]):"";
		if ($title == "" || $message == "") {
			$core->regError("", "Заголовок и Текст новости должен быть заполнен.", 1, "news");
		} else {
			$db->Query("INSERT INTO `news`(`sid`, `uid`, `ntitle`, `ntext`, `ntime`) VALUES ('".$site->id."', '".$user->uid."', '".$title."', '".$message."', '".(time())."')");
			$new_id = $db->getLastID();
			header("Location: /news.lm?id=".$new_id);
			die();
		}
	}
	twig("/news.lm", ["block"	=>	"create"]);
}
if ($user->auth() && $act == "clean" && $acl->isNewsModer){
	if (isset($_POST["send"])){
		$db->Query("DELETE FROM `news` WHERE `sid` = '".$site->id."'");
		header("Location: /news.lm?msg=cleaned");
		die();
	} 
	twig("/news.lm", ["block"	=>	"clean"]);
}
if ($id != 0){
	$obj = $db->getArray("SELECT * FROM `news` WHERE `sid` = '".$site->id."' AND `nid` = '".$id."' LIMIT 1");
	if ($obj != NULL){
		$arr = [
			"id"		=>	$obj["nid"],
			"time"		=>	$obj["ntime"],
			"title"		=> 	$core->drawString($obj["ntitle"]),
			"message"	=> 	$core->drawString($obj["ntext"]),
			"user"		=>	getUserInfo($obj["uid"]),
		];
		if ($act == "del" && $user->auth() && $acl->isNewsModer){
			if (isset($_POST["send"])){
				$db->Query("DELETE FROM `news` WHERE `sid` = '".$site->id."' AND `nid` = '".$id."' LIMIT 1");
				header("Location: /news.lm?msg=deleted");
				die();
			}
			twig("/news.lm", ["block"	=>	"delete", "post"	=> $arr]);
		} elseif ($act == "edit" && $user->auth() && $acl->isNewsModer){
			if (isset($_POST["send"])){
				$title 		= isset($_POST["title"]) && !empty($_POST["title"])?$core->FilterString($_POST["title"]):"";
				$message 	= isset($_POST["message"]) && !empty($_POST["message"])?$core->FilterString($_POST["message"]):"";
				if ($title == "" || $message == "") {
					$core->regError("", "Заголовок и Текст новости должен быть заполнен.", 1, "news");
				} else {
					$db->Query("UPDATE `news` SET `ntitle`='".$title."',`ntext`='".$message."' WHERE `sid` = '".$site->id."' AND `nid` = '".$id."' LIMIT 1");
					header("Location: /news.lm?id=".$id);
					die();
				}
			}
			$arr["title"] = $obj["ntitle"];
			$arr["message"] = $obj["ntext"];
			twig("/news.lm", ["block"	=>	"edit", "post"	=> $arr]);
		}
		twig("/news.lm", ["block"	=>	"view", "post"	=> $arr]);
	} else {
		twig("/news.lm", ["block"	=>	"404"]);
	}
}

$max 		= $VarIO->DefS("items_per_page", 10);
$k_post 	= $db->Count("news","WHERE `sid` = '".$site->id."'");
$k_page 	= $core->k_page($k_post,$max);
$page 		= $core->page($k_page);
$start 		= $max*$page-$max;
$elements	= [];

$news = $db->Query("SELECT * FROM `news` WHERE `sid` = '".$site->id."' ORDER BY `nid` DESC LIMIT $start, $max"); 
while ($elem = mysqli_fetch_assoc($news)){
	$elements[] = [
		"id"		=>	$elem["nid"],
		"time"		=>	$elem["ntime"],
		"title"		=> 	$core->drawString($elem["ntitle"]),
		"message"	=> 	$core->drawString($elem["ntext"]),
		"user"		=>	getUserInfo($elem["uid"]),
	];
	#echo linkGen('/news.lm?id='.$ns["nid"],$ns["ntitle"],["data-id"=>"link-news"],$core->RealTime($ns["ntime"]));
}
twig("/news.lm", ["block"	=>	"base","elems" => $elements, "pages" => [
	"elemsCount"	=> 	$k_post,
	"api"			=>	$core->twig_page_mod($k_page, $page, ["msg","userID"]),
]]);
?>