<?php 
function new_line_collection_setup() {
    $input_line = array(
        'fieldtype' => $_POST['fieldtype'],
        'type_id'   => $_POST['type_id'],
    );
     
    require_once("edit_collection_custom_setup_line_cardform.inc.php");
}
function load_sitepart_form() {
    $collectionTypes = \DynCom\mysyde\common\classes\CollectionTypes::get();

    $code = $collectionTypes[$_POST['sitepartid']]['code'];

    $include = MODULE_PATH . "collection/edit_collection_" . $code . "_config.inc.php";
    if (file_exists($include)) {
        require_once($include);
    }
}
function save_line_collection_setup() {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages    = array();
    $id          = $_REQUEST["input_line_id"];
    $is_teaser   = ($_POST["main_input_is_teaser"] == "on") ? 1 : 0;
    $is_bulletlist   = ($_POST["main_input_is_bulletlist"] == "on") ? 1 : 0;
    $show_description_title   = ($_POST["main_input_show_description_title"] == "on") ? 1 : 0;
    $mandatory   = ($_POST["main_input_mandatory"] == "on") ? 1 : 0;
    $main_collection_id = $_POST['collection_id'];
    $options = array();
    // echo '<pre>';print_r($_POST);exit;
    foreach ($_POST as $key => $value) {
        if (substr($key, 0, 16) == 'collection_setup') {
            $options[$key] = $value;
        }
    }
    $serialize_options = serialize($options);
   
    if ($_REQUEST["input_line_id"] <> '') {
        $query    = "UPDATE main_collection_setup_content SET
					fieldtype = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["collection_type"]) . "', 
                    note = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_note"]) . "', 
                    input_maxlength = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_maxlength"]) . "', 
					fieldname = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_description"]) . "', 
					code = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_code"]) . "',
                    is_teaser = " . $is_teaser . ", 
                    is_bulletlist = " . $is_bulletlist . ", 
                    show_description_title = " . $show_description_title . ", 
                    mandatory = " . $mandatory . ", 
                    option_string = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_option_string"]) . "',
					type_id = '" . (int)$_REQUEST['main_input_choose_type'] . "',
					options = '" . $serialize_options . "',
                    main_collection_id = '".$main_collection_id."'
				WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        $inserted = FALSE;
    } else {
        $_REQUEST["main_input_code"] = "field-".time();
        $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM main_collection_setup_content WHERE main_collection_setup_id = '" . $_REQUEST["input_id"]."'"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO main_collection_setup_content (main_collection_setup_id, fieldtype, note, input_maxlength, fieldname, code, is_teaser, is_bulletlist, show_description_title, mandatory, option_string, type_id, options, sorting, main_collection_id) VALUES (
			'" . (int)$_REQUEST["input_id"] . "',
			'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["collection_type"]) . "', 
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_note"]) . "', 
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_maxlength"]) . "',
			'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_description"]) . "', 
			'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_code"]) . "',
            " . $is_teaser . ", 
            " . $is_bulletlist . ", 
            " . $show_description_title . ", 
            " . $mandatory . ", 
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["main_input_option_string"]) . "',
			'" . (int)$_REQUEST['main_input_choose_type'] . "',
			'" . $serialize_options . "',
			" . $sorting . ",
            " .$main_collection_id . "
		)";
        
        $inserted = TRUE;
    }
 
    if (($_REQUEST["main_input_choose_type"] == '') | ($_REQUEST["main_input_choose_type"] == 0)) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_choose_inputtype") . "</div>\n";
        $error      = TRUE;
    }

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

    if ($_REQUEST["main_input_code"] <> urlencode($_REQUEST["main_input_code"])) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_encoding") . "</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 === FALSE) {
            update_sitepart_setup($options, $id);
        }

        if ($_REQUEST['save_and_close'] == 1) {
            
            edit_collection_setup();
        } else {
            if ($inserted === TRUE) {
                $id         = mysqli_insert_id($GLOBALS['mysql_con']);
                $messages[] = '<div class="successbox">' . $translation->get("collection_line_msg_success1") . '</div>';
            } else {
                $messages[] = '<div class="successbox">' . $translation->get("collection_line_msg_success2") . '</div>';
            }
           
            edit_line_collection_setup($id, $messages);
        }

    } else {
        $input_line["id"]        = $_REQUEST["input_line_id"];
        $input_line["is_teaser"] = $_REQUEST["main_input_is_teaser"];
        $input_line["fieldname"] = $_REQUEST["main_input_description"];
        $input_line["fieldtype"] = $_REQUEST["collection_type"];
        $input_line['type_id']   = $_REQUEST["main_input_choose_type"];
        $input_line["code"]      = $_REQUEST["main_input_code"];
        $input_line['options']   = $serialize_options;
        require_once("edit_collection_custom_setup_line_cardform.inc.php");
    }
}
function update_sitepart_setup( $options, $setup_content_id ) {
    if ($_REQUEST["collection_type"] != "siteparts") {
        return;
    }

    $sitepartId = (int)$_REQUEST['main_input_choose_type'];

    $query               = "SELECT main_sitepart_header_id FROM main_collection_link WHERE main_collection_setup_content_id = '" . $setup_content_id . "' AND main_sitepart_id = '" . $sitepartId."'";
    $result              = @mysqli_query($GLOBALS['mysql_con'], $query);
    $sitepart_header_ids = array();
    while ($row = @mysqli_fetch_array($result, 1)) {
        $sitepart_header_ids[] = $row['main_sitepart_header_id'];
    }

    if (count($sitepart_header_ids) == 0) {
        return;
    }


    switch ($sitepartId) {
        case 2:
            update_sitepart_header_slideshow($options, $sitepart_header_ids);
            break;
        case 3:
            update_sitepart_header_contactform($options, $sitepart_header_ids);
            break;
        case 6:
            update_sitepart_header_gallery($options, $sitepart_header_ids);
            break;
        case 7:
            update_sitepart_header_magicscroll($options, $sitepart_header_ids);
            break;
        case 8:
            update_sitepart_header_googlemaps($options, $sitepart_header_ids);
            break;
    }
}
function update_sitepart_header_slideshow( $options, $sitepart_header_ids ) {
    $query = "UPDATE slideshow_header SET

		width = " . (int)$options['collection_setup_width'] . ",
		height = " . (int)$options['collection_setup_height'] . ",
		effect = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_effect']) . "',
		arrows = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_arrows']) . "',
		eff_interval = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_eff_interval']) . "',
		effect_duration = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_effect_duration']) . "',
		text_effect = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_text_effect']) . "',
		text_pos = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_text_pos']) . "'

	WHERE id IN (" . join(',', $sitepart_header_ids) . ")";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}

function update_sitepart_header_contactform( $options, $sitepart_header_ids ) {
    $query = "UPDATE contactform_header SET

		sender_name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_sender_name"]) . "', 
		sender_email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_sender_email"]) . "', 
		recipient_name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_recipient_name"]) . "',
		recipient_email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_recipient_email"]) . "',
		subject = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_input_subject"]) . "' 

	WHERE id IN (" . join(',', $sitepart_header_ids) . ")";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}

function update_sitepart_header_gallery( $options, $sitepart_header_ids ) {
    $query = "UPDATE gallery_header SET

		width = " . (int)$options["collection_setup_gallery_width"] . ", 
		height = " . (int)$options["collection_setup_gallery_height"] . ",
		thumb_width = " . (int)$options["collection_setup_gallery_thumb_width"] . ", 
		thumb_height = " . (int)$options["collection_setup_gallery_thumb_height"] . "

	WHERE id IN (" . join(',', $sitepart_header_ids) . ")";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}

function update_sitepart_header_magicscroll( $options, $sitepart_header_ids ) {
    $query = "UPDATE scrollbar_header SET

		width = " . (int)$options["collection_setup_width"] . ", 
		height = " . (int)$options["collection_setup_height"] . ", 
		item_width = " . (int)$options["collection_setup_item_width"] . ", 
		item_height = " . (int)$options["collection_setup_item_height"] . ", 
		arrows = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_arrows"]) . "',
		eff_interval = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_eff_interval"]) . "',
		effect_duration = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_effect_duration"]) . "',
		items = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_items"]) . "',
		step = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_step"]) . "' 

	WHERE id IN (" . join(',', $sitepart_header_ids) . ")";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}

function update_sitepart_header_googlemaps( $options, $sitepart_header_ids ) {
    $query = "UPDATE google_maps_header SET

		width = " . (int)$options["collection_setup_google_maps_width"] . ", 
		height = " . (int)$options["collection_setup_google_maps_height"] . ",
		icon_location = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options["collection_setup_icon_location"]) . "'

	WHERE id IN (" . join(',', $sitepart_header_ids) . ")";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}
function edit_line_collection_setup( $_id = "", $messages = array() ) {

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

    if ($line_id <> '') {

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

        }

    } else {

        require_once("edit_collection_custom_setup_line_cardform.inc.php");

    }
}
function edit_collection_setup( $_id = "", $messages = array() ) {

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

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

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

        if (@mysqli_num_rows($result) == 1) {

            $input_collection = @mysqli_fetch_array($result);

            require_once("edit_collection_cardform.inc.php");

        }

    } else {

        require_once("edit_collection_cardform.inc.php");

    }
}
function delete_line_collection_setup() {
    
    if ($_REQUEST["input_line_id"] <> '') {
        $query = "DELETE FROM main_collection_setup_content WHERE id = '" . $_REQUEST["input_line_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    edit_collection_setup();
}
function update_sortorder_collection_setup() {
    $sorting = 1;
    foreach ($_POST['linklistid'] as $itemId) {
        $query = "UPDATE main_collection_setup_content SET sorting = " . $sorting . " WHERE id = '" . $itemId."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $sorting++;
    }
}
?>