<?PHP
/**
 * This page offers the opportunity to edit static pages.
 */

/*
 * 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", "title", "content", "higher", "public"]);
if (isset($task) and !isset($public)) $public = false;

// Load from ID

if (isset($id) and is_numeric($id) and $id) {

    $existent = json_decode(file_get_contents(__DIR__ . "/../data/static/$id.json"), true);
    foreach (["title", "content", "higher", "public"] as $var) {
        if (!isset($$var) and isset($existent[$var])) $$var = $existent[$var];
    }

}

// Set page title

if (!isset($id)) $pageTitle = $translations['newPage'];
else $pageTitle = $translations['edit'] . ': ' . $title;

/*
 * Store if need be
 */
if (isset($task)) {

    if ($task == "update") {

        if (!isset($title)) $title = "";
        if (!isset($content)) $content = "";
        if (!isset($higher) or !is_numeric($higher)) $higher = "0";
        if (!isset($id)) $id = "0";

        $targetID = storePage($pages, ["title" => (string)$title, "content" => (string)$content, "higher" => $higher, "public" => (bool)$public], (int)$id);
        header('Location: page.php?id=' . $targetID);

    }

    else if ($task == "delete") {

        if (!isset($id)) {
            echo printErrorPage($translations['specifyToDelete']); return;
        }

        unlink(__DIR__ . "/../data/static/$id.json");
        generateStaticPgCaches();
        header('Location: pages.php');
    }

}


/*
 * Output
 */

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

echo printBackendHead($settings, $pageTitle, $pageTitle, $settings['logo']);
echo printBackendHeader($pageTitle, $translations['helpSinglePage']);

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

echo printBackendNav($translations);

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

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

        <aside>

            <div id="staticPageOptions">

                <!-- Public or draft -->

                <span class="labelLine">
                    <label for="public">' . $translations['staticPagePublic'] . '</label>
                    ' . generateHelpToolTip("helpPublic", $translations['staticPagePublic'], $translations['helpStaticPagePublic']) . '
                </span>

                <span>
                    <label class="switch">
                        <input name="public" id="public" type="checkbox"'; if (isset($public) and $public) echo " checked"; echo '>
                        <span class="slider round"></span>
                    </label>
                </span>

                <!-- Determine hierarchy -->

                <span class="labelLine">
                    <label for="higher">' . $translations['staticPageHigher'] . '</label>
                    ' . generateHelpToolTip("helpHigher", $translations['staticPageHigher'], $translations['helpStaticPageHigher']) . '
                </span>

                <span>
                    <select name="higher" id="higher">
                        <option value="0"></option>';
foreach ($pages as $page) {
    echo '<option value="' . $page['id'] . '"';
    if (isset($higher) and $page['id'] == $higher) echo " selected";
    echo '>' . $page['title'] . '</option>';
}
echo '
                    </select>
                </span>

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

            </div>

            <div id="pageTools">

                <h3>' . $translations['tools'] . '</h3>

                <div id="embedGenerator">

                </div>
';

if (isset($id)) {
    echo '
                <div>
                    <a href="../?id=' . $id . '&preview=1" class="buttonLike">' . $translations['preview'] . '</a>';

    // Check if there is a page subordinate to the current one.
    $hasSubordinate = false;
    foreach ($pages as $page) {
        if ($page['higher'] == $id) $hasSubordinate = true;
    }

    if (!$hasSubordinate) echo '
                    <a href="page.php?id=' . $id . '&task=delete" class="buttonLike">' . $translations['delete'] . '</a>';
    echo '
                </div>
    ';
}

echo '
            </div>

        </aside>

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


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

';

echo printBackendEnd();

?>
