scc24php/admin/edit-content.php

174 lines
6.3 KiB
PHP

<?php
require('../.hta_config/env.php');
require('../.hta_header.php');
require('../.hta_admin_header.php');
$current_date_time = date("Y-m-d H:i:s");
?>
<style>
#createNewPage {
padding: 10px 50px;
margin: 10px auto;
background-color: #ffddcc;
border-radius: 20px;
width: 100%;
max-width: 60%;
}
/* For mobile screens */
@media (max-width: 768px) {
#createNewPage {
max-width: 100%;
width: 100%;
padding: 10px 20px; /* Optional: adjust padding for smaller screens */
}
}
#createNewPage > form > input, select {
width:100%;
/* margin-top:10px; */
margin-bottom:10px;
padding: 10px;
border-radius: 6px;
}
#createNewPage > form > textarea{
width:100%;
margin:10px;
height:200px;
}
#createButton{
background-color: #402517;
color: #FFFFFF;
border-radius: 10px;
padding: 10px;
outline: none;
border: none;
display: flex;
}
.pell-content{
background: #fff;
border-bottom: 1px solid black;
}
</style>
<link rel="stylesheet" type="text/css" href="/assets/pell.css">
<script src="/assets/pell.js"></script>
<?php
$fileName = $_GET['slug'] . '.html';
$contentType = $_GET['type'];
$getID = $_GET['id'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$conn = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("UPDATE scc24 SET `type` = :type, `title` = :title, `slug` = :slug, `content` = :content WHERE `id` = :getID");
$stmt->bindParam(':getID', $getID);
$stmt->bindParam(':type', $_POST['type']);
$stmt->bindParam(':title', $_POST['title']);
$stmt->bindParam(':slug', $_POST['slug']);
$stmt->bindParam(':content', $_POST['content']);
$stmt->execute();
// echo "<script>window.location.href='edit-news-list';</script>";
if ($contentType === 'page') {
$contentFolder = '/';
} elseif ($contentType === 'notice') {
$contentFolder = '/notice/';
} else {
$contentFolder = ''; // Default case, you can adjust this as needed
}
$filePath = $_SERVER['DOCUMENT_ROOT'] . $contentFolder . $fileName;
// echo $filePath;
if (file_exists($filePath)) {
if (unlink($filePath)) {
// echo "File deleted successfully.";
} else {
// echo "Error: Unable to delete the file.";
}
} else {
// echo "File does not exist.";
}
} catch (PDOException $e) {
echo "Err: " . $e->getMessage();
}
}
?>
<?php
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT * FROM scc24 WHERE `id` = :id");
$stmt->bindParam(':id', $getID);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
echo ' <div id="createNewPage" >
<h3 style="text-align: center; margin: 10px 0;">Edit New Page or Notice</h3>
<form id="createNewPageForm" method="POST" enctype="multipart/form-data">
<label for="type">Select Type</label>';
?>
<select name="type" id="type" style="padding: 10px;">
<?php
// echo $row['type'];
$typeOptions = ['notice', 'tender', 'page', 'documents', 'events'];
foreach($typeOptions as $type){
$selected = ($row['type'] === $type) ? 'selected' : '';
echo '<option value="'.$type.'" '.$selected.'>'.ucfirst($type).'</option>';
}
?>
</select><br/>
<?php
echo '
<label for="title">Page Title:</label><br> <input id="title" name="title" type="text" value="'.$row['title'].'" />
<label for="slug">Page unique slug:</label><br> <input id="slug" name="slug" type="text" value="'.$row['slug'].'" />
<label for="editor">Content:</label>
<div style="color: #000;" id="editor"></div>
<textarea name="content" id="content" rows="" col="0" class="border-2 border-primary p-2 rounded" style="visibility: hidden; height: 10px;"></textarea>
<button type="submit" id="createButton" type="button" > &nbsp; Save &nbsp; </button>
</form>
</div>';
echo '<script>
window.onload = function() {
getTypeValue(); // Call the function on window load
};
const pell = window.pell;
const editor = document.getElementById("editor");
let contentTextArea = document.getElementById(\'content\');
// Initialize pell editor and assign the return value to editorInstance
let editorInstance = pell.init({
element: editor,
onChange: (html) => {
console.log(html);
contentTextArea.value = html;
}
});
// Function to update editor content dynamically
function updateEditorContent(newContent) {
editorInstance.content.innerHTML = newContent;
// Trigger change event to update contentTextArea
contentTextArea.value = newContent;
}
// Example usage:
updateEditorContent(' . json_encode($row['content']) . ');
</script>';
};
// exit();
} catch (PDOException $e) {
echo json_encode(array('success' => false, 'message' => 'Database error: ' . $e->getMessage()));
}
?>
<?php
// echo $fileName;
// echo $contentType;
require('../.hta_footer.php');
?>