<?

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_group_faq_setup':
        save_group_faq_setup();
        break;
    case 'new_faq_setup_group':
        require_once("edit_faq_setup_group_cardform.inc.php");
        break;
    case 'edit_group_faq_setup':
        edit_group_faq_setup();
        break;
    case 'delete_group_faq_setup':
        delete_group_faq_setup();
        break;
    case 'update_sortorder_faq_group':
        update_sortorder_faq_group();
    break;

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



/* ++ DuP breadcrumb GmbH ++ */
function update_sortorder_faq_group() {
    $sorting = 1;
    foreach ($_POST['linklistid'] as $itemId) {
        $query = "UPDATE main_faq_setup_group SET sorting = " . $sorting . " WHERE id = '" . $itemId."'";
        echo $query;
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $sorting++;
    }
}

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

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

    if ($input_id <> '') {

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

        }

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

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

    if ($_REQUEST["input_group_id"] <> '') {
        $query    = "UPDATE main_faq_setup_group SET
                    description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "' ,
                    default_active = '" . $default_active . "'
                WHERE id = " . $_REQUEST["input_group_id"] . " LIMIT 1";
        $inserted = FALSE;
    } else {
        $query    = "INSERT INTO main_faq_setup_group (description, default_active) VALUES (
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
            '" . $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 ($_REQUEST['save_and_close'] == 1) {
            faq_edit_setup();
        } else {
            if ($inserted === TRUE) {
                $id         = mysqli_insert_id($GLOBALS['mysql_con']);
                createMySydeLog('main_faq_setup_group', $id, 'INSERT');
                $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
            } else {
                createMySydeLog('main_faq_setup_group', $_REQUEST['input_group_id'], 'UPDATE');
                $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success2") . '</div>';
            }
            edit_group_faq_setup($id, $messages);
        }

    } else {
        $input_line["id"]          = $_REQUEST["input_group_id"];
        $input_line["description"] = $_REQUEST["input_description"];
        require_once("edit_faq_setup_group_cardform.inc.php");
    }
}



/* ++ DuP breadcrumb GmbH ++ */



function delete_group_faq_setup() {
    if ($_REQUEST["input_group_id"] <> '') {
        $query = "DELETE FROM main_faq_setup_group WHERE id = '" . $_REQUEST["input_group_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        //Gruppe verlinkung loeschen
        $query = "DELETE FROM main_faq_group_link WHERE main_faq_setup_group_id = '" . $_REQUEST["input_group_id"] . "'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        createMySydeLog('main_faq_setup_group', $_REQUEST['input_group_id'], 'DELETE');
    }

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