<?PHP
/**
 * Start page of the backend.
 * Offers a dashboard.
 *
 * @author Joshua Ramon Enslin <joshua@jrenslin.de>
 */

/*
 * 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 data.
 */

// Check for vars.
loadHttpToGlobals(["task", "startPage", "pageTitle", "logo", "url", "mdVersion", "mdImgFolder", "cacheRefreshInterval", "maxFileSize", "defaultLang"]);

if (isset($task) and $task == "update") { // Adding new users.

    if (isset($defaultLang) and !in_array("$defaultLang.php", scanDirConts(__DIR__ . "/translations"))) {
        $_SESSION["editHistory"] = ["changesAborted", $translations['languageUnavailable']];
        header('Location: settings.php');
    }

    // Ensure that URLs end with a trailing slash.
    if (isset($mdVersion)) $mdVersion = rtrim($mdVersion, "/") . "/";
    if (isset($mdImgFolder)) $mdImgFolder = rtrim($mdImgFolder, "/") . "/";

    foreach (["startPage", "pageTitle", "logo", "url", "mdVersion", "mdImgFolder", "cacheRefreshInterval", "maxFileSize", "defaultLang"] as $var) {
        if (isset($$var)) $settings[$var] = $$var;
    }

    // Store the users array.
    file_put_contents(__DIR__ . "/../data/settings.json", json_encode($settings), LOCK_EX);

    $_SESSION["editHistory"] = ["changesStored", $translations['settingsUpdated'] . " $username"];
    header('Location: settings.php');
    return;

}

/*
 * Output
 */

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

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

echo printBackendNav($translations);

echo '
    <main>

    <section id="listUsers">

        <form action="" method="POST">
        <table class="obj_cha_maintable">

            <!-- Start page -->
            <tr>
                <th><label for="startPage">' . $translations['startPage'] . '</label></th>
                <td>
                    <select id="startPage" name="startPage" placeholder="' . $translations['startPage']. '">';
$publicPages = loadPublicPages();
foreach ($publicPages as $page) {
    echo '<option value="' . $page['id'] . '"';
    if (isset($settings['startPage']) and $page['id'] == $settings['startPage']) echo ' selected';
    echo '>' . $page['title'] . '</option>';
}
unset($publicPages);
echo '
 value="'.$settings['url'].'" required />
                    </select>
                </td>
                <td>' . generateHelpToolTip("helpStartPage", $translations['startPage'], $translations['helpStartPage']) . '</td>
            </tr>

            <!-- Page Title -->
            <tr>
                <th><label for="pageTitle">' . $translations['settingsPageTitle'] . '</label></th>
                <td><input type="text" id="pageTitle" name="pageTitle" placeholder="' . $translations['settingsPageTitle']. '" value="'.$settings['pageTitle'].'" required /></td>
                <td>' . generateHelpToolTip("helpSettingsPageTitle", $translations['settingsPageTitle'], $translations['helpSettingsPageTitle']) . '</td>
            </tr>

            <!-- Logo -->
            <tr>
                <th><label for="logo">' . $translations['logo'] . '</label></th>
                <td><input type="text" id="logo" name="logo" placeholder="' . $translations['logo']. '" value="'.$settings['logo'].'" required /></td>
                <td>' . generateHelpToolTip("helpLogo", $translations['logo'], $translations['helpLogo']) . '</td>
            </tr>

            <!-- URL -->
            <tr>
                <th><label for="url">' . $translations['urlAbbr'] . '</label></th>
                <td><input type="url" id="url" name="url" placeholder="' . $translations['url']. '" value="'.$settings['url'].'" required /></td>
                <td>' . generateHelpToolTip("helpURL", $translations['url'], $translations['helpURL']) . '</td>
            </tr>

            <!-- MD Version -->
            <tr>
                <th><label for="mdVersion">' . $translations['mdVersion'] . '</label></th>
                <td><input type="url" id="mdVersion" name="mdVersion" placeholder="' . $translations['mdVersion']. '" value="'.$settings['mdVersion'].'" required /></td>
                <td>' . generateHelpToolTip("helpMDVersion", $translations['mdVersion'], $translations['helpMDVersion']) . '</td>
            </tr>

            <!-- MD Image Folder -->
            <tr>
                <th><label for="mdImgFolder">' . $translations['mdImgFolder'] . '</label></th>
                <td><input type="url" id="mdImgFolder" name="mdImgFolder" placeholder="' . $translations['mdImgFolder']. '" value="'.$settings['mdImgFolder'].'" required /></td>
                <td>' . generateHelpToolTip("helpMDimgFolder", $translations['mdImgFolder'], $translations['helpMDimgFolder']) . '</td>
            </tr>

            <!-- Refresh Interval -->
            <tr>
                <th><label for="cacheRefreshInterval">' . $translations['cacheRefreshInterval'] . '</label></th>
                <td><input type="number" id="cacheRefreshInterval" name="cacheRefreshInterval" placeholder="' . $translations['cacheRefreshInterval']. '" value="'.$settings['cacheRefreshInterval'].'" required /></td>
                <td>' . generateHelpToolTip("helpCacheRefreshInterval", $translations['cacheRefreshInterval'], $translations['helpCacheRefreshInterval']) . '</td>
            </tr>

            <!-- Max Upload Size -->
            <tr>
                <th><label for="maxFileSize">' . $translations['maxFileSize'] . '</label></th>
                <td><input type="number" id="maxFileSize" name="maxFileSize" placeholder="' . $translations['maxFileSize']. '" value="'.$settings['maxFileSize'].'" required /></td>
                <td>' . generateHelpToolTip("helpMaxFileSize", $translations['maxFileSize'], $translations['helpMaxFileSize']) . '</td>
            </tr>

            <tr>
                <th><label for="language">' . $translations['language'] . '</label></th>
                <td>
                    <select name="defaultLang" id="language">
                    ';
foreach (scanDirConts(__DIR__ . "/translations") as $lang) {
    $lang = pathinfo($lang)['filename'];
    echo '<option value="' . $lang . '"';
    if ($settings['defaultLang'] == $lang) echo ' selected';
    echo '>' . $lang . '</option>';
}
echo '
                    </select>
                </td>
                <td>' . generateHelpToolTip("helpLanguage", $translations['language'], $translations['helpLanguage']) . '</td>
            </tr>

            <tr>
                <th></th>
                <td><button type="submit">' . $translations['submit'] . '</button></td>
                <td>
                    ' . printHiddenInputs(['task' => 'update'], 16) . '
                </td>
            </tr>

        </table>
        </form>

    </section>

    </main>
</div>';

echo printBackendEnd();

?>
