<?php
session_start();
// Apabila user belum login
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
  echo "<link href=\"../../css/style_login.css\" rel=\"stylesheet\" type=\"text/css\" />
        <div id=\"login\"><h1 class=\"fail\">Untuk mengakses modul, Anda harus login dulu.</h1>
        <p class=\"fail\"><a href=\"../../index.php\">LOGIN</a></p></div>";  
}
// Apabila user sudah login dengan benar, maka terbentuklah session
else{
  include "../../../config/koneksi.php";
  include "../../../config/fungsi_seo.php";  
  include "../../../config/fungsi_thumbnail.php";

  $module = $_GET['module'];
  $act    = $_GET['act'];

  // Hapus video2
  if ($module=='video2' AND $act=='hapus'){
    $query = "SELECT gambar FROM video2 WHERE id_video2='$_GET[id]'";
    $hapus = mysqli_query($konek, $query);
    $r     = mysqli_fetch_array($hapus);
    
    if ($r['gambar']!=''){
      $namafile = $r['gambar'];
      
      // hapus filenya
      unlink("../../../foto_video/$namafile");   
      unlink("../../../foto_video/small_$namafile");   

      // hapus data video2 di database 
      mysqli_query($konek, "DELETE FROM video2 WHERE id_video2='$_GET[id]'");
    }
    else{
      mysqli_query($konek, "DELETE FROM video2 WHERE id_video2='$_GET[id]'");
    }
    header("location:../../media.php?module=".$module);
  }

  // Input video2
  elseif ($module=='video2' AND $act=='input'){
   

    $judul_video2  = addslashes($_POST['judul_video2']);
    $video2_seo    = seo_title($_POST['judul_video2']);
    $deskripsi    = addslashes($_POST['deskripsi']);
    $link_youtube = addslashes($_POST['link_youtube']);


  $rand = rand();
$ekstensi =  array('png','jpg','jpeg','gif','webp');
$filename = $_FILES['foto']['name'];
$ukuran = $_FILES['foto']['size'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);



 if (empty($filename)){
      mysqli_query($konek, "INSERT INTO video2(judul_video2,
                                    video2_seo,  
                                    link_youtube, 
                                    deskripsi,
                                    gambar) 
                            VALUES('$judul_video2',
                                   '$video2_seo', 
                                   '$link_youtube', 
                                   '$deskripsi',
                                    '$xx')");
    header("location:../../media.php?module=".$module);
    }
      // Apabila ada gambar yang di upload
    else{

 
 
if(!in_array($ext,$ekstensi) ) {
  header("location:index.php?alert=gagal_ekstensi");
}else{
  if($ukuran < 1044070){    
    $xx = $rand.'_'.$filename;
    $folder = "../../../foto_video/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "INSERT INTO video2(judul_video2,
                                    video2_seo,  
                                    link_youtube, 
                                    deskripsi,
                                    gambar) 
                            VALUES('$judul_video2',
                                   '$video2_seo', 
                                   '$link_youtube', 
                                   '$deskripsi',
                                    '$xx')");
    header("location:../../media.php?module=".$module);
  }else{
     echo "<script>window.alert('Upload Gagal! Ukuran Terlalu Besar');
              window.location=('../../media.php?module=album)</script>";
  }
}


}

   
  }

  // Update video2
  elseif ($module=='video2' AND $act=='update'){


    $id           = $_POST['id'];
    $judul_video2  = addslashes($_POST['judul_video2']);
    $video2_seo    = seo_title($_POST['judul_video2']);
    $deskripsi    = addslashes($_POST['deskripsi']);
    $link_youtube = addslashes($_POST['link_youtube']);





$rand = rand();
$ekstensi =  array('png','jpg','jpeg','gif','webp');
$filename = $_FILES['foto']['name'];
$ukuran = $_FILES['foto']['size'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);


    // Apabila gambar tidak diganti
    if (empty($filename)){
      mysqli_query($konek, "UPDATE video2 SET judul_video2  = '$judul_video2',
                                    video2_seo    = '$video2_seo',
                                    link_youtube = '$link_youtube',
                                    deskripsi    = '$deskripsi'
                              WHERE id_video2     = '$id'");
    header("location:../../media.php?module=".$module);
    }
    else{
        

if(!in_array($ext,$ekstensi) ) {
  header("location:index.php?alert=gagal_ekstensi");
}else{
  if($ukuran < 1044070){    
    $xx = $rand.'_'.$filename;
    $folder = "../../../foto_video/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "UPDATE video2 SET judul_video2  = '$judul_video2',
                                    video2_seo    = '$video2_seo',
                                    link_youtube = '$link_youtube',
                                    deskripsi    = '$deskripsi',   
                                    gambar       = '$xx' 
                              WHERE id_video2     = '$id'");
    header("location:../../media.php?module=".$module);
  }else{
     echo "<script>window.alert('Upload Gagal! Ukuran Terlalu Besar');
              window.location=('../../media.php?module=album)</script>";
  }
} 

   
    } 
    
  }
}
?>
