<?

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

if (isset($custom_action)) {
    $action = $custom_action;
   
} else {
    $action = $_REQUEST["action"];
}
switch ($action) {
    case 'edit_contactform':
        edit_contactform();
        break;
    // case 'edit_multi_contactform':    
    case 'delete_contactform':
        delete_contactform();
        break;
    case 'new_contactform':
        
        require_once("edit_contactform_cardform.inc.php");
        break;
    case 'new_multi_contactform':
        require_once("edit_multi_contactform_cardform.inc.php");
        break;  
    case 'save_contactform':
        save_contactform();
        break;

    case 'save_line_contactform':
        save_line_contactform();
        break;
    case 'new_line_contactform':
        new_line_contactform();
        break;
    case 'new_line_contactform_level':
        new_line_contactform_level();
        break;  
    case 'edit_line_level':
        new_line_contactform_level($_REQUEST['step_id']);
        break;      
    case 'new_line_contactform_section':
          new_line_contactform_section();
          break; 
    case 'edit_line_section':
        new_line_contactform_section($_REQUEST['section_id']);
        break;        
    case 'edit_line_contactform':
        edit_line_contactform();
        break;
    case 'delete_line_contactform':
        delete_line_contactform();
        break;
    case 'moveup_line_contactform':
        moveup_line_contactform();
        edit_contactform();
        break;
    case 'movedown_line_contactform':
        movedown_line_contactform();
        edit_contactform();
        break;

    case 'update_sortorder_contactform':
        update_sortorder_contactform();
        break;
    case 'multi_contactform':
           require_once("edit_multi_contactform_listform.inc.php");
           break; 
    case 'save_line_contactform_level':
          save_line_contactform_level();
          break;  
    case 'save_line_contactform_section':
          save_line_contactform_section();
          break; 
    case 'delete_line_contactform_level':
            delete_line_contactform_level();
            break;    
    case 'delete_line_contactform_section':
        delete_line_contactform_section();
        break;        
            //   multi
     case 'list_collection_list_page':   $input_line = array(); require_once(MODULE_PATH . "collection/contact_collection_preview_cardform.inc.php"); break;     
     case 'list_collection_preview_page': $input_line = array(); $_REQUEST['is_collection_preview']  = 1; require_once(MODULE_PATH . "collection/contact_collection_preview_cardform.inc.php"); break;
		case 'edit_collection_preview_page': $_REQUEST['is_collection_preview']  = 1; edit_collection_preview_page(); break;
		case 'save_collection_preview_page': save_collection_preview_page(); break;
		case 'load_collection_pages_page' : load_collection_pages(); break;
		case 'load_collections_page' :  load_collections(); break;   
    case 'edit_line_multi_contact': edit_line_contact(); break;       
    case 'edit_collection_list_page': edit_collection_preview_page(); break;   
    case 'delete_line_contact' : delete_sitepart(); break;  
    case 'new_contact_slideshow' :
        load_default_content();
        break;
    case 'save_slideshow':
        load_default_content();
        break;
    case 'new_line_slideshow';
        require_once(MODULE_PATH."slideshow/edit_slideshow.inc.php");
        break;    
    case 'edit_slideshow' :
        require_once(MODULE_PATH."slideshow/edit_slideshow.inc.php");
        break;    
    case 'save_line_slideshow':
        require_once(MODULE_PATH."slideshow/edit_slideshow.inc.php");
        break;  
    case 'edit_line_slideshow':
        require_once(MODULE_PATH."slideshow/edit_slideshow.inc.php");
        break; 
    case 'delete_slideshow':
         
        require_once(MODULE_PATH."slideshow/edit_slideshow.inc.php");
        break;  
    case 'delete_line_contactform_bild':
            delete_line_contactform_bild();    
    case 'reorder_content_page': reorder_content_page(); break;    
    case 'new_contact_textcontent' :
            load_default_content();
            break;
    case 'save_textcontent':
        load_default_content();
        break;        
            
    case 'delete_textcontent':
   
        load_default_content();
        break;  
    case 'moveup_line_multicontactform':
        moveup_line_multicontactform();
        break;
    case 'movedown_line_multicontactform':
        movedown_line_multicontactform();
            break;       
    default:
   
        if($_REQUEST['level_2'] == 'multistep_forms'){
            require_once("edit_multi_contactform_listform.inc.php");
        }else{
           try{
            require_once("edit_contactform_listform.inc.php");
           }catch( Exception $e ){
               print_r($e);
           }
            
        }
        
        break;
}
function reorder_content_page() {
	$list = $_POST['list'];
	if(!is_array($list) || count($list) == 0) { return; }
	
	$sorting = 0;
	foreach($list as $site) {
		$sorting++;
		$query = "UPDATE multi_contactform_link SET sorting = " . $sorting . ", main_page_link_parent_id = 0 WHERE id = " . $site['id'];
		mysqli_query($GLOBALS['mysql_con'],$query);
		
		if(!isset($site['children']) || count($site['children']) == 0) continue; // keine children enthalten, weiter
			
		foreach($site['children'] as $child) {
			$sorting++;
			$query = "UPDATE multi_contactform_link SET sorting = " . $sorting . ", main_page_link_parent_id = " . $site['id'] . " WHERE id = " . $child['id'];
			mysqli_query($GLOBALS['mysql_con'],$query);
		}
	}
}
function delete_sitepart() {
	$query = "DELETE FROM multi_contactform_link WHERE id = '" . $_POST['input_id'] . "' LIMIT 1";
	@mysqli_query($GLOBALS['mysql_con'],$query);
	
	$query = "UPDATE multi_contactform_link SET main_section_link_parent_id = 0 WHERE main_section_link_parent_id = '" . $_POST['input_id']."'";
	@mysqli_query($GLOBALS['mysql_con'],$query);
	
	 
	
	$query = "DELETE FROM main_contact_collection_group_link WHERE main_contact_link_id = '" . $_POST['input_id']."'";
	@mysqli_query($GLOBALS['mysql_con'],$query);
	
	edit_contactform($_REQUEST['input_contactform_id']);
}
function save_collection_preview_page() {
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$error = false;

	if($_POST['main_collection_setup_id'] == "") {
		$messages[] = "<div class=\"errorbox\">" . $translation->get("error_collection_choose_setup") . "</div>\n";
		$error = true;
	}
	
	if($_REQUEST['is_collection_preview'] == 1) {
	
		if(!isset($_POST['main_collection_page_list_id']) || $_POST['main_collection_page_list_id'] == "") {
			$messages[] = "<div class=\"errorbox\">" . $translation->get("error_collection_choose_page_with_list") . "</div>\n";
			$error = true;
		}
		
		if($_POST['main_collection_view_type'] == 0 && (int)$_POST['main_collection_id'] <= 0) {
			$messages[] = "<div class=\"errorbox\">" . $translation->get("error_collection_choose_collection_for_preview") . "</div>\n";
			$error = true;
		}
		
		if($_POST['main_collection_view_type'] == 1 && (int)$_POST['main_collection_items'] <= 0) {
			$messages[] = "<div class=\"errorbox\">" . $translation->get("error_collection_choose_collection_items") . "</div>\n";
			$error = true;
		}
		
	}
    $active = 0;
    if(isset($_REQUEST['input_active'])){
        $active = 1;
    }
    $layout_class_defined = '';
    if(isset($_REQUEST['layout_class_defined'])){
        $layout_class_defined =  $_REQUEST['layout_class_defined'];
    }
	
	if($error === false) {
		if($_REQUEST["input_id"] != "") {
            
 
			// update
			$query = "UPDATE multi_contactform_link SET 
				main_collection_id = '" . (int)$_POST['main_collection_id'] . "',
				main_collection_setup_id = '" . (int)$_POST['main_collection_setup_id'] . "',
				main_collection_section_list_id = '" . (int)$_POST['main_collection_page_list_id'] . "',
				main_collection_items = '" . (int)$_POST['main_collection_items'] . "',
				main_collection_view_type = '" . (int)$_POST['main_collection_view_type'] . "',
                contact_form_id = '".(int)$_POST['input_contactform_id']."',
                main_section_id = '".(int)$_POST['input_section_id']."',
                
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
                layout_area_id = " .$_REQUEST['input_layout_area_id'].",
				active = " . $active . ",
                modified_date = " . time() . ",
                layout_class_defined = '".$layout_class_defined."'

				WHERE id = " . $_REQUEST["input_id"] . "
			";
          
			@mysqli_query($GLOBALS['mysql_con'],$query);
			
			$messages = array(
				'<div class="successbox">' . $translation->get("collection_preview_edit_success") . '</div>'
			);
			
		} else {
			assoc_collection_list();
			
			$messages = array(
				'<div class="successbox">' . $translation->get("page_line_assoc_success") . '</div>'
			);
		}
		
		// gruppen hinzufuegen
		mysqli_query($GLOBALS['mysql_con'],"DELETE FROM main_contact_collection_group_link WHERE main_contact_link_id = '" . (int)$_REQUEST["input_id"]."'");
		
		$query = "SELECT * FROM main_collection_setup_group WHERE main_collection_setup_id = '" . (int)$_POST['main_collection_setup_id']."'";
		$result = @mysqli_query($GLOBALS['mysql_con'],$query);
		while ($row = @mysqli_fetch_assoc($result)) {
			$arr_ind = "input_collection_group_" . $row["id"] . "_active";
			if ($_POST[$arr_ind] != "on") {
				continue;
			}
			
			$query_3 = "INSERT INTO main_contact_collection_group_link SET
				main_contact_link_id = '" . (int)$_REQUEST["input_id"] . "',
				main_collection_setup_group_id = " . $row["id"];
			@mysqli_query($GLOBALS['mysql_con'],$query_3);
			
		}
		
		if($_REQUEST['save_and_close'] == 1) {
            edit_contactform();
		} else {
             
			edit_collection_preview_page("", $messages);
		}
		
	} else {
		$input_line = $_POST;
		require_once(MODULE_PATH . "collection/contact_collection_preview_cardform.inc.php");
	}
}
function new_line_contactform() {

    $input_line = array();

    if (is_collection_edit()) {
        $collection_link = get_collection_link($_REQUEST['input_collection_id'], $_REQUEST['collection_setup_content_id']);
        if (isset($collection_link['main_sitepart_header_id'])) {
            $input_line['header_id'] = $collection_link['main_sitepart_header_id'];
        }
    } else {
        $input_line['header_id'] = $_REQUEST['input_id'];
    }
    $queryform  = "SELECT * FROM contactform_header WHERE id = '" . $input_line['header_id'] . "' LIMIT 1";
    $resultForm = @mysqli_query($GLOBALS['mysql_con'], $queryform);
    $contactform  = @mysqli_fetch_array($resultForm);
    
    if($contactform['is_multistep']){
         
        require_once("edit_contactform_multi_line_cardform.inc.php");
    }else{
        require_once("edit_contactform_line_cardform.inc.php");
    }

     
}
function assoc_collection_list() {
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	
	if($_REQUEST['is_collection_preview'] == 1) {
         
		set_multi_contact_link(0, 0, 2, (int)$_POST['main_collection_id'], (int)$_POST['main_collection_setup_id'], (int)$_POST['main_collection_view_type'], (int)$_POST['main_collection_items'], (int)$_POST['main_collection_page_list_id']);
	} else {
		set_multi_contact_link(0, 0, 1, 0, $_POST['main_collection_setup_id']);
	}
	
	
 
}
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_contact_collection_group_link where main_contact_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);
	$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'];
	$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'];
		}
	}
	
	input_select($translation->get("choose_collection"),"main_collection_id",$values = $collection_ids,$value_names = $collections,"", false, false);
}
function edit_collection_preview_page($_id = "", $messages = array()) {
	if((int)$_id > 0) {
		$input_id = $_id;
	} else if(isset($_REQUEST['data-inputid'])){
        $input_id = $_REQUEST["data-inputid"];
    } else if(isset($_REQUEST["input_id"])) {
		$input_id = $_REQUEST["input_id"];
	}
	// echo 'recipe';print_r($input_id);
	if($input_id <> '') {
		$query="SELECT * FROM multi_contactform_link 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(MODULE_PATH . "collection/contact_collection_preview_cardform.inc.php");
		} 
	} else {
		$input_line = array();
		require_once(MODULE_PATH . "collection/contact_collection_preview_cardform.inc.php");
	}
}

function load_default_content(){
    // Ansonsten ist es eine subpart action
	$siteparts = \DynCom\mysyde\common\classes\Siteparts::get();
	$sitepart_id = null;
	if(isset($_POST['sitepart_id']) && !empty($_POST['sitepart_id'])) {
		$sitepart_id = (int) $_POST['sitepart_id'];
	} elseif(isset($_POST['data-sitepartid']) && !empty($_POST['data-sitepartid'])) {
		$sitepart_id = (int) $_POST['data-sitepartid'];
	}
	if($sitepart_id != null && $siteparts[$sitepart_id]['header_table'] == "") {
		// keine header_table, einfach direkt zuordnen
		set_multi_contact_link($sitepart_id, 0);
		require_once("edit_multi_contactform_cardform.inc.php");
	} else {
		
		// schneller hack um das sitepart anhand des codes herauszufinden
		//@TODO Sitepart ID sollte immer uebergeben werden
		$currentSitepart = null;
		foreach($siteparts as $sitepartData) {
			if($subaction == $sitepartData['code']) {
				$currentSitepart = $sitepartData;
				break;
			}
		}
		
		if($sitepart_id != null) {
			$currentSitepart = $siteparts[$sitepart_id];
		}
		
		$path = realpath(MODULE_PATH . $currentSitepart['folder']) . DIRECTORY_SEPARATOR . 'edit_' . $currentSitepart['code'] . ".inc.php";
		 
        if(file_exists($path)) {
			require_once($path);
		}
	}
}
function update_sortorder_contactform() {
    $sorting = 1;
    foreach ($_POST['linklistid'] as $itemId) {
        $query = "UPDATE contactform_line SET sorting = " . $sorting . " WHERE id = '" . $itemId."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $sorting++;
    }
}


function new_line_contactform_level($_id = ""){
    if ((int)$_id > 0) {
        $line_id = $_id;
    } else {
        $line_id = $_REQUEST["step_id"];
    }
    
    if ($line_id <> '') {
        $query  = "SELECT * FROM contactform_steps 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);
            $input_line['header_id'] = $input_line['contactform_id'];
            require_once("edit_multi_contactform_line_cardform.ic.php");
        }
    } else {
        $input_line = array();
        $input_line['header_id'] = $_REQUEST['input_id'];
        require_once("edit_multi_contactform_line_cardform.ic.php");
    }
   
         

    
}
function new_line_contactform_section($_id = ''){
    if ((int)$_id > 0) {
        $line_id = $_id;
    } else {
        $line_id = $_REQUEST["section_id"];
    }
    
    if ($line_id <> '') {
        $query  = "SELECT * FROM contactform_section 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);
            $input_line['header_id'] = $input_line['contactform_id'];
            require_once("edit_multi_contactform_line_cardform_section.inc.php");
        }
    } else {
        $input_line = array();
        $input_line['header_id'] = $_REQUEST['input_id'];
        require_once("edit_multi_contactform_line_cardform_section.inc.php");
    }

        
        
}
function delete_line_contactform() {
    if ($_REQUEST["input_line_id"] <> '') {
        $query = "DELETE FROM contactform_line WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $queryDelete = "DELETE FROM multi_contactform_link WHERE contactform_line_id = '" .$_REQUEST["input_line_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $queryDelete); 
        $queryDelete = "DELETE FROM contactform_line_option WHERE line_id = '" . $_REQUEST["input_line_id"] ."'";
        @mysqli_query($GLOBALS['mysql_con'], $queryDelete); 
    }
    edit_contactform();
}
function delete_line_contactform_level() {
   
    if ($_REQUEST["input_line_id"] <> '') {
        $query = "DELETE FROM contactform_steps WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        // print_r($query);
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    edit_contactform();
}
function delete_line_contactform_section() {
   
    if ($_REQUEST["input_line_id"] <> '') {
        $query = "DELETE FROM contactform_section WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        print_r($query);
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    edit_contactform();
}
function save_line_contactform() {
    $translation                 = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages                    = array();
    $_REQUEST["input_mandatory"] = ($_REQUEST["input_mandatory"]) ? 1 : 0;
    $_REQUEST["input_infield"]   = ($_REQUEST["input_infield"]) ? 1 : 0;
    $id                          = $_REQUEST["input_line_id"];
     
     
    $linked_field = (isset($_POST["linked_field"])) ? 1 : 0;
    $linked_placeholder = 'NA';
    if(isset($_REQUEST['linked_placeholder'])){
        $linked_placeholder = $_REQUEST['linked_placeholder'];
    }
    // wenn in kollektion, dann die dummy header id auslesen bzw erstmal anlegen
    if (is_collection_edit() && (!isset($_REQUEST['input_id']) || (int)$_REQUEST['input_id'] == 0)) {
        // neue dummy gallery anlegen
        // optionen auslesen
        $query  = "SELECT options FROM main_collection_setup_content WHERE id ='" . $_POST['collection_setup_content_id']."'";
        $result = mysqli_query($GLOBALS['mysql_con'], $query);
        $row    = mysqli_fetch_array($result);

        $options = unserialize($row['options']);
        // hinzufuegen zur datenbank
        $insertquery = "INSERT INTO contactform_header
			(main_language_id, description, modified_date, modified_user, sender_name, sender_email, recipient_name, recipient_email, subject collection_header)
			VALUES (
				'" . $GLOBALS["language"]['id'] . "',
				'',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_sender_name']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_sender_email']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_recipient_name']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_recipient_email']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $options['collection_setup_input_subject']) . "',
				1
			)";

        @mysqli_query($GLOBALS['mysql_con'], $insertquery);
        $header_id = mysqli_insert_id($GLOBALS['mysql_con']);

        $query = "INSERT INTO main_collection_link (main_collection_id, main_collection_setup_content_id, main_sitepart_header_id, main_sitepart_id)
		VALUES (
			'" . $_REQUEST['input_collection_id'] . "',
			'" . $_REQUEST['collection_setup_content_id'] . "',
			" . $header_id . ",
			3
		)";
        mysqli_query($GLOBALS['mysql_con'], $query);
    } else {
        $header_id = $_REQUEST['input_id'];
    }

    if ($_REQUEST["input_line_id"] <> '') {
        $query    = "UPDATE contactform_line 
                      SET code = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_code"]) . "', 
                      name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_name"]) . "', 
                      typ = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_typ"]) . "', 
                      mandatory = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_mandatory"]) . "', 
                      option_string = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_option_string"]) . "', 
                      infield = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_infield"]) . "' 
                      
                    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 contactform_line WHERE header_id = '" . $_REQUEST["input_id"]."'"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO contactform_line (header_id, code,name,typ,mandatory,option_string,infield,sorting) 
                      VALUES (
                      " . (int)$header_id . ", 
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_code"]) . "',
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_name"]) . "',
                      '" . $_REQUEST["input_typ"] . "',
                      '" . $_REQUEST["input_mandatory"] . "', 
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_option_string"]) . "',
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_infield"]) . "', 
                      " . $sorting . "
                     
                      )";
        $inserted = TRUE;
    }
 
    if (($_REQUEST["input_code"] == '') | ($_REQUEST["input_name"] == '')) {
        $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;
    }
    $id_text = ($_REQUEST["input_id"] <> '') ? " AND header_id = '" . $header_id . "'" : "";

    $result  = @mysqli_query($GLOBALS['mysql_con'], "SELECT * FROM contactform_line WHERE code = '" . $_REQUEST["input_code"] . "'" . $id_text);
    $numrows = @mysqli_num_rows($result);

    if ($numrows > 1) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</div>\n";
        $error      = TRUE;
    }

    if ($_REQUEST["input_id"] <> '' && $numrows == 1) {
        $curr_line = @mysqli_fetch_array($result);
        if ($curr_line['id'] != $_REQUEST["input_line_id"]) {
            $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</div>\n";
            $error      = TRUE;
        }
    }
    

    // benoetigt fuer ajax calls
    if ($error === TRUE) {
        header('EDIT_ERROR: 1');
    }

    if (!$error) {
        $lineID;
        
        @mysqli_query($GLOBALS['mysql_con'], $query);
        if ($inserted === TRUE) {
            $lineID = mysqli_insert_id($GLOBALS['mysql_con']);
        }else{
            $lineID = $_REQUEST["input_line_id"];
        }
        // for multiForm
        $queryform  = "SELECT * FROM contactform_header WHERE id = '" . $_REQUEST['input_id'] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $queryform);
        $contactform  = @mysqli_fetch_array($result);
        $_REQUEST['is_multiform'] = $contactform['is_multistep'];
         
       
       
         
            save_options($lineID);
          
        if($contactform['is_multistep'] == 1){
            $fileName = $_REQUEST['field_img_old'];
            if(isset($_FILES) && !empty($_FILES) && $_FILES['field_img']['size'] != 0 ){
                $fileName = saveImage('field_img', $lineID);
            }
            
                $queryUpd    = "UPDATE contactform_line 
                      SET section_id = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["section_id"]) . "', 
                      next_section_id = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["next_section_id"]) . "', 
                      field_img = '" . $fileName . "', 
                      icon = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["icon"]) . "'
                    WHERE id = '" . $lineID . "' LIMIT 1";
                    // @mysqli_query($GLOBALS['mysql_con'], $queryUpd); 
                    $queryDelete = "DELETE FROM multi_contactform_link WHERE contactform_line_id = '" . $lineID."'";
                    @mysqli_query($GLOBALS['mysql_con'], $queryDelete); 
                    if (line_already_exists($_REQUEST["section_id"], 1, '404', $lineID) === FALSE) {
                       
                        $_REQUEST["input_section_id"] = $_REQUEST["section_id"];
                        $_REQUEST['input_contactform_id'] = $_REQUEST['input_id'];
                        $_REQUEST['contactform_line_id'] = $lineID;
                        assoc_sitepart(1, '404', FALSE);
                    }
                     $queryUpd = "UPDATE multi_contactform_link SET layout_area_id  = ".$_REQUEST['input_layout_area_id']." WHERE contactform_line_id = '" . $lineID."'";	
                    
                    @mysqli_query($GLOBALS['mysql_con'],$queryUpd);
                    if(isset($_POST['input_option'])){
                        $queryDelete = "DELETE FROM contactform_line_option WHERE line_id = '" . $lineID."'";
                            @mysqli_query($GLOBALS['mysql_con'], $queryDelete); 
                        foreach($_POST['input_option'] as $key => $item){
                            $option_string = '';
                            if(isset($item['name'])){
                                $option_string = $item['name'];
                            }
                            $type = $_REQUEST["input_typ"];
                            $field_img = '';
                            if(isset($item['old_bild'])){
                                $field_img  = $item['old_bild'];
                            }
                            if(isset($_FILES) && !empty($_FILES) && $_FILES['input_option']['size'][$key]['bild'] != 0 ){
                                // $fileName = saveImage('field_img', $lineID);
                               
                                // save filePath
                                $erlaubte_endungen = array("jpg", "jpeg", "gif", "png");
                                $filename = strtolower($_FILES['input_option']['name'][$key]['bild']);
                                $filename_expl = explode(".", $filename);
                                if (!in_array($filename_expl[count($filename_expl) - 1], $erlaubte_endungen)) {
                                    $messages = "<div class='errorbox'>" . $translation->get("error_slideshow_line2") . "</div>\n";
                                    $error = TRUE;
                                }
                                
                                $saveFilename = $input_id . "_" . time() . "_" . cleanFilename($_FILES['input_option']['name'][$key]['bild']);
                
                                $width = 300;
                                $height = 200;
                                require_once(MODULE_PATH . "collection/collection_config.inc.php");
                                move_uploaded_file($_FILES['input_option']['tmp_name'][$key]['bild'], PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename);
                                image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, PATH_RESIZE_COLLECTION . $saveFilename, $width, $height, TRUE);
                                image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, "../.." . PATH_ICON_COLLECTION . $saveFilename, 200, 100, TRUE);
                                // SavePath
                                $field_img = $saveFilename;
                            }
                            $icon = '';
                            if(isset($item['icon'])){
                                $icon = $item['icon'];
                            }
                            $next_section_id = '';
                            if(isset($item['nextSection'])){
                                $next_section_id = $item['nextSection'];
                            }
                            $linked_field = (isset($item["linked_field"])) ? 1 : 0;
                            $linked_placeholder = 'NA';
                            if(isset($item['linked_placeholder'])){
                                $linked_placeholder = $item['linked_placeholder'];
                            }
                            
                            $queryInsertLineOpt    = "INSERT INTO contactform_line_option (line_id,type,option_string, field_img, icon, next_section_id, linked_field, linked_placeholder) 
                                VALUES (
                                " . (int)$lineID . ",  
                                " . (int)$type . ",  
                                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $option_string) . "',
                                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $field_img) . "',
                                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $icon) . "',
                                '" . (int)$next_section_id  . "',
                                '" . (int)$linked_field  . "',
                                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $linked_placeholder) . "'
                                )";
                                @mysqli_query($GLOBALS['mysql_con'], $queryInsertLineOpt);    
                        }
                    } 
                        
        }
       
     
    // For MultiFormEnd
        if ($_REQUEST['save_and_close'] == 1) {

            edit_contactform();
        } else {
            if ($inserted === TRUE) {
                $id = $lineID;
            }
            edit_line_contactform($id);
        }

    } else {
        $input_line["id"]            = $_REQUEST["input_line_id"];
        $input_line["header_id"]     = $header_id;
        $input_line["typ"]           = $_REQUEST["input_typ"];
        $input_line["code"]          = $_REQUEST["input_code"];
        $input_line["name"]          = $_REQUEST["input_name"];
        $input_line["option_string"] = $_REQUEST["input_option_string"];
        $input_line["mandatory"]     = $_REQUEST["input_mandatory"];
        $input_line["infield"]       = $_REQUEST["input_infield"];
        $queryform  = "SELECT * FROM contactform_header WHERE id = '" . $input_line['header_id'] . "' LIMIT 1";
        $resultForm = @mysqli_query($GLOBALS['mysql_con'], $queryform);
        $contactform  = @mysqli_fetch_array($resultForm);
        
        if($contactform['is_multistep']){
           
            require_once("edit_contactform_multi_line_cardform.inc.php");
        }else{
            require_once("edit_contactform_line_cardform.inc.php");
        }
         
    }
}
function save_options($lineID){
    if(isset($_POST['input_option'])){
        $queryDelete = "DELETE FROM contactform_line_option WHERE line_id = '" . $lineID."'";
            @mysqli_query($GLOBALS['mysql_con'], $queryDelete); 
        foreach($_POST['input_option'] as $key => $item){
            $option_string = '';
            if(isset($item['name'])){
                $option_string = $item['name'];
            }
            $type = $_REQUEST["input_typ"];
            $field_img = '';
            if(isset($item['old_bild'])){
                $field_img  = $item['old_bild'];
            }
            if(isset($_FILES) && !empty($_FILES) && $_FILES['input_option']['size'][$key]['bild'] != 0 ){
                // $fileName = saveImage('field_img', $lineID);
               
                // save filePath
                $erlaubte_endungen = array("jpg", "jpeg", "gif", "png");
                $filename = strtolower($_FILES['input_option']['name'][$key]['bild']);
                $filename_expl = explode(".", $filename);
                if (!in_array($filename_expl[count($filename_expl) - 1], $erlaubte_endungen)) {
                    $messages = "<div class='errorbox'>" . $translation->get("error_slideshow_line2") . "</div>\n";
                    $error = TRUE;
                }
                
                $saveFilename = $input_id . "_" . time() . "_" . cleanFilename($_FILES['input_option']['name'][$key]['bild']);

                $width = 300;
                $height = 200;
                require_once(MODULE_PATH . "collection/collection_config.inc.php");
                move_uploaded_file($_FILES['input_option']['tmp_name'][$key]['bild'], PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename);
                image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, PATH_RESIZE_COLLECTION . $saveFilename, $width, $height, TRUE);
                image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, "../.." . PATH_ICON_COLLECTION . $saveFilename, 200, 100, TRUE);
                // SavePath
                $field_img = $saveFilename;
            }
            $icon = '';
            if(isset($item['icon'])){
                $icon = $item['icon'];
            }
            $next_section_id = '';
            if(isset($item['nextSection'])){
                $next_section_id = $item['nextSection'];
            }
            $linked_field = (isset($item["linked_field"])) ? 1 : 0;
            $linked_placeholder = 'NA';
            if(isset($item['linked_placeholder'])){
                $linked_placeholder = $item['linked_placeholder'];
            }
            
            $queryInsertLineOpt    = "INSERT INTO contactform_line_option (line_id,type,option_string, field_img, icon, next_section_id, linked_field, linked_placeholder) 
                VALUES (
                " . (int)$lineID . ",  
                " . (int)$type . ",  
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $option_string) . "',
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $field_img) . "',
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $icon) . "',
                '" . (int)$next_section_id  . "',
                '" . (int)$linked_field  . "',
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $linked_placeholder) . "'
                )";
                @mysqli_query($GLOBALS['mysql_con'], $queryInsertLineOpt);    
        }
    } 
}
function delete_collection_image_($filename)
{
     
    require_once(MODULE_PATH . "collection/collection_config.inc.php");
    $removed = FALSE;
    if (file_exists($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/original/' . $filename)) {
        unlink($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/original/' .  $filename);
      
        $removed = TRUE;
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/icon/' .  $filename)) {
        unlink($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/icon/' .  $filename);
       
        $removed = TRUE;
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/resize/' . $filename)) {
        unlink($_SERVER['DOCUMENT_ROOT'].'/userdata/collection/resize/' . $filename);
    
        $removed = TRUE;
    } 
    return $removed;
}


function saveImage($filekey = 'field_img', $input_id){
    $erlaubte_endungen = array("jpg", "jpeg", "gif", "png");
    $filename = strtolower($_FILES[$filekey]['name']);
    $filename_expl = explode(".", $filename);
    if (!in_array($filename_expl[count($filename_expl) - 1], $erlaubte_endungen)) {
        $messages = "<div class='errorbox'>" . $translation->get("error_slideshow_line2") . "</div>\n";
        $error = TRUE;
    }
    
    $saveFilename = $input_id . "_" . time() . "_" . cleanFilename($_FILES[$filekey]['name']);

    $width = 300;
    $height = 200;
    require_once(MODULE_PATH . "collection/collection_config.inc.php");
    move_uploaded_file($_FILES[$filekey]['tmp_name'], PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename);
    image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, PATH_RESIZE_COLLECTION . $saveFilename, $width, $height, TRUE);
    image_resize(PATH_ORIGINAL_PICTURE_COLLECTION . $saveFilename, "../.." . PATH_ICON_COLLECTION . $saveFilename, 200, 100, TRUE);

    return $saveFilename;
    
}
function save_line_contactform_section(){
    $translation                 = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages                    = array();
    $_REQUEST["input_mandatory"] = ($_REQUEST["input_mandatory"]) ? 1 : 0;
    $_REQUEST["input_infield"]   = ($_REQUEST["input_infield"]) ? 1 : 0;
      
    $header_id = $_REQUEST['input_id'];
    // Save Data
    if ($_REQUEST["input_line_id"] <> '') {
        $query    = "UPDATE contactform_section 
                      SET name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["name"]) . "', 
                      class = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["class"]) . "',
                      step_id = '" . $_REQUEST['step_id'] . "'
                    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 contactform_section WHERE contactform_id = '" . $_REQUEST["input_id"]."'"));
        $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        $query    = "INSERT INTO contactform_section (contactform_id,name,class, sorting, step_id) 
                      VALUES (
                      " . (int)$header_id . ",  
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["name"]) . "',
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["class"]) . "',
                      
                      " . $sorting . ",
                      " . $_REQUEST['step_id'] . "
                      )";
        $inserted = TRUE;
    }
    if (($_REQUEST["name"] == '') || ($_REQUEST["step_id"] == '') ) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_description") . "</div>\n";
        $error      = TRUE;
    }
     
    $id_text = ($_REQUEST["input_id"] <> '') ? " AND contactform_id = '" . $header_id . "'" : "";

    $result  = @mysqli_query($GLOBALS['mysql_con'], "SELECT * FROM contactform_section WHERE id = '" . $_REQUEST["input_line_id"] . "'" . $id_text);
    $numrows = @mysqli_num_rows($result);

    if ($numrows > 1) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</div>\n";
        $error      = TRUE;
    }

    if ($_REQUEST["input_id"] <> '' && $numrows == 1) {
        $curr_line = @mysqli_fetch_array($result);
        if ($curr_line['id'] != $_REQUEST["input_line_id"]) {
            $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</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_contactform();
        } else {
            if ($inserted === TRUE) {
                $id = mysqli_insert_id($GLOBALS['mysql_con']);
                new_line_contactform_section($id);
            }else{
                new_line_contactform_section($_REQUEST["input_line_id"]);
            }
             
        }

    } else {
        $input_line["id"]            = $_REQUEST["input_line_id"];
        $input_line["header_id"]     = $header_id;
        $input_line["typ"]           = $_REQUEST["input_typ"];
        $input_line["code"]          = $_REQUEST["input_code"];
        $input_line["name"]          = $_REQUEST["input_name"];
        $input_line["option_string"] = $_REQUEST["input_option_string"];
        $input_line["mandatory"]     = $_REQUEST["input_mandatory"];
        $input_line["infield"]       = $_REQUEST["input_infield"];
        require_once("edit_multi_contactform_line_cardform_section.inc.php");
    }
    // Save Data End
}
function save_line_contactform_level(){
    $translation                 = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages                    = array();
    $_REQUEST["input_mandatory"] = ($_REQUEST["input_mandatory"]) ? 1 : 0;
    $_REQUEST["input_infield"]   = ($_REQUEST["input_infield"]) ? 1 : 0;
   
    $header_id = $_REQUEST['input_id'];
    // Save Data
    if ($_REQUEST["input_line_id"] <> '') {
        $sorting;
        if(isset($_REQUEST['sorting'])){
            $sorting = $_REQUEST['sorting'];
        }else{
            $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT sorting AS 'newsorting' FROM contactform_steps WHERE id = '" .  $_REQUEST["input_line_id"]."'"));
            $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        }
        $query    = "UPDATE contactform_steps 
                      SET name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["name"]) . "', 
                      class = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["class"]) . "',
                      sorting = ".$sorting."
                    WHERE id = '" . $_REQUEST["input_line_id"] . "' LIMIT 1";
        $inserted = FALSE;
    } else {
        $sorting;
        if(isset($_REQUEST['sorting'])){
            $sorting = $_REQUEST['sorting'];
        }else{
            $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM contactform_steps WHERE contactform_id = '" . $_REQUEST["input_id"]."'"));
            $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
        }
        
        $query    = "INSERT INTO contactform_steps (contactform_id,name,class, sorting) 
                      VALUES (
                      " . (int)$header_id . ",  
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["name"]) . "',
                      '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["class"]) . "',
                      
                      " . $sorting . "
                      )";
        $inserted = TRUE;
    }
    if (($_REQUEST["name"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_description") . "</div>\n";
        $error      = TRUE;
    }
     
    $id_text = ($_REQUEST["input_id"] <> '') ? " AND contactform_id = '" . $header_id . "'" : "";

    $result  = @mysqli_query($GLOBALS['mysql_con'], "SELECT * FROM contactform_steps WHERE id = '" . $_REQUEST["input_line_id"] . "'" . $id_text);
    $numrows = @mysqli_num_rows($result);

    if ($numrows > 1) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</div>\n";
        $error      = TRUE;
    }

    if ($_REQUEST["input_id"] <> '' && $numrows == 1) {
        $curr_line = @mysqli_fetch_array($result);
        if ($curr_line['id'] != $_REQUEST["input_line_id"]) {
            $messages[] = "<div class=\"errorbox\">" . $translation->get("error_textkey_exists") . "</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_contactform();
        } else {
            if ($inserted === TRUE) {
                $id = mysqli_insert_id($GLOBALS['mysql_con']);
                new_line_contactform_level($id);
                 

            }else{
                $id = $_REQUEST["input_line_id"];
               
                new_line_contactform_level($id);
            }
            
        }

    } else {
        $input_line["id"]            = $_REQUEST["input_line_id"];
        $input_line["header_id"]     = $header_id;
        $input_line["typ"]           = $_REQUEST["input_typ"];
        $input_line["code"]          = $_REQUEST["input_code"];
        $input_line["name"]          = $_REQUEST["input_name"];
        $input_line["option_string"] = $_REQUEST["input_option_string"];
        $input_line["mandatory"]     = $_REQUEST["input_mandatory"];
        $input_line["infield"]       = $_REQUEST["input_infield"];
        require_once("edit_multi_contactform_line_cardform.ic.php");
    }
    // Save Data End
}
function moveup_line_contactform() {
    move_line_contactform("<", "DESC", $_REQUEST["input_line_id"]);
}

function movedown_line_contactform() {
    move_line_contactform(">", "ASC", $_REQUEST["input_line_id"]);
}
function moveup_line_multicontactform(){
    
    move_line_multicontactform("<", "DESC", $_REQUEST["input_line_id"], $_REQUEST["section_id"]);
}
function movedown_line_multicontactform(){
 
    move_line_multicontactform(">", "ASC", $_REQUEST["input_line_id"], $_REQUEST["section_id"]);
    
}
function move_line_multicontactform( $way, $order, $field_id, $section_id ) {
     
    $query  = "SELECT * FROM multi_contactform_link WHERE id = '" . $field_id . "' AND main_section_id ='".$section_id."' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $curr_line = @mysqli_fetch_array($result);
    }
    $query  = "SELECT * FROM multi_contactform_link WHERE main_section_id = " . $curr_line["main_section_id"] . " AND sorting " . $way . " " . $curr_line["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
     
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $change_line = @mysqli_fetch_array($result);
    }
    if (($curr_line["id"] <> '') && ($change_line["id"] <> '')) {
        $query = "UPDATE multi_contactform_link SET sorting = " . $change_line["sorting"] . " WHERE id = " . $curr_line["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "UPDATE multi_contactform_link SET sorting = " . $curr_line["sorting"] . " WHERE id = " . $change_line["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    $input_line['header_id'] = $_REQUEST['input_id'];
    $query  = "SELECT * FROM contactform_header WHERE id = '" . $_REQUEST['input_id'] . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $input_id =      $_REQUEST['input_id'];
    $input_contactform = @mysqli_fetch_array($result);
    require_once("edit_multi_contactform_cardform.inc.php");
}
function move_line_contactform( $way, $order, $field_id ) {
    $query  = "SELECT * FROM contactform_line WHERE id = '" . $field_id . "' LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $curr_line = @mysqli_fetch_array($result);
    }
    $query  = "SELECT * FROM contactform_line WHERE header_id = " . $curr_line["header_id"] . " AND sorting " . $way . " " . $curr_line["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 1) {
        $change_line = @mysqli_fetch_array($result);
    }
    if (($curr_line["id"] <> '') && ($change_line["id"] <> '')) {
        $query = "UPDATE contactform_line SET sorting = " . $change_line["sorting"] . " WHERE id = " . $curr_line["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "UPDATE contactform_line SET sorting = " . $curr_line["sorting"] . " WHERE id = " . $change_line["id"];
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}
function delete_line_contactform_bild(){
   $query = 'SELECT * FROM contactform_line_option WHERE id = '.$_REQUEST['opt_id'];
   $result =  @mysqli_query($GLOBALS['mysql_con'], $query);
   $row = @mysqli_fetch_array($result);
    
    if(delete_collection_image_($row['field_img'])){
        $query = "UPDATE contactform_line_option SET field_img  = ' ' WHERE id = '" . $row['id']."'";	
        
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
    
    edit_line_contactform($_REQUEST['input_line_id']);
}
function edit_line_contactform( $_id = "" ) {
    if ((int)$_id > 0) {
        $line_id = $_id;
    } else {
        $line_id = $_REQUEST["input_line_id"];
    }

    if ($line_id <> '') {
        $query  = "SELECT * FROM contactform_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);
            $queryform  = "SELECT * FROM contactform_header WHERE id = '" . $input_line['header_id'] . "' LIMIT 1";
            $resultForm = @mysqli_query($GLOBALS['mysql_con'], $queryform);
            $contactform  = @mysqli_fetch_array($resultForm);
            
            if($contactform['is_multistep']){
               
                require_once("edit_contactform_multi_line_cardform.inc.php");
            }else{
                require_once("edit_contactform_line_cardform.inc.php");
            }
            
        }
    } else {
        require_once("edit_contactform_cardform.inc.php");
    }
}
function edit_line_contactform_section($_id = "" ){

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

    if ($line_id <> '') {
        $query  = "SELECT * FROM contactform_steps 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);
            $input_line['header_id'] = $input_line['contactform_id'];
            require_once("edit_multi_contactform_line_cardform.ic.php");
        }
    } else {
        require_once("edit_multi_contactform_cardform.inc.php");
    }

}
function edit_line_contactform_level($_id = "" ){

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

    if ($line_id <> '') {
        $query  = "SELECT * FROM contactform_section 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);
            $input_line['header_id'] = $input_line['contactform_id'];
            require_once("edit_multi_contactform_line_cardform_section.inc.php");
        }
    } else {
        require_once("edit_multi_contactform_cardform.inc.php");
    }

}
function edit_contactform( $_id = "", $messages = array() ) {
    
    if (is_collection_edit()) {
        edit_collection();
        return;
    }

    if ((int)$_id > 0) {
        $input_id = $_id;
    } if(isset($_REQUEST['input_contactform_id'])){
        $input_id = $_REQUEST["input_contactform_id"];
    }   
    else {
        $input_id = $_REQUEST["input_id"];
    }
 
    if ($input_id <> '') {
        $query  = "SELECT * FROM contactform_header WHERE id = '" . $input_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_contactform = @mysqli_fetch_array($result);
            if($input_contactform['is_multistep'] || $_REQUEST['level_2'] == 'multistep_forms'){
                require_once("edit_multi_contactform_cardform.inc.php");
            }else{
                require_once("edit_contactform_cardform.inc.php");
            }
        }
    } else {
        $input_contactform = array();
        if($_REQUEST['level_2'] == 'multistep_forms'){
            require_once("edit_multi_contactform_cardform.inc.php");
        }else{
            require_once("edit_contactform_cardform.inc.php");
        }
        require_once("edit_contactform_cardform.inc.php");
    }
}
function line_already_exists($_main_section_id , $_sitepartid = '1', $sitepart_headerid = '404', $line_id = '') {
    $query;
   
    if($line_id == ''){
        $query = "SELECT id FROM multi_contactform_link WHERE main_section_id = '" . (int)$_main_section_id . "' AND main_sitepart_id = '" . (int)$_sitepartid . "' AND main_sitepart_header_id = '" . (int)$sitepart_headerid."'";
    }else{
        $query = "SELECT id FROM multi_contactform_link WHERE main_section_id = '" . (int)$_main_section_id . "' AND main_sitepart_id = '" . (int)$_sitepartid . "' AND main_sitepart_header_id = '" . (int)$sitepart_headerid."' AND contactform_line_id = '".$line_id."'";
    }
 
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) >= 1) {
		$row = @mysqli_fetch_array($result);
        $active = 0;
                if(isset($_REQUEST['input_active'])){
                    $active = 1;
                }
         $layout_class_defined = ''; 
         if(isset($_REQUEST['layout_class_defined'])){
            $layout_class_defined =  $_REQUEST['layout_class_defined']; 
        }      
        $query = "UPDATE multi_contactform_link SET layout_area_id  = ".$_REQUEST['input_layout_area_id'].", active = ". $active .", layout_class_defined = '".$layout_class_defined."' WHERE id = '" . $row['id']."'";	
      
        @mysqli_query($GLOBALS['mysql_con'],$query);
        return $row['id'];
        
	} else {
		return false;
	}
}
function assoc_sitepart($_sitepartId="", $_inputid = "", $_close = true) {
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	
	if($_sitepartId != "") {
		$sitepartId = $_sitepartId;
	} else {
		$sitepartId = $_REQUEST['data-sitepartid'];
	}
	
	if($_inputid != "") {
		$inputid = $_inputid;
	} else {
		$inputid = $_REQUEST['input_id'];
	}
 
	set_multi_contact_link($sitepartId, $inputid);
	
	$messages = array(
		'<div class="successbox">' . $translation->get("page_line_assoc_success") . '</div>'
	);
	
	if($_close === true) {
		require_once("edit_multi_contactform_line_listform.inc.php");
	}
}
function delete_contactform() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM contactform_header WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $query = "DELETE FROM contactform_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 = 3 AND main_sitepart_header_id = '" . $_REQUEST["input_id"]."'";
        @mysqli_query($GLOBALS['mysql_con'], $query);

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

function save_contactform() {

    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id    = "";
    $input_all_languages = ($_POST["input_all_languages"] == "on") ? 1 : 0;
    $input_recaptcha_active = ($_POST["input_recaptcha_active"] == "on") ? 1 : 0;
    $is_multistep = (isset($_POST["is_multistep"])) ? 1 : 0;
    $is_survey = (isset($_POST["is_survey"])) ? 1 : 0;
    $is_registration = (isset($_POST["is_registration"])) ? 1 : 0;
    
    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE contactform_header SET
				description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
				sender_name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_sender_name"]) . "', 
				sender_email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_sender_email"]) . "', 
				recipient_name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_recipient_name"]) . "',
				recipient_email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_recipient_email"]) . "',
				subject = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_subject"]) . "',
				auto_answer = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_auto_answer"]) . "',
				all_languages = '" . $input_all_languages . "',
                recaptcha_active = " . $input_recaptcha_active . ",
                is_multistep = ".$is_multistep.",
                is_survey = ".$is_survey.",
                is_registration = ".$is_registration."
				WHERE id = '" . $_REQUEST["input_id"] . "'
			LIMIT 1";
        $inserted = FALSE;
        $input_id = $_REQUEST["input_id"];
       
    } else {
        //$query = "INSERT INTO main_contactform (id, main_site_id, code,name,site_name,std_main_navigation_id,site_title_name,meta_description,meta_keywords,main_layout_id,company,shop_code,shop_contactform_code,logout_site_id,logout_contactform_id,logout_navigation_id) VALUES (NULL, '" . $GLOBALS["site"]["id"] . "', '" . $_REQUEST["input_code"]. "','" . $_REQUEST["input_name"]. "','" . $_REQUEST["input_site_name"] . "','" . $_REQUEST["input_std_main_navigation_id"] . "','" . $_REQUEST["input_site_title_name"] . "','" . $_REQUEST["input_meta_description"] . "','" . $_REQUEST["input_meta_keywords"] . "','" . $_REQUEST["input_main_layout_id"] . "','" . $_REQUEST["input_company"] . "','" . $_REQUEST["input_shopcode"] . "','" . $_REQUEST["input_contactform_code"] . "','".$_REQUEST["input_login_site"]."','".$_REQUEST["input_login_contactform"]."','".$_REQUEST["input_login_navigation"]."')";
        $query    = "INSERT INTO contactform_header
			(main_language_id, description, modified_date, modified_user, sender_name, sender_email, recipient_name, recipient_email, subject, auto_answer, all_languages, recaptcha_active, is_multistep, is_survey, is_registration)
			VALUES (
				" . (int)$GLOBALS["language"]['id'] . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_description']) . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_sender_name']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_sender_email']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_recipient_name']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_recipient_email']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_subject']) . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_auto_answer']) . "',
				'" . $input_all_languages . "',
                 " . $input_recaptcha_active . ",
                 ".$is_multistep.",
                 ".$is_survey.",
                 ".$is_registration."
			)";
            
        $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("contactform_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("contactform_msg_success2") . '</div>';
        }

        update_sitepart_changes(3, $input_id);

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

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

            edit_contactform($input_id, $messages);

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

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

            edit_contactform($input_id, $messages);
        } else {
            // check for the registration and add name/email to line
            // $arrayCode = array();
            // $queryLine = "SELECT * FROM contactform_line where header_id = ".$input_id." AND code = 'registration_name'";
            // $resultLine = @mysqli_query($GLOBALS['mysql_con'],$queryLine);
            // if(@mysqli_num_rows($resultLine) == 0){
            //     $arrayCode[0]['code'] = 'registration_name';
            //     $arrayCode[0]['name'] = 'Name';
            // }
            // $queryLine = "SELECT * FROM contactform_line where header_id = ".$input_id." AND code = 'registration_email'";
            // $resultLine = @mysqli_query($GLOBALS['mysql_con'],$queryLine);
            // if(@mysqli_num_rows($resultLine) == 0){
 
            //     $arrayCode[1]['code'] = 'registration_email';
            //     $arrayCode[1]['name'] = 'Email';
            // }
            // if (!empty($arrayCode)) {
            //   foreach($arrayCode as $key => $val ){
            //         $sorting  = @mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'], "SELECT MAX(sorting)+1 AS 'newsorting' FROM contactform_line WHERE header_id = '" . $input_id."'"));
            //         $sorting  = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
            //         $query    = "INSERT INTO contactform_line (header_id, code,name,typ,mandatory,option_string,infield,sorting) 
            //                     VALUES (
            //                     " . (int)$input_id . ", 
            //                     '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $val['code'] ) . "',
            //                     '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $val['name']) . "',
            //                     '" . '1' . "',
            //                     '" . '1' . "', 
            //                     '',
            //                     '', 
            //                     " . $sorting . "
                                
            //                     )";
            //         @mysqli_query($GLOBALS['mysql_con'],$query)      ;        
            //   }

            // }
            // add name/email to line
            edit_contactform($input_id, $messages);
        }

    } else {
        header('EDIT_ERROR: 1');
        $input_contactform["description"]     = $_REQUEST["input_description"];
        $input_contactform["sender_name"]     = $_REQUEST["input_sender_name"];
        $input_contactform["sender_email"]    = $_REQUEST["input_sender_email"];
        $input_contactform["recipient_name"]  = $_REQUEST["input_recipient_name"];
        $input_contactform["recipient_email"] = $_REQUEST["input_recipient_email"];
        $input_contactform["subject"]         = $_REQUEST["input_subject"];
        $input_contactform["id"]              = $_REQUEST["input_id"];
        $input_contactform["all_languages"]   = $input_all_languages;
        require_once("edit_contactform_cardform.inc.php");
    }
}

function show_contact_link_rows($_section_ID = '' ) {
   
	$main_layout_id = $GLOBALS["language"]["main_layout_id"];
	$input_section_id = $_section_ID;
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	global $layoutareas;
	
	// alle layoutareas
	$layoutareas = array();
	$query = "SELECT * FROM main_layout_area where main_layout_id = '" . $main_layout_id."'";
	if($result = @mysqli_query($GLOBALS['mysql_con'],$query)) {
		while ($row = @mysqli_fetch_array($result,1)) {
			$layoutareas[$row['id']] = $row;
		}
	}
    
	$query = "SELECT 
				multi_contactform_link.id, 
				multi_contactform_link.main_sitepart_id, 
				multi_contactform_link.main_sitepart_header_id,
				multi_contactform_link.main_collection_list, 
				multi_contactform_link.main_collection_id, 
				multi_contactform_link.main_collection_setup_id,
				multi_contactform_link.main_section_group,
				multi_contactform_link.main_section_group_name,
				multi_contactform_link.active,
				multi_contactform_link.layout_area_id,
				from_unixtime(multi_contactform_link.modified_date, '%d.%m.%Y %H:%i:%s') AS aenderungs_datum, 
				main_admin_user.name AS username ,
                multi_contactform_link.contactform_line_id
			from 
				multi_contactform_link left join main_admin_user 
			ON 
				multi_contactform_link.modified_user = main_admin_user.id 
			where 
				multi_contactform_link.main_section_id = '" . $input_section_id . "' 
				AND multi_contactform_link.main_section_link_parent_id = 0
			ORDER BY 
				sorting ASC";
                 
	if($result = @mysqli_query($GLOBALS['mysql_con'],$query)) {
		$num_rows = @mysqli_num_rows($result);
		if($num_rows >= 1) {
			echo "  <table cellpadding=0 cellspacing=0 border=0 class=\"linklist\">\n";
			
			echo "<tr>";
			
			echo "<td><div>" . $translation->get("description") . "</div></td>";
			echo "<td width=\"150\" align=\"left\"><div></div></td>";
			echo "<td width=\"150\" align=\"left\"><div>" . $translation->get("type") . "</div></td>";
			echo "<td width=\"150\" align=\"left\"><div>" . $translation->get("state") . "</div></td>";
			echo "<td width=\"250\" align=\"left\"><div>" . $translation->get("layout_area") . "</div></td>";
			
			echo "</tr>";
			
			echo "<tr>";
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo "</tr>";
			
			echo "  </table>\n";
			
			get_real_contact_link_id();
			
			echo "<ol class=\"linklist sortable contentlist\">";
			while ($row = @mysqli_fetch_array($result,1)) {
				handle_page_link_row($row);
			}
			echo "</ol>";
			
			
		} else {
			echo "<div class=\"infobox\">" . $translation->get("no_rows_found") . "</div>";
		}
		
	}
}
function get_real_contact_link_id() {
	if(isset($_REQUEST['get_real_page_link_id']) && $_REQUEST['get_real_page_link_id'] == 1) {
		$_REQUEST['input_id'] = line_already_exists($_REQUEST["input_page_id"], $_REQUEST['data-sitepartid'], $_REQUEST["input_id"]);
	}
}

function handle_page_link_row($row, $level=0) {
	$siteparts = \DynCom\mysyde\common\classes\Siteparts::get();
	global $layoutareas;
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	
	$isGroup = false;
	
	$sitepartDescription = "";
	$row_sitepart_description = array();
	if((int)$row['main_sitepart_id'] > 0) {
		
		if((int)$row['main_sitepart_header_id'] <= 0) {
			
			// siteparts, die keine header_table brauchen
			$sitepartType = $siteparts[$row['main_sitepart_id']]['description'];
			$row_sitepart_description['description'] = $siteparts[$row['main_sitepart_id']]['sub_description'];
			$dblClickAction = "edit_line_properties_page";
			
		} 
        
        else {
			
			// siteparts mit einer header_table
			$query_description = "SELECT description FROM " . $siteparts[$row['main_sitepart_id']]['header_table'] . " WHERE id = " . $row['main_sitepart_header_id'];
			$result_description = @mysqli_query($GLOBALS['mysql_con'],$query_description);
			$row_sitepart_description = @mysqli_fetch_array($result_description);
			$sitepartType = $siteparts[$row['main_sitepart_id']]['description'];
			$dblClickAction = "edit_line_multi_contact";
			if(empty($row_sitepart_description['description'])) {
				$row_sitepart_description['description'] = $siteparts[$row['main_sitepart_id']]['description'];
			}			
		}
		
	} elseif((int)$row['main_collection_list'] == 2) {
		
		// kollektionsvorschau
		$sitepartType = $translation->get("collection_preview");
		$query_description = "SELECT description FROM main_collection_setup WHERE id = " . $row['main_collection_setup_id'];
		$result_description = @mysqli_query($GLOBALS['mysql_con'],$query_description);
		$row_sitepart_description = @mysqli_fetch_array($result_description);
		$dblClickAction = "edit_collection_preview_page";
		
	} elseif((int)$row['main_collection_list'] == 1) {
		
		// Komplette Kollektion
		$sitepartType = $translation->get("collection");
		$query_description = "SELECT description FROM main_collection_setup WHERE id = " . $row['main_collection_setup_id'];
		$result_description = @mysqli_query($GLOBALS['mysql_con'],$query_description);
		$row_sitepart_description = @mysqli_fetch_array($result_description);
		$dblClickAction = "edit_collection_list_page";
	} elseif((int)$row['main_section_group'] > 0) {
		
		// Gruppe
		$sitepartType = $translation->get("group");
		$row_sitepart_description['description'] =  $row['main_section_group_name'];
		$dblClickAction = "edit_group_page";
		
		$isGroup = true;
	}
    if($row['contactform_line_id']  != NULL && $row['contactform_line_id']  != 0 ) {
        $sitepartType = 'Formularfeld';
        $queryform  = "SELECT * FROM contactform_line WHERE id = '" . $row['contactform_line_id'] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $queryform);
        $contactform  = @mysqli_fetch_array($result);
        $row_sitepart_description['description'] = $contactform['name'];

    } 
    
	
	
	$layoutarea_description = $layoutareas[$row['layout_area_id']]['name'];
	
	$sitepartCode = $siteparts[$row['main_sitepart_id']]['code'];
	
	$checkedRow = $row['id'] == $_REQUEST["input_id"] ? 'true' : 'false';
	
	$aktivText = ($row["active"] == 1 ? $translation->get("active") : $translation->get("inactive"));
	$activeClass = ($row["active"] == 1) ? 'active' : 'inactive';
	
	
//	echo "    <tr class=\"linklist_content\" data-sitepartid=\"{$row['main_sitepart_id']}\" data-checked=\"{$checkedRow}\" data-inputname=\"input_id\" data-action=\"" . $dblClickAction . "\" data-inputid=\"{$row['id']}\">\n";
//	
	
//	echo format_value($sitepartType, "input_id");
//	echo format_value($row_sitepart_description['description'], "input_id");
//	echo format_value($aktivText, "input_id");
//	echo format_value($layoutarea_description, "input_id");
//	
//	echo "    </tr>\n";
	
	$noNestingClass = "mjs-nestedSortable-no-nesting";
	if($isGroup === true) {
		$noNestingClass = "";
	}
	
	
	echo "<li class=\"" . $noNestingClass . " ".strtolower($activeClass)."\" id=\"list_{$row['id']}\">";
	echo "<div  data-inputid=\"" . $row['id'] . "\" data-sitepartid=\"{$row['main_sitepart_id']}\" data-action=\"" . $dblClickAction . "\" data-inputname=\"input_id\" data-checked=\"{$checkedRow}\" class=\"linklist_content dd-handle\"><span class=\"disclose\"><span>&nbsp;</span></span>" . $row_sitepart_description['description'] . "<div class=\"dd-icon\"></div>";
	echo "<div class=\"dd-code\">" . $sitepartType . "</div>";
	echo "<div class=\"dd-status\">" . $aktivText . "</div>";
	echo "<div class=\"dd-layout\">" . $layoutarea_description . "</div></div>";
	
	if($isGroup === true) {
		$level++;
		// wenn gruppe, dann alle datensaetze aus der gruppe auslesen
		$query = "SELECT 
				multi_contactform_link.id, 
				multi_contactform_link.main_sitepart_id, 
				multi_contactform_link.main_sitepart_header_id,
				multi_contactform_link.main_collection_id, 
				multi_contactform_link.main_collection_setup_id,
				multi_contactform_link.main_collection_list,
				multi_contactform_link.main_section_group,
				multi_contactform_link.main_section_group_name,
				multi_contactform_link.active,
				multi_contactform_link.layout_area_id,
				from_unixtime(multi_contactform_link.modified_date, '%d.%m.%Y %H:%i:%s') AS aenderungs_datum, 
				main_admin_user.name AS username ,
                multi_contactform_link.contactform_line_id
			from 
				multi_contactform_link left join main_admin_user
			ON 
				multi_contactform_link.modified_user = main_admin_user.id 
			where 
				multi_contactform_link.main_section_link_parent_id = " . $row['id'] . "
			ORDER BY 
				sorting ASC";
		
		if(!$result = @mysqli_query($GLOBALS['mysql_con'],$query)) {
			return;
		}
		
		$num_rows = @mysqli_num_rows($result);
		if($num_rows <= 0) {		
			return;
		}
		echo "<ol>";
		while ($row = @mysqli_fetch_array($result,1)) {
			handle_page_link_row($row, $level);
		}
		echo "</ol>";
	}
	
	echo "</li>";
}
function edit_line_contact() {
     
	// anhand der input_id sitepart art auslesen
	$sitepartId = $_REQUEST['data-sitepartid'];
	$query = "SELECT * FROM multi_contactform_link WHERE id = '" . $_POST['data-inputid']."'";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	$row = @mysqli_fetch_array($result);
	if($row['contactform_line_id'] != null && $row['contactform_line_id'] != 0 ){
        edit_line_contactform($row['contactform_line_id']);
        return;
    }
	if($row['main_page_group'] == 1) {
		edit_group_page();
		return;
	}
	
	if($row['main_collection_list'] == 1) {
		edit_collection_preview_page();
		return;
	}
	
	if($row['main_collection_list'] == 2) {
		$_REQUEST['is_collection_preview']  = 1;
		edit_collection_preview_page();
		return;
	}
	
	if((int)$row['main_sitepart_header_id'] <= 0) {
		// keine header_table, also nur Seiteneinstellungen aufrufen
		edit_line_properties_page();
		return;
	}
	
	$siteparts = \DynCom\mysyde\common\classes\Siteparts::get();
	
	$sitepartcode = $siteparts[$row['main_sitepart_id']]['code'];
	$sitepartfolder = $siteparts[$row['main_sitepart_id']]['folder'];
	$_REQUEST['input_id'] = $row['main_sitepart_header_id'];
	
	$custom_action = 'edit_' . $sitepartcode;
	require_once(MODULE_PATH . $sitepartfolder . "/edit_" . $sitepartcode . ".inc.php");
}
?>