<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	
	$banner_judul		= mysqli_real_escape_string($koneksi, $_POST['banner_judul']);
	$banner_desk		= mysqli_real_escape_string($koneksi, $_POST['banner_desk']);
	$banner_deskripsi	= mysqli_real_escape_string($koneksi, $_POST['banner_deskripsi']);
	$banner_keterangan	= mysqli_real_escape_string($koneksi, $_POST['banner_keterangan']);
	$banner_status		= mysqli_real_escape_string($koneksi, $_POST['banner_status']);
	$banner_url			= mysqli_real_escape_string($koneksi, $_POST['banner_url']);
	
	$bannerkat_id		= mysqli_real_escape_string($koneksi, $_POST['bannerkat_id']);
	$bannerkat_nama		= mysqli_real_escape_string($koneksi, $_POST['bannerkat_nama']);
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$banner_judul));
	$nama_file 			= $_FILES["banner_image"]["name"];
	$tipe_file 			= $_FILES["banner_image"]["type"];
	$alamat 			= $_FILES["banner_image"]["tmp_name"];
	$nama_baru 			= "banner_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/banner/$nama_baru";
	
	$banner_imagepos	= mysqli_real_escape_string($koneksi, $_POST['banner_imagepos']);
	
	$postdated			= date('Y-m-d H:i:s');
	$updated			= date('Y-m-d H:i:s');
		
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_banner (banner_id, banner_judul, banner_desk, banner_deskripsi, banner_keterangan, banner_status, banner_url, banner_image, banner_imageid, banner_imagepos, bannerkat_id, bannerkat_nama, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$banner_judul', '$banner_desk', '$banner_deskripsi', '$banner_keterangan', '$banner_status', '$banner_url', '$banner_image', '$banner_imageid', '$banner_imagepos', '$bannerkat_id', '$bannerkat_nama', '$postdated', '$updated', '$posted_id', '$posted_username', '$posted_nama', '$updated_id', '$updated_username', '$updated_nama')") or die (mysqli_error($koneksi));
		
		$notification_ip = $_SERVER['REMOTE_ADDR'];
		mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input', 'Banner: $banner_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=banner_index&id=$bannerkat_id&is=is'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" AND $tipe_file != "image/jpg" AND $tipe_file != "image/jpeg" AND $tipe_file != "image/png") {
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input & Upload File', 'Banner: $banner_judul, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=banner_input&ufe=ufe'>";
		} else {
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_banner (banner_id, banner_judul, banner_desk, banner_deskripsi, banner_keterangan, banner_status, banner_url, banner_image, banner_imageid, banner_imagepos, bannerkat_id, bannerkat_nama, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$banner_judul', '$banner_desk', '$banner_deskripsi', '$banner_keterangan', '$banner_status', '$banner_url', '$nama_baru', '1', '$banner_imagepos', '$bannerkat_id', '$bannerkat_nama', '$postdated', '$updated', '$posted_id', '$posted_username', '$posted_nama', '$updated_id', '$updated_username', '$updated_nama')") or die (mysqli_error($koneksi));
			
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input & Upload File', 'Banner: $banner_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
			echo "<meta http-equiv='refresh' content='0; url=?m=banner_index&id=$bannerkat_id&is=is'>";
		}
	}
?>