Sindbad~EG File Manager

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

<?php
/**
 * Fix Duplicate SMS Management Modules
 * Run this to remove duplicate SMS Management entries
 */

require_once 'config/config.php';

checkLogin();

// Only superusers can run this
if (!isSuperuser()) {
    redirect('dashboard.php');
}

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

try {
    $db->beginTransaction();
    
    // Find all SMS Management entries
    $stmt = $db->query("
        SELECT id, module_name, module_url, category, display_order, created_at 
        FROM module_management 
        WHERE module_url = 'modules/sms/index.php' 
        OR module_name = 'SMS Management'
        ORDER BY id ASC
    ");
    $smsModules = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $count = count($smsModules);
    
    if ($count == 0) {
        $warnings[] = 'No SMS Management module found in database';
    } elseif ($count == 1) {
        $success[] = 'Only one SMS Management module found - no duplicates to remove';
        $success[] = 'Module ID: ' . $smsModules[0]['id'];
    } else {
        // Keep the first one (oldest), delete the rest
        $keepId = $smsModules[0]['id'];
        $success[] = "Found $count SMS Management entries";
        $success[] = "Keeping module ID: $keepId (oldest entry)";
        
        // Delete duplicates
        for ($i = 1; $i < $count; $i++) {
            $deleteId = $smsModules[$i]['id'];
            
            // Delete module access levels for this duplicate
            $stmt = $db->prepare("DELETE FROM module_access_levels WHERE module_id = ?");
            $stmt->execute([$deleteId]);
            
            // Delete the duplicate module
            $stmt = $db->prepare("DELETE FROM module_management WHERE id = ?");
            $stmt->execute([$deleteId]);
            
            $success[] = "Deleted duplicate module ID: $deleteId";
        }
        
        // Update the kept module to ensure it's correct
        $stmt = $db->prepare("
            UPDATE module_management 
            SET module_name = 'SMS Management',
                module_url = 'modules/sms/index.php',
                module_icon = 'sms',
                category = 'Communication',
                display_order = 160,
                is_active = 1
            WHERE id = ?
        ");
        $stmt->execute([$keepId]);
        $success[] = 'Updated remaining module to ensure correct settings';
        
        // Ensure access levels are set for the kept module
        $accessLevels = ['assembly', 'district', 'area', 'superuser'];
        foreach ($accessLevels as $level) {
            $stmt = $db->prepare("
                INSERT INTO module_access_levels (module_id, access_level, is_enabled, enabled_by)
                VALUES (?, ?, 1, 1)
                ON DUPLICATE KEY UPDATE is_enabled = 1
            ");
            $stmt->execute([$keepId, $level]);
        }
        $success[] = 'Access levels verified for all user types';
        
        $duplicatesRemoved = $count - 1;
        $success[] = "Removed $duplicatesRemoved duplicate(s) successfully!";
    }
    
    $db->commit();
    
} catch (PDOException $e) {
    if ($db->inTransaction()) {
        $db->rollBack();
    }
    $errors[] = 'Database error: ' . $e->getMessage();
} catch (Exception $e) {
    if ($db->inTransaction()) {
        $db->rollBack();
    }
    $errors[] = 'Error: ' . $e->getMessage();
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fix Duplicate SMS Management</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">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Inter', sans-serif; }
        .gradient-bg { background: linear-gradient(135deg, #1E40AF 0%, #9333EA 50%, #F97316 100%); }
    </style>
</head>
<body class="gradient-bg min-h-screen flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl shadow-2xl p-8 max-w-2xl w-full">
        <div class="text-center mb-8">
            <div class="inline-block p-4 bg-gradient-to-r from-orange-600 to-red-600 rounded-full mb-4">
                <i class="fas fa-broom text-5xl text-white"></i>
            </div>
            <h1 class="text-3xl font-bold text-gray-800">Fix Duplicate Modules</h1>
            <p class="text-gray-600 mt-2">Remove duplicate SMS Management entries</p>
        </div>

        <?php if (!empty($success)): ?>
            <div class="bg-green-100 border border-green-400 text-green-800 px-6 py-4 rounded-lg mb-6">
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-2xl mr-4 mt-1"></i>
                    <div class="flex-1">
                        <h3 class="font-bold text-lg mb-2">Success!</h3>
                        <ul class="space-y-2">
                            <?php foreach ($success as $msg): ?>
                                <li class="flex items-start">
                                    <i class="fas fa-check mr-2 text-green-600 mt-1"></i>
                                    <span><?php echo htmlspecialchars($msg); ?></span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php if (!empty($errors)): ?>
            <div class="bg-red-100 border border-red-400 text-red-800 px-6 py-4 rounded-lg mb-6">
                <div class="flex items-start">
                    <i class="fas fa-exclamation-circle text-2xl mr-4 mt-1"></i>
                    <div class="flex-1">
                        <h3 class="font-bold text-lg mb-2">Errors</h3>
                        <ul class="space-y-2">
                            <?php foreach ($errors as $error): ?>
                                <li class="flex items-start">
                                    <i class="fas fa-times mr-2 text-red-600 mt-1"></i>
                                    <span><?php echo htmlspecialchars($error); ?></span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php if (!empty($warnings)): ?>
            <div class="bg-yellow-100 border border-yellow-400 text-yellow-800 px-6 py-4 rounded-lg mb-6">
                <div class="flex items-start">
                    <i class="fas fa-exclamation-triangle text-2xl mr-4 mt-1"></i>
                    <div class="flex-1">
                        <h3 class="font-bold text-lg mb-2">Warnings</h3>
                        <ul class="space-y-2">
                            <?php foreach ($warnings as $warning): ?>
                                <li class="flex items-start">
                                    <i class="fas fa-info-circle mr-2 text-yellow-600 mt-1"></i>
                                    <span><?php echo htmlspecialchars($warning); ?></span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <!-- What Was Fixed -->
        <div class="bg-blue-50 rounded-lg p-6 mb-6">
            <h3 class="font-bold text-gray-800 mb-3 flex items-center">
                <i class="fas fa-wrench text-blue-600 mr-2"></i>What This Script Does
            </h3>
            <div class="space-y-2 text-sm text-gray-700">
                <p><i class="fas fa-check text-green-600 mr-2"></i>Finds all SMS Management entries</p>
                <p><i class="fas fa-check text-green-600 mr-2"></i>Keeps the oldest entry (first installed)</p>
                <p><i class="fas fa-check text-green-600 mr-2"></i>Removes duplicate entries</p>
                <p><i class="fas fa-check text-green-600 mr-2"></i>Cleans up module access levels</p>
                <p><i class="fas fa-check text-green-600 mr-2"></i>Verifies correct settings</p>
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="flex flex-col sm:flex-row gap-3">
            <a href="dashboard.php" class="flex-1 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white text-center px-6 py-3 rounded-lg font-semibold transition duration-200 transform hover:scale-105 shadow-lg">
                <i class="fas fa-home mr-2"></i>Back to Dashboard
            </a>
            <a href="modules/sms/index.php" class="flex-1 bg-gradient-to-r from-green-600 to-green-700 hover:from-green-700 hover:to-green-800 text-white text-center px-6 py-3 rounded-lg font-semibold transition duration-200 transform hover:scale-105 shadow-lg">
                <i class="fas fa-sms mr-2"></i>Go to SMS Management
            </a>
        </div>

        <div class="mt-6 p-4 bg-gradient-to-r from-blue-50 to-purple-50 rounded-lg">
            <p class="text-sm text-gray-700 text-center">
                <i class="fas fa-info-circle text-blue-600 mr-2"></i>
                Refresh your dashboard and sidebar to see the changes
            </p>
        </div>
    </div>
</body>
</html>

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