Sindbad~EG File Manager

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

<?php
require_once __DIR__ . '/config/config.php';

// Check if user is logged in and is a superuser
if (!isset($_SESSION['user_id']) || ($_SESSION['access_level'] ?? '') != 'superuser') {
    header('Location: login.php');
    exit;
}

$page_title = "Install Directory Module";
$installation_complete = false;
$errors = [];

// Get database connection
$db = Database::getInstance()->getConnection();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // 1. Create standalone_directory and core_management tables
        // Execute SQL file statement by statement to avoid multi-query issues
        $sql = file_get_contents(__DIR__ . '/sql/directory.sql');
        
        // Split SQL into individual statements
        $statements = array_filter(array_map('trim', explode(';', $sql)));
        
        foreach ($statements as $statement) {
            if (!empty($statement)) {
                $db->exec($statement);
            }
        }
        
        // 2. Register Members Directory module
        $stmt = $db->prepare("SELECT id FROM module_management WHERE module_name = 'Members Directory'");
        $stmt->execute();
        $membersResult = $stmt->fetch();
        
        if (!$membersResult) {
            $stmt = $db->prepare("INSERT INTO module_management (
                module_name, 
                module_description, 
                module_icon, 
                module_url, 
                required_role, 
                is_active, 
                display_order,
                category
            ) VALUES (
                'Members Directory',
                'Search and view members directory',
                'address-book',
                'modules/directory/index.php',
                'user',
                1,
                81,
                'Core Management'
            )");
            $stmt->execute();
            $membersModuleId = $db->lastInsertId();
            
            // Add access levels for Members Directory
            $accessLevels = ['superuser', 'area', 'district', 'assembly'];
            foreach ($accessLevels as $level) {
                $stmt = $db->prepare("INSERT INTO module_access_levels (module_id, access_level, is_enabled) VALUES (?, ?, 1)");
                $stmt->execute([$membersModuleId, $level]);
            }
        }
        
        // 3. Register Standalone Directory module
        $stmt = $db->prepare("SELECT id FROM module_management WHERE module_name = 'Standalone Directory'");
        $stmt->execute();
        $standaloneResult = $stmt->fetch();
        
        if (!$standaloneResult) {
            $stmt = $db->prepare("INSERT INTO module_management (
                module_name, 
                module_description, 
                module_icon, 
                module_url, 
                required_role, 
                is_active, 
                display_order,
                category
            ) VALUES (
                'Standalone Directory',
                'Manage standalone directory entries',
                'id-card',
                'modules/directory/standalone.php',
                'user',
                1,
                82,
                'Core Management'
            )");
            $stmt->execute();
            $standaloneModuleId = $db->lastInsertId();
            
            // Add access levels for Standalone Directory
            $accessLevels = ['superuser', 'area', 'district', 'assembly'];
            foreach ($accessLevels as $level) {
                $stmt = $db->prepare("INSERT INTO module_access_levels (module_id, access_level, is_enabled) VALUES (?, ?, 1)");
                $stmt->execute([$standaloneModuleId, $level]);
            }
        }
        
        // 4. Check if core management entry exists
        $stmt = $db->prepare("SELECT id FROM core_management WHERE module_name = 'Directory'");
        $stmt->execute();
        $result = $stmt->fetch();
        
        if (!$result) {
            // 5. Add to core_management for enable/disable functionality
            $stmt = $db->prepare("INSERT INTO core_management (
                module_name,
                module_description,
                is_enabled,
                display_order,
                category
            ) VALUES (
                'Directory',
                'Members and Standalone Directory Management',
                1,
                80,
                'Core Management'
            )");
            $stmt->execute();
        }
        
        $installation_complete = true;
        
    } catch (Exception $e) {
        $errors[] = $e->getMessage();
    }
}

include 'includes/header.php';
?>

<div class="min-h-screen bg-gray-50 py-8">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="gradient-bg rounded-xl shadow-lg p-8 text-white">
                <h1 class="text-3xl font-bold mb-2">Directory Module Installation</h1>
                <p class="text-blue-100">Install the comprehensive Directory management system</p>
            </div>
        </div>

        <?php if ($installation_complete): ?>
            <!-- Success Message -->
            <div class="bg-white rounded-xl shadow-md p-8 text-center">
                <div class="mb-6">
                    <svg class="w-20 h-20 mx-auto text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                </div>
                <h2 class="text-2xl font-bold text-gray-800 mb-4">Installation Complete!</h2>
                <p class="text-gray-600 mb-6">The Directory module has been successfully installed.</p>
                
                <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6 text-left">
                    <h3 class="font-semibold text-blue-800 mb-2">What's Installed:</h3>
                    <ul class="list-disc list-inside space-y-1 text-sm text-blue-700">
                        <li>Created <code>standalone_directory</code> database table</li>
                        <li>Registered Directory module in module management</li>
                        <li>Added Directory to core management settings</li>
                        <li>Created DirectoryManager class for data operations</li>
                        <li>Set up Members Directory pages (search & list view)</li>
                        <li>Set up Standalone Directory pages (CRUD & bulk upload)</li>
                    </ul>
                </div>
                
                <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-6 text-left">
                    <h3 class="font-semibold text-green-800 mb-2">Module Features:</h3>
                    <ul class="list-disc list-inside space-y-1 text-sm text-green-700">
                        <li><strong>Members Directory:</strong> Search and view existing members with filters</li>
                        <li><strong>Members List:</strong> Complete list with district/assembly filters, sort, export (CSV/PDF), print</li>
                        <li><strong>Standalone Directory:</strong> Manage non-member directory entries</li>
                        <li><strong>CRUD Operations:</strong> Add, edit, delete standalone entries</li>
                        <li><strong>Bulk Upload:</strong> Import multiple entries via CSV or Excel</li>
                        <li><strong>Export Features:</strong> Export to CSV, print preview</li>
                        <li><strong>Advanced Filters:</strong> Filter by district and assembly</li>
                        <li><strong>Core Management:</strong> Enable/disable module from settings</li>
                    </ul>
                </div>
                
                <div class="flex gap-4 justify-center">
                    <a href="modules/directory/index.php" class="btn-gradient px-8 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300 inline-flex items-center">
                        <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
                        </svg>
                        Go to Members Directory
                    </a>
                    <a href="modules/directory/standalone.php" class="btn-gradient-orange px-8 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300 inline-flex items-center">
                        <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/>
                        </svg>
                        Go to Standalone Directory
                    </a>
                    <a href="dashboard.php" class="px-8 py-3 border border-gray-300 rounded-lg font-semibold text-gray-700 hover:bg-gray-50 transition-all duration-300">
                        Back to Dashboard
                    </a>
                </div>
            </div>
            
        <?php elseif (!empty($errors)): ?>
            <!-- Error Messages -->
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="mb-6 text-center">
                    <svg class="w-20 h-20 mx-auto text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                </div>
                <h2 class="text-2xl font-bold text-gray-800 mb-4 text-center">Installation Failed</h2>
                <div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-6">
                    <h3 class="font-semibold text-red-800 mb-2">Errors:</h3>
                    <ul class="list-disc list-inside space-y-1 text-sm text-red-700">
                        <?php foreach ($errors as $error): ?>
                            <li><?= htmlspecialchars($error) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <div class="text-center">
                    <a href="install_directory_module.php" class="btn-gradient px-8 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300">
                        Try Again
                    </a>
                </div>
            </div>
            
        <?php else: ?>
            <!-- Installation Form -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="px-6 py-4 bg-gradient-primary text-white">
                    <h2 class="text-xl font-bold">Installation Overview</h2>
                </div>
                <div class="p-8">
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-800 mb-3">What will be installed:</h3>
                        <div class="space-y-3">
                            <div class="flex items-start">
                                <svg class="w-6 h-6 text-green-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                </svg>
                                <div>
                                    <strong>Database Table:</strong>
                                    <p class="text-sm text-gray-600">Creates <code>standalone_directory</code> table for managing non-member directory entries</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <svg class="w-6 h-6 text-green-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                </svg>
                                <div>
                                    <strong>Module Registration:</strong>
                                    <p class="text-sm text-gray-600">Registers Directory module in module management system</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <svg class="w-6 h-6 text-green-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                </svg>
                                <div>
                                    <strong>Core Management Integration:</strong>
                                    <p class="text-sm text-gray-600">Adds module to core management for enable/disable control</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-800 mb-3">Module Components:</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div class="border border-gray-200 rounded-lg p-4">
                                <h4 class="font-semibold text-primary mb-2">Members Directory</h4>
                                <ul class="text-sm text-gray-600 space-y-1">
                                    <li>• Quick search interface</li>
                                    <li>• Full members list view</li>
                                    <li>• District/Assembly filters</li>
                                    <li>• CSV export & print</li>
                                </ul>
                            </div>
                            <div class="border border-gray-200 rounded-lg p-4">
                                <h4 class="font-semibold text-secondary mb-2">Standalone Directory</h4>
                                <ul class="text-sm text-gray-600 space-y-1">
                                    <li>• Add/Edit/Delete entries</li>
                                    <li>• Bulk CSV/Excel upload</li>
                                    <li>• Advanced filtering</li>
                                    <li>• Export & print features</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4 mb-6">
                        <h3 class="font-semibold text-yellow-800 mb-2">Important Notes:</h3>
                        <ul class="list-disc list-inside space-y-1 text-sm text-yellow-700">
                            <li>This installation is safe and can be run multiple times</li>
                            <li>Existing data will not be affected</li>
                            <li>Module will be immediately available after installation</li>
                            <li>You can enable/disable the module from Settings > Core Management</li>
                        </ul>
                    </div>
                    
                    <form method="POST" action="" class="text-center">
                        <button type="submit" class="btn-gradient px-12 py-4 rounded-lg font-semibold hover:shadow-lg transition-all duration-300 text-lg">
                            <svg class="w-6 h-6 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                            </svg>
                            Install Directory Module
                        </button>
                    </form>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

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

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