<?

if (!isset($urlType[2]) || $user['id_level'] == 0) {
	Redirection('/');
}

$count = $sql->rowCount("SELECT * FROM `news` WHERE `link` = ? LIMIT 1", [$urlType[2]]);
if ($count == 0) {
	Redirection('/');
}
$news = $sql->fetch("SELECT * FROM `news` where link = ? ", [$urlType[2]]);

$PageConfig = [
	'title' =>  'Редактирование - '. $news['title']
];

PageConfig($PageConfig);

if (isset($_POST['save'], $_POST['razd'], $_POST['title'], $_POST['text'], $_POST['link'])) {
	$razd = $_POST['razd'];
	$title = $_POST['title'];
	$text = $_POST['text'];
	$link = $_POST['link'];
	$keywords = $_POST['keywords'];
	$description = $_POST['description'];

	$sortid = $sql->fetch("SELECT `id` FROM `news_razd` ORDER BY `id` DESC LIMIT 1");
	$sortid = $sortid['id'];
	
	if ($razd > $sortid){
		$razd = 0;
	}
	$_SESSION['news_razd_2'] = $razd;
	$_SESSION['news_title_2'] = $title;
	$_SESSION['news_text_2'] = $text;
	$_SESSION['news_link_2'] = $link;
	$_SESSION['news_description_2'] = $description;
	$_SESSION['news_keywords_2'] = $keywords;
	
	if ($sql->rowCount("SELECT * FROM `news` WHERE `link` = ?", [$link]) == 1 AND $news['link'] != $link) {
		$err[] = 'Такая ссылка уже есть';
	}
	if (FileSystem::TextStrlen($keywords) > 256) {
		$err[] = 'Keywords слишком длинный';
	}
	if (FileSystem::TextStrlen($description) > 256) {
		$err[] = 'Description слишком длинный';
	}	
	if (FileSystem::TextStrlen($text) > 30000) {
		$err[] = 'Сообщение слишком длинное';
	}
	if (FileSystem::TextStrlen($text) < 3) {
		$err[] = 'Слишком короткое сообщение';
	}
	if (FileSystem::TextStrlen($title) > 128) {
		$err[] = 'Заголовок слишком длинный';
	}
	if (FileSystem::TextStrlen($title) < 3) {
		$err[] = 'Заголовок слишком короткий';
	}
	if (FileSystem::TextStrlen($link) > 128) {
		$err[] = 'Ссылка слишком длинная';
	}
	if (FileSystem::TextStrlen($link) < 5) {
		$err[] = 'Ссылка слишком короткая';
	}	
	$link = preg_replace("/[^-a-zа-я-0-9\s]/ui", "", $link);
	$link = str_replace(' ','-', $link);
	$link = str_replace('  ','', $link);
	$link = trim($link);
	$text = trim($text);
	$title = trim($title);
	
	$file = false;
	if (isset($_FILES['file']['name']) AND FileSystem::TextStrlen($_FILES['file']['name']) > 3 ) {
		$file = $_FILES['file']['tmp_name'];
	}
	
	if (!isset($err)) {
		$img = $news['img'];
		if ($file) {
			unlink(H.'files/news-images/big/'. $img .'.jpg');
			unlink(H.'files/news-images/mid/'. $img .'.jpg');
			unlink(H.'files/news-images/min/'. $img .'.jpg');
			$img = md5(time() . mt_rand(1,9999999));
			
			imgResize($file, H.'files/news-images/big/'. $img .'.jpg', 900);
			$imgs = new acResizeImage($file);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(300, 500)->
			save(H.'files/news-images/mid/', $img, 'jpg', true, 75);		
			
			$imgs = new acResizeImage($file);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(128, 300)->
			save(H.'files/news-images/large/', $img, 'jpg', true, 75);			
			
			$imgs = new acResizeImage($file);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(50, 300)->
			save(H.'files/news-images/min/', $img, 'jpg', true, 75);			
		}
		$sql->query("UPDATE `news` SET `description` = ?, `keywords` = ?, `title` = ?, `text` = ?, `razd` = ? , `link` = ? , `img` = ? WHERE `id` = ?", 
		[
			$description, 
			$keywords, 
			$title, 
			$text, 
			$razd, 
			$link, 
			$img, 
			$news['id']
		]);
		
		CacheDelete::news($news['link']);
		unset($_SESSION['news_razd_2'], $_SESSION['news_title_2'], $_SESSION['news_text_2'], $_SESSION['news_link_2'], $_SESSION['news_description_2'], $_SESSION['news_keywords_2']);
		Redirection('/news/view/'. $link .'/', 'Успешно');
	}
}

if (isset($err)) {
	err($err);
}
$razd = [];
$query = $sql->fetchAll("SELECT * FROM `news_razd` ORDER BY `sort` ASC");
foreach ($query AS $p) {
	$razd[] = $p; 
}
$t->assign('razd', $razd);
$t->assign('news', $news);
echo $t->fetch('news.edit');
