Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/chatbot/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/chatbot/faqs.php

<?php
require_once '../../config/config.php';
checkLogin();

$pageTitle = "Manage FAQs - " . APP_NAME;

// Check access
if (!isSuperuser()) {
    redirect('../../dashboard.php');
}

$db = Database::getInstance()->getConnection();
$success = '';
$error = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add_faq'])) {
        $question = trim($_POST['question'] ?? '');
        $answer = trim($_POST['answer'] ?? '');
        $category = trim($_POST['category'] ?? 'General');
        $keywords = trim($_POST['keywords'] ?? '');
        $priority = (int)($_POST['priority'] ?? 0);
        
        if (empty($question) || empty($answer)) {
            $error = "Question and answer are required";
        } else {
            $stmt = $db->prepare("
                INSERT INTO chatbot_faqs (question, answer, category, keywords, priority, created_by)
                VALUES (:question, :answer, :category, :keywords, :priority, :user_id)
            ");
            if ($stmt->execute([
                'question' => $question,
                'answer' => $answer,
                'category' => $category,
                'keywords' => $keywords,
                'priority' => $priority,
                'user_id' => $_SESSION['user_id']
            ])) {
                $success = "FAQ added successfully!";
            } else {
                $error = "Failed to add FAQ";
            }
        }
    } elseif (isset($_POST['update_faq'])) {
        $id = (int)$_POST['faq_id'];
        $question = trim($_POST['question'] ?? '');
        $answer = trim($_POST['answer'] ?? '');
        $category = trim($_POST['category'] ?? 'General');
        $keywords = trim($_POST['keywords'] ?? '');
        $priority = (int)($_POST['priority'] ?? 0);
        
        $stmt = $db->prepare("
            UPDATE chatbot_faqs 
            SET question = :question, answer = :answer, category = :category, 
                keywords = :keywords, priority = :priority
            WHERE id = :id
        ");
        if ($stmt->execute([
            'question' => $question,
            'answer' => $answer,
            'category' => $category,
            'keywords' => $keywords,
            'priority' => $priority,
            'id' => $id
        ])) {
            $success = "FAQ updated successfully!";
        } else {
            $error = "Failed to update FAQ";
        }
    } elseif (isset($_POST['toggle_faq'])) {
        $id = (int)$_POST['faq_id'];
        $stmt = $db->prepare("UPDATE chatbot_faqs SET is_active = NOT is_active WHERE id = :id");
        $stmt->execute(['id' => $id]);
        $success = "FAQ status updated!";
    } elseif (isset($_POST['delete_faq'])) {
        $id = (int)$_POST['faq_id'];
        $stmt = $db->prepare("DELETE FROM chatbot_faqs WHERE id = :id");
        if ($stmt->execute(['id' => $id])) {
            $success = "FAQ deleted successfully!";
        } else {
            $error = "Failed to delete FAQ";
        }
    }
}

// Get all FAQs
$stmt = $db->query("
    SELECT id, question, answer, category, keywords, priority, is_active, view_count, 
           helpful_count, not_helpful_count, created_at
    FROM chatbot_faqs
    ORDER BY priority DESC, created_at DESC
");
$faqs = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get categories
$stmt = $db->query("SELECT DISTINCT category FROM chatbot_faqs ORDER BY category");
$categories = $stmt->fetchAll(PDO::FETCH_COLUMN);

include '../../includes/header.php';
include '../../includes/sidebar.php';
?>

<main class="main-content md:ml-64 pt-16">
    <div class="container mx-auto px-4 py-8">
        <!-- Header -->
        <div class="mb-6 flex justify-between items-center">
            <div>
                <h1 class="text-3xl font-bold text-gray-800">
                    <i class="fas fa-question-circle mr-2 text-blue-500"></i>Manage FAQs
                </h1>
                <p class="text-gray-600 mt-2">Create and manage frequently asked questions</p>
            </div>
            <a href="index.php" class="bg-gray-600 text-white px-4 py-2 rounded-lg hover:bg-gray-700 transition">
                <i class="fas fa-arrow-left mr-2"></i>Back
            </a>
        </div>

        <?php if ($success): ?>
            <div id="successMessage" class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6 flex items-center justify-between">
                <div><i class="fas fa-check-circle mr-2"></i><?php echo $success; ?></div>
                <button onclick="dismissMessage('successMessage')" class="text-green-700 hover:text-green-900">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div id="errorMessage" class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6 flex items-center justify-between">
                <div><i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?></div>
                <button onclick="dismissMessage('errorMessage')" class="text-red-700 hover:text-red-900">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        <?php endif; ?>

        <!-- Add FAQ Form -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h3 class="text-lg font-bold text-gray-800 mb-4">
                <i class="fas fa-plus-circle mr-2"></i>Add New FAQ
            </h3>
            <form method="POST" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Category</label>
                        <input type="text" name="category" list="categories" 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                               placeholder="e.g., Membership, Events, Technical">
                        <datalist id="categories">
                            <?php foreach ($categories as $cat): ?>
                                <option value="<?php echo htmlspecialchars($cat); ?>">
                            <?php endforeach; ?>
                        </datalist>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Priority (0-10)</label>
                        <input type="number" name="priority" min="0" max="10" value="5"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Question *</label>
                    <input type="text" name="question" required
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="Enter the question">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Answer *</label>
                    <textarea name="answer" rows="4" required
                              class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                              placeholder="Enter the answer"></textarea>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Keywords (comma-separated)</label>
                    <input type="text" name="keywords"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="e.g., register, signup, join, create account">
                </div>
                <button type="submit" name="add_faq" class="bg-gradient-to-r from-blue-500 to-blue-600 text-white px-6 py-2 rounded-lg hover:shadow-lg transition">
                    <i class="fas fa-plus mr-2"></i>Add FAQ
                </button>
            </form>
        </div>

        <!-- FAQs List -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h3 class="text-lg font-bold text-gray-800 mb-4">
                <i class="fas fa-list mr-2"></i>All FAQs (<?php echo count($faqs); ?>)
            </h3>
            <div class="space-y-4">
                <?php foreach ($faqs as $faq): ?>
                    <div class="border border-gray-200 rounded-lg p-4 <?php echo $faq['is_active'] ? '' : 'bg-gray-50 opacity-60'; ?>">
                        <div class="flex justify-between items-start">
                            <div class="flex-1">
                                <div class="flex items-center gap-2 mb-2">
                                    <span class="px-2 py-1 bg-blue-100 text-blue-700 text-xs rounded"><?php echo htmlspecialchars($faq['category']); ?></span>
                                    <span class="px-2 py-1 bg-purple-100 text-purple-700 text-xs rounded">Priority: <?php echo $faq['priority']; ?></span>
                                    <?php if ($faq['is_active']): ?>
                                        <span class="px-2 py-1 bg-green-100 text-green-700 text-xs rounded">Active</span>
                                    <?php else: ?>
                                        <span class="px-2 py-1 bg-gray-100 text-gray-700 text-xs rounded">Inactive</span>
                                    <?php endif; ?>
                                    <span class="text-xs text-gray-500">Views: <?php echo $faq['view_count']; ?></span>
                                </div>
                                <h4 class="font-bold text-gray-800 mb-2"><?php echo htmlspecialchars($faq['question']); ?></h4>
                                <p class="text-gray-600 text-sm mb-2"><?php echo nl2br(htmlspecialchars($faq['answer'])); ?></p>
                                <?php if ($faq['keywords']): ?>
                                    <p class="text-xs text-gray-500">Keywords: <?php echo htmlspecialchars($faq['keywords']); ?></p>
                                <?php endif; ?>
                            </div>
                            <div class="flex gap-2 ml-4">
                                <form method="POST" class="inline">
                                    <input type="hidden" name="faq_id" value="<?php echo $faq['id']; ?>">
                                    <button type="submit" name="toggle_faq" 
                                            class="text-gray-600 hover:text-blue-600 transition" title="Toggle Status">
                                        <i class="fas fa-power-off"></i>
                                    </button>
                                </form>
                                <button onclick="editFAQ(<?php echo $faq['id']; ?>)" 
                                        class="text-gray-600 hover:text-green-600 transition" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <form method="POST" class="inline" onsubmit="return confirm('Delete this FAQ?')">
                                    <input type="hidden" name="faq_id" value="<?php echo $faq['id']; ?>">
                                    <button type="submit" name="delete_faq" 
                                            class="text-gray-600 hover:text-red-600 transition" title="Delete">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</main>

<?php include '../../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists