<?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";
  include "../../../config/library.php";

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

  // Hapus galeri foto
  if ($module=='galerifoto' AND $act=='hapus'){
    // cari informasi nama file foto yang ada di tabel galeri
    $query = "SELECT gambar FROM galeri WHERE id_galeri='$_GET[id]'";
    $hapus = mysqli_query($konek, $query);
    $r     = mysqli_fetch_array($hapus);
    
    // kalau ada file fotonya
    if ($r['gambar']!=''){
      $namafile = $r['gambar'];
      
      // hapus file foto yang berhubungan dengan galeri tersebut
      unlink("../../../foto_galeri/$namafile");   
      unlink("../../../foto_galeri/small_$namafile");   

      // kemudian baru hapus data galeri di database 
      mysqli_query($konek, "DELETE FROM galeri WHERE id_galeri='$_GET[id]'");      
    }
    // kalau tidak ada file fotonya
    else{
      mysqli_query($konek, "DELETE FROM galeri WHERE id_galeri='$_GET[id]'");
    }
    header("location:../../media.php?module=".$module);
  }


  // Input galeri foto
  elseif ($module=='galerifoto' AND $act=='input'){

  
$judul_galeri = addslashes($_POST['judul_galeri']);
    $galeri_seo   = seo_title($_POST['judul_galeri']);
    $isig = addslashes($_POST['isig']);
    $tgl = $_POST['tgl'];


$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 galeri(judul_galeri,
                                    galeri_seo,
                                    isig,
                                    tgl)
                            VALUES('$judul_galeri',
                                   '$galeri_seo',
                                   '$isig',
                                   '$tgl')");
    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_galeri/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "INSERT INTO galeri(judul_galeri,
                                    galeri_seo,
                                    isig,
                                    tgl,
                                    gambar)
                            VALUES('$judul_galeri',
                                   '$galeri_seo',
                                   '$isig',
                                   '$tgl',
                                    '$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 galeri foto
  elseif ($module=='galerifoto' AND $act=='update'){
 

    $id           = $_POST['id'];
    $judul_galeri = addslashes($_POST['judul_galeri']);
    $galeri_seo   = seo_title($_POST['judul_galeri']);
    $isig = addslashes($_POST['isig']);
    $tgl = $_POST['tgl'];
    $fotoku           = $_POST['fotoku'];


$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 galeri SET judul_galeri = '$judul_galeri',
                                   galeri_seo   = '$galeri_seo',
                                   isig   = '$isig',
                                   tgl   = '$tgl'
                             WHERE id_galeri    = '$id'";
      mysqli_query($konek, $update);
      
      header("location:../../media.php?module=".$module);
    }
    else{

      $namafile = $fotoku;
      
      // hapus filenya
      unlink("../../../foto_galeri/$namafile");   
      unlink("../../../foto_galeri/small_$namafile");
        

if(!in_array($ext,$ekstensi) ) {
  header("location:index.php?alert=gagal_ekstensi");
}else{
  if($ukuran < 1044070){    
    $xx = $rand.'_'.$filename;
    $folder = "../../../foto_galeri/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "
    

  


UPDATE galeri SET judul_galeri = '$judul_galeri',
                                     galeri_seo   = '$galeri_seo',
                                      isig   = '$isig',
                                      tgl   = '$tgl',
                                     gambar         = '$xx' 
                               WHERE id_galeri    = '$id'

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













    }
  }
}
?>
