DSP Project first push, date: 29/01/2026
This commit is contained in:
218
admin/manage_faq.php
Normal file
218
admin/manage_faq.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
// Start session and include necessary files
|
||||
session_start();
|
||||
require_once '../config.php';
|
||||
require_once '../includes/auth.php';
|
||||
require_once '../classes/Faq.php';
|
||||
require_once '../classes/User.php'; // To get person_id for the Faq class
|
||||
|
||||
// Redirect if not logged in or not a DAC Staff
|
||||
redirect_if_not_logged_in('../index.php');
|
||||
redirect_if_not_role('DAC Staff', '../index.php');
|
||||
|
||||
// Initialize Faq class
|
||||
$faq = new Faq($pdo);
|
||||
$user = new User($pdo); // To get the person_id of the logged-in user
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
$id = $_GET['id'] ?? null;
|
||||
|
||||
// Handle form submissions
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action_type = $_POST['action_type'] ?? ''; // 'add' or 'edit' or 'delete'
|
||||
$title = trim($_POST['title'] ?? ''); // This is the question
|
||||
$description = trim($_POST['description'] ?? ''); // This is the answer
|
||||
$faq_id = $_POST['faq_id'] ?? null;
|
||||
|
||||
// Get the person_id of the currently logged-in user
|
||||
$currentUserDetails = $user->getUserDetails($_SESSION['user_id']);
|
||||
$fkisp_id_of = $currentUserDetails['fkisp_id_of'];
|
||||
|
||||
if ($action_type === 'delete') {
|
||||
if ($faq_id) {
|
||||
try {
|
||||
$faq->deleteFaq($faq_id);
|
||||
set_message('FAQ entry deleted successfully!', 'success');
|
||||
} catch (Exception $e) {
|
||||
set_message('Error deleting FAQ entry: ' . $e->getMessage(), 'danger');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (empty($title) || empty($description)) {
|
||||
set_message('Question and Answer cannot be empty.', 'danger');
|
||||
} else {
|
||||
try {
|
||||
if ($action_type === 'add') {
|
||||
$faq->addFaq($title, $description, $_SESSION['user_id'], $fkisp_id_of);
|
||||
set_message('FAQ entry added successfully!', 'success');
|
||||
} elseif ($action_type === 'edit' && $faq_id) {
|
||||
$faq->updateFaq($faq_id, $title, $description, $_SESSION['user_id'], $fkisp_id_of);
|
||||
set_message('FAQ entry updated successfully!', 'success');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
set_message('Error: ' . $e->getMessage(), 'danger');
|
||||
}
|
||||
}
|
||||
}
|
||||
header('Location: manage_faq.php');
|
||||
exit();
|
||||
}
|
||||
|
||||
// Fetch FAQ entries for display
|
||||
$faqEntries = $faq->getAllFaqs();
|
||||
|
||||
// Prepare data for editing if action is 'edit'
|
||||
$editFaq = null;
|
||||
if ($action === 'edit' && $id) {
|
||||
$editFaq = $faq->getFaqById($id);
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<!-- Header -->
|
||||
<?php
|
||||
// Include header file for admin pages
|
||||
include_once("../includes/header_admin.php");
|
||||
?>
|
||||
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<!-- Sidebar -->
|
||||
<?php
|
||||
// Include header file for admin pages
|
||||
include_once("../includes/nav_admin.php");
|
||||
?>
|
||||
|
||||
<!-- Main Content -->
|
||||
<div class="main-content">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4 rounded-3">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">Manage FAQ</a>
|
||||
<div class="d-flex">
|
||||
<span class="navbar-text me-3">
|
||||
Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<?php
|
||||
// Display session messages
|
||||
if (isset($_SESSION['message'])) {
|
||||
echo '<div class="alert alert-' . $_SESSION['message_type'] . ' alert-dismissible fade show rounded" role="alert">' . htmlspecialchars($_SESSION['message']) . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
|
||||
unset($_SESSION['message']);
|
||||
unset($_SESSION['message_type']);
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="card mb-4">
|
||||
<div class="card-header bg-primary text-white">
|
||||
<h5 class="mb-0"><?php echo $editFaq ? 'Edit' : 'Add New'; ?> FAQ Entry</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form action="manage_faq.php" method="POST">
|
||||
<input type="hidden" name="action_type" value="<?php echo $editFaq ? 'edit' : 'add'; ?>">
|
||||
<?php if ($editFaq): ?>
|
||||
<input type="hidden" name="faq_id" value="<?php echo htmlspecialchars($editFaq['pkdspsfaq_id']); ?>">
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="title" class="form-label">Question</label>
|
||||
<input type="text" class="form-control rounded" id="title" name="title" value="<?php echo htmlspecialchars($editFaq['dspsfaq_title_en'] ?? ''); ?>" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label d-flex justify-content-between align-items-center">
|
||||
<span>Answer</span>
|
||||
</label>
|
||||
<textarea class="form-control rounded-3" id="description" name="description" rows="6" required><?php echo htmlspecialchars($editFaq['dspsfaq_description'] ?? ''); ?></textarea>
|
||||
<div class="form-text">Rich formatting appears on the public FAQ page—emphasise key steps and link to related resources.</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary rounded">
|
||||
<i class="fas fa-<?php echo $editFaq ? 'save' : 'plus'; ?> me-2"></i> <?php echo $editFaq ? 'Update' : 'Add'; ?> FAQ
|
||||
</button>
|
||||
<?php if ($editFaq): ?>
|
||||
<a href="manage_faq.php" class="btn btn-secondary rounded ms-2">Cancel Edit</a>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header text-white" style="background-color: #28a745;">
|
||||
<h5 class="mb-0">All FAQ Entries</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Question</th>
|
||||
<th>Answer</th>
|
||||
<th>Reg. Date</th>
|
||||
<th>Mod. Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (!empty($faqEntries)): ?>
|
||||
<?php foreach ($faqEntries as $entry): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($entry['pkdspsfaq_id']); ?></td>
|
||||
<td><?php echo htmlspecialchars($entry['dspsfaq_title_en']); ?></td>
|
||||
<td><?php echo htmlspecialchars(substr($entry['dspsfaq_description'], 0, 100)) . (strlen($entry['dspsfaq_description']) > 100 ? '...' : ''); ?></td>
|
||||
<td><?php echo htmlspecialchars($entry['dspsfaq_reg_datetime']); ?></td>
|
||||
<td><?php echo htmlspecialchars($entry['dspsfaq_mod_datetime']); ?></td>
|
||||
<td>
|
||||
<a href="manage_faq.php?action=edit&id=<?php echo htmlspecialchars($entry['pkdspsfaq_id']); ?>" class="btn btn-sm btn-warning rounded btn-action">
|
||||
<i class="fas fa-edit"></i>
|
||||
</a>
|
||||
<form action="manage_faq.php" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this FAQ entry?');">
|
||||
<input type="hidden" name="action_type" value="delete">
|
||||
<input type="hidden" name="faq_id" value="<?php echo htmlspecialchars($entry['pkdspsfaq_id']); ?>">
|
||||
<button type="submit" class="btn btn-sm btn-danger rounded btn-action">
|
||||
<i class="fas fa-trash-alt"></i>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<tr>
|
||||
<td colspan="6" class="text-center">No FAQ entries found.</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
<?php
|
||||
// Include Footer file for owner pages
|
||||
include_once("../includes/footer_admin.php");
|
||||
?>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@ckeditor/ckeditor5-build-classic@38.1.1/build/ckeditor.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var textarea = document.querySelector('#description');
|
||||
if (textarea && typeof ClassicEditor !== 'undefined') {
|
||||
ClassicEditor
|
||||
.create(textarea, {
|
||||
toolbar: [
|
||||
'heading','|','bold','italic','underline','bulletedList','numberedList','blockQuote',
|
||||
'|','link','insertTable','undo','redo'
|
||||
]
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.error('Failed to initialise rich text editor', error);
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user