<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set("Asia/Jakarta");
	
	$logo_id		= mysqli_real_escape_string($koneksi, $_POST['logo_id']);
	$logo_nama		= mysqli_real_escape_string($koneksi, $_POST['logo_nama']);
	$logo_lebar		= mysqli_real_escape_string($koneksi, $_POST['logo_lebar']);
	$logo_panjang	= mysqli_real_escape_string($koneksi, $_POST['logo_panjang']);
	
	$code		   	= md5(uniqid(rand()));
	$gencode		= substr($code, 0, 3);
	$nama_file 		= $_FILES["logo_gambar"]["name"];
	$tipe_file 		= $_FILES["logo_gambar"]["type"];
	$alamat 		= $_FILES["logo_gambar"]["tmp_name"];
	$nama_baru 		= "logo_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 		= "assets/images/$nama_baru";
	
	$updated		= date("Y-m-d H:i:s");
	$updated_id		= $udt['admin_id'];
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_logo SET 
		logo_nama			= '$logo_nama',
		logo_lebar			= '$logo_lebar',
		logo_panjang		= '$logo_panjang',
		updated				= '$updated',
		updated_id			= '$updated_id'
		WHERE logo_id		= '$logo_id'") 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, 'Update', 'Logo', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=logo_index&us=us'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" && $tipe_file != "image/jpg" && $tipe_file != "image/jpeg" && $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, 'Upload File', 'Logo > Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=logo_edit&id=$logo_id&ufe=ufe'>";
		} else { 
			$db = mysqli_query($koneksi, "SELECT * FROM cmas_logo WHERE logo_id='$logo_id'");
			if(mysqli_num_rows($db) > 0 ) {
				$dt = mysqli_fetch_array($db);
				@unlink('assets/images/'.$dt['logo_gambar']);
			}
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "UPDATE cmas_logo SET 
			logo_nama			= '$logo_nama',
			logo_lebar			= '$logo_lebar',
			logo_panjang		= '$logo_panjang',
			logo_gambar			= '$nama_baru',
			updated				= '$updated',
			updated_id			= '$updated_id'
			WHERE logo_id		= '$logo_id'") 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, 'Update & Upload File', 'Logo', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=logo_index&us=us'>";
		}
	}
?>