View file pages/user/news-add.php

File size: 3.66Kb
<?

if (!$user) {
	Redirection('/user/login', 'Требуется авторизация');
}


$PageConfig = [
	'title' =>  'Добавление новостей'
];

PageConfig($PageConfig);

if (isset($_POST['save'], $_POST['razd'], $_POST['title'], $_POST['text'])) {
	$razd = $_POST['razd'];
	$title = $_POST['title'];
	$text = $_POST['text'];
	$keywords = $_POST['keywords'];
	$description = $_POST['description'];
	
	$link = Translit($title , 1, 1, 1);
	$link = preg_replace("/[^-a-zа-я-0-9\s]/ui", "", $link);
	$link = str_replace(' ','-', $link);
	$link = str_replace('  ','', $link);
	$link = trim($link);	
	
	$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'] = $razd;
	$_SESSION['news_title'] = $title;
	$_SESSION['news_text'] = $text;
	$_SESSION['news_description'] = $description;
	$_SESSION['news_keywords'] = $keywords;
	
	if ($sql->rowCount("SELECT * FROM `news` WHERE `link` = ?", [$link]) == 1) {
		$link = $link .'-'. time();
	}
	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[] = 'Заголовок слишком короткий';
	}

	$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 = md5(time().$user['id'].time());
		$randpic = mt_rand(1, 4);
		$randpic = 4;
		
		if (!$file) {	
			$file = H."files/news-images/big/_default_{$randpic}.jpg";
		}
		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);			
	
		$token = md5(time().$user['id']);
		$key = md5(time().$user['id'].time());
		$act = 3;
		if ($user['id_level'] >= 1) {
			$act = 5;
		}
		$sql->query("INSERT INTO `news` 
			(`date_create`,  `id_user`, `link`, `token`, `key`, `act`, `title`, `text`, `img`, `razd`) 
			VALUES 
			('{$system['time']}', '{$user['id']}', '{$link}', '{$token}', '{$key}', '{$act}', '{$title}', '{$text}', '{$img}', '{$razd}');
		");
		
		unset(
			$_SESSION['news_razd'], 
			$_SESSION['news_title'], 
			$_SESSION['news_text'],
			$_SESSION['news_description'], 
			$_SESSION['news_keywords']
		);
		Redirection('/news/view/'. $link .'/', 'Новость отправлена на модерацию');
	}
}
$razd = [];
$query = $sql->fetchAll("SELECT * FROM `news_razd` ORDER BY `sort` ASC");
foreach ($query AS $p) {
	$razd[] = $p; 
}

if (isset($err)) {
	err($err);
}

$t->assign('razd', $razd);
echo $t->fetch('user.news-add');