<?php
	date_default_timezone_set('Asia/Jakarta');
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$bus_nama			= mysqli_real_escape_string($koneksi, $_POST['bus_nama']);
	$bus_sdesc			= mysqli_real_escape_string($koneksi, $_POST['bus_sdesc']);
	$bus_desc			= mysqli_real_escape_string($koneksi, $_POST['bus_desc']);
	$bus_model			= mysqli_real_escape_string($koneksi, $_POST['bus_model']);
	$bus_status			= mysqli_real_escape_string($koneksi, $_POST['bus_status']);
	$bus_seo			= strtolower(str_replace($stringreplace,'-',$bus_nama));
	$bus_metatitle		= mysqli_real_escape_string($koneksi, $_POST['bus_metatitle']);
	$bus_metadesc		= mysqli_real_escape_string($koneksi, $_POST['bus_metadesc']);
	$bus_metakey		= mysqli_real_escape_string($koneksi, $_POST['bus_metakey']);
	
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$bus_nama));
	$nama_file 			= $_FILES["bus_image"]["name"];
	$tipe_file 			= $_FILES["bus_image"]["type"];
	$alamat 			= $_FILES["bus_image"]["tmp_name"];
	$nama_baru 			= "bus_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/bus/$nama_baru";
	
	$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_bus (bus_id, bus_nama, bus_sdesc, bus_desc, bus_model, bus_status, bus_seo, bus_metatitle, bus_metadesc, bus_metakey, bus_image, bus_imageid, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$bus_nama', '$bus_sdesc', '$bus_desc', '$bus_model', '$bus_status', '$bus_seo', '$bus_metatitle', '$bus_metadesc', '$bus_metakey', '$bus_image', '$bus_imageid', '$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") or die (mysqli_error($koneksi));
		
		echo "<meta http-equiv='refresh' content='0; url=?m=bus_index&is=is'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" && $tipe_file != "image/jpg" && $tipe_file != "image/jpeg" && $tipe_file != "image/png") {
			echo "<meta http-equiv='refresh' content='0; url=?m=bus_input&ufe=ufe'>";
		} else {
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_bus (bus_id, bus_nama, bus_sdesc, bus_desc, bus_model, bus_status, bus_seo, bus_metatitle, bus_metadesc, bus_metakey, bus_image, bus_imageid, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$bus_nama', '$bus_sdesc', '$bus_desc', '$bus_model', '$bus_status', '$bus_seo', '$bus_metatitle', '$bus_metadesc', '$bus_metakey', '$nama_baru', '1', '$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") or die (mysqli_error($koneksi));
			
			echo "<meta http-equiv='refresh' content='0; url=?m=bus_index&is=is'>";
		}
	}
?>