<?php
//error_reporting(0);
session_start();
ob_start();
include "../../../../konfig/dbconnect.php";

$db = new database();
$db->dbsql();   

include "../../../../model/viewpost.php";
include "../../../../model/viewgambar.php";
include "../../../../controller/upload.php";

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $post = new viewpost();
    $gambar = new viewgambar();
    $upload = new upload();

    $lokasi_file    = $_FILES["gambar"]["tmp_name"];
    $tipe_file      = $_FILES["gambar"]["type"];
    $nama_file      = $_FILES["gambar"]["name"];
    $acak           = rand(1,99);
    $nama_unik      = $acak.$nama_file;

    $upload->post($nama_unik,$db->linkupload());

    $isi = str_replace("../rswsweb/", $db->link(), $_POST[isi]);

    if($_POST[key]==1){
        

        $post->addpost($_POST[kategori],$_POST[judul],$isi,$_POST[keterangan],$nama_unik,'Y');

        header("location:../../../../index.php?module=post");
    }
    elseif ($_POST[key]==2) {

        $post->addpost($_POST[kategori],$_POST[judul],$isi,$_POST[keterangan],$nama_unik,'N');

        header("location:../../../../index.php?module=post");
    } 
}
elseif($aksi=='edit'){
    $post = new viewpost();
    $gambar = new viewgambar();
    $upload = new upload();

    $lokasi_file    = $_FILES['gambar']['tmp_name'];
    $tipe_file      = $_FILES['gambar']['type'];
    $nama_file      = $_FILES['gambar']['name'];
    $acak           = rand(1,99);
    $nama_unik      = $acak.$nama_file;

    if (empty($nama_file)) {
        $file = $_POST[gambar_post];
    }else{
        $file = $nama_unik;
        $upload->post($nama_unik,$db->linkupload());
    }

    if($_POST[key]==1){

        $post->id = $_POST[id];
        $post->editpost($_POST[kategori],$_POST[judul],$_POST[isi],$_POST[keterangan],$file,'Y');

        header("location:../../../../index.php?module=post");
    }
    elseif ($_POST[key]==2) {

        $post->id = $_POST[id];
        $post->editpost($_POST[kategori],$_POST[judul],$_POST[isi],$_POST[keterangan],$file,$_POST[publish]);

        header("location:../../../../index.php?module=post");
    }
}
elseif($aksi=='delete'){
    $post = new viewpost();
    
    $post->id = $_GET[id];
    $post->deletepost();
    
    header("location:../../../../index.php?module=post");
}
