<?php
include_once "Functions/get_content.php"; // needs to be included, to be able to use getPageContent()
?>
<?php
// ***************** if user is logged in, show CMS content *****************
if (isset($_SESSION["email"])) {
$options = array('welcome', 'contact', 'T3sT');
$pageExists = false;
$pageData = [];
if (!isset($_POST['DATA_pages'])) {
$_POST['DATA_pages'] = $options[0];
}
if (in_array($_POST['DATA_pages'], $options)) {
$pageName = $_POST['DATA_pages'];
$pageData = getPageContent($dbc, $pageName);
$pageExists = !empty($pageData);
}
if (isset($_POST['BUTTON_save'])) {
$updatedContent = $_POST['DATA_websiteContent'];
$sql = "UPDATE `page` SET `content` = ? WHERE `pageName` = ?";
$stmt = mysqli_prepare($dbc, $sql);
mysqli_stmt_bind_param($stmt, "ss", $updatedContent, $pageName);
mysqli_stmt_execute($stmt);
$isSuccessMessage = true;
$errorMessage = "Changes saved successfully!";
$pageData = getPageContent($dbc, $pageName);
}
echo '<h2>Welcome to the CMS</h2>';
echo '<form method="POST" id="form-web-page-contents">
<label for="sel-pages">Please select a page to edit:</label>
<select name="DATA_pages" id="sel-pages" onchange="this.form.submit();">';
foreach ($options as $option) {
echo '<option value="' . $option . '"' . ($_POST['DATA_pages'] == $option ? ' selected' : '') . '>' . $option . '</option>';
}
echo '</select><br>';
if ($pageExists) {
echo '<textarea name="DATA_websiteContent" rows="20" cols="70">' . ($pageData['content'] ?? '') . '</textarea>';
echo '<br><button type="submit" name="BUTTON_save">Save</button>';
} else {
$errorMessage = "The selected page does not exist.";
}
echo '</form>';
} else {
// ***************** if user is not logged in, show the login form *****************
?>
<form method="post" action="index.php?page=cms" class="form-login">
<p>
<label for="email">E-Mail<span style="color:red;"> *</span></label>
<input id="email" name="DATA_email" placeholder="E-Mail" type="email" required minlength="5" maxlength="50"
pattern="[a-zA-z\d._%+-]+@[a-zA-Z\d.-]+\.[a-z]{2,}$" title="Please write your email in this format: sign@sign.domain">
</p>
<p>
<label for="password">Password<span style="color:red;"> *</span></label>
<input id="password" name="DATA_password" placeholder="Password" type="password" required minlength="4" maxlength="20">
</p>
<input class="btn-login" type="submit" name="BUTTON_send" value="Login">
</form>
<?php
}
?>