<?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 'edit_forwarding':
        edit_forwarding();
        break;
    case 'delete_forwarding':
        delete_forwarding();
        break;
    case 'new_forwarding':
        require_once("edit_forwarding_cardform.inc.php");
        break;
    case 'save_forwarding':
        save_forwarding();
        break;
        //Line Forwarding - Unterpunkt
    case 'save_line_forwarding':
        save_line_forwarding();
        break;
    case 'new_line_forwarding':
        new_line_forwarding();
        break;
    case 'edit_line_forwarding':
        edit_line_forwarding();
        break;
    case 'delete_line_forwarding':
        delete_line_forwarding();
        break;

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

function save_forwarding()
{
    $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 forwarding_header SET
				description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . "
				WHERE id = '" . $_REQUEST["input_id"] . "' 
			LIMIT 1";
        $inserted = FALSE;
        $input_id = $_REQUEST["input_id"];
    } else {
        $query    = "INSERT INTO forwarding_header
			(main_language_id, description, modified_date, modified_user)
			VALUES (
				'" . $GLOBALS["language"]['id'] . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_description']) . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . "
			)";
        $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("url_management_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("url_management_msg_success2") . '</div>';
        }

        update_sitepart_changes(17, $input_id);

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

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

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

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

            edit_forwarding($input_id, $messages);
        } else {
            edit_forwarding($input_id, $messages);
        }
    } else {
        header('EDIT_ERROR: 1');
        $input_forwarding["description"] = $_REQUEST["input_description"];
        require_once("edit_forwarding_cardform.inc.php");
    }
}

function edit_forwarding()
{

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

    if ($input_id <> '') {
        $query  = "SELECT * FROM forwarding_header WHERE id = '" . $input_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_forwarding = @mysqli_fetch_array($result);
            require_once("edit_forwarding_cardform.inc.php");
        }
    } else {
        $input_forwarding = array();
        require_once("edit_forwarding_cardform.inc.php");
    }
}

function delete_forwarding()
{

    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM forwarding_header WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "DELETE FROM forwarding_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 = 17 AND main_sitepart_header_id = '" . $_REQUEST["input_id"] . "'";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        update_sitepart_changes(17, $_REQUEST["input_id"], TRUE);
    }
    require_once("edit_forwarding_listform.inc.php");
}

//Line Forwarding
function new_line_forwarding()
{

    $input_line = array();

    $input_line['header_id'] = $_REQUEST['input_id'];


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

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

    $id = $_REQUEST["input_line_id"];
    $header_id = $_REQUEST['input_id'];
    $forward_url = "";
    $forward_navigation_id = 0;


    switch ($_POST["input_forward_type"]) {

        case 1: // interne weiterleitung / Navigation
            $forward_navigation_id = $_POST['input_forward_navigation_id'];
            break;

        case 2: // interne pfad weiterleitung
            $forward_url = $_POST['input_forward_url'];
            break;

        case 3: // externe weiterleitung
            $forward_url = addhttp($_POST['input_forward_url_external']);
            break;
    }

    if ($_REQUEST["input_line_id"] <> '') {
        $query    = "UPDATE forwarding_line SET
				description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
				forward_type = '" . (int)$_POST["input_forward_type"] . "', 
                forward_navigation_id = " . $forward_navigation_id . ",
                forward_url = '" . $forward_url . "',
                forward_parameter = '" . $_POST["input_forward_parameter"] . "',
                forward_icon = '" . $_POST["input_forward_icon"] . "'
				WHERE id = '" . $_REQUEST["input_line_id"] . "' 
			LIMIT 1";
        $inserted = FALSE;
    } else {
        $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM forwarding_line WHERE header_id = '" . $_REQUEST["input_id"] . "'"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO forwarding_line
			(header_id, description, forward_type, forward_navigation_id, forward_url, forward_parameter, forward_icon, sorting)
			VALUES (
				'" . (int)$header_id . "', 
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_description']) . "',
                '" . (int)$_POST["input_forward_type"] . "',
                '" . $forward_navigation_id . "',
                '" . $forward_url . "',
                '" . $_POST["input_forward_parameter"] . "',
                '" . $_POST["input_forward_icon"] . "',
                '" . $sorting . "'
			)";
        $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) {

            edit_forwarding();
        } else {
            if ($inserted === TRUE) {
                $id = mysqli_insert_id($GLOBALS['mysql_con']);
            }
            edit_line_forwarding($id);
        }
    } else {
        $input_line["id"] = $_REQUEST["input_line_id"];
        $input_line["description"] = $_REQUEST['input_description'];
        $input_line["header_id"] = $header_id;
        $input_line["forward_type"] = $_REQUEST['forward_type'];
        $input_line["forward_url"] = $_REQUEST['forward_url'];
        $input_line["input_forward_url_external"] = $_REQUEST['input_forward_url_external'];

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


function edit_line_forwarding($_id = "")
{
    if ((int)$_id > 0) {
        $line_id = $_id;
    } else {
        $line_id = $_REQUEST["input_line_id"];
    }

    if ($line_id <> '') {
        $query  = "SELECT * FROM forwarding_line WHERE id = '" . $line_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_forwarding_line_cardform.inc.php");
        }
    } else {
        require_once("edit_forwarding_line_cardform.inc.php");
    }
}

function delete_line_forwarding()
{
    if ($_REQUEST["input_line_id"] <> '') {
        $query = "DELETE FROM forwarding_line WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    edit_forwarding();
}

/** Dropdown für icons 20210707 DuP **/
function create_preview_select($input_line)
{
    require 'forwarding_config.inc.php';
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");

    echo "<div class\"icon-select-container\">";

    echo "<div class=\"label\"><label for=\"forward_icon\">Icon auswählen</label></div>";

    echo "<div class\"input\">";
    echo "<select style=\"float:left;\" id=\"forward_icon\" class=\"code\" type=\"text\" name=\"input_forward_icon\" >";

    foreach ($forwarding_icon_images as $key => $data) {
        $selected_text = ($key == $input_line["forward_icon"]) ? " selected=\"selected\"" : "";
        echo "<option value=\"" . $key . "\" $selected_text>" . $data["name"] . "</option>";
        $value++;
    }
    echo "</select>";

    echo "<div class=\"dropdownPreview\" data-image=\"" . $input_line['forward_icon'] . "\" style=\"float:left\" ></div>";
    echo "</div>";
    echo "</div>";
}
