<?
use DynCom\mysyde\common\classes\CollectionTypes;
date_default_timezone_set('Europe/Berlin');

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

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

switch ($action) {
    case 'new_collection_setup':
        require_once("edit_collection_setup_cardform.inc.php");
        break;
    case 'save_collection_setup':
        save_collection_setup();
        break;
    case 'edit_collection_setup':
        edit_collection_setup();
        break;
    case 'delete_collection_setup' :
        delete_collection_setup();
        break;

    case 'new_line_collection_setup' :
        new_line_collection_setup();
        break;
    case 'edit_line_collection_setup':
        edit_line_collection_setup();
        break;
    case 'delete_line_collection_setup':
        delete_line_collection_setup();
        break;
    case 'save_line_collection_setup':
        save_line_collection_setup();
        break;
    case 'moveup_line_collection_setup':
        moveup_line_collection_setup();
        edit_collection_setup();
        break;
    case 'movedown_line_collection_setup':
        movedown_line_collection_setup();
        edit_collection_setup();
        break;

    case 'new_group_collection_setup' :
        new_group_collection_setup();
        break;
    case 'edit_group_collection_setup':
        edit_group_collection_setup();
        break;
    case 'delete_group_collection_setup':
        delete_group_collection_setup();
        break;
    case 'save_group_collection_setup':
        save_group_collection_setup();
        break;

    case 'load_sitepart_form':
        load_sitepart_form();
        break;

    case 'update_sortorder_collection_setup':
        update_sortorder_collection_setup();
        break;

    // Kopieren eines Kollektionssetups
    case 'copy_collection_setup': copy_collection_setup(); break;
    case 'load_languages': load_site_languages(); break;
    case 'copy_and_save_collection_setup': copy_and_save_collection_setup(); break;

    // preview
    case 'loadPage_collection'   :  load_collection_pages_();break;  
    case 'load_collection' :  load_collections_(); break;   
    // Preview End
    case 'close_collection_setup_world':
        require_once("edit_collection_setup_listform.inc.php");
        break;
    case 'load_collection_setup_world':
        require_once("edit_collection_setup_world_listform.inc.php");
        break;
    case 'new_collection_setup_world' :
        require_once("edit_collection_setup_world_cardform.inc.php");
        break;
    case 'edit_collection_setup_world':
        edit_collection_setup_world();
        break;
    case 'delete_collection_setup_world':
        delete_collection_setup_world();
        break;
    case 'save_collection_setup_world':
        save_collection_setup_world();
        break;
    case 'moveup_collection_setup_world':
        moveup_collection_setup_world();
        break;
    case 'movedown_collection_setup_world':
        movedown_collection_setup_world();
        break;
    case 'update_sortorder_collection_setup_world':
        update_sortorder_collection_setup_world();
        break;
    default:
        require_once("edit_collection_setup_listform.inc.php");
        break;
}

function copy_collection_setup($_id = "", $messages = array()) {
    if ((int)$_id > 0) {
        $input_id = $_id;
    } else {
        $input_id = $_REQUEST["input_id"];
    }

    if ($input_id <> '') {
        $query = "SELECT * FROM main_collection_setup 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_setup_copy.inc.php");
        }
    } else {
        require_once("edit_collection_setup_cardform.inc.php");
    }
}

function load_site_languages() {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    ob_start();
    language_select($translation->get("language"), "main_language_id", null, FALSE, $_POST['site_id'], true, "", true);
    $output1 = ob_get_clean();

    echo json_encode(array('site_languages' => $output1));
}

function copy_and_save_collection_setup() {
    require_once("edit_language_functions.php");
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages = array();
    $error = false;

    $input_collection_setup_id = $_REQUEST["input_id"];
    $input_site_id = $_REQUEST["site_id"];
    $input_language_id = (isset($_REQUEST["main_language_id"]) ? $_REQUEST["main_language_id"] : "");

    if(($input_site_id == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("copy_choose_site_error") . "</div>\n";
        $error = true;
    }

    if(($input_language_id == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("copy_choose_language_error") . "</div>\n";
        $error = true;
    }

    if(!$error) {

        $languageIds = array();

        $query = "DELETE FROM copy_link WHERE session_id = '" . session_id() . "'";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        // pruefen ob die language Id gleich ist
        if($input_language_id == "all") {
            $result = mysqli_query($GLOBALS['mysql_con'],"SELECT * FROM main_language WHERE main_site_id = '" . $input_site_id."'");
            while($language = mysqli_fetch_array($result)) {
                $languageIds[] = $language["id"];
            }
        } else {
            $languageIds[] = $input_language_id;
        }

        foreach($languageIds as $languageId) {

            $LanguageFunctions = new LanguageFunctions($GLOBALS["language"]['id'], null, $languageId);
            $LanguageFunctions->set_noCopyPageComponentIncludeLink();
            $LanguageFunctions->set_noCopyPageCollectionGroupLink();
            $LanguageFunctions->set_copyOnlySpecificSiteparts();

            // Siteparts muessen immer neu kopiert werden
            $query = "SELECT main_sitepart_header_id, main_sitepart_id FROM main_collection_link WHERE main_sitepart_id > 0 and main_collection_setup_content_id in (select id from main_collection_setup_content where main_collection_setup_id = '" . $input_collection_setup_id . "')";
            $lineResult = @mysqli_query($GLOBALS['mysql_con'], $query);
            while($lineRow = mysqli_fetch_array($lineResult)) {
                $LanguageFunctions->addSitePartId($lineRow['main_sitepart_id']);
                $LanguageFunctions->addSitePartHeaderId($lineRow['main_sitepart_header_id']);
            }

            // kontaktformular muss auch immer neu erstellt werden
            $query = "SELECT registration_contactform_id FROM main_collection WHERE registration_contactform_id > 0 and main_collection_setup_id = '" . $input_collection_setup_id."'";
            $lineResult = @mysqli_query($GLOBALS['mysql_con'], $query);
            while($lineRow = mysqli_fetch_array($lineResult)) {
                $LanguageFunctions->addSitePartHeaderId($lineRow['registration_contactform_id']);
                $LanguageFunctions->addSitePartId(3);
            }

            $LanguageFunctions->copy_siteparts();
            $LanguageFunctions->copy_collection_setup($input_collection_setup_id, true);
            $LanguageFunctions->copy_collection($input_collection_setup_id, true);
        }

        $messages[] = '<div class="successbox">' . $translation->get("copy_collection_success") . '</div>';
        copy_collection_setup($input_collection_setup_id, $messages);

    } else {
        header('EDIT_ERROR: 1');
        copy_collection_setup($input_collection_setup_id, $messages);
    }
}

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

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

    if ($line_id <> '') {

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

        }

    } else {

        require_once("edit_collection_setup_group_cardform.inc.php");

    }
}

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

        $query = "DELETE FROM main_collection_group_link WHERE main_collection_setup_group_id = '" . $_REQUEST["input_group_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        $query = "DELETE FROM main_component_collection_group_link WHERE main_collection_setup_group_id = '" . $_REQUEST["input_group_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);

        $query = "DELETE FROM main_page_collection_group_link WHERE main_collection_setup_group_id = '" . $_REQUEST["input_group_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    edit_collection_setup();
}

/**
 * save_group_collection_setup() Speichern der inputfelder
 *
 */

function save_group_collection_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_collection_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_collection_setup_group (main_collection_setup_id, description, default_active) VALUES (
			'" . (int)$_REQUEST["input_id"] . "',
			'" . 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) {
            edit_collection_setup();
        } else {
            if ($inserted === TRUE) {
                $id         = mysqli_insert_id($GLOBALS['mysql_con']);
                $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
            } else {
                $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success2") . '</div>';
            }
            edit_group_collection_setup($id, $messages);
        }

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

function new_group_collection_setup() {
    require_once("edit_collection_setup_group_cardform.inc.php");
}

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++;
    }
}

function new_line_collection_setup() {
     
    $input_line = array(
        'fieldtype' => $_POST['fieldtype'],
        'type_id'   => $_POST['type_id']
    );
    require_once("edit_collection_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 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_setup_line_cardform.inc.php");

        }

    } else {

        require_once("edit_collection_setup_line_cardform.inc.php");

    }
}


/**
 * delete_picture() Löschen einzelner Bilder
 *
 * @param mixed $sitepart
 *
 * @return
 */

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

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

    if ($input_id <> '') {

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

        }

    } else {

        require_once("edit_collection_setup_cardform.inc.php");

    }
}


function save_collection_setup() {
    $translation  = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id     = "";
    $input_linked = ($_POST["input_linked"] == "on") ? 1 : 0;
    $input_all_languages = ($_POST["input_all_languages"] == "on") ? 1 : 0;
	$input_show_filter = ($_POST["input_show_filter"] == "on") ? 1 : 0;
    $is_custom_collection = ($_POST["is_custom_collection"] == "on") ? 1 : 0;
    $is_multidomain = ($_POST["is_multidomain"] == "on") ? 1 : 0;
    $frontend_creation = ($_POST["frontend_creation"] == "on") ? 1 : 0;
    $websites = array();
    $website_ids = $_POST['select_website'];
    if($website_ids != NULL && isset($_POST['select_website'])){
        $websites = explode(',', $website_ids);
    }
    $worlds = array();
    $world_ids = $_POST['select_world'];
    if($world_ids != NULL && isset($_POST['select_world'])){
        $worlds = explode(',', $world_ids);
    } 
    $collection_type = $_POST['collection_type'];

    $desc_title = ($_POST["input_desc_title"] == "on") ? 1 : 0;
    $show_group_name = ($_POST["input_show_group_name"] == "on") ? 1 : 0;
    $show_in_menu = ($_POST["input_show_in_menu"] == "on") ? 1 : 0;
    //pruefen ob Bild ausgewaehlt
    if ($_REQUEST["input_id"] <> '') {
        $insertquery = " UPDATE main_collection_setup SET
				description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
				linked = " . (int)$input_linked . ",
				icon = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['setup_icon']) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
				code = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_code']) . "',
				all_languages = " . $input_all_languages .
				", show_filter = '" . $input_show_filter . "' " .
                ", is_custom_collection = '" . $is_custom_collection . "' " .
                ", is_multidomain = '" . $is_multidomain . "' " .
                ", desc_title = '" . $desc_title . "' " .
                ", show_group_name = '" . $show_group_name . "' " .
                ", show_in_menu = '" . $show_in_menu . "' " .
                ", collection_type = '" . $collection_type . "' " .
                ", frontend_creation = '" . $frontend_creation . "' " .
				"WHERE id = " . $_REQUEST["input_id"] . " 
			LIMIT 1";
        $inserted    = FALSE;
        $input_id    = $_REQUEST["input_id"];
        createMySydeLog('main_collection_setup', $input_id, 'UPDATE');
    } else {
        $insertquery = "INSERT INTO main_collection_setup
			(main_language_id, description, code, linked, icon, modified_date, modified_user, all_languages, show_filter, is_custom_collection, is_multidomain, desc_title, show_group_name, collection_type, show_in_menu, frontend_creation)
			VALUES (
				'" . $GLOBALS["language"]['id'] . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_description']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_code']) . "',
				" . (int)$input_linked . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['setup_icon']) . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ",
				" . $input_all_languages . ", " .
				//IC - TH - 20170517 ---
				$input_show_filter . ", " .
                $is_custom_collection.", ".
                $is_multidomain. ",
                " . $desc_title . ",
                " . $show_group_name .",
                " . $collection_type .",
                " . $show_in_menu .",
                " . $frontend_creation .
                
				//IC - TH - 20170517 +++
				
			")";
        $inserted    = TRUE;
    }
    if (($_REQUEST["input_code"] == '') | ($_REQUEST["input_description"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_description") . "</div>\n";
        $error      = TRUE;
    }
    // if ($_REQUEST["input_code"] <> urlencode($_REQUEST["input_code"])) {
    //     $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_encoding") . "</div>\n";
    //     $error      = TRUE;
    // }

    if (!$error) {
        // print_r($insertquery);
        @mysqli_query($GLOBALS['mysql_con'], $insertquery);
         

        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            createMySydeLog('main_collection_setup', $input_id, 'INSERT');
            $messages[] = '<div class="successbox">' . $translation->get("collection_setup_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("collection_setup_msg_success2") . '</div>';
        }
        // websites
        
           
            if(!empty($websites) && gettype($websites) == 'array')  { 
                $deleteOldWebShow = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM main_collection_setup_websites WHERE main_collection_setup_id='".$input_id."'");    
                
                    foreach($websites as $key => $website){
                        $query = "INSERT INTO main_collection_setup_websites
                        (main_site_id, main_collection_setup_id)
                        VALUES (
                            " .  $website . ",
                            '" . $input_id . "'
                        )";
                        @mysqli_query($GLOBALS['mysql_con'], $query);
                    }
                
                
            }else if(empty($websites)){
                $deleteOldWebShow = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM main_collection_setup_websites WHERE main_collection_setup_id='".$input_id."'");    
            }
        // websites

        // world
        
        if(!empty($worlds) && gettype($worlds) == 'array')  { 
            $deleteOldWebShow = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM main_collection_setup_worlds_link WHERE main_collection_setup_id='".$input_id."'");    
            
                foreach($worlds as $key => $world){
                    $query = "INSERT INTO main_collection_setup_worlds_link
                    (main_collection_setup_world_id, main_collection_setup_id)
                    VALUES (
                        " .  $world . ",
                        '" . $input_id . "'
                    )";
                    @mysqli_query($GLOBALS['mysql_con'], $query);
                }
            
            
        }else if(empty($worlds)){
            $deleteOldWebShow = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM main_collection_setup_worlds_link WHERE main_collection_setup_id='".$input_id."'");    
        }
        // world

        edit_collection_setup($input_id, $messages);

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

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

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

        $query = "DELETE FROM main_collection_setup_content WHERE main_collection_setup_id = '" . $_REQUEST["input_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        createMySydeLog('main_collection_setup', $_REQUEST['input_id'], 'DELETE');
    }
    require_once("edit_collection_setup_listform.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();
}

/**
 * save_line_collection_setup() Speichern der inputfelder
 *
 */

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;

    $options = array();
    foreach ($_POST as $key => $value) {
        if (substr($key, 0, 16) == 'collection_setup') {
            $options[$key] = $value;
        }
    }
    $serialize_options = serialize($options);
    if(isset($_REQUEST['select_contactform']) && $_REQUEST['select_contactform']  != NULL){
        $_REQUEST["main_input_option_string"] = $_REQUEST['select_contactform'];
    }


    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 . "'
				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 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) 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 . "
		)";
        $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(isset($_POST['preview_settings']) && $_POST['preview_settings'] == 1){
                 
                add_to_preview_setup( $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_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);
}

/**
 * Erzeugt eine selectbox zum auswaehlen von input-typen bei einer kollektion
 *
 * @access public
 *
 * @param string $name       Label
 * @param string $selectname name der selectbox
 *
 * @return void
 */
function create_type_select( $name, $selectname = "input_choose_type", $input_line ) {

    $translation     = \DynCom\mysyde\common\classes\Registry::get("translation");
    $collectionTypes = \DynCom\mysyde\common\classes\CollectionTypes::get();
    $siteparts       = \DynCom\mysyde\common\classes\Siteparts::get();
    echo "<div class=\"label\"><label for=\"" . $selectname . "\">" . $name . "</label></div>";
    echo "<div class=\"input\"><select onchange=\"load_sitepart_settings(this.value);\" class=\"bigselect\" name=\"" . $selectname . "\" id=\"" . $selectname . "\">";


    // neue typen die nur fuer kollektionen vorgesehen sind
    foreach ($collectionTypes as $typeId => $typeData) {
        $selected = '';
        if (isset($input_line['fieldtype']) && $input_line['fieldtype'] == 'standard' && $typeId == $input_line['type_id']) {
            $selected = 'selected="selected"';
        }
        echo "<option " . $selected . " value=\"" . $typeId . "\">" . $typeData['description'] . "</option>";
    }

//	// siteparts anzeigen
//	echo "<optgroup label=\"Siteparts\" value=\"siteparts\">";
//	foreach($siteparts as $sitepartId => $sitepartData) {
//		$selected = '';
//		
//		if(isset($input_line['fieldtype']) && $input_line['fieldtype'] == 'siteparts' && $sitepartId == $input_line['type_id']) {
//			$selected = 'selected="selected"';
//		}
//		echo "<option " . $selected . " value=\"" . $sitepartId . "\">" . $sitepartData['description'] . "</option>";
//	}
//	echo "</optgroup>";

    echo "</select>";
}


function moveup_line_collection_setup() {
    move_line_collection_setup("<", "DESC", $_POST["input_line_id"]);
}

function movedown_line_collection_setup() {
    move_line_collection_setup(">", "ASC", $_POST["input_line_id"]);
}

function move_line_collection_setup( $way, $order, $pic_id ) {
    $query  = "SELECT * FROM main_collection_setup_content WHERE id = '" . $pic_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $curr_pic = @mysqli_fetch_array($result);
    }
    $query  = "SELECT * FROM main_collection_setup_content WHERE main_collection_setup_id = " . $curr_pic['main_collection_setup_id'] . " AND sorting " . $way . " " . $curr_pic["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $change_pic = @mysqli_fetch_array($result);
    }
    if (($curr_pic["id"] <> '') && ($change_pic["id"] <> '')) {
        $query = "UPDATE main_collection_setup_content SET sorting = " . $change_pic["sorting"] . " WHERE id = " . $curr_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "UPDATE main_collection_setup_content SET sorting = " . $curr_pic["sorting"] . " WHERE id = " . $change_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}
// Holen Sie sich die Site-Sprachen zur Auswahl (Get the site languages for selection)
function collection_setup_websites($collection_setup){
    
    
    
    // 
            $query = "SELECT main_site.id , main_language.name, main_site.name as site_name  FROM main_language Join  main_site on main_language.main_site_id = main_site.id INNER JOIN main_site_admin_user_link as msaul on main_site.id = msaul.main_site_id WHERE msaul.main_admin_user_id = ".$GLOBALS["admin_user"]['id'];
            
           
            $site_collection = @mysqli_query($GLOBALS['mysql_con'], $query);
            
            if (@mysqli_num_rows($site_collection) == 0) { 
            return;
            }
            
            
            $preselect = array();
            
            $query = "SELECT * FROM main_collection_setup_websites WHERE  main_collection_setup_id = ".$collection_setup['id'];
            
            
            
            $result = @mysqli_query($GLOBALS['mysql_con'], $query);
            
            while($row = @mysqli_fetch_array($result)){
            
                array_push($preselect,$row['main_site_id']);
            }
            

            ?>
            <div id="websiteSelection" style="display:none;">
                <div class="label" >Website auswählen</div>
                <div class="websites bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
                    <input type="hidden" name="select_website">
                    <i class="dropdown icon"></i>
                    <div class="default text">Website auswählen</div>
                    <div class="menu">
                        <?  
                            
                        
                            while($site = @mysqli_fetch_array($site_collection)) {
                                echo "<div class=\"item\" data-value=\"{$site['id']}\">".$site["site_name"]."</div>";
                            }
                        ?>
                    </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('.websites.ui.fluid.multiple.dropdown').dropdown('set selected', <?= json_encode($preselect) ?>); 
                });

            </script>
    


<?
        
}
function load_collection_pages_() {
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	ob_start();
	create_page_with_collection_select($_POST['collection_setup_id'], $translation->get("page_with_collections"), "main_collection_page_list_id", array(), false);
	$output1 = ob_get_clean();
	
	$active_groups = array();
	$query = "SELECT main_collection_setup_group_id from main_page_collection_group_link where main_page_link_id = '" . $_POST['page_link_id']."'";
 
    $result = @mysqli_query($GLOBALS['mysql_con'],$query);
	while ($row = @mysqli_fetch_array($result)) {
		$active_groups[] = $row['main_collection_setup_group_id'];	
	}	
	ob_start();
	// create_collection_preview_group_select($_POST['collection_setup_id'], $_POST['page_link_id'], $active_groups, 'select_group_name');
	$output2 = ob_get_clean();
	
	echo json_encode(array('page_with_collection_select' => $output1, 'collection_preview_group_select' => $output2));
}
function load_collections_() {
	// ajax call
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$setup_id = $_POST['main_collection_setup_id_'];
    $input_id = $_POST['input_id'];

 
	$collections = array();
	$collection_ids = array();
	if($setup_id != "") {
		$query = "SELECT  *  FROM main_collection   WHERE main_collection_setup_id = '" . $setup_id . "'";
		$result = @mysqli_query($GLOBALS['mysql_con'],$query);
		while ($row = @mysqli_fetch_array($result,1)) {
			$collections[] = $row['description'];
			$collection_ids[] = $row['id'];
		}
	}
	$query                    = "SELECT * FROM main_collection_setup WHERE id = '" . $setup_id."'";
    $result                   = @mysqli_query($GLOBALS['mysql_con'], $query);
    $collection_setup    = @mysqli_fetch_array($result);
	input_select($translation->get("choose_collection"),"main_collection_id",$values = $collection_ids,$value_names = $collections,"", false, false);
    if($collection_setup['code'] == 'ausbildungsstellen'){
        $queryFirmen = "SELECT mc.* FROM main_collection as mc INNER JOIN main_collection_setup as mcs on mc.main_collection_setup_id = mcs.id WHERE code = 'firmen' AND mc.id != ".$input_id;
                      
                        $resultFirmen     = @mysqli_query($GLOBALS['mysql_con'], $queryFirmen);
                        $collection_ids = array();
                        $collections = array();
                        
                        $collection_ids[0] = '999111000';
                        $collections[0] = 'Alle';              
    $i = 1;
                        while ($rowFirmen = @mysqli_fetch_array($resultFirmen)) {
                            
                            $collection_ids[$i] =  $rowFirmen['id'];
                            $collections[$i] =  $rowFirmen['description'];
                            $i++;
                        }
                         
                        
                        
                        // input_select($translation->get("choose_firmen"), "firmen_id_collection_".$field_id, $values = $collection_ids, $value_names = $collections, $input_collection["firmen_id"], FALSE, FALSE);
    }
    
}
function add_to_preview_setup($field_id){
     
    $query = "SELECT * FROM main_collection_setup_preview where field_id = ".$field_id.' LIMIT 1';
    $result =  @mysqli_query($GLOBALS['mysql_con'], $query);
    $queryPreview =  '';
    $main_collection_setup_id_selected = 0;
    $main_collection_id = 0;
    if($_POST['main_collection_view_type'] == 0 && $_POST['main_collection_view_type']  != NULL){
        $main_collection_id = $_POST['main_collection_id'];
    }
    
    $collectionItems = 0;
    if(isset($_POST['main_collection_items']) && $_POST['main_collection_items']  != NULL){
        $collectionItems = $_POST['main_collection_items'];
    }
    if(isset($_REQUEST['select_setup']) && $_REQUEST['select_setup']  != NULL){
        $main_collection_setup_id_selected = $_REQUEST['select_setup']; 
     }
    $group = '0';
    if(isset($_POST['select_group_name']) && $_POST['select_group_name']  != NULL){
        $group = $_POST['select_group_name'];
    }
    $page = 0;
    if(isset($_POST['main_collection_page_list_id'])  && $_POST['main_collection_page_list_id']  != NULL){
        $page = $_POST['main_collection_page_list_id'];
    }
    
    
    if (@mysqli_num_rows($result)  > 0) {
        $row = @mysqli_fetch_array($result);
        
         
        $queryPreview =  "UPDATE main_collection_setup_preview SET
				 
				main_collection_view_type = " . $_POST['main_collection_view_type'.$key] . ",
				main_collection_setup_id = '" . $_POST['input_id'] . "',
				main_collection_page_list_id = " . $page . ",
				main_collection_id_selected = " . $main_collection_id . ",
                
                field_id = ".$field_id.",
                main_collection_items = ".$collectionItems.",
                groups_selected = ".$group.",
                main_collection_setup_id_selected = ".$main_collection_setup_id_selected."
                 
				WHERE id = '" . $row["id"] . "' 
			LIMIT 1";
    }else{
        
        $queryPreview = "INSERT INTO main_collection_setup_preview
			( main_collection_view_type, main_collection_setup_id, main_collection_page_list_id, main_collection_id_selected, 
             main_collection_items, groups_selected, field_id, main_collection_setup_id_selected)
			VALUES (
				 
				'" . $_POST['main_collection_view_type'] . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_POST['input_id']) . "',
				" . $page . ",
				'" . $main_collection_id . "',
				 
				" . $collectionItems . ",
				" . $group. ",
                ".$field_id.",
                ".$main_collection_setup_id_selected."
                
			)";
            
    }
   
    
    if(isset($_POST['select_setup']) && $_POST['select_setup'] != NULL && $_POST['select_setup'] != 0){
       
        @mysqli_query($GLOBALS['mysql_con'], $queryPreview);
    }
     
}

// Collection Worlds

function edit_collection_setup_world($_id = "") {
    if ((int)$_id > 0) {
        $world_id = $_id;
    } else {
        $world_id = $_REQUEST["input_world_id"];
    }
    if ($world_id <> '') {
        $query  = "SELECT * FROM main_collection_setup_world WHERE id = '" . $world_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);

        if (@mysqli_num_rows($result) == 1) {
            $input_world = @mysqli_fetch_array($result);
            require_once("edit_collection_setup_world_cardform.inc.php");
        }
    } else {
        require_once("edit_collection_setup_world_cardform.inc.php");
    }
}

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

    if ($_REQUEST["input_world_id"] <> '') {
        $query    = "UPDATE main_collection_setup_world SET
					description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "'
				WHERE id = '" . $_REQUEST["input_world_id"] . "' LIMIT 1";
    } else {
        $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM main_collection_setup_world"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO main_collection_setup_world (description, sorting) VALUES (
            '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
			" . $sorting . "
		)";
    }
    @mysqli_query($GLOBALS['mysql_con'], $query);
    
    if ($_REQUEST['save_and_close'] == 1) {
        require_once("edit_collection_setup_world_listform.inc.php");
    } else {
        require_once("edit_collection_setup_world_cordform.inc.php");
    }
}

function delete_collection_setup_world() {
    if ($_REQUEST["input_world_id"] <> '') {
        $query = "DELETE FROM main_collection_setup_world WHERE id = '" . $_REQUEST["input_world_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    require_once("edit_collection_setup_world_listform.inc.php");
}

function moveup_collection_setup_world() {
    move_collection_setup_world("<", "DESC", $_POST["input_world_id"]);
    require_once("edit_collection_setup_world_listform.inc.php");
}

function movedown_collection_setup_world() {
    move_collection_setup_world(">", "ASC", $_POST["input_world_id"]);
    require_once("edit_collection_setup_world_listform.inc.php");
}

function move_collection_setup_world( $way, $order, $pic_id ) {
    $query  = "SELECT * FROM main_collection_setup_world WHERE id = '" . $pic_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $curr_pic = @mysqli_fetch_array($result);
    }
    $query  = "SELECT * FROM main_collection_setup_world WHERE sorting " . $way . " " . $curr_pic["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $change_pic = @mysqli_fetch_array($result);
    }
    if (($curr_pic["id"] <> '') && ($change_pic["id"] <> '')) {
        $query = "UPDATE main_collection_setup_world SET sorting = " . $change_pic["sorting"] . " WHERE id = " . $curr_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "UPDATE main_collection_setup_world SET sorting = " . $curr_pic["sorting"] . " WHERE id = " . $change_pic["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}

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

function collection_setup_world($collection_setup){
    $translation   = \DynCom\mysyde\common\classes\Registry::get("translation");
    $query = "SELECT * FROM main_collection_setup_world ORDER BY sorting ASC";
    $world_collection = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($world_collection) == 0) { 
        return;
    } 

    $preselect = array();
    $query = "SELECT * FROM main_collection_setup_worlds_link WHERE main_collection_setup_id = '" . $collection_setup_id . "'";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($row = @mysqli_fetch_array($result)){
        array_push($preselect,$row['main_collection_setup_world_id']);
    }
    
    ?>
    <div class="label" ><?php echo $translation->get('choose_world'); ?></div>
    <div class="world bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
        <input type="hidden" name="select_world">
        <i class="dropdown icon"></i>
        <div class="default text"><?php echo $translation->get('choose_world'); ?></div>
        <div class="menu">
            <?  
                
            
                while($world = @mysqli_fetch_array($world_collection)) {
                    echo "<div class=\"item\" data-value=\"{$world['id']}\">".$world["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('.world.ui.fluid.multiple.dropdown').dropdown('set selected', <?= json_encode($preselect) ?>); 
        });
    </script>
<?     
}