<?
date_default_timezone_set('Europe/Berlin');

$messages = array();
$error    = FALSE;

if (isset($custom_action)) {
    $action = $custom_action;
} else {
    $action = $_REQUEST["action"];
}

switch ($action) {
    case 'close_community_board_post':
        require_once("community_board_post_listform.inc.php");
        break;
    case 'load_community_board_post':
        require_once("community_board_post_listform.inc.php");
        break;
    case 'new_community_board_post' :
        require_once("community_board_post_cardform.inc.php");
        break;
    case 'edit_community_board_post':
        edit_community_board_post();
        break;
    case 'delete_community_board_post':
        delete_community_board_post();
        break;
    case 'save_community_board_post':
        save_community_board_post();
        break;
    default:
        require_once("community_board_post_listform.inc.php");
        break;
}

function getContactFromID($main_contact_id){
    $query  = "SELECT * FROM main_contact WHERE id = '" . $main_contact_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $main_contact = @mysqli_fetch_array($result);
    }
    return $main_contact['name'];
}

function loadComments($post_id){
    $query  = "SELECT * FROM community_board_comment WHERE community_board_post_id = '" . $post_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    echo "<div class='community_board_chat'>";
    echo "<h4>Kommentare</h4>";
    while ($comment = @mysqli_fetch_array($result)) {
        echo "<p><strong>".getContactFromID($comment['main_contact_id']).": </strong>".$comment['comment']."</p>";
    }
    echo "</div>";
}

function edit_community_board_post($_id = "") {
    if ((int)$_id > 0) {
        $post_id = $_id;
    } else {
        $post_id = $_REQUEST["input_post_id"];
    }
    if ($post_id <> '') {
        $query  = "SELECT * FROM community_board_post WHERE id = '" . $post_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_post = @mysqli_fetch_array($result);
            require_once("community_board_post_cardform.inc.php");
        }
    } else {
        require_once("community_board_post_cardform.inc.php");
    }
}

function save_community_board_post() {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");

    if ($_REQUEST["input_post_id"] <> '') {
        $query    = "UPDATE community_board_post SET
					description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "'
				WHERE id = '" . $_REQUEST["input_post_id"] . "' LIMIT 1";
    } else {
        $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM main_community_board_post"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO community_board_post (description, sorting) VALUES (
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
			" . $sorting . "
		)";
    }
    @mysqli_query($GLOBALS['mysql_con'], $query);
    
    if ($_REQUEST['save_and_close'] == 1) {
        require_once("community_board_post_listform.inc.php");
    } else {
        require_once("community_board_post_cordform.inc.php");
    }
}

function delete_community_board_post() {
    if ($_REQUEST["input_post_id"] <> '') {
        $query = "DELETE FROM community_board_post WHERE id = '" . $_REQUEST["input_post_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    require_once("community_board_post_listform.inc.php");
}