<?
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_category':
        require_once("community_board_category_listform.inc.php");
        break;
    case 'load_community_board_category':
        require_once("community_board_category_listform.inc.php");
        break;
    case 'new_community_board_category' :
        require_once("community_board_category_cardform.inc.php");
        break;
    case 'edit_community_board_category':
        edit_community_board_category();
        break;
    case 'delete_community_board_category':
        delete_community_board_category();
        break;
    case 'save_community_board_category':
        save_community_board_category();
        break;
    case 'moveup_community_board_category':
        moveup_community_board_category();
        break;
    case 'movedown_community_board_category':
        movedown_community_board_category();
        break;
    case 'update_sortorder_community_board_category':
        update_sortorder_community_board_category();
        break;
    default:
        require_once("community_board_category_listform.inc.php");
        break;
}

function edit_community_board_category($_id = "") {
    if ((int)$_id > 0) {
        $category_id = $_id;
    } else {
        $category_id = $_REQUEST["input_category_id"];
    }
    if ($category_id <> '') {
        $query  = "SELECT * FROM community_board_category WHERE id = '" . $category_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);

        if (@mysqli_num_rows($result) == 1) {
            $input_category = @mysqli_fetch_array($result);
            require_once("community_board_category_cardform.inc.php");
        }
    } else {
        require_once("community_board_category_cardform.inc.php");
    }
}

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

    if ($_REQUEST["input_category_id"] <> '') {
        $query    = "UPDATE community_board_category SET
					description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "'
				WHERE id = '" . $_REQUEST["input_category_id"] . "' LIMIT 1";
    } else {
        $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM main_community_board_category"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO community_board_category (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_category_listform.inc.php");
    } else {
        require_once("community_board_category_cordform.inc.php");
    }
}

function delete_community_board_category() {
    if ($_REQUEST["input_category_id"] <> '') {
        $query = "DELETE FROM mcommunity_board_category WHERE id = '" . $_REQUEST["input_category_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    require_once("community_board_category_listform.inc.php");
}

function moveup_community_board_category() {
    move_community_board_category("<", "DESC", $_POST["input_category_id"]);
    require_once("community_board_category_listform.inc.php");
}

function movedown_community_board_category() {
    move_community_board_category(">", "ASC", $_POST["input_category_id"]);
    require_once("community_board_category_listform.inc.php");
}

function move_community_board_category( $way, $order, $pic_id ) {
    $query  = "SELECT * FROM community_board_category WHERE id = '" . $pic_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $curr_pic = @mysqli_fetch_array($result);
    }
    $query  = "SELECT * FROM community_board_category WHERE sorting " . $way . " " . $curr_pic["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $change_pic = @mysqli_fetch_array($result);
    }
    if (($curr_pic["id"] <> '') && ($change_pic["id"] <> '')) {
        $query = "UPDATE community_board_category SET sorting = " . $change_pic["sorting"] . " WHERE id = " . $curr_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "UPDATE community_board_category SET sorting = " . $curr_pic["sorting"] . " WHERE id = " . $change_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}

function update_sortorder_community_board_category() {
    $sorting = 1;
    foreach ($_POST['linklistid'] as $itemId) {
        $query = "UPDATE community_board_category SET sorting = " . $sorting . " WHERE id = '" . $itemId."'";
        var_dump($query);
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $sorting++;
    }
}