<?php
$action = $_POST['action'];
require '../ImageCompressor.php';

require '../../include/dbconn.php';
//store function
if ($action == 'store') {

    $pdo = dbConnforTutle::getConnection();

    $imagename = $_FILES['imagename']['name'];
    $imagenameSrc = $_FILES['imagename']['tmp_name'];
    $title = $_POST['title']; 
    $description = $_POST['description']; 
    $publish_date = $_POST['publish_date']; 
    
    if($imagename =="" || $title=="" ||  $description == "" || $publish_date == ""){
        echo 'Enter all mandatory field';
        exit;
    } 
 
    if($imagename){
        $logoImgName = ImageCompressFn($imagename, $imagenameSrc, 1200, 600, "../images/blog/");
    }

    $slug = slugify($_POST['title']);
    $stmt = $pdo->prepare('SELECT * FROM blog WHERE slug="'.$slug.'"');
    $stmt->execute();
    $alldata = $stmt->fetchAll();
    if(count($alldata) > 0){
        $slug = $slug.'-'.count($alldata)+1;
    }
    
    $stmt = $pdo->prepare("INSERT INTO blog (imagename,title, description, slug, publish_date) VALUES(:imagename,:title, :description, :slug, :publish_date)");
    $stmt->bindParam(':imagename', $logoImgName);
    $stmt->bindParam(':title', $title);  
    $stmt->bindParam(':description', $description); 
    $stmt->bindParam(':slug', $slug); 
    $stmt->bindParam(':publish_date', $publish_date);
    $boolean = $stmt->execute();

    if ($boolean) { 
        echo 'ok';
        exit(0);
    } else {
        echo 'Error';
        exit(0);
    } 

    //update function
} else if ($action == 'update') {
    $pdo = dbConnforTutle::getConnection();

    
    $tblid = $_POST['tblid'];
    $imagename = $_FILES['imagename']['name'];
    $imagenameSrc = $_FILES['imagename']['tmp_name'];
    $title = $_POST['title']; 
    $logoImgName = $_POST['old_imagename'];  
    $description = $_POST['description']; 
    $publish_date = $_POST['publish_date']; 

    if($title=="" ){
        echo 'Enter all mandatory field';
        exit;
    }


    if($imagename){
        unlink('../images/blog/'.$logoImgName);
        unlink('../images/blog/thumb_'.$logoImgName);
        unlink('../images/blog/'.substr($logoImgName, 15));
        $logoImgName = ImageCompressFn($imagename, $imagenameSrc, 900, 400, "../images/blog/");
    }

    $slug = slugify($_POST['title']);
    $stmt = $pdo->prepare('SELECT * FROM blog WHERE slug="'.$slug.'"');
    $stmt->execute();
    $alldata = $stmt->fetchAll();
    
    if(count($alldata) > 0){
        $count = count($alldata)+1; 
        $slug = $slug.'-'.$count;
    }
 

    $stmt = $pdo->prepare("UPDATE blog SET imagename=:imagename,title=:title, publish_date=:publish_date, description=:description, slug=:slug WHERE id=:tblid");
    $stmt->bindParam(':imagename', $logoImgName);
    $stmt->bindParam(':title', $title);  
    $stmt->bindParam(':description', $description); 
    $stmt->bindParam(':tblid', $tblid);
    $stmt->bindParam(':slug', $slug);
     $stmt->bindParam(':publish_date', $publish_date); 
    $boolean = $stmt->execute();

    if ($boolean) {
        echo 'ok';
        exit(0);
    } else {
        echo 'Error';
        exit(0);
    }
    //delete function
} else if ($action == 'delete') {
    $pdo = dbConnforTutle::getConnection();

    $tblid = $_POST['tblid'];

    $stmt = $pdo->prepare('SELECT * FROM blog WHERE id=' . $tblid);
    $stmt->execute();
    $getdata = $stmt->fetchAll();
    $imagename = $getdata[0]['imagename'];

    if ($imagename) {
        unlink('../images/blog/' . $imagename);
    }

    $stmt = $pdo->prepare("DELETE FROM blog WHERE id=:tblid");
    $stmt->bindParam(':tblid', $tblid);
    $boolean = $stmt->execute();

    if ($boolean) {
        echo 'ok';
        exit(0);
    } else {
        echo 'Error';
        exit(0);
    }
}


function slugify($text, string $divider = '-')
{
  // replace non letter or digits by divider
  $text = preg_replace('~[^\pL\d]+~u', $divider, $text);

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  // trim
  $text = trim($text, $divider);

  // remove duplicate divider
  $text = preg_replace('~-+~', $divider, $text);

  // lowercase
  $text = strtolower($text);

  if (empty($text)) {
    return 'n-a';
  }

  return $text;
}
