<?php
$messages = array();
$error = false;
if(isset($custom_action)) {
	$action = $custom_action;
} else {
	$action = $_REQUEST["action"];
}
switch($action) {
	
	case 'edit_intranet': edit_intranet(); break;
	case 'delete_intranet': delete_intranet(); break;
	case 'new_intranet': require_once("edit_intranet_cardform.inc.php"); break;
	case 'list_intranet_page': require_once("edit_intranet_cardform.inc.php"); break;
	case 'save_intranet': save_intranet(); break;
	
	// activities siteparts
	case 'new_line_support': new_support_cue_entry(); break;
	case 'edit_line_support': edit_support_cue_entry(); break;
    case 'delete_line_support': delete_support_cue_entry(); break;
    case 'save_line_support': save_support_cue_entry(); break; 
    case 'moveup_line_support': moveup_cue(); edit_support(); break;
	case 'movedown_line_support': movedown_cue(); edit_support(); break;
	
	// belege siteparts
	case 'new_line_order_support': new_support_order_entry(); break;
	case 'edit_line_order_support': edit_support_order_entry(); break;
    case 'delete_line_order_support': delete_support_order_entry(); break;
    case 'save_line_order_support': save_support_order_entry(); break; 
    case 'moveup_line_order_support': moveup_order(); edit_support(); break;
	case 'movedown_line_order_support': movedown_order(); edit_support(); break;
	
	// dataroom siteparts
	case 'new_line_dataroom_support': new_support_dataroom_entry(); break;
	case 'edit_line_dataroom_support': edit_support_dataroom_entry(); break;
    case 'delete_line_dataroom_support': delete_support_dataroom_entry(); break;
    case 'save_line_dataroom_support': save_support_dataroom_entry(); break; 
    case 'moveup_line_dataroom_support': moveup_dataroom(); edit_support(); break;
	case 'movedown_line_dataroom_support': movedown_dataroom(); edit_support(); break;
	
	case 'update_sortorder_support': update_sortorder_support(); break;
	case 'update_sortorder_order_support' : update_sortorder_order_support(); break;
	case 'update_sortorder_dataroom_support' : update_sortorder_dataroom_support(); break;
	
	default: require_once("edit_support_listform.inc.php"); break;
}

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

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

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

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

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

function delete_support() {
	if($_REQUEST["input_id"] <> '') {
		$query = "DELETE FROM main_support_sitepart WHERE id = " . $_REQUEST["input_id"] . " LIMIT 1";
		@mysqli_query($GLOBALS['mysql_con'],$query);
		
		// zuordnung von Seiten loeschen
		$query = "DELETE FROM main_page_link WHERE main_sitepart_id = 1 AND main_sitepart_header_id = " . $_REQUEST["input_id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		
		// alle eintraege von activites loeschen
		$query = "DELETE FROM support_cue_entry WHERE main_support_sitepart_id = " . $_REQUEST["main_support_sitepart_id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		
		update_sitepart_changes(15, $_REQUEST["input_id"], true);
	}
	require_once("edit_support_listform.inc.php");
}	

function save_intranet() {
	
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$input_id = 0;
	$error = false;
    $learning_certificate_type = ($_REQUEST["input_learning_certificate_type"] == "on") ? 1 : 0;
    $appointment_type = ($_REQUEST["input_appointment_type"] == "on") ? 1 : 0;
	$sitepart_items = (isset($_REQUEST['input_sitepart_items']) ? (int)$_REQUEST['input_sitepart_items'] : 0);
	
	if((int)$_REQUEST["input_id"] > 0) {
		$query = "UPDATE main_intranet_sitepart SET
				description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'],$_REQUEST['input_description']) . "',
				type = '" . $_REQUEST["input_type"] . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
				learning_certificate_type = " . $learning_certificate_type . ",
				sitepart_items = " . $sitepart_items . ",
				appointment_type = " . $appointment_type . "
				WHERE id = " . $_REQUEST["input_id"] . " 
			LIMIT 1";
		$inserted = false;
		$input_id = $_REQUEST["input_id"];
	} else {
		$query = "INSERT INTO main_intranet_sitepart
			(main_language_id, description, type, modified_date, modified_user, learning_certificate_type, sitepart_items, appointment_type)
			VALUES (
				'" . $GLOBALS["language"]['id'] . "',
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'],$_REQUEST['input_description']) . "',
				'" . $_REQUEST['input_type'] . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ",
				" . $learning_certificate_type . ",
				" . $sitepart_items . ",
				" . $appointment_type . "
			)";
		$inserted = 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("support_msg_success1") . '</div>';
		} else {
			$messages[] = '<div class="successbox">' . $translation->get("support_msg_success2") . '</div>';
		}
		
		if(is_page_edit()) {
			header('EDIT_ERROR: 1'); // verhindern, dass overlay geschlossen wird
			if(line_already_exists($_REQUEST["input_page_id"], 15, $input_id) === false) {
				assoc_sitepart(15, $input_id, false);
			}
			
			if($_REQUEST['force_close'] == 1) {
				require_once("edit_page_line_listform.inc.php");
				return;
			}
			
			intranet($input_id, $messages);
			
		} elseif(is_component_edit()) {
			header('EDIT_ERROR: 1'); // verhindern, dass overlay geschlossen wird
			if(line_already_exists($_REQUEST["input_component_id"], 15, $input_id) === false) {
				assoc_sitepart(15, $input_id, false);
			} 
			
			if($_REQUEST['force_close'] == 1) {
				edit_component("", $messages);
				return;
			}
			
			intranet($input_id, $messages);
		} else {
			intranet($input_id, $messages);
		}
		
	} else {
		header('EDIT_ERROR: 1');
		$input_support["type"] = $_REQUEST["input_type"];
		$input_support["id"] = $_REQUEST["input_id"];
		require_once("edit_intranet_cardform.inc.php");
	}
}

function new_support_cue_entry() {
	$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['main_support_sitepart_id'] = $collection_link['main_sitepart_header_id'];
		}
	} else {
		$input_line['main_support_sitepart_id'] = $_REQUEST['input_id'];
	}
	
	require_once("edit_support_activities_cardform.inc.php");
}

function edit_support_cue_entry($_id = "", $messages = array()) {
	if((int)$_id > 0) {
		$line_id = $_id;
	} else {
		$line_id = $_REQUEST["input_line_id"];
	}
	
	if($line_id <> '') {
		$query="SELECT * FROM support_cue_entry 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_support_activities_cardform.inc.php");
		}
	} else {
		require_once("edit_support_activities_cardform.inc.php");
	}
}
 

function delete_support_cue_entry(){
	
    if($_POST["input_line_id"] <> '') {
        $query = "Select * from support_cue_entry where id = ".$_POST['input_line_id'] . " limit 1";
        $result = mysqli_query($GLOBALS['mysql_con'],$query);
        if(mysqli_num_rows($result) > 0){
    		$query = "DELETE FROM support_cue_entry WHERE id = ".$_POST["input_line_id"]." LIMIT 1";
    		@mysqli_query($GLOBALS['mysql_con'],$query);
        } 
	} 
	edit_support();
}

 
function save_support_cue_entry(){
	
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$messages = array();
	$id = $_REQUEST["input_line_id"];
	$error = false;
	$header_id = $_REQUEST['input_id'];

	if($id <> '') {
		$inserted = false;
		$query = "
			UPDATE 
				support_cue_entry 
			SET 
				description = '".$_POST['input_description']."'
				, description_filter = '".$_POST['input_description_filter']."'
				, tooltip= '".$_POST['input_tooltip']."'
				, cue_type_id= '".$_POST['input_cue_type']."' 
				, owner= '".($_POST['input_owner'] == "on")."' 
				, statistic_group_filter= '".$_POST['input_statistic_group_filter']."' 
				, category_filter= '".$_POST['input_category_filter']."' 
				, language_code = '".$GLOBALS['language']['code']."'
			WHERE 
				id = " .$id;
	} else {
		$inserted = true;
		$sorting = mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'],"SELECT MAX(sorting)+1 AS 'newsorting' FROM support_cue_entry WHERE main_support_sitepart_id = " . $_POST["input_id"]));
		$sorting = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
	
		$query = "
			INSERT INTO 
				support_cue_entry 
			(
				description
				,description_filter				
				,info
				,main_navigation_has_sitepart_id
				,sorting
				,cue_type_id
				,tooltip
				,owner
				,statistic_group_filter
				,category_filter
				,main_support_sitepart_id
				,language_code
			) VALUES (
				'".$_POST['input_description']."'
				,'".$_POST['input_description']."'
				,'hochgeladen von ".$GLOBALS['admin_user']['name']." am ".date('d.m.Y H:i:s')."'
				,0
				,'".$sorting."'
				,'".$_POST['input_cue_type']."'
				,'".$_POST['input_tooltip']."'
				,'".($_POST['input_owner'] == "on")."'
				,'".$_POST['input_statistic_group_filter']."'
				,'".$_POST['input_category_filter']."'
				," . $_POST["input_id"] . "
				,'".$GLOBALS['language']['code']."'
			)";
	}
	
	if(($_REQUEST["input_description"] == '')) {
		$messages[] = "<div class=\"errorbox\">" . $translation->get("error_description") . "</div>\n";
		$error = true;
	}
		
	if($error === false) {
		mysqli_query($GLOBALS['mysql_con'],$query);
		
		if($inserted == true) {
			$id = mysqli_insert_id($GLOBALS['mysql_con']);
			$messages[] = '<div class="successbox">' . $translation->get("suppor_activity_msg_success1") . '</div>';
		} else {
			$messages[] = '<div class="successbox">' . $translation->get("suppor_activity_msg_success2") . '</div>';
		}
		
		if($_REQUEST['save_and_close'] == 1) {
			edit_support();
		} else {
			edit_support_cue_entry($id, $messages);
		}
		return; 
	} 
	
	// benoetigt fuer ajax calls
	if($error === true) {
		header('EDIT_ERROR: 1');	
	}
	
	$input_line = array();
	$input_line["id"] = $_REQUEST["input_line_id"];
	$input_line["main_support_sitepart_id"] = $header_id;
	$input_line["description"] = $_REQUEST['input_description'];
	$input_line["description_filter"] = $_REQUEST['input_description_filter'];
	$input_line["tooltip"] = $_REQUEST['input_tooltip'];
	$input_line["statistic_group_filter"] = $_REQUEST['input_statistic_group_filter'];
	$input_line["category_filter"] = $_REQUEST['input_category_filter'];
	$input_line["owner"] = ($_POST['input_owner'] == "on");
	$input_line["cue_type_id"] = $_REQUEST['input_cue_type'];
	
	require_once("edit_support_activities_cardform.inc.php");

}

function moveup_cue() {
	move_cue("<","DESC",$_POST["input_line_id"]);
}


function movedown_cue() {
	move_cue(">","ASC",$_POST["input_line_id"]);
}

function move_cue($way,$order,$cue_id) {	
	$query = "SELECT * FROM support_cue_entry WHERE id = " . $cue_id . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$curr_cue = @mysqli_fetch_array($result);
	}
	$query = "SELECT * FROM support_cue_entry WHERE main_support_sitepart_id = " . $curr_cue["main_support_sitepart_id"] . " AND sorting " . $way . " " . $curr_cue["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$change_cue = @mysqli_fetch_array($result);
	}
	if(($curr_cue["id"]<>'') && ($change_cue["id"]<>'')) {
		$query = "UPDATE support_cue_entry SET sorting = " . $change_cue["sorting"] . " WHERE id = " . $curr_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		$query = "UPDATE support_cue_entry SET sorting = " . $curr_cue["sorting"] . " WHERE id = " . $change_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
	}
	
}

function new_support_order_entry() {
	$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['main_support_sitepart_id'] = $collection_link['main_sitepart_header_id'];
		}
	} else {
		$input_line['main_support_sitepart_id'] = $_REQUEST['input_id'];
	}
	
	require_once("edit_support_order_cardform.inc.php");
}

function edit_support_order_entry($_id = "", $messages = array()) {
	if((int)$_id > 0) {
		$line_id = $_id;
	} else {
		$line_id = $_REQUEST["input_line_id"];
	}
	
	if($line_id <> '') {
		$query="SELECT * FROM support_order_entry 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_support_order_cardform.inc.php");
		}
	} else {
		require_once("edit_support_order_cardform.inc.php");
	}
}
 

function delete_support_order_entry(){
	
    if($_POST["input_line_id"] <> '') {
        $query = "Select * from support_order_entry where id = ".$_POST['input_line_id'] . " limit 1";
        $result = mysqli_query($GLOBALS['mysql_con'],$query);
        if(mysqli_num_rows($result) > 0){
    		$query = "DELETE FROM support_order_entry WHERE id = ".$_POST["input_line_id"]." LIMIT 1";
    		@mysqli_query($GLOBALS['mysql_con'],$query);
        } 
	} 
	edit_support();
}

 
function save_support_order_entry(){
	
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$messages = array();
	$id = $_REQUEST["input_line_id"];
	$error = false;
	$header_id = $_REQUEST['input_id'];

	if($id <> '') {
		$inserted = false;
		$query = "
			UPDATE 
				support_order_entry 
			SET 
				description = '".$_POST['input_description']."'
				, tooltip= '".$_POST['input_tooltip']."'
				, order_type_id= '".$_POST['input_order_type']."' 
				, owner= '".($_POST['input_owner'] == "on")."' 
			WHERE 
				id = " .$id;
	} else {
		$inserted = true;
		$sorting = mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'],"SELECT MAX(sorting)+1 AS 'newsorting' FROM support_order_entry WHERE main_support_sitepart_id = " . $_POST["input_id"]));
		$sorting = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
	
		$query = "
			INSERT INTO 
				support_order_entry 
			(
				description
				,info
				,sorting
				,order_type_id
				,tooltip
				,owner
				,main_support_sitepart_id
				,language_code
			) VALUES (
				'".$_POST['input_description']."'
				,'hochgeladen von ".$GLOBALS['admin_user']['name']." am ".date('d.m.Y H:i:s')."'
				,'".$sorting."'
				,'".$_POST['input_order_type']."'
				,'".$_POST['input_tooltip']."'
				,'".($_POST['input_owner'] == "on")."'
				,'" . $_POST["input_id"] . "',
				'" . $GLOBALS['language']['code'] . "'
			)";
	}
	
	if(($_REQUEST["input_description"] == '')) {
		$messages[] = "<div class=\"errorbox\">" . $translation->get("error_description") . "</div>\n";
		$error = true;
	}
		
	if($error === false) {
		mysqli_query($GLOBALS['mysql_con'],$query);
		
		if($inserted == true) {
			$id = mysqli_insert_id($GLOBALS['mysql_con']);
			$messages[] = '<div class="successbox">' . $translation->get("suppor_order_msg_success1") . '</div>';
		} else {
			$messages[] = '<div class="successbox">' . $translation->get("suppor_order_msg_success2") . '</div>';
		}
		
		if($_REQUEST['save_and_close'] == 1) {
			edit_support();
		} else {
			edit_support_order_entry($id, $messages);
		}
		return; 
	} 
	
	// benoetigt fuer ajax calls
	if($error === true) {
		header('EDIT_ERROR: 1');	
	}
	
	$input_line = array();
	$input_line["id"] = $_REQUEST["input_line_id"];
	$input_line["main_support_sitepart_id"] = $header_id;
	$input_line["description"] = $_REQUEST['input_description'];
	$input_line["tooltip"] = $_REQUEST['input_tooltip'];
	$input_line["owner"] = ($_POST['input_owner'] == "on");
	$input_line["order_type_id"] = $_REQUEST['input_order_type'];
	
	require_once("edit_support_order_cardform.inc.php");

}

function moveup_order() {
	move_order("<","DESC",$_POST["input_line_id"]);
}


function movedown_order() {
	move_order(">","ASC",$_POST["input_line_id"]);
}

function move_order($way,$order,$cue_id) {	
	$query = "SELECT * FROM support_order_entry WHERE id = " . $cue_id . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$curr_cue = @mysqli_fetch_array($result);
	}
	$query = "SELECT * FROM support_order_entry WHERE main_support_sitepart_id = " . $curr_cue["main_support_sitepart_id"] . " AND sorting " . $way . " " . $curr_cue["sorting"] . " ORDER BY sorting " . $order . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$change_cue = @mysqli_fetch_array($result);
	}
	if(($curr_cue["id"]<>'') && ($change_cue["id"]<>'')) {
		$query = "UPDATE support_order_entry SET sorting = " . $change_cue["sorting"] . " WHERE id = " . $curr_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		$query = "UPDATE support_order_entry SET sorting = " . $curr_cue["sorting"] . " WHERE id = " . $change_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
	}
	
}


function new_support_dataroom_entry() {
	$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['main_support_sitepart_id'] = $collection_link['main_sitepart_header_id'];
		}
	} else {
		$input_line['main_support_sitepart_id'] = $_REQUEST['input_id'];
	}
	
	require_once("edit_support_dataroom_cardform.inc.php");
}

function edit_support_dataroom_entry($_id = "", $messages = array()) {
	if((int)$_id > 0) {
		$line_id = $_id;
	} else {
		$line_id = $_REQUEST["input_line_id"];
	}
	
	if($line_id <> '') {
		$query="SELECT * FROM support_dataroom_entry 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_support_dataroom_cardform.inc.php");
		}
	} else {
		require_once("edit_support_dataroom_cardform.inc.php");
	}
}
 

function delete_support_dataroom_entry(){
	
    if($_POST["input_line_id"] <> '') {
        $query = "Select * from support_dataroom_entry where id = ".$_POST['input_line_id'] . " limit 1";
        $result = mysqli_query($GLOBALS['mysql_con'],$query);
        if(mysqli_num_rows($result) > 0){
    		$query = "DELETE FROM support_dataroom_entry WHERE id = ".$_POST["input_line_id"]." LIMIT 1";
    		@mysqli_query($GLOBALS['mysql_con'],$query);
        } 
	} 
	edit_support();
}

 
function save_support_dataroom_entry(){
	
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$messages = array();
	$id = $_REQUEST["input_line_id"];
	$error = false;
	$header_id = $_REQUEST['input_id'];

	if($id <> '') {
		$inserted = false;
		$query = "
			UPDATE 
				support_dataroom_entry 
			SET 
				description = '".$_POST['input_description']."'
				, tooltip= '".$_POST['input_tooltip']."'
				, dataroom_line_no= ".$_POST['input_dataroom_line_no']." 
				, file_list_type= ".$_POST['input_file_list_type']." 
			WHERE 
				id = " .$id;
	} else {
		$inserted = true;
		$sorting = mysqli_fetch_array(@mysqli_query($GLOBALS['mysql_con'],"SELECT MAX(sorting)+1 AS 'newsorting' FROM support_dataroom_entry WHERE main_support_sitepart_id = " . $_POST["input_id"]));
		$sorting = ($sorting["newsorting"] <> "") ? $sorting = $sorting["newsorting"] : $sorting = 1;
	
		$query = "
			INSERT INTO 
				support_dataroom_entry 
			(
				description
				,info
				,sorting
				,dataroom_line_no
				,file_list_type
				,tooltip
				,main_support_sitepart_id
				,language_code
			) VALUES (
				'".$_POST['input_description']."'
				,'hochgeladen von ".$GLOBALS['admin_user']['name']." am ".date('d.m.Y H:i:s')."'
				,'".$sorting."'
				,'".$_POST['input_dataroom_line_no']."'
				,'".$_POST['input_file_list_type']."'
				,'".$_POST['input_tooltip']."'
				,'" . $_POST["input_id"] . "'
				, '" . $GLOBALS['language']['code'] . "'
			)";
	}
	
	if(($_REQUEST["input_description"] == '')) {
		$messages[] = "<div class=\"errorbox\">" . $translation->get("error_description") . "</div>\n";
		$error = true;
	}
		
	if($error === false) {
		mysqli_query($GLOBALS['mysql_con'],$query);
		
		if($inserted == true) {
			$id = mysqli_insert_id($GLOBALS['mysql_con']);
			$messages[] = '<div class="successbox">' . $translation->get("suppor_dataroom_msg_success1") . '</div>';
		} else {
			$messages[] = '<div class="successbox">' . $translation->get("suppor_dataroom_msg_success2") . '</div>';
		}
		
		if($_REQUEST['save_and_close'] == 1) {
			edit_support();
		} else {
			edit_support_dataroom_entry($id, $messages);
		}
		return; 
	} 
	
	// benoetigt fuer ajax calls
	if($error === true) {
		header('EDIT_ERROR: 1');	
	}
	
	$input_line = array();
	$input_line["id"] = $_REQUEST["input_line_id"];
	$input_line["main_support_sitepart_id"] = $header_id;
	$input_line["description"] = $_REQUEST['input_description'];
	$input_line["tooltip"] = $_REQUEST['input_tooltip'];
	$input_line["dataroom_line_no"] = $_REQUEST['input_dataroom_line_no'];
	$input_line["file_list_type"] = $_REQUEST['input_file_list_type'];
	
	require_once("edit_support_dataroom_cardform.inc.php");

}

function moveup_dataroom() {
	move_dataroom("<","DESC",$_POST["input_line_id"]);
}


function movedown_dataroom() {
	move_dataroom(">","ASC",$_POST["input_line_id"]);
}

function move_dataroom($way,$dataroom,$cue_id) {	
	$query = "SELECT * FROM support_dataroom_entry WHERE id = " . $cue_id . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$curr_cue = @mysqli_fetch_array($result);
	}
	$query = "SELECT * FROM support_dataroom_entry WHERE main_support_sitepart_id = " . $curr_cue["main_support_sitepart_id"] . " AND sorting " . $way . " " . $curr_cue["sorting"] . " order BY sorting " . $dataroom . " LIMIT 1";
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	if(@mysqli_num_rows($result) == 1) {
		$change_cue = @mysqli_fetch_array($result);
	}
	if(($curr_cue["id"]<>'') && ($change_cue["id"]<>'')) {
		$query = "UPDATE support_dataroom_entry SET sorting = " . $change_cue["sorting"] . " WHERE id = " . $curr_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		$query = "UPDATE support_dataroom_entry SET sorting = " . $curr_cue["sorting"] . " WHERE id = " . $change_cue["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
	}
	
}



function create_optionfield($table,$optionfield_name,$table_field_key,$table_field_description,$selected_value = '',$caption,$sql_options = null,$optionfield_options = null){
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$query = "select $table_field_key,$table_field_description from $table";	
	if ($sql_options != null){
		 
		//Nur Aktive
		if($sql_options['aktiv'])
			$query .= " where aktiv = '1'";
			
		//Sortierung	
		if($sql_options['order_by'] != ""){
			$order_by = $sql_options['order_by'];
			if (substr($order_by,0,1) == '+'){
				$order_by = substr($order_by,1,strlen($order_by));
				$query .= " order by $order_by desc";	
			}else{
				$query .= " order by $order_by ";	
			}
		}
	}
	
	$select_values = array();
	$select_names = array();
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	
	while($OptionResult = @mysqli_fetch_array($result)){
		
		$select_values[] = $OptionResult[0];
		$select_names[] = $OptionResult[1];
	}
	input_select($caption,$optionfield_name,$select_values,$select_names,$selected_value);
	
}

/**
 * Erzeugt eine selectbox zum auswählen von Seiten, die eine Ticketliste enthalten
 * 
 * @access public
 * @param string $name Label
 * @param string $selectname name der selectbox
 * @return void
 */
function create_page_with_ticketlist_select($support_sitepart_id, $support_type_id, $name,$selectname = "input_choose", $input_line, $disabled=false) {
	
	if((int)$support_type_id <= 0) {
		return;
	}
	
	$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
	$siteparts = \DynCom\mysyde\common\classes\Siteparts::get();
	$disabledText = "";
	if($disabled === true) {
		$disabledText = 'disabled="disabled"';
	}
	
	$supportIds = array();
	$query = "SELECT id FROM main_support_sitepart WHERE type = " . $support_type_id . " and main_language_id = " . $GLOBALS["language"]['id'];
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	while ($row = @mysqli_fetch_array($result,1)) {
		$supportIds[] = $row['id'];
	}
	
	if(count($supportIds) == 0) {
		echo "<p class=\"bold info\">Kein Supportsitepart vom Typ $support_type_id angelegt.</p>";
		return;
	}
	
	$pageIds = array();
	$query = "SELECT main_page_id FROM main_page_link WHERE main_sitepart_header_id IN (" . join(",", $supportIds) . ") and main_sitepart_id = " . $support_sitepart_id;
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	while ($row = @mysqli_fetch_array($result,1)) {
		$pageIds[] = $row['main_page_id'];
	}
	
	if(count($pageIds) == 0) {
		echo "<p class=\"bold info\">Supportsitepart vom Typ $support_type_id wurde noch keiner Seite zugeordnet.</p>";
		return;
	}
	
	$pages = array();
	$query = "SELECT * FROM main_navigation WHERE forward_page_id IN (" . join(",", $pageIds) . ") AND forward_type = 1 AND main_language_id = " . $GLOBALS["language"]['id'];
	$result = @mysqli_query($GLOBALS['mysql_con'],$query);
	while ($row = @mysqli_fetch_array($result,1)) {
		$pages[$row['id']] = $row;
	}
	
	if(count($pages) == 0) {
		echo "<p class=\"bold info\">Keine Seite mit dem Supporttyp $support_type_id gefunden.</p>";
		return;
	}
	
	echo "<div class=\"label\"><label for=\"" . $selectname . "\">" . $name . "</label></div>";
	echo "<div class=\"input\"><select " . $disabledText . " class=\"bigselect\" name=\"" . $selectname . "\" id=\"" . $selectname . "\">";

	foreach($pages as $pageId => $pageData) {
		$selected = '';
		
		if($pageId == $input_line[$selectname]) {
			$selected = 'selected="selected"';
		}
		echo "<option " . $selected . " value=\"" . $pageId . "\">" . $pageData['menu_name'] . "</option>";
	}
	
	echo "</select></div>";
}