Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/
Upload File :
Current File : /home/copmadinaarea/.trash/fix_migration.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();

$migration_log = [];
$errors = [];

function log_migration($message, $success = true) {
    global $migration_log;
    $migration_log[] = [
        'message' => $message,
        'success' => $success,
        'timestamp' => date('Y-m-d H:i:s')
    ];
    echo ($success ? '[SUCCESS] ' : '[ERROR] ') . $message . "\n";
}

function execute_query($conn, $query, $description) {
    global $errors;
    try {
        $result = $conn->exec($query);
        log_migration($description);
        return true;
    } catch (PDOException $e) {
        $error_msg = "$description - Error: " . $e->getMessage();
        log_migration($error_msg, false);
        $errors[] = $error_msg;
        return false;
    }
}

echo "Fixing Migration Issues...\n";
echo "==========================\n\n";

// Fix 1: Insert sample editor users with correct column names
$sample_editors = [
    [
        'username' => 'editor_accra',
        'email' => 'editor.accra@copnews.org',
        'password' => password_hash('editor123', PASSWORD_DEFAULT),
        'name' => 'Accra District Editor',
        'location_id' => 5, // Accra Central District
        'account_type' => 'editor',
        'can_edit_others' => 1,
        'editorial_scope' => 'district'
    ],
    [
        'username' => 'editor_tema',
        'email' => 'editor.tema@copnews.org',
        'password' => password_hash('editor123', PASSWORD_DEFAULT),
        'name' => 'Tema District Editor',
        'location_id' => 4, // Tema District
        'account_type' => 'editor',
        'can_edit_others' => 1,
        'editorial_scope' => 'district'
    ],
    [
        'username' => 'editor_area_accra',
        'email' => 'area.editor.accra@copnews.org',
        'password' => password_hash('editor123', PASSWORD_DEFAULT),
        'name' => 'Greater Accra Area Editor',
        'location_id' => 1, // Greater Accra Area
        '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']
        ]);
        log_migration("Created/updated sample editor: " . $editor['name']);
    } catch (PDOException $e) {
        log_migration("Failed to create sample editor " . $editor['name'] . ": " . $e->getMessage(), false);
    }
}

// Fix 2: Add system settings without created_at column
$settings = [
    ['setting_key' => 'editorial_auto_assign', 'setting_value' => '1', 'description' => 'Automatically assign editors based on location hierarchy'],
    ['setting_key' => 'editorial_email_notifications', 'setting_value' => '1', 'description' => 'Send email notifications for editorial actions'],
    ['setting_key' => 'editorial_review_timeout_days', 'setting_value' => '7', 'description' => 'Days before review timeout notification'],
    ['setting_key' => 'editorial_require_comments_on_reject', 'setting_value' => '1', 'description' => 'Require comments when rejecting articles']
];

foreach ($settings as $setting) {
    $query = "INSERT INTO settings (setting_key, setting_value, description) 
              VALUES (?, ?, ?) 
              ON DUPLICATE KEY UPDATE 
              setting_value = VALUES(setting_value), 
              description = VALUES(description)";
    
    try {
        $stmt = $conn->prepare($query);
        $stmt->execute([$setting['setting_key'], $setting['setting_value'], $setting['description']]);
        log_migration("Added/updated setting: " . $setting['setting_key']);
    } catch (PDOException $e) {
        log_migration("Failed to add setting " . $setting['setting_key'] . ": " . $e->getMessage(), false);
    }
}

// Fix 3: Create audit log entry with correct column names
$audit_query = "INSERT INTO audit_logs (user_id, action, table_name, record_id, created_at) 
                VALUES (?, 'MIGRATION', 'SYSTEM', 0, NOW())";

try {
    $stmt = $conn->prepare($audit_query);
    $stmt->execute([$_SESSION['user_id']]);
    log_migration("Created audit log entry for migration fix");
} catch (PDOException $e) {
    log_migration("Failed to create audit log entry: " . $e->getMessage(), false);
}

// Fix 4: Create editor assignments for sample editors
$assignments = [
    ['editor_username' => 'editor_accra', 'location_id' => 5], // Accra Central District
    ['editor_username' => 'editor_accra', 'location_id' => 8], // Osu Assembly
    ['editor_username' => 'editor_tema', 'location_id' => 4], // Tema District
    ['editor_username' => 'editor_tema', 'location_id' => 7], // Community 1/9 Assembly
    ['editor_username' => 'editor_area_accra', 'location_id' => 1], // Greater Accra Area
];

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) {
            log_migration("Created editor assignment: " . $assignment['editor_username'] . " -> Location ID " . $assignment['location_id']);
        } else {
            log_migration("Editor assignment already exists: " . $assignment['editor_username'] . " -> Location ID " . $assignment['location_id']);
        }
    } catch (PDOException $e) {
        log_migration("Failed to create editor assignment: " . $e->getMessage(), false);
    }
}

echo "\n==========================\n";
echo "Fix Summary:\n";
echo "==========================\n";

$success_count = 0;
$error_count = 0;

foreach ($migration_log as $entry) {
    if ($entry['success']) {
        $success_count++;
    } else {
        $error_count++;
    }
}

echo "Total operations: " . count($migration_log) . "\n";
echo "Successful: $success_count\n";
echo "Errors: $error_count\n\n";

if ($error_count > 0) {
    echo "ERRORS ENCOUNTERED:\n";
    echo "===================\n";
    foreach ($errors as $error) {
        echo "- $error\n";
    }
    echo "\nPlease review and fix errors before proceeding.\n";
} else {
    echo "✅ Migration fixes completed successfully!\n\n";
    echo "Sample editor accounts created:\n";
    echo "- Username: editor_accra, Password: editor123\n";
    echo "- Username: editor_tema, Password: editor123\n";
    echo "- Username: editor_area_accra, Password: editor123\n\n";
    echo "You can now test the proofreading system!\n";
}

echo "Migration fixes completed.\n";
?>

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