<?php
require_once (dirname(__DIR__, 2) . '/init.php');
//if (!m_admin_check()) {
//    m_redirect(ADMIN_URL . '/login');
//}
$db = new DB();

$data = sanitizeData($_POST['data']);
$action = $data['module'] ? $data['module'] :  m_u_p('action');
$site_lang =  m_get_session('lang') ?  m_get_session('lang') : lang_is_default('lang_code');
$lang = $data['lang'] ? $data['lang'] : $site_lang;
if (is_array($data['ids'])){
    $ids = json_encode($data['ids']);
}else{
    $ids = $data['ids'];
}


switch ($action) {
    case $action:
        if (is_array($data)) {
            if ($data['source'] == 'automatic'){
                $post = posts_ajax('data',$action, $lang, $data['order'],$data['direction'],$data['limit'],$data['category_id']);
            }else{
                $data_array = json_decode(html_entity_decode($ids), true);
                $key_list = array_keys($data_array);
                $placeholders = implode(",", array_fill(0, count($key_list), "?"));
                $query = "SELECT * FROM posts WHERE id IN ($placeholders)";
                $query = $db->query($query, $key_list);
                if ($query) {
                    $post = $query->fetchAll(PDO::FETCH_ASSOC);
                } else {
                    var_dump($db->error());
                }
            }
        } else {
            $post = post($data);
        }
        ob_start();
        include '../include/'.$action.'.php';
        $content = ob_get_clean();
        $data = [
            'posts' => $post,
            'data' => $data,
        ];
        $json = json_encode($content);
        echo $json;
        break;

    default:
        echo json_encode(['error' => 'Geçersiz işlem']);
        break;
}