Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/migrate.php

<?php
/**
 * Database Migration Script for Locations Integration
 * Run this file once to update your database schema
 */

require_once 'config/config.php';

try {
    $database = new Database();
    $conn = $database->getConnection();
    
    echo "<h2>COP News Portal - Database Migration</h2>";
    echo "<p>Updating database schema for locations integration...</p>";
    
    // Create locations table if it doesn't exist
    $sql = "CREATE TABLE IF NOT EXISTS locations (
        id INT PRIMARY KEY AUTO_INCREMENT,
        name VARCHAR(100) NOT NULL,
        type ENUM('area', 'district', 'assembly') NOT NULL,
        parent_id INT NULL,
        address TEXT,
        contact_person VARCHAR(100),
        contact_phone VARCHAR(20),
        contact_email VARCHAR(100),
        description TEXT,
        status ENUM('active', 'inactive') DEFAULT 'active',
        created_by INT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        FOREIGN KEY (parent_id) REFERENCES locations(id) ON DELETE SET NULL,
        UNIQUE KEY unique_location (name, type)
    )";
    
    $conn->exec($sql);
    echo "<p>✓ Locations table created/verified</p>";
    
    // Insert sample locations
    $locations = [
        [1, 'Greater Accra Area', 'area', null, 'Accra, Ghana', 'Elder John Mensah', '+233244123456', 'accra.area@cop.org', 'Greater Accra Area Office'],
        [2, 'Ashanti Area', 'area', null, 'Kumasi, Ghana', 'Elder Grace Owusu', '+233244123457', 'ashanti.area@cop.org', 'Ashanti Area Office'],
        [3, 'Northern Area', 'area', null, 'Tamale, Ghana', 'Elder Isaac Alhassan', '+233244123458', 'northern.area@cop.org', 'Northern Area Office'],
        [4, 'Tema District', 'district', 1, 'Tema, Ghana', 'Pastor Samuel Osei', '+233244234567', 'tema.district@cop.org', 'Tema District Office'],
        [5, 'Accra Central District', 'district', 1, 'Accra Central, Ghana', 'Pastor Rebecca Adjei', '+233244234568', 'accra.central@cop.org', 'Accra Central District Office'],
        [6, 'Kumasi District', 'district', 2, 'Kumasi, Ghana', 'Pastor Emmanuel Boateng', '+233244234569', 'kumasi.district@cop.org', 'Kumasi District Office'],
        [7, 'Community 1 Assembly', 'assembly', 4, 'Community 1, Tema', 'Elder Mary Asante', '+233244345678', 'comm1.assembly@cop.org', 'Community 1 Local Assembly'],
        [8, 'Community 9 Assembly', 'assembly', 4, 'Community 9, Tema', 'Elder Joseph Tetteh', '+233244345679', 'comm9.assembly@cop.org', 'Community 9 Local Assembly'],
        [9, 'Osu Assembly', 'assembly', 5, 'Osu, Accra', 'Elder Sarah Mensah', '+233244345680', 'osu.assembly@cop.org', 'Osu Local Assembly'],
        [10, 'Adum Assembly', 'assembly', 6, 'Adum, Kumasi', 'Elder Peter Asante', '+233244345681', 'adum.assembly@cop.org', 'Adum Local Assembly']
    ];
    
    // Insert TinyMCE API key setting
    $conn->exec("INSERT IGNORE INTO settings (setting_key, setting_value, setting_type, description) VALUES ('tinymce_api_key', 'cy1it2xoa81hi6fiynvv5pcgjnms5lqa8uhr1n5vdukymhud', 'text', 'TinyMCE API key for rich text editor')");
    
    $stmt = $conn->prepare("INSERT IGNORE INTO locations (id, name, type, parent_id, address, contact_person, contact_phone, contact_email, description, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
    
    foreach ($locations as $location) {
        $stmt->execute($location);
    }
    echo "<p>✓ Sample locations inserted</p>";
    
    // Add location_id column to news table if it doesn't exist
    $stmt = $conn->query("SHOW COLUMNS FROM news LIKE 'location_id'");
    if ($stmt->rowCount() == 0) {
        // First add the column as nullable
        $conn->exec("ALTER TABLE news ADD COLUMN location_id INT NULL AFTER title");
        
        // Set a default location_id for existing records (use first location)
        $locationStmt = $conn->query("SELECT id FROM locations LIMIT 1");
        $firstLocation = $locationStmt->fetch(PDO::FETCH_ASSOC);
        if ($firstLocation) {
            $conn->exec("UPDATE news SET location_id = " . $firstLocation['id'] . " WHERE location_id IS NULL");
        }
        
        // Now make it NOT NULL and add foreign key
        $conn->exec("ALTER TABLE news MODIFY location_id INT NOT NULL");
        $conn->exec("ALTER TABLE news ADD FOREIGN KEY (location_id) REFERENCES locations(id)");
        echo "✓ Added location_id column to news table<br>";
    } else {
        echo "✓ News table already has location_id column<br>";
    }
    
    // Check if location_id column exists in users table
    $result = $conn->query("SHOW COLUMNS FROM users LIKE 'location_id'");
    if ($result->rowCount() == 0) {
        // Add location_id column
        $conn->exec("ALTER TABLE users ADD COLUMN location_id INT NULL AFTER user_picture");
        echo "<p>✓ Added location_id column to users table</p>";
        
        // Add foreign key constraint
        $conn->exec("ALTER TABLE users ADD CONSTRAINT users_location_fk FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE SET NULL");
        echo "<p>✓ Added foreign key constraint</p>";
        
        // Migrate existing data - set all users to Greater Accra Area for now
        $conn->exec("UPDATE users SET location_id = 1 WHERE location_id IS NULL");
        echo "<p>✓ Migrated existing user location data</p>";
    } else {
        echo "<p>✓ location_id column already exists</p>";
    }
    
    echo "<h3 style='color: green;'>Migration completed successfully!</h3>";
    echo "<p><a href='index.php'>← Back to Application</a></p>";
    
} catch (PDOException $e) {
    echo "<h3 style='color: red;'>Migration Error:</h3>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "<p>Please check your database configuration and try again.</p>";
}
?>

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