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

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

  // Hapus berita
  if ($module=='berita' AND $act=='hapus'){
    $query = "SELECT gambar FROM berita WHERE id_berita='$_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_berita/$namafile");   
      unlink("../../../foto_berita/small_$namafile");   
      
      // hapus data berita di database 
      mysqli_query($konek, "DELETE FROM berita WHERE id_berita='$_GET[id]'");      
    }
    else{
      mysqli_query($konek, "DELETE FROM berita WHERE id_berita='$_GET[id]'");
    }
    header("location:../../media.php?module=".$module);
  }

  // Input berita
  elseif ($module=='berita' AND $act=='input'){
  
   if (!empty($_POST['tag_seo'])){
      $tag_seo = $_POST['tag_seo'];
      $tag     = implode(',',$tag_seo);
    }
   
    
    $judul      = addslashes($_POST['judul']);          
    $judul_seo  = seo_title($_POST['judul']);
    $kategori   = $_POST['kategori'];
    $headline   = $_POST['headline'];
    $publish    = $_POST['publish'];
    $isi_berita = addslashes($_POST['isi_berita']);



 $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 berita(judul,
                                   judul_seo,     
                                   id_kategori, 
                                   username,
                                   headline,
                                   publish,
                                   isi_berita,
                                   hari,
                                   tanggal,
                                   jam,
                                   tag) 
                           VALUES('$judul',
                                  '$judul_seo', 
                                  '$kategori', 
                                  '$_SESSION[namauser]',
                                  '$headline',
                                  '$publish',
                                  '$isi_berita',
                                  '$hari_ini',
                                  '$tgl_sekarang',
                                  '$jam_sekarang',
                                  '$tag')";
      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_berita/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "INSERT INTO berita(judul,
                                   judul_seo,     
                                   id_kategori, 
                                   username,
                                   headline,
                                   publish,
                                   isi_berita,
                                   hari,
                                   tanggal,
                                   jam,
                                   tag,
                                   gambar) 
                           VALUES('$judul',
                                  '$judul_seo', 
                                  '$kategori', 
                                  '$_SESSION[namauser]',
                                  '$headline',
                                  '$publish',
                                  '$isi_berita',
                                  '$hari_ini',
                                  '$tgl_sekarang',
                                  '$jam_sekarang',
                                  '$tag',
                                  '$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 berita
  elseif ($module=='berita' AND $act=='update'){
 

    if (!empty($_POST['tag_seo'])){
      $tag_seo = $_POST['tag_seo'];
      $tag     = implode(',',$tag_seo);
    }

    $id         = $_POST['id'];
    $judul      = addslashes($_POST['judul']);           
    $judul_seo  = seo_title($_POST['judul']);
    $kategori   = $_POST['kategori'];
    $headline   = $_POST['headline'];
    $publish    = $_POST['publish'];
    $isi_berita = addslashes($_POST['isi_berita']);






$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 berita SET judul       = '$judul',
                                   judul_seo   = '$judul_seo', 
                                   id_kategori = '$kategori',
                                   headline    = '$headline',
                                   publish     = '$publish',
                                   isi_berita  = '$isi_berita',  
                                   tag         = '$tag' 
                             WHERE id_berita   = '$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_berita/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $folder.$rand.'_'.$filename);
    mysqli_query($konek, "
    



  UPDATE berita SET judul       = '$judul',
                                     judul_seo   = '$judul_seo', 
                                     id_kategori = '$kategori',
                                     headline    = '$headline',
                                     publish     = '$publish',
                                     isi_berita  = '$isi_berita',  
                                     tag         = '$tag',
                                     gambar      = '$xx' 
                               WHERE id_berita   = '$id'


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






    } 


  }
}
?>
