<?php

session_start();

require '../../config.php';
require '../functions.php';

if (isset($_SESSION['user_email'])){

$connect = connect($database);

$check_access = check_access($connect);

if ($check_access['user_role'] == 1 || $check_access['user_role'] == 2){

// DataTables server-side processing
$query = "SELECT bp.*, bc.blog_cat_name, u.user_name 
          FROM blog_posts bp
          LEFT JOIN blog_categories bc ON bp.post_category = bc.blog_cat_id
          LEFT JOIN users u ON bp.post_author = u.user_id
          ORDER BY bp.created_at DESC";

$sentence = $connect->prepare($query);
$sentence->execute();
$data = $sentence->fetchAll(PDO::FETCH_ASSOC);

// Format for DataTables
$output = array(
    "aaData" => $data
);

echo json_encode($output);

}else{
    echo json_encode(array("aaData" => array()));
}

}else{
    echo json_encode(array("aaData" => array()));
}

?>
