<?PHP
/**
 * This page offers the opportunity to edit static page elements: the side bar and the footer.
 */

/*
 * Require files and ensure environment.
 */

require_once __DIR__ . "/inc/functions.php";

ensureEnvironment();            // Ensure existence of system files.
$translations = loadLanguage(); // Load translations.
ensureBackendEnv();             // Ensure session is started etc.
$pages = loadPages();           // Load overview of pages.

/*
 * Load contents
 */

// Check for vars.
loadHttpToGlobals(["id", "task", "content"]);

define("targetFile", __DIR__ . "/../data/$id.htm");

if (!isset($id) or !in_array($id, ['footer', 'aside', 'banner'])) {
    echo printErrorPage($translations['specifyToEdit']); return;
}

// Read file contents if there is no new content sent by $_POST.

if (!isset($content)) {
    if (file_exists(targetFile)) {
        $content = file_get_contents(targetFile);
    }
    else $content = "";
}

// Set page title

$pageTitle = $translations['edit'] . ': ' . $translations[$id];

/*
 * Store if need be
 */
if (isset($task) and $task == "update") {

    file_put_contents(targetFile, $content, LOCK_EX);

    header('Location: editHTMLPage.php?id=' . $id);
    return;

}


/*
 * Output
 */

if (!isset($public)) $public = false;

echo printBackendHead($settings, $pageTitle, $pageTitle, $settings['logo']);
echo printBackendHeader($pageTitle, $translations["help$id"]);

echo '
<div id="mainWrapper">
';

echo printBackendNav($translations);

echo '
    <main class="noPadding">
    <form action="" method="POST">

        <textarea type="text" name="content" id="pageContent" class="mceEditor" placeholder="' . $translations['staticPageTitle'] . '" required>';
if (isset($content)) echo $content;
echo '
        </textarea>

        <span>
            <button type="submit">' . $translations['submit'] . '</button>
            ' . printHiddenInputs(["task" => "update", "id" => $id]) . '
        </span>

    </form>
    </main>
</div>


    <script src="./js/tinymce471/js/tinymce/tinymce.min.js"></script>
    <script type="text/javascript" src="./js/runTinyMCE.js"></script>

';

echo printBackendEnd();

?>
