<?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_faq_list':
        save_faq();
        break;
    case 'save_faq_page_link':
        save_faq_page_link();
        break;
    case 'new_screen':
        require_once("edit_faq_cardform.inc.php");
        break;
    case 'edit_screen':
        edit_screen();
        break;
    case 'delete_screen':
        delete_faq();
        break;
    case 'new_faq':
        require_once("page_faq_preview_cardform.inc.php");
        break;
    case 'edit_faq':
        edit_faq_page_link();
    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_screens( $_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_faq() {
    $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_faq SET
                    description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "' ,
                    modified_date = '" . time() . "',
                    modified_user = '" . (int)$GLOBALS["admin_user"]['id'] . "',
                    default_active = '" . $default_active . "'
                WHERE id = " . $_REQUEST["input_id"] . " LIMIT 1";
        $inserted = FALSE;
    } else {
        $query    = "INSERT INTO main_faq (main_language_id, description, modified_date, modified_user, default_active) VALUES (
                        '".(int)$GLOBALS["language"]['id']."',
                        '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"])."',
                        '".time()."',
                        '".(int)$GLOBALS["admin_user"]['id']."',
                        '".$default_active."'
        )";
        $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_faq', $header_id, 'INSERT');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
        } else {
            createMySydeLog('main_faq', $_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"];
    }
    
    if (!$error) {

        if ($_REQUEST["input_id"] <> '') {

            $query2    = "UPDATE main_faq_line SET
                    text1 = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_question"]) . "' ,
                    text2 = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_answer"]) . "',
                    hashtags = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_hashtags"]) . "'
                WHERE main_faq_id = " . $_REQUEST["input_id"] . " LIMIT 1";
            
        } else {
            
            $query2    = "INSERT INTO main_faq_line (main_faq_id, text1, text2, hashtags) VALUES (
                            '".(int)$header_id."',
                            '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_question"])."',
                            '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_answer"])."',
                            '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["text_hashtags"])."')";

        }

        @mysqli_query($GLOBALS['mysql_con'], $query2);
    }

    $groups = array();
    $group_ids = $_POST['select_group'];
    if($group_ids != NULL && isset($_POST['select_group'])){
        $groups = explode(',', $group_ids);
    }

    if (!$error) {

        // gruppen speichern
        $query = "SELECT id, default_active FROM main_faq_setup_group";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        $box = array();
        while ($box = @mysqli_fetch_assoc($result)) {
            $query_2 = "SELECT id, active FROM main_faq_group_link WHERE main_faq_setup_group_id = '" . $box["id"] . "' AND main_faq_id = '" . $id . "' LIMIT 1";
            $result_2 = @mysqli_query($GLOBALS['mysql_con'], $query_2);
            // $arr_ind = "input_faq_group_" . $box["id"] . "_active";
            if (!empty($groups) && in_array($box['id'], $groups)) {
                $active = '1';
            } else {
                $active = '0';
            }
            if (@mysqli_num_rows($result_2) == 0) {
                if ($header_id == "") {
                    $header_id = $_REQUEST["input_id"];
                }
                $query_3 = "INSERT INTO main_faq_group_link SET
                main_faq_id = " . $header_id . ",
                main_faq_setup_group_id = " . $box["id"] . ",
                active = " . $active;
                @mysqli_query($GLOBALS['mysql_con'], $query_3);
            } else {
                $row = @mysqli_fetch_assoc($result_2);
                $result_3 = $row[0];
                if ($active != $result_3["active"]) {
                    $query_3 = "UPDATE main_faq_group_link SET
                    active = " . $active . " WHERE
                    main_faq_id = " . $id . " AND
                    main_faq_setup_group_id = " . $box["id"];
                    @mysqli_query($GLOBALS['mysql_con'], $query_3);
                }
            }
        }

        edit_faq($header_id, $messages);

    } else {
        edit_faq($header_id, $messages);
    }

}


//FAQ 



//Checkbox Groups
function create_faq_groups_select2($input_faq)
{
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");

    $faq_id = $input_faq['id'];
    $setup_id = $input_collection['main_collection_setup_id'];

    $query = "SELECT * FROM main_faq_setup_group";
    $result = mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) {
        return;
    }

    while ($row = @mysqli_fetch_array($result)) {
        $query2 = "SELECT active FROM main_faq_group_link WHERE
            main_faq_id = '" . $faq_id . "' AND
            main_faq_setup_group_id = " . $row['id'];

        $result2 = mysqli_query($GLOBALS['mysql_con'], $query2);
        if (@mysqli_num_rows($result2) == 0) {
            input($row['description'], "input_faq_group_" . $row["id"] . "_active", "checkbox");
        } else {
            input($row['description'], "input_faq_group_" . $row["id"] . "_active", "checkbox", @mysqli_result($result2, 0));
        }
    }
    
}

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

    $faq_id = $input_faq['id'];
    $setup_id = $input_collection['main_collection_setup_id'];


    $query = "SELECT * FROM main_faq_setup_group";
    $result = mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) {
        return;
    }

    $preselect = array();
    $query2 = "SELECT * FROM main_faq_group_link WHERE main_faq_id = '" . $faq_id . "' AND active = 1";
    $result2 = mysqli_query($GLOBALS['mysql_con'], $query2);

    while($row2 = @mysqli_fetch_array($result2)){
        array_push($preselect,$row2['main_faq_setup_group_id']);
    }
    ?>
     <div class="label"><label><?php echo 'FAQ-Gruppen'; ?></label></div>
            <div class="groups bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
                <input type="hidden" name="select_group">
                <i class="dropdown icon"></i>
                <div class="default text"><?php echo 'FAQ-Gruppen'; ?></div>
                <div class="menu">
                    <?  
                    
                        while($row = @mysqli_fetch_array($result)) {
                            echo "<div class=\"item\" data-value=\"{$row['id']}\">".$row["description"]."</div>";
                        }

                    ?>
                </div>
        </div>
        <script type="text/javascript" src="/plugins/jquery/jquery-3.5.1.min.js"></script>
                <script type="text/javascript" src="/plugins/Semantic-UI-master/semantic.min.js"></script>
                <link rel="stylesheet" type="text/css" href="/plugins/Semantic-UI-master/bc-semantic.css?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/plugins/Semantic-UI-master/bc-semantic.css')?>">
                <script>
                    var $y = jQuery.noConflict();

                    $y( document ).ready(function() {
                        $y('.groups.ui.fluid.multiple.dropdown').dropdown('set selected', <?= json_encode($preselect) ?>); 
                    });


        </script>
    <? }

function delete_faq() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM main_faq WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        createMySydeLog('main_faq', $_REQUEST['input_id'], 'DELETE');
        //Gruppe verlinkung loeschen
        $query = "DELETE FROM main_faq_group_link WHERE main_faq_id = '" . $_REQUEST["input_id"] . "'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        echo "$query";

        //Lines loeschen loeschen
        $query = "DELETE FROM main_faq_line WHERE main_faq_id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        /*$query = "DELETE FROM slidecontent_line WHERE header_id = '" . $_REQUEST["input_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);*/

        // zuordnung von Seiten loeschen
        /*$query = "DELETE FROM main_page_link WHERE main_sitepart_id = 4 AND main_sitepart_header_id = '" . $_REQUEST["input_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);*/

        //update_sitepart_changes(4, $_REQUEST["input_id"], TRUE);
    }
    require_once("edit_faq_listform.inc.php");
}

//FAQ speichern im Seitenstruktur
function save_faq_page_link() {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id    = "";
    $input_all_languages = ($_POST["input_all_languages"] == "on") ? 1 : 0;

    if ($_REQUEST["input_id"] <> '') {

        $query = "UPDATE main_faq_header SET
                description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
                modified_date = " . time() . ",
                modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
                all_languages = " . $input_all_languages . "
                WHERE id = '" . $_REQUEST["input_id"] . "' 
            LIMIT 1";
        $inserted    = FALSE;
        $input_id    = $_REQUEST["input_id"];
        save_header_group($input_id);
    } else {
        $query = "INSERT INTO main_faq_header
            (main_language_id, modified_date, modified_user, description, all_languages)
            VALUES (
                " . (int)$GLOBALS["language"]['id'] . ",
                " . time() . ",
                " . (int)$GLOBALS["admin_user"]['id'] . ",
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
                " . $input_all_languages . "
            )";
        $inserted    = TRUE;
    }

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

        @mysqli_query($GLOBALS['mysql_con'], $query);

        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            $messages[] = '<div class="successbox">' . $translation->get("slidecontent_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("slidecontent_msg_success2") . '</div>';
        }

        update_sitepart_changes(16, $input_id);

        if (is_page_edit()) {
            header('EDIT_ERROR: 1'); // verhindern, dass overlay geschlossen wird
            if (line_already_exists($_REQUEST["input_page_id"], 16, $input_id) === FALSE) {
                assoc_sitepart(16, $input_id, FALSE);
            }

            if ($_REQUEST['force_close'] == 1) {
                edit_content_page();
                return;
            }

            edit_faq_page_link($input_id, $messages);

        } elseif (is_component_edit()) {
            header('EDIT_ERROR: 1'); // verhindern, dass overlay geschlossen wird
            if (line_already_exists($_REQUEST["input_component_id"], 16, $input_id) === FALSE) {
                assoc_sitepart(16, $input_id, FALSE);
            }

            if ($_REQUEST['force_close'] == 1) {
                edit_component("", $messages);
                return;
            }

            edit_faq_page_link($input_id, $messages);
        } else {
            edit_faq_page_link($input_id, $messages);
        }

    } else {
        header('EDIT_ERROR: 1');
        $input_slidecontent                = array();
        $input_slidecontent["id"]          = $_REQUEST["input_id"];
        $input_slidecontent["description"] = $_REQUEST["input_description"];
        $input_slidecontent["all_languages"]= $input_all_languages;

        require_once("page_faq_preview_cardform.inc.php");
    }
}

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


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

    if ($input_id <> '') {

        $query = "SELECT * FROM main_faq_header 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("page_faq_preview_cardform.inc.php");

        }

    } else {

        require_once("page_faq_preview_cardform.inc.php");

    }


}

function loadFAQGroup($faq_id){
    $query = "SELECT * FROM main_faq_setup_group";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    if (@mysqli_num_rows($result) == 0) {
        return;
    }
    while ($row = @mysqli_fetch_array($result)) {
        $query2 = "SELECT * FROM main_faq_header_group_link WHERE header_id =" . $faq_id . " AND faq_group_id = " . $row['id'];
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        $row2 = @mysqli_fetch_array($result2);
        $checked = 0;
        if(isset($row2)){
            $checked = $row2['active'];
        }
        input($row['description'], "input_group_".$row['id'], "checkbox", $checked);
    }
}

function save_header_group($faq_id){
    $query = "SELECT * FROM main_faq_setup_group";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    if (@mysqli_num_rows($result) == 0) {
        return;
    }
    while ($row = @mysqli_fetch_array($result)) {
        if($_POST['input_group_'.$row['id']] == 'on'){
            $active = 1;
        }else {
            $active = 0;
        }
        $query2 = "SELECT * FROM main_faq_header_group_link WHERE header_id =" . $faq_id . " AND faq_group_id = " . $row['id'];
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        $row2 = @mysqli_fetch_array($result2);
        if(isset($row2)){
            $query3 = "UPDATE main_faq_header_group_link SET
            active = ".$active."
            WHERE id = '" . $row2['id'] . "' 
            LIMIT 1";
        }else {
            $query3 = "INSERT INTO main_faq_header_group_link
            (header_id, faq_group_id, active)
            VALUES (
                " . $faq_id . ",
                " . $row['id'] . ",
                " . $active . "
            )";
        }
        @mysqli_query($GLOBALS['mysql_con'], $query3);
    }
}