Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/
Upload File :
Current File : /home/copmadinaarea/.trash/fix_migration_final.php

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

// Check if user is superuser
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['account_type'] !== 'superuser') {
    die('Access denied. Superuser privileges required.');
}

$database = new Database();
$conn = $database->getConnection();

echo "Final Migration Fix - Checking Available Locations...\n";
echo "====================================================\n\n";

// First, let's see what locations exist
try {
    $stmt = $conn->prepare("SELECT id, name, type FROM locations ORDER BY type, id");
    $stmt->execute();
    $locations = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "Available Locations:\n";
    foreach ($locations as $loc) {
        echo "ID: {$loc['id']} - {$loc['name']} ({$loc['type']})\n";
    }
    echo "\n";
    
    // Find suitable location IDs for our editors
    $area_location = null;
    $district_locations = [];
    
    foreach ($locations as $loc) {
        if ($loc['type'] === 'area' && !$area_location) {
            $area_location = $loc;
        }
        if ($loc['type'] === 'district' && count($district_locations) < 2) {
            $district_locations[] = $loc;
        }
    }
    
    if (!$area_location || count($district_locations) < 2) {
        echo "[ERROR] Not enough locations found to create sample editors\n";
        echo "Need at least 1 area and 2 districts\n";
        exit;
    }
    
    echo "Using locations for sample editors:\n";
    echo "Area Editor: {$area_location['name']} (ID: {$area_location['id']})\n";
    echo "District Editor 1: {$district_locations[0]['name']} (ID: {$district_locations[0]['id']})\n";
    echo "District Editor 2: {$district_locations[1]['name']} (ID: {$district_locations[1]['id']})\n\n";
    
    // Create sample editors with correct location IDs
    $sample_editors = [
        [
            'username' => 'editor_district1',
            'email' => 'editor.district1@copnews.org',
            'password' => password_hash('editor123', PASSWORD_DEFAULT),
            'name' => $district_locations[0]['name'] . ' Editor',
            'location_id' => $district_locations[0]['id'],
            'account_type' => 'editor',
            'can_edit_others' => 1,
            'editorial_scope' => 'district'
        ],
        [
            'username' => 'editor_district2',
            'email' => 'editor.district2@copnews.org',
            'password' => password_hash('editor123', PASSWORD_DEFAULT),
            'name' => $district_locations[1]['name'] . ' Editor',
            'location_id' => $district_locations[1]['id'],
            'account_type' => 'editor',
            'can_edit_others' => 1,
            'editorial_scope' => 'district'
        ],
        [
            'username' => 'editor_area',
            'email' => 'editor.area@copnews.org',
            'password' => password_hash('editor123', PASSWORD_DEFAULT),
            'name' => $area_location['name'] . ' Editor',
            'location_id' => $area_location['id'],
            'account_type' => 'editor',
            'can_edit_others' => 1,
            'editorial_scope' => 'area'
        ]
    ];
    
    foreach ($sample_editors as $editor) {
        $query = "INSERT INTO users (username, email, password, name, location_id, account_type, can_edit_others, editorial_scope) 
                  VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                  ON DUPLICATE KEY UPDATE 
                  name = VALUES(name),
                  account_type = VALUES(account_type),
                  can_edit_others = VALUES(can_edit_others),
                  editorial_scope = VALUES(editorial_scope)";
        
        try {
            $stmt = $conn->prepare($query);
            $stmt->execute([
                $editor['username'],
                $editor['email'],
                $editor['password'],
                $editor['name'],
                $editor['location_id'],
                $editor['account_type'],
                $editor['can_edit_others'],
                $editor['editorial_scope']
            ]);
            echo "[SUCCESS] Created/updated sample editor: " . $editor['name'] . " (Username: " . $editor['username'] . ")\n";
        } catch (PDOException $e) {
            echo "[ERROR] Failed to create sample editor " . $editor['name'] . ": " . $e->getMessage() . "\n";
        }
    }
    
    // Create editor assignments
    echo "\nCreating editor assignments...\n";
    
    // Get all assembly locations for assignment
    $stmt = $conn->prepare("SELECT id, name FROM locations WHERE type = 'assembly'");
    $stmt->execute();
    $assemblies = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Assign district editors to their districts and some assemblies
    $assignments = [
        ['editor_username' => 'editor_district1', 'location_id' => $district_locations[0]['id']],
        ['editor_username' => 'editor_district2', 'location_id' => $district_locations[1]['id']],
        ['editor_username' => 'editor_area', 'location_id' => $area_location['id']],
    ];
    
    // Add some assembly assignments if available
    if (count($assemblies) > 0) {
        $assignments[] = ['editor_username' => 'editor_district1', 'location_id' => $assemblies[0]['id']];
        if (count($assemblies) > 1) {
            $assignments[] = ['editor_username' => 'editor_district2', 'location_id' => $assemblies[1]['id']];
        }
    }
    
    foreach ($assignments as $assignment) {
        $query = "INSERT INTO editor_assignments (editor_id, location_id, created_by) 
                  SELECT u.id, ?, 1 FROM users u WHERE u.username = ? AND NOT EXISTS (
                      SELECT 1 FROM editor_assignments ea WHERE ea.editor_id = u.id AND ea.location_id = ?
                  )";
        
        try {
            $stmt = $conn->prepare($query);
            $stmt->execute([
                $assignment['location_id'],
                $assignment['editor_username'],
                $assignment['location_id']
            ]);
            
            if ($stmt->rowCount() > 0) {
                echo "[SUCCESS] Created editor assignment: " . $assignment['editor_username'] . " -> Location ID " . $assignment['location_id'] . "\n";
            } else {
                echo "[INFO] Editor assignment already exists: " . $assignment['editor_username'] . " -> Location ID " . $assignment['location_id'] . "\n";
            }
        } catch (PDOException $e) {
            echo "[ERROR] Failed to create editor assignment: " . $e->getMessage() . "\n";
        }
    }
    
    echo "\n====================================================\n";
    echo "✅ Migration completed successfully!\n\n";
    echo "Sample Editor Accounts Created:\n";
    foreach ($sample_editors as $editor) {
        echo "- Username: {$editor['username']}, Password: editor123\n";
    }
    echo "\nYou can now test the proofreading system!\n";
    echo "Next: Visit test_proofreading_workflow.php to verify everything works.\n";
    
} catch (Exception $e) {
    echo "[ERROR] Database error: " . $e->getMessage() . "\n";
}
?>

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