<?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";

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

// Hapus berita
  if ($module=='album1' AND $act=='hapus'){
    $query = "SELECT gambar FROM album1 WHERE id_album1='$_GET[id]'";
    $hapus = mysqli_query($konek, $query);
    $r     = mysqli_fetch_array($hapus);
    
    if ($r['gambar']!=''){
      $namafile = $r['gambar'];
      // hapus file gambar yang berhubungan dengan berita tersebut
      unlink("../../../foto_album/$namafile");   
      unlink("../../../foto_album/small_$namafile");   
      
      // hapus data berita di database 
      mysqli_query($konek, "DELETE FROM album1 WHERE id_album1='$_GET[id]'");      
    }
    else{
      mysqli_query($konek, "DELETE FROM album1 WHERE id_album1='$_GET[id]'");
    }
    header("location:../../media.php?module=".$module);
  }




  // Input album1
  elseif ($module=='album1' AND $act=='input'){
   
    $nama_album1 = $_POST['nama_album1'];
    $album1_seo  = seo_title($_POST['nama_album1']);


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



 if (empty($filename)){
      $input = "INSERT INTO album1(nama_album1, album1_seo) VALUES('$nama_album1', '$album1_seo')";
      mysqli_query($konek, $input);

      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_album/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "INSERT INTO album1 (nama_album1, album1_seo, gambar) VALUES('$nama_album1','$album1_seo', '$xx')");
    header("location:../../media.php?module=".$module);
  }else{
     echo "<script>window.alert('Upload Gagal! Ukuran Terlalu Besar');
              window.location=('../../media.php?module=album1)</script>";
  }
}


}

  }

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


    $id         = $_POST['id'];
    $nama_album1 = $_POST['nama_album1'];
    $album1_seo  = seo_title($_POST['nama_album1']);
    $kete = $_POST['kete'];
    $aktif      = $_POST['aktif'];


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


    // Apabila gambar tidak diganti
    if (empty($filename)){
      $update = "UPDATE album1 SET nama_album1 = '$nama_album1',
                                  album1_seo  = '$album1_seo', 
                                  aktif      = '$aktif'
                            WHERE id_album1   = '$id'";
      mysqli_query($konek, $update);
      
      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_album/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "
    

    UPDATE album1 SET nama_album1 = '$nama_album1',
                                  album1_seo  = '$album1_seo', 
                                  aktif      = '$aktif',
                                  gambar = '$xx'
                            WHERE id_album1   = '$id'




    ");
    header("location:../../media.php?module=".$module);
  }else{
     echo "<script>window.alert('Upload Gagal! Ukuran Terlalu Besar');
              window.location=('../../media.php?module=album1)</script>";
  }
} 



    } 
  }
}
?>
