Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/install_ai_chatbot.php

<?php
/**
 * AI Chatbot System Installation Script
 * This script installs the AI chatbot with FAQ management, document learning, and system knowledge integration
 */

require_once 'config/config.php';

// Check if user is logged in and is superuser
if (!isLoggedIn() || !isSuperuser()) {
    die('Access denied. This script requires superuser access.');
}

$db = Database::getInstance()->getConnection();
$errors = [];
$success = [];

// Step 1: Create tables
try {
    $sql = file_get_contents(__DIR__ . '/sql/ai_chatbot.sql');
    
    // Split by semicolons and execute each statement
    $statements = array_filter(array_map('trim', explode(';', $sql)));
    
    foreach ($statements as $statement) {
        if (!empty($statement)) {
            $db->exec($statement);
        }
    }
    
    $success[] = "✅ Database tables created successfully";
} catch (Exception $e) {
    $errors[] = "❌ Database creation failed: " . $e->getMessage();
}

// Step 2: Create docs/chatbot directory
try {
    $chatbotDocsDir = __DIR__ . '/docs/chatbot';
    if (!is_dir($chatbotDocsDir)) {
        mkdir($chatbotDocsDir, 0777, true);
        $success[] = "✅ Created docs/chatbot directory";
    } else {
        $success[] = "✅ docs/chatbot directory already exists";
    }
} catch (Exception $e) {
    $errors[] = "❌ Failed to create docs/chatbot directory: " . $e->getMessage();
}

// Step 3: Register chatbot module
try {
    $stmt = $db->prepare("
        SELECT id FROM module_management WHERE module_name = 'AI Chatbot' LIMIT 1
    ");
    $stmt->execute();
    $existing = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($existing) {
        $success[] = "ℹ️ AI Chatbot module already registered";
    } else {
        $stmt = $db->prepare("
            INSERT INTO module_management (
                module_name, module_description, module_url, module_icon, 
                is_active, required_role, display_order, created_at
            ) VALUES (
                'AI Chatbot',
                'Manage AI chatbot, FAQs, and knowledge base',
                'modules/chatbot/index.php',
                'robot',
                1,
                'superuser',
                180,
                NOW()
            )
        ");
        $stmt->execute();
        $moduleId = $db->lastInsertId();
        
        // Add access level
        $stmt = $db->prepare("
            INSERT INTO module_access_levels (module_id, access_level, is_enabled)
            VALUES (?, 'superuser', 1)
        ");
        $stmt->execute([$moduleId]);
        
        $success[] = "✅ AI Chatbot module registered successfully";
    }
} catch (Exception $e) {
    $errors[] = "❌ Module registration failed: " . $e->getMessage();
}

// Step 4: Initialize system context
try {
    // Check if context already exists
    $stmt = $db->query("SELECT COUNT(*) as count FROM chatbot_context");
    $count = $stmt->fetch(PDO::FETCH_ASSOC)['count'];
    
    if ($count == 0) {
        $success[] = "✅ System context initialized (from SQL file)";
    } else {
        $success[] = "ℹ️ System context already exists ($count entries)";
    }
} catch (Exception $e) {
    $errors[] = "❌ Context initialization failed: " . $e->getMessage();
}

// Step 5: Verify installation
try {
    $tables = ['chatbot_faqs', 'chatbot_documents', 'chatbot_conversations', 'chatbot_messages', 'chatbot_context', 'chatbot_settings'];
    $allTablesExist = true;
    
    foreach ($tables as $table) {
        $stmt = $db->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() == 0) {
            $allTablesExist = false;
            $errors[] = "❌ Table $table not found";
        }
    }
    
    if ($allTablesExist) {
        $success[] = "✅ All database tables verified";
    }
} catch (Exception $e) {
    $errors[] = "❌ Verification failed: " . $e->getMessage();
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Chatbot Installation - <?php echo APP_NAME; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="bg-gray-50">
    <div class="container mx-auto px-4 py-8 max-w-4xl">
        <!-- Header -->
        <div class="text-center mb-8">
            <div class="inline-block p-4 bg-gradient-to-r from-blue-500 to-purple-600 rounded-full mb-4">
                <i class="fas fa-robot text-5xl text-white"></i>
            </div>
            <h1 class="text-4xl font-bold text-gray-800 mb-2">AI Chatbot System</h1>
            <p class="text-gray-600">Installation Complete</p>
        </div>

        <!-- Results -->
        <div class="bg-white rounded-xl shadow-lg p-8 mb-6">
            <?php if (!empty($success)): ?>
                <div class="mb-6">
                    <h2 class="text-xl font-bold text-green-700 mb-4">
                        <i class="fas fa-check-circle mr-2"></i>Success
                    </h2>
                    <ul class="space-y-2">
                        <?php foreach ($success as $msg): ?>
                            <li class="text-green-600"><?php echo $msg; ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <?php if (!empty($errors)): ?>
                <div class="mb-6">
                    <h2 class="text-xl font-bold text-red-700 mb-4">
                        <i class="fas fa-exclamation-triangle mr-2"></i>Errors
                    </h2>
                    <ul class="space-y-2">
                        <?php foreach ($errors as $error): ?>
                            <li class="text-red-600"><?php echo $error; ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>
        </div>

        <!-- Features -->
        <div class="bg-white rounded-xl shadow-lg p-8 mb-6">
            <h2 class="text-2xl font-bold text-gray-800 mb-6">
                <i class="fas fa-sparkles mr-2 text-yellow-500"></i>Features Installed
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="flex items-start gap-3 p-4 bg-blue-50 rounded-lg">
                    <i class="fas fa-question-circle text-2xl text-blue-600 mt-1"></i>
                    <div>
                        <h3 class="font-bold text-gray-800">FAQ Management</h3>
                        <p class="text-sm text-gray-600">Create and manage frequently asked questions</p>
                    </div>
                </div>
                <div class="flex items-start gap-3 p-4 bg-purple-50 rounded-lg">
                    <i class="fas fa-file-upload text-2xl text-purple-600 mt-1"></i>
                    <div>
                        <h3 class="font-bold text-gray-800">Document Learning</h3>
                        <p class="text-sm text-gray-600">Upload documents for AI knowledge base</p>
                    </div>
                </div>
                <div class="flex items-start gap-3 p-4 bg-green-50 rounded-lg">
                    <i class="fas fa-brain text-2xl text-green-600 mt-1"></i>
                    <div>
                        <h3 class="font-bold text-gray-800">System Learning</h3>
                        <p class="text-sm text-gray-600">AI learns from system context and interactions</p>
                    </div>
                </div>
                <div class="flex items-start gap-3 p-4 bg-orange-50 rounded-lg">
                    <i class="fas fa-comments text-2xl text-orange-600 mt-1"></i>
                    <div>
                        <h3 class="font-bold text-gray-800">Live Chat Widget</h3>
                        <p class="text-sm text-gray-600">Floating chatbot widget on all pages</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Quick Start Guide -->
        <div class="bg-gradient-to-r from-blue-500 to-purple-600 rounded-xl shadow-lg p-8 text-white mb-6">
            <h2 class="text-2xl font-bold mb-4">
                <i class="fas fa-rocket mr-2"></i>Quick Start Guide
            </h2>
            <ol class="space-y-3 text-blue-50">
                <li><strong>1. Go to AI Chatbot Management</strong> - Access from your dashboard sidebar</li>
                <li><strong>2. Add FAQs</strong> - Create frequently asked questions for common queries</li>
                <li><strong>3. Upload Documents</strong> - Add PDF, DOC, or TXT files to the knowledge base</li>
                <li><strong>4. Configure Settings</strong> - Customize chatbot appearance and behavior</li>
                <li><strong>5. Test the Chatbot</strong> - Look for the floating chat button on the bottom right</li>
            </ol>
        </div>

        <!-- Sample FAQs Info -->
        <div class="bg-white rounded-xl shadow-lg p-8 mb-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4">
                <i class="fas fa-info-circle mr-2 text-blue-500"></i>Sample Data Included
            </h2>
            <p class="text-gray-600 mb-4">The system comes with 8 sample FAQs covering:</p>
            <ul class="list-disc list-inside text-gray-600 space-y-2">
                <li>Membership registration and cards</li>
                <li>Event check-ins</li>
                <li>Member directory access</li>
                <li>Profile management</li>
                <li>Password reset</li>
                <li>Admin contact information</li>
                <li>System requirements</li>
            </ul>
        </div>

        <!-- Action Buttons -->
        <div class="flex justify-center gap-4">
            <a href="modules/chatbot/index.php" class="bg-gradient-to-r from-blue-500 to-blue-600 text-white px-8 py-3 rounded-lg font-bold hover:shadow-lg transition inline-flex items-center">
                <i class="fas fa-robot mr-2"></i>
                Open Chatbot Management
            </a>
            <a href="dashboard.php" class="bg-gray-600 text-white px-8 py-3 rounded-lg font-bold hover:bg-gray-700 transition inline-flex items-center">
                <i class="fas fa-home mr-2"></i>
                Go to Dashboard
            </a>
        </div>

        <!-- Cleanup Notice -->
        <div class="mt-8 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
            <p class="text-sm text-yellow-800">
                <i class="fas fa-info-circle mr-2"></i>
                <strong>Note:</strong> You can delete this installation file (install_ai_chatbot.php) after installation is complete.
            </p>
        </div>
    </div>
</body>
</html>

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