Sindbad~EG File Manager

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

<?php
/**
 * Officership Module Installation Script
 * Creates tables, registers module, and sets up permissions
 */

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

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

$installation_log = [];
$errors = [];
$success = false;

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

// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $installation_log[] = "Installation process started...";
    try {
        // Start transaction
        $pdo->beginTransaction();
        
        // 1. Create ordination table
        $installation_log[] = "Creating ordination table...";
        $sql = "CREATE TABLE IF NOT EXISTS `ordination` (
            `id` INT(11) NOT NULL AUTO_INCREMENT,
            `member_id` INT(11) NOT NULL,
            `date_ordained` DATE NOT NULL,
            `ordained_by` VARCHAR(255) NOT NULL,
            `venue_ordained` VARCHAR(255) DEFAULT NULL,
            `district_ordained_in` INT(11) DEFAULT NULL,
            `area_ordained_in` INT(11) DEFAULT NULL,
            `assembly_ordained_in` INT(11) DEFAULT NULL,
            `card_no` VARCHAR(50) DEFAULT NULL,
            `ordination_title` VARCHAR(50) NOT NULL,
            `certificate_no` VARCHAR(100) DEFAULT NULL,
            `is_suspended` TINYINT(1) DEFAULT 0,
            `suspended_by` VARCHAR(255) DEFAULT NULL,
            `suspended_date` DATE DEFAULT NULL,
            `suspension_reason` TEXT DEFAULT NULL,
            `reactivated_date` DATE DEFAULT NULL,
            `notes` TEXT DEFAULT NULL,
            `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by` INT(11) DEFAULT NULL,
            `updated_by` INT(11) DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_district` (`district_ordained_in`),
            KEY `idx_ordination_title` (`ordination_title`),
            KEY `idx_is_suspended` (`is_suspended`),
            CONSTRAINT `fk_ordination_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        $pdo->exec($sql);
        $installation_log[] = "✓ Ordination table created successfully";
        
        // 2. Create retiree_details table
        $installation_log[] = "Creating retiree_details table...";
        $sql = "CREATE TABLE IF NOT EXISTS `retiree_details` (
            `id` INT(11) NOT NULL AUTO_INCREMENT,
            `member_id` INT(11) NOT NULL,
            `ordination_id` INT(11) DEFAULT NULL,
            `date_retired` DATE NOT NULL,
            `retired_by` VARCHAR(255) NOT NULL,
            `venue_retired` VARCHAR(255) DEFAULT NULL,
            `district_retired_in` INT(11) DEFAULT NULL,
            `area_retired_in` INT(11) DEFAULT NULL,
            `assembly_retired_in` INT(11) DEFAULT NULL,
            `years_of_service` INT(11) DEFAULT NULL,
            `retirement_reason` VARCHAR(255) DEFAULT NULL,
            `retirement_type` ENUM('voluntary', 'mandatory_age', 'health', 'other') DEFAULT 'voluntary',
            `benefits_status` VARCHAR(100) DEFAULT NULL,
            `certificate_issued` TINYINT(1) DEFAULT 0,
            `certificate_no` VARCHAR(100) DEFAULT NULL,
            `notes` TEXT DEFAULT NULL,
            `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by` INT(11) DEFAULT NULL,
            `updated_by` INT(11) DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_ordination_id` (`ordination_id`),
            KEY `idx_retirement_type` (`retirement_type`),
            CONSTRAINT `fk_retiree_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE,
            CONSTRAINT `fk_retiree_ordination` FOREIGN KEY (`ordination_id`) REFERENCES `ordination` (`id`) ON DELETE SET NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        $pdo->exec($sql);
        $installation_log[] = "✓ Retiree details table created successfully";
        
        // 3. Create officer_transfers table
        $installation_log[] = "Creating officer_transfers table...";
        $sql = "CREATE TABLE IF NOT EXISTS `officer_transfers` (
            `id` INT(11) NOT NULL AUTO_INCREMENT,
            `member_id` INT(11) NOT NULL,
            `ordination_id` INT(11) DEFAULT NULL,
            `transfer_type` ENUM('in', 'out') NOT NULL,
            `transfer_date` DATE NOT NULL,
            `from_district_id` INT(11) DEFAULT NULL,
            `from_area_id` INT(11) DEFAULT NULL,
            `from_assembly_id` INT(11) DEFAULT NULL,
            `to_district_id` INT(11) DEFAULT NULL,
            `to_area_id` INT(11) DEFAULT NULL,
            `to_assembly_id` INT(11) DEFAULT NULL,
            `officer_title` VARCHAR(50) DEFAULT NULL,
            `transfer_reason` TEXT DEFAULT NULL,
            `approved_by` VARCHAR(255) DEFAULT NULL,
            `approval_date` DATE DEFAULT NULL,
            `status` ENUM('pending', 'approved', 'rejected', 'completed') DEFAULT 'pending',
            `effective_date` DATE DEFAULT NULL,
            `notes` TEXT DEFAULT NULL,
            `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by` INT(11) DEFAULT NULL,
            `updated_by` INT(11) DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_transfer_type` (`transfer_type`),
            KEY `idx_status` (`status`),
            CONSTRAINT `fk_transfer_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE,
            CONSTRAINT `fk_transfer_ordination` FOREIGN KEY (`ordination_id`) REFERENCES `ordination` (`id`) ON DELETE SET NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
        $pdo->exec($sql);
        $installation_log[] = "✓ Officer transfers table created successfully";
        
        // 4. Register module
        $installation_log[] = "Registering Officership Management module...";
        $checkModule = $pdo->prepare("SELECT id FROM module_management WHERE module_url = ?");
        $checkModule->execute(['modules/officership/index.php']);
        
        if (!$checkModule->fetch()) {
            $stmt = $pdo->prepare("INSERT INTO module_management 
                (module_name, module_url, module_description, required_role, module_icon, display_order, is_active) 
                VALUES (?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([
                'Officership Management',
                'modules/officership/index.php',
                'Manage church officers, ordination, retirement, and transfers',
                'superuser,admin',
                'users',
                160,
                1
            ]);
            $installation_log[] = "✓ Module registered successfully";
        } else {
            $installation_log[] = "✓ Module already registered";
        }
        
        // Mark as successful before committing
        $success = true;
        $installation_log[] = "";
        $installation_log[] = "=== INSTALLATION COMPLETED SUCCESSFULLY ===";
        
        // Commit transaction if still active
        if ($pdo->inTransaction()) {
            $pdo->commit();
            $installation_log[] = "✓ Transaction committed";
        }
        
    } catch (Exception $e) {
        // Check if all critical operations succeeded even if transaction failed
        $criticalSuccess = strpos(implode(' ', $installation_log), 'Module registered successfully') !== false;
        
        if ($criticalSuccess && !$success) {
            $success = true;
            $installation_log[] = "";
            $installation_log[] = "=== INSTALLATION COMPLETED (with minor warnings) ===";
        }
        
        if ($pdo->inTransaction()) {
            $pdo->rollBack();
        }
        
        if (!$success) {
            $errors[] = "Installation failed: " . $e->getMessage();
        }
        $installation_log[] = "⚠ Warning: " . $e->getMessage();
    }
}

// Get theme colors
try {
    $stmt = $pdo->query("SELECT * FROM general_settings LIMIT 1");
    $settings = $stmt->fetch(PDO::FETCH_ASSOC);
    $primary_color = $settings['theme_primary_color'] ?? '#1E40AF';
    $secondary_color = $settings['theme_secondary_color'] ?? '#F97316';
} catch (Exception $e) {
    // Use default colors if table doesn't exist yet
    $primary_color = '#1E40AF';
    $secondary_color = '#F97316';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Install Officership Module</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, <?php echo $primary_color; ?> 0%, #9333EA 50%, <?php echo $secondary_color; ?> 100%);
        }
        .gradient-primary {
            background: linear-gradient(135deg, <?php echo $primary_color; ?> 0%, #9333EA 100%);
        }
        .btn-gradient {
            background: linear-gradient(135deg, <?php echo $primary_color; ?> 0%, #9333EA 100%);
        }
    </style>
</head>
<body class="bg-gray-50">
    <div class="min-h-screen py-12 px-4">
        <div class="max-w-4xl mx-auto">
            <!-- Header -->
            <div class="gradient-bg text-white rounded-t-xl p-8 text-center">
                <i class="fas fa-users text-6xl mb-4"></i>
                <h1 class="text-4xl font-bold mb-2">Officership Management Module</h1>
                <p class="text-xl opacity-90">Installation Wizard</p>
            </div>
            
            <div class="bg-white rounded-b-xl shadow-lg p-8">
                <?php if (!$success && empty($_POST)): ?>
                    <!-- Installation Info -->
                    <div class="mb-8">
                        <h2 class="text-2xl font-bold mb-4 text-gray-800">What will be installed?</h2>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                            <div class="border-l-4 border-blue-500 pl-4">
                                <h3 class="font-bold text-lg mb-2"><i class="fas fa-database text-blue-500 mr-2"></i>Database Tables</h3>
                                <ul class="text-gray-600 space-y-1">
                                    <li>• ordination - Ordination records</li>
                                    <li>• retiree_details - Retirement data</li>
                                    <li>• officer_transfers - Transfer tracking</li>
                                </ul>
                            </div>
                            
                            <div class="border-l-4 border-purple-500 pl-4">
                                <h3 class="font-bold text-lg mb-2"><i class="fas fa-cogs text-purple-500 mr-2"></i>Module Registration</h3>
                                <ul class="text-gray-600 space-y-1">
                                    <li>• Dashboard menu item</li>
                                    <li>• Access permissions</li>
                                    <li>• Module configuration</li>
                                </ul>
                            </div>
                        </div>
                        
                        <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6">
                            <h3 class="font-bold text-lg mb-2"><i class="fas fa-users text-blue-600 mr-2"></i>Officer Titles Tracked:</h3>
                            <div class="grid grid-cols-2 gap-4 mt-3">
                                <div>
                                    <p class="font-semibold text-blue-700 mb-1">Pastorate:</p>
                                    <ul class="text-sm text-gray-700">
                                        <li>• Apostle</li>
                                        <li>• Prophet</li>
                                        <li>• Evangelist</li>
                                        <li>• Pastor</li>
                                        <li>• Overseer</li>
                                        <li>• Probational Overseer</li>
                                    </ul>
                                </div>
                                <div>
                                    <p class="font-semibold text-purple-700 mb-1">Officers:</p>
                                    <ul class="text-sm text-gray-700">
                                        <li>• Elder</li>
                                        <li>• Deacon</li>
                                        <li>• Deaconess</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6">
                            <h3 class="font-bold text-lg mb-2"><i class="fas fa-check-circle text-green-600 mr-2"></i>Features:</h3>
                            <ul class="grid grid-cols-1 md:grid-cols-2 gap-2 text-sm text-gray-700">
                                <li>✓ Ordination tracking with card numbers</li>
                                <li>✓ Retirement ceremony records</li>
                                <li>✓ Transfer management (in/out)</li>
                                <li>✓ Suspension/reactivation support</li>
                                <li>✓ Dashboard with 6 sections</li>
                                <li>✓ Export to PDF/CSV/Excel</li>
                                <li>✓ Member self-service portal</li>
                                <li>✓ Years of service calculation</li>
                            </ul>
                        </div>
                    </div>
                    
                    <!-- Install Form -->
                    <form method="POST" action="" class="text-center">
                        <input type="hidden" name="install_officership" value="1">
                        <button type="submit" name="submit" class="btn-gradient text-white px-8 py-4 rounded-lg text-lg font-bold hover:shadow-lg transition-all transform hover:-translate-y-1">
                            <i class="fas fa-download mr-2"></i>Install Officership Module
                        </button>
                        <p class="text-gray-500 mt-4 text-sm">This will create all necessary tables and configurations</p>
                    </form>
                    
                <?php elseif ($success): ?>
                    <!-- Success Message -->
                    <div class="text-center mb-8">
                        <div class="inline-block p-6 bg-green-100 rounded-full mb-4">
                            <i class="fas fa-check-circle text-6xl text-green-600"></i>
                        </div>
                        <h2 class="text-3xl font-bold text-green-600 mb-4">Installation Successful!</h2>
                        <p class="text-gray-600 mb-6">The Officership Management module has been installed successfully.</p>
                    </div>
                    
                    <!-- Installation Log -->
                    <div class="bg-gray-50 rounded-lg p-6 mb-6 max-h-96 overflow-y-auto">
                        <h3 class="font-bold mb-3 text-gray-800">Installation Log:</h3>
                        <?php foreach ($installation_log as $log): ?>
                            <p class="text-sm font-mono text-gray-700 mb-1"><?php echo htmlspecialchars($log); ?></p>
                        <?php endforeach; ?>
                    </div>
                    
                    <!-- Next Steps -->
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6">
                        <h3 class="font-bold text-lg mb-3"><i class="fas fa-lightbulb text-blue-600 mr-2"></i>Next Steps:</h3>
                        <ol class="list-decimal list-inside space-y-2 text-gray-700">
                            <li>Go to your dashboard and look for "Officership Management" in the menu</li>
                            <li>Start adding ordination details for existing officers</li>
                            <li>Configure card numbering system for your church</li>
                            <li>Import historical retirement data if available</li>
                            <li>Train staff on using the module</li>
                        </ol>
                    </div>
                    
                    <div class="text-center space-x-4">
                        <a href="dashboard.php" class="btn-gradient text-white px-6 py-3 rounded-lg font-bold inline-block hover:shadow-lg transition-all">
                            <i class="fas fa-home mr-2"></i>Go to Dashboard
                        </a>
                        <a href="modules/officership/index.php" class="bg-gray-600 text-white px-6 py-3 rounded-lg font-bold inline-block hover:bg-gray-700 transition-all">
                            <i class="fas fa-users mr-2"></i>Open Officership Module
                        </a>
                    </div>
                    
                <?php else: ?>
                    <!-- Error Message -->
                    <div class="text-center mb-8">
                        <div class="inline-block p-6 bg-red-100 rounded-full mb-4">
                            <i class="fas fa-times-circle text-6xl text-red-600"></i>
                        </div>
                        <h2 class="text-3xl font-bold text-red-600 mb-4">Installation Failed</h2>
                        <p class="text-gray-600 mb-6">There were errors during installation. Please check the logs below.</p>
                    </div>
                    
                    <!-- Error Log -->
                    <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6">
                        <h3 class="font-bold mb-3 text-red-800">Errors:</h3>
                        <?php foreach ($errors as $error): ?>
                            <p class="text-sm text-red-700 mb-2"><i class="fas fa-exclamation-triangle mr-2"></i><?php echo htmlspecialchars($error); ?></p>
                        <?php endforeach; ?>
                    </div>
                    
                    <!-- Installation Log -->
                    <?php if (!empty($installation_log)): ?>
                    <div class="bg-gray-50 rounded-lg p-6 mb-6 max-h-96 overflow-y-auto">
                        <h3 class="font-bold mb-3 text-gray-800">Installation Log:</h3>
                        <?php foreach ($installation_log as $log): ?>
                            <p class="text-sm font-mono text-gray-700 mb-1"><?php echo htmlspecialchars($log); ?></p>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                    
                    <div class="text-center">
                        <a href="install_officership.php" class="btn-gradient text-white px-6 py-3 rounded-lg font-bold inline-block hover:shadow-lg transition-all">
                            <i class="fas fa-redo mr-2"></i>Try Again
                        </a>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Footer -->
            <div class="text-center mt-8 text-gray-500 text-sm">
                <p><i class="fas fa-info-circle mr-1"></i>Officership Management Module v1.0</p>
                <p>For support, check the OFFICERSHIP_MODULE_IMPLEMENTATION.md file</p>
            </div>
        </div>
    </div>
</body>
</html>

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