<?php
date_default_timezone_set('Europe/Berlin');

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

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

switch ($action) {
    case 'save_preset':
        save_preset();
        break;
    case 'new_preset':
        require_once("infopoint_presets_cardform.inc.php");
        break;
    case 'edit_preset':
        edit_preset();
        break;
    case 'delete_preset':
        delete_preset();
        break;
	case 'delete_line_preset': 
		delete_line(); 
		break;
    case 'moveup_line_preset':
        moveup_line_preset();
		require_once("infoboard_presets_cardform.inc.php");
    	break;
    case 'movedown_line_preset':
        movedown_line_preset();
		require_once("infoboard_presets_cardform.inc.php");
    	break;
    default:
        require_once("infopoint_presets_listform.inc.php");
        break;
}

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

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

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

    if ($input_id <> '') {

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

        }

    } else {
        require_once("infopoint_presets_cardform.inc.php");
    }

}

function save_preset() {
    $translation    = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages       = array();
    $id             = $_REQUEST["input_id"];
    $default_active = $_REQUEST["input_default_active"];
    $header_id = "";

    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE main_infopoint_presets SET
                    description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "' ,
                    modified_date = '" . time() . "',
                    modified_user = '" . (int)$GLOBALS["admin_user"]['id'] . "',
                    main_infopoint_preset_id = '" . $_REQUEST["input_infopoint_preset"] . "'
                WHERE id = " . $_REQUEST["input_id"] . " LIMIT 1";
        $inserted = FALSE;
    } else {
        $hash = base64_encode(random_bytes(18));
        $query    = "INSERT INTO main_infopoint_presets (hash, description, modified_date, modified_user, main_infopoint_preset_id) VALUES (
                        '".$hash."',
                        '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"])."',
                        '".time()."',
                        '".(int)$GLOBALS["admin_user"]['id']."',
                        '".$_REQUEST["input_infopoint_preset"]."'
        )";
        $inserted = TRUE;
    }


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

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

    if (!$error) {
        @mysqli_query($GLOBALS['mysql_con'], $query);
 
        if ($inserted === TRUE) {
            $header_id = mysqli_insert_id($GLOBALS['mysql_con']);
            createMySydeLog('main_infoboard_presets', $header_id, 'INSERT');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
        } else {
            createMySydeLog('main_infoboard_presets', $_REQUEST['input_id'], 'UPDATE');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success2") . '</div>';
        }
        
    } else {
        $input_line["id"]          = $_REQUEST["input_id"];
        $input_line["description"] = $_REQUEST["input_description"];
    }
    require_once("infopoint_presets_cardform.inc.php");
}

function delete_preset() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM main_infopoint_presets WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        createMySydeLog('main_infopoint_presets', $_REQUEST['input_id'], 'DELETE');
    }
    require_once("infopoint_presets_listform.inc.php");
}

function show_preset_rows() {
 
	$main_layout_id = $GLOBALS["language"]["main_layout_id"];
	$input_preset_id = $_REQUEST["input_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 
				main_infopoint_preset_link.id,
                main_infopoint_pages.description,
                main_infopoint_pages.type,
                main_infopoint_preset_link.time,
				main_infopoint_preset_link.active,
				from_unixtime(main_infopoint_preset_link.modified_date, '%d.%m.%Y %H:%i:%s') AS aenderungs_datum, 
				main_admin_user.name AS username 
			FROM 
                main_infopoint_preset_link 
            LEFT JOIN 
                main_admin_user ON main_infopoint_preset_link.modified_user = main_admin_user.id 
            LEFT JOIN 
                main_infopoint_pages ON main_infopoint_preset_link.main_infopoint_page_id = main_infopoint_pages.id 
			WHERE 
            main_infopoint_preset_link.main_infopoint_preset_id = '" . $input_preset_id . "' 
			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("infopoint_time") . "</div></td>";
			echo "<td width=\"150\" align=\"left\"><div>" . $translation->get("action") . "</div></td>";
			
			echo "</tr>";
			
			echo "<tr>";
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo format_seperator("");
			echo "</tr>";
			
			echo "  </table>\n";
			
			get_real_page_link_id();
			
			echo "<ol class=\"linklist sortable contentlist\">";
			while ($row = @mysqli_fetch_array($result,1)) {
                $aktivText = ($row["active"] == 1 ? $translation->get("active") : $translation->get("inactive"));
                $activeClass = ($row["active"] == 1) ? 'active' : 'inactive';

                echo "<li class=\"mjs-nestedSortable-no-nesting ".strtolower($activeClass)."\" id=\"list_{$row['id']}\">";
                echo "<div  data-inputid=\"" . $row['id'] . "\" data-action=\"" . $dblClickAction . "\" data-inputname=\"input_id\" data-checked=\"{$checkedRow}\" class=\"linklist_content dd-handle\"><span class=\"disclose\"><span>&nbsp;</span></span>" . $row['description'] . "<div class=\"dd-icon\"></div>";
                echo "<div class=\"dd-code\">" . $row['type'] . "</div>";
                echo "<div class=\"dd-status\">" . $aktivText . "</div>";
                echo "<div class=\"dd-layout\">" . $row['time'] . "</div>";
				
                echo '<div class="dd-status" style="right:0 !important;">'.'<a data-formname="form_field_list" class="button" href="javascript:void(0);" onclick="setCurrentToolbarClicked(this);loadCard(\'copy_content_page\');"> <span class="material-icons">content_copy</span></a>'.'</div></div>';
                echo '</li>';
            }
			echo "</ol>";
			
			
		} else {
			echo "<div class=\"infobox\">" . $translation->get("no_rows_found") . "</div>";
		}
		
	}
}

function get_real_page_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 moveup_line_preset() {
	move_line_preset("<","DESC",$_REQUEST["input_id"]);
}

function movedown_line_preset() {
	move_line_preset(">","ASC",$_REQUEST["input_id"]);
}

function move_line_preset($way,$order,$field_id) {	
	$query = "SELECT * FROM main_infopoint_preset_link 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 main_infopoint_preset_link WHERE main_infopoint_preset_id = " . (int)$curr_line["main_infopoint_preset_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 main_infopoint_preset_link SET sorting = " . $change_line["sorting"] . " WHERE id = " . $curr_line["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		$query = "UPDATE main_infopoint_preset_link SET sorting = " . $curr_line["sorting"] . " WHERE id = " . $change_line["id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
		$query = "UPDATE main_infopoint_preset_link set modified_date = " . time() . ", modified_user = " . (int)$GLOBALS["admin_user"]['id'] . " where id = " . $curr_line["main_page_id"];
		@mysqli_query($GLOBALS['mysql_con'],$query);
	}
}

function delete_line() {
	$query = "DELETE FROM main_infopoint_preset_link WHERE id = '" . $_POST['input_id'] . "' LIMIT 1";
	@mysqli_query($GLOBALS['mysql_con'],$query);
	
	$query = "UPDATE main_infopoint_preset set modified_date = " . time() . ", modified_user = " . (int)$GLOBALS["admin_user"]['id'] . " where id = '" . (int)$_REQUEST["input_preset_id"]."'";
	@mysqli_query($GLOBALS['mysql_con'],$query);
	
	require_once("infopoint_presets_cardform.inc.php");
}