<?php
date_default_timezone_set('Europe/Berlin');

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

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

switch ($action) {
    case 'save_screen':
        save_screen();
        break;
    case 'new_screen':
        require_once("infopoint_screens_cardform.inc.php");
        break;
    case 'edit_screen':
        edit_screen();
        break;
    case 'delete_screen':
        delete_screen();
        break;
    case 'open_screen':
        open_screen();
        break;
    case 'update_sortorder_faq':
        update_sortorder_faq();
    break;

    default:
        require_once("infopoint_screens_listform.inc.php");
        break;
}

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

function edit_screen( $_id = "", $messages = array() ) {

    if ((int)$_id > 0) {
        $input_id = $_id;
    } else {
        $input_id = $_REQUEST["input_id"];
    }

    if ($input_id <> '') {

        $query = "SELECT * FROM main_infopoint_screens WHERE id = '" . $input_id . "' LIMIT 1";

        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        
        if (@mysqli_num_rows($result) == 1) {

            $input_line = @mysqli_fetch_array($result);

            require_once("infopoint_screens_cardform.inc.php");

        }

    } else {
        require_once("infopoint_screens_cardform.inc.php");
    }

}

function save_screen() {
    $translation    = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages       = array();
    $id             = $_REQUEST["input_id"];
    $default_active = $_REQUEST["input_default_active"];
    $header_id = "";

    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE main_infopoint_screens SET
                    description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "' ,
                    modified_date = '" . time() . "',
                    modified_user = '" . (int)$GLOBALS["admin_user"]['id'] . "',
                    main_infopoint_preset_id = '" . $_REQUEST["input_infopoint_preset"] . "'
                WHERE id = " . $_REQUEST["input_id"] . " LIMIT 1";
        $inserted = FALSE;
    } else {
        $hash = base64_encode(random_bytes(18));
        $query    = "INSERT INTO main_infopoint_screens (hash, description, modified_date, modified_user, main_infopoint_preset_id) VALUES (
                        '".$hash."',
                        '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"])."',
                        '".time()."',
                        '".(int)$GLOBALS["admin_user"]['id']."',
                        '".$_REQUEST["input_infopoint_preset"]."'
        )";
        $inserted = TRUE;
    }


    if ($_REQUEST["input_description"] == '') {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_description") . "</div>\n";
        $error      = TRUE;
    }

    // benoetigt fuer ajax calls
    if ($error === TRUE) {
        header('EDIT_ERROR: 1');
    }

    if (!$error) {
        @mysqli_query($GLOBALS['mysql_con'], $query);
 
        if ($inserted === TRUE) {
            $header_id = mysqli_insert_id($GLOBALS['mysql_con']);
            createMySydeLog('main_infoboard_screens', $header_id, 'INSERT');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
        } else {
            createMySydeLog('main_infoboard_screens', $_REQUEST['input_id'], 'UPDATE');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success2") . '</div>';
        }
        
    } else {
        $input_line["id"]          = $_REQUEST["input_id"];
        $input_line["description"] = $_REQUEST["input_description"];
    }
    require_once("infopoint_screens_cardform.inc.php");
}

function delete_screen() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM main_infopoint_screens WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        createMySydeLog('main_infopoint_screens', $_REQUEST['input_id'], 'DELETE');
    }
    require_once("infopoint_screens_listform.inc.php");
}

function open_screen(){
    if ($_REQUEST["input_id"] <> '') {
        $query = "SELECT * FROM main_infopoint_screens WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        $input = @mysqli_fetch_array($result);
        $url = "https://".$_SERVER['HTTP_HOST']."/module/infopoint/show_infopoint.inc.php?hash=".$input['hash'];
    ?>
    <script>
        window.open('<?php echo $url ?>', '_blank'); 
    </script>
    <?php }
    require_once("infopoint_screens_listform.inc.php");
}