<?php
session_start();
$currDir = __DIR__;
$rootDir = dirname(dirname($currDir));
$mysydeDir = $rootDir . '/mysyde';

require_once($rootDir .  '/vendor/autoload.php');
require_once($rootDir . "/mysyde/frontend/frontend_functions.inc.php");

$envDir = $rootDir . '/config';
$envFilePath = $envDir . '/.env';
if (is_dir($envDir) && file_exists($envFilePath) && is_file($envFilePath) && is_readable($envFilePath)) {
    $dotenv = new \Dotenv\Dotenv($envDir);
    $dotenv->load();
}
$common = $mysydeDir ."/common/common_functions.inc.php";
require_once($common);

$connEnv = (local_environment()) ? $mysydeDir."/dc.config.php" : $mysydeDir."/dc-server.config.php";

require_once($connEnv);

db_connect();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $post_id = $_POST['post_id'];
    switch ($_POST['action']) {
        case 'complete_task':
            complete_task($task_id, $_POST['status']);
            break;
        case 'delete_post':
            delete_post($post_id);
            break;
        case 'count_comments':
            count_comment_ajax($post_id);
            break;
        case 'count_likes':
            count_likes_ajax($post_id);
            break;
        case 'update_likes':
            update_likes($post_id, $_POST['main_contact_id']);
            break;
        default:
            break;
    }
}

function delete_post($post_id){
    $query_link = "DELETE FROM community_board_post_category_link WHERE community_board_post_id = ".$post_id;
    $result_link = @mysqli_query($GLOBALS['mysql_con'], $query_link);

    $query_comment = "DELETE FROM community_board_comment WHERE community_board_post_id = ".$post_id;
    $result_comment = @mysqli_query($GLOBALS['mysql_con'], $query_comment);

    $query_post = "DELETE FROM community_board_post WHERE id = ".$post_id;
    $result_post = @mysqli_query($GLOBALS['mysql_con'], $query_post);
}

function get_main_contact($main_contact_id){
    $query = "SELECT * FROM main_contact WHERE id = ".$main_contact_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $row = @mysqli_fetch_array($result);
    return $row;
}

function count_comment_ajax($post_id){
    $query = "SELECT * FROM community_board_comment WHERE community_board_post_id = ".$post_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $count = 0;
    while($row = @mysqli_fetch_array($result)){
        $count++;
    }
    echo $count;
}

function count_likes_ajax($post_id){
    $query = "SELECT * FROM community_board_post_likes WHERE community_board_post_id = ".$post_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $count = 0;
    while($row = @mysqli_fetch_array($result)){
        $count++;
    }
    echo $count;
}

function update_likes($post_id, $main_contact_id){
    $query = "SELECT * FROM community_board_post_likes WHERE community_board_post_id = ".$post_id." AND main_contact_id = ".$main_contact_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) >= 1) {
        $query = "DELETE FROM community_board_post_likes WHERE community_board_post_id = ".$post_id." AND main_contact_id = ".$main_contact_id;
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    }else{
        $query = "INSERT INTO community_board_post_likes
        (community_board_post_id, main_contact_id)
        VALUES (
            '" . $post_id . "',
            '" . $main_contact_id . "'
        )";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}