<?php
require_once('session.php');

$users = $u['user'];
$author = $u['full_name'];

$title = str_replace(array("’","'"),"&apos;",$_POST['title']);
$slugs = preg_replace("/[^a-zA-Z0-9]/","-",$title);
$slug = strtolower($slugs);

$content = str_replace(array("’","'"),"&apos;",$_POST['content']);
$postID = $_POST['postID'];

$date = date('Y-m-d');
$kode = date('YdmHis');

$gbr = $_FILES['image']['name'];
$error = $_FILES['image']['error'];

$explode = explode('.', $gbr);
$extensi = strtolower(end($explode));

$newname = 'blog_'.$users.'_'.$kode.'.'.$extensi;

/* PATH ASLI SISTEM */
$upload_dir = "../../upload/";

/* buat folder jika belum ada */
if(!file_exists($upload_dir)){
    mkdir($upload_dir,0777,true);
}

if($postID == ''){

    // INSERT
    if($gbr != "" && $error == 0){

        if(move_uploaded_file($_FILES['image']['tmp_name'],$upload_dir.$newname)){

            mysqli_query($conn,"
            INSERT INTO tb_post
            (slug,title,image,content,author,created_date,last_update,user,status)
            VALUES
            ('$slug','$title','$newname','$content','$author','$date','$date','$users',1)
            ") or die(mysqli_error($conn));

            header('location:../promo/?notif=1');
            exit();

        } else {

            header('location:../promo/?notif=4');
            exit();
        }

    } else {

        mysqli_query($conn,"
        INSERT INTO tb_post
        (slug,title,image,content,author,created_date,last_update,user,status)
        VALUES
        ('$slug','$title','no-photo.jpg','$content','$author','$date','$date','$users',1)
        ") or die(mysqli_error($conn));

        header('location:../promo/?notif=1');
        exit();
    }

} else {

    // UPDATE
    $query_old = mysqli_query($conn,"SELECT image FROM tb_post WHERE cuid='$postID'");
    $data_old = mysqli_fetch_assoc($query_old);

    $old_image = $data_old['image'] ?? '';

    if($gbr != "" && $error == 0){

        if(move_uploaded_file($_FILES['image']['tmp_name'],$upload_dir.$newname)){

            if(
                $old_image != 'no-photo.jpg' &&
                !empty($old_image) &&
                file_exists($upload_dir.$old_image)
            ){
                unlink($upload_dir.$old_image);
            }

            mysqli_query($conn,"
            UPDATE tb_post SET
            slug='$slug',
            title='$title',
            image='$newname',
            content='$content',
            author='$author',
            last_update='$date',
            user='$users'
            WHERE cuid='$postID'
            ") or die(mysqli_error($conn));

            header('location:../promo/?postID='.$postID.'&notif=1');
            exit();

        } else {

            header('location:../promo/?postID='.$postID.'&notif=4');
            exit();
        }

    } else {

        mysqli_query($conn,"
        UPDATE tb_post SET
        slug='$slug',
        title='$title',
        content='$content',
        author='$author',
        last_update='$date',
        user='$users'
        WHERE cuid='$postID'
        ") or die(mysqli_error($conn));

        header('location:../promo/?postID='.$postID.'&notif=1');
        exit();
    }
}
?>