Sindbad~EG File Manager

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

<?php
/**
 * Cleanup Duplicate Modules Script
 * Removes duplicate module entries from module_management table
 */

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

// Only superusers can run this
if (!isSuperuser()) {
    die('Access denied. Only superusers can run this script.');
}

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

try {
    // Find and remove duplicate Module Management entries
    // Keep only the one with the lowest ID
    
    $duplicates = $db->query("
        SELECT id, module_name, COUNT(*) as count
        FROM module_management
        WHERE module_name = 'Module Management'
        GROUP BY module_name
        HAVING count > 1
    ")->fetch();
    
    if ($duplicates) {
        echo "<h2>Found {$duplicates['count']} Module Management entries</h2>";
        
        // Get all Module Management IDs
        $stmt = $db->query("
            SELECT id FROM module_management 
            WHERE module_name = 'Module Management'
            ORDER BY id ASC
        ");
        $ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
        
        // Keep the first one, delete the rest
        $keepId = array_shift($ids);
        echo "<p>Keeping ID: $keepId</p>";
        
        foreach ($ids as $deleteId) {
            echo "<p>Deleting duplicate ID: $deleteId</p>";
            
            // Delete from module_access_levels first (foreign key)
            $db->prepare("DELETE FROM module_access_levels WHERE module_id = ?")->execute([$deleteId]);
            
            // Delete from module_management
            $db->prepare("DELETE FROM module_management WHERE id = ?")->execute([$deleteId]);
        }
        
        echo "<p style='color: green; font-weight: bold;'>✅ Cleanup complete! Removed " . count($ids) . " duplicate(s).</p>";
        echo "<p><a href='dashboard.php'>Return to Dashboard</a> | <a href='modules/module-management/index.php'>Open Module Management</a></p>";
    } else {
        echo "<p style='color: blue;'>No duplicates found. Module Management appears only once.</p>";
        echo "<p><a href='dashboard.php'>Return to Dashboard</a></p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}
?>

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