<?php
date_default_timezone_set('Europe/Berlin');
require_once("infopoint_config.inc.php");

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

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

switch ($action) {
    case 'save_page':
        save_page();
        break;
    case 'new_page':
        require_once("infopoint_pages_cardform.inc.php");
        break;
    case 'edit_page':
        edit_page();
        break;
    case 'delete_page':
        delete_page();
        break;
    case 'update_sortorder_faq':
        update_sortorder_faq();
    break;

    default:
        require_once("infopoint_pages_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_page( $_id = "", $messages = array() ) {

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

    if ($input_id <> '') {

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

        }

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

}

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

    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE main_infopoint_pages SET
                    description = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"]) . "' ,
                    type = '".$type."',
                    modified_date = '" . time() . "',
                    modified_user = '" . (int)$GLOBALS["admin_user"]['id'] . "'
                WHERE id = " . $_REQUEST["input_id"] . " LIMIT 1";
        $inserted = FALSE;
    } else {
        $query    = "INSERT INTO main_infopoint_pages (description, type, type_id, image, modified_date, modified_user) VALUES (
                        '".mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_description"])."',
                        '".$type."',
                        '0',
                        '0',
                        '".time()."',
                        '".(int)$GLOBALS["admin_user"]['id']."'
        )";
        $inserted = TRUE;
    }

    switch ($type) {
        case 'textcontent':
            save_textcontent($id);
            break;
        case 'image':
            saveImg($id);
            break;
        
        default:
            # code...
            break;
    }

    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_pages', $header_id, 'INSERT');
            $messages[] = '<div class="successbox">' . $translation->get("collection_group_msg_success1") . '</div>';
        } else {
            createMySydeLog('main_infoboard_pages', $_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_pages_cardform.inc.php");
}

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

function saveImg($page_id){
    $filekey = 'input_collection_link_' . $page_id;

    $error = FALSE;
    if (!empty($_FILES[$filekey]['name'])) {
        $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;
        }
    }

    if (!empty($_FILES[$filekey]['name']) && $error === FALSE) {

        $saveFilename = $input_id . "_" . (int)$page_id . "_" . cleanFilename($_FILES[$filekey]['name']);

        delete_infopoint_image($saveFilename);

        move_uploaded_file($_FILES[$filekey]['tmp_name'], PATH_ORIGINAL_PICTURE_INFOPOINT . $saveFilename);

        $postValue = $saveFilename;

        $query = "UPDATE main_infopoint_pages SET 
        type = 'image',
        image = '".$postValue."'
        WHERE id = " . $page_id . " LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}

function delete_infopoint_image($filename) {
    if (file_exists(PATH_ORIGINAL_PICTURE_INFOPOINT . $filename) && !empty($filename)) {
        unlink(PATH_ORIGINAL_PICTURE_INFOPOINT . $filename);
    }
}


function create_infopoint_input_field($page_id){
    $query = "SELECT * FROM main_infopoint_pages WHERE id = '" . $page_id . "'";
    $result =  @mysqli_query($GLOBALS['mysql_con'], $query);
    $row = @mysqli_fetch_array($result);
    switch ($row['type']) {
        case 'image':
            if ($row['image'] != "") {
                echo "<table cellpadding=\"0\" cellspacing=\"0\">";
                echo "<tr><td>";
                echo "<img class='infopoint_icon' src=\"" . PATH_ICON_INFOPOINT . $row['image'] . "?time=" . time() . "\" /></td><td><input type=\"checkbox\" name=\"delete_collection_image_" . $page_id . "\" />&nbsp;Bild entfernen</td>";
                echo "</tr>";
                echo "</table>";
            }
            echo "<input id=\"input_collection_link_" . $page_id . "\" class=\"code\" type=\"file\" name=\"input_collection_link_" . $page_id . "\">";
            break;
        case 'textcontent':
            $query = "SELECT * FROM textcontent_header WHERE id = '" . $row['type_id'] . "'";
            $result =  @mysqli_query($GLOBALS['mysql_con'], $query);
            $headerData = @mysqli_fetch_array($result);
            echo '<input type="hidden" name="infopoint_modul_input_'.$fieldSetup['id'].'" value="textcontent" />';
            show_ck_editor($headerData['content'], "dc", 'layout/frontend/' . $GLOBALS["layout"]["code"] . '/dist/css/fck.css', 'layout/frontend/' . $GLOBALS["layout"]["code"] . '/fckstyles.xml', "module_input_textcontent_text_" . $page_id);
            break;
        default:
            # code...
            break;
    }
}

function save_textcontent($page_id){
    $query = "SELECT * FROM main_infopoint_pages WHERE id = '" . $page_id . "'";
    $result =  @mysqli_query($GLOBALS['mysql_con'], $query);
    $infopoint_row = @mysqli_fetch_array($result);

    if (is_array($infopoint_row)) {
        // update
        $query = "UPDATE textcontent_header SET
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . ",
				content = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['module_input_textcontent_text_' . $page_id]) . "', 
				type = 0
				WHERE id = " . $infopoint_row['type_id'] . " 
			LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
    } else {
        // insert
        $query = "INSERT INTO textcontent_header
			(main_language_id, description, modified_date, modified_user, type, content, collection_header)
			VALUES (
				'" . $GLOBALS["language"]['id'] . "',
				'',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ",
				0,
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['module_input_textcontent_text_' . $page_id]) . "',
				1
			)";

        @mysqli_query($GLOBALS['mysql_con'], $query);
        $header_id = mysqli_insert_id($GLOBALS['mysql_con']);
    }
    $query = "UPDATE main_infopoint_pages SET 
    type = 'textcontent',
    type_id = ".$header_id."
    WHERE id = " . $page_id . " LIMIT 1";
    @mysqli_query($GLOBALS['mysql_con'], $query);
}