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

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

include "../../../../model/viewpage.php";
include "../../../../controller/upload.php";

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $page = new viewpage();
    $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->page($nama_unik,$db->linkupload());

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

        $page->addpage($_POST[judul],$_POST[isi],$_POST[keterangan],$nama_unik,'Y');

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

        $page->addpage($_POST[judul],$_POST[isi],$_POST[keterangan],$nama_unik,'N');

        header("location:../../../../index.php?module=page");
    } 
}
elseif($aksi=='edit'){
    $page = new viewpage();
    $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_page];
    }else{
        $file = $nama_unik;
        $upload->page($nama_unik,$db->linkupload());
    }

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

        $page->id = $_POST[id];
        $page->editpage($_POST[judul],$_POST[isi],$_POST[keterangan],$file,'Y');

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

        $page->id = $_POST[id];
        $page->editpage($_POST[judul],$_POST[isi],$_POST[keterangan],$file,'N');

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