Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/migrate_gps_location.php

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

// Check if user is logged in and is superuser
if (!isLoggedIn() || !hasRole('superuser')) {
    redirect('login.php');
}

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

$success_message = '';
$error_message = '';

// Check if GPS fields already exist
function checkGPSFields($conn) {
    try {
        $query = "SHOW COLUMNS FROM attendance_records LIKE 'latitude'";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        return $stmt->rowCount() > 0;
    } catch (Exception $e) {
        return false;
    }
}

// Handle migration
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['migrate'])) {
    if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
        $error_message = 'Invalid security token. Please try again.';
    } else {
        try {
            // Check if fields already exist
            if (checkGPSFields($conn)) {
                $error_message = 'GPS location fields already exist in the database.';
            } else {
                // Add GPS location fields
                $migrations = [
                    "ALTER TABLE attendance_records 
                     ADD COLUMN latitude DECIMAL(10, 8) NULL AFTER user_agent",
                    
                    "ALTER TABLE attendance_records 
                     ADD COLUMN longitude DECIMAL(11, 8) NULL AFTER latitude",
                    
                    "ALTER TABLE attendance_records 
                     ADD COLUMN location_accuracy FLOAT NULL AFTER longitude",
                    
                    "ALTER TABLE attendance_records 
                     ADD COLUMN location_timestamp TIMESTAMP NULL AFTER location_accuracy",
                    
                    "ALTER TABLE attendance_records 
                     ADD COLUMN location_address TEXT NULL AFTER location_timestamp",
                    
                    "CREATE INDEX idx_attendance_location ON attendance_records(latitude, longitude)"
                ];
                
                foreach ($migrations as $sql) {
                    $stmt = $conn->prepare($sql);
                    $stmt->execute();
                }
                
                logActivity($_SESSION['user_id'], 'database_migration', 'Added GPS location fields to attendance_records');
                $success_message = 'GPS location fields added successfully to the database.';
            }
        } catch (Exception $e) {
            $error_message = 'Migration failed: ' . $e->getMessage();
        }
    }
}

$gpsFieldsExist = checkGPSFields($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GPS Location Migration - Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#F59E0B',
                        accent: '#6B7280'
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-50">
    <!-- Include Sidebar -->
    <?php include 'includes/sidebar.php'; ?>

    <!-- Main Content -->
    <div class="md:ml-64">
        <!-- Header -->
        <header class="bg-white shadow-sm border-b">
            <div class="px-6 py-4">
                <div class="flex items-center justify-between">
                    <h1 class="text-2xl font-bold text-gray-900">GPS Location Migration</h1>
                    <a href="settings.php" class="bg-gray-600 text-white px-4 py-2 rounded-lg hover:bg-gray-700 transition duration-300">
                        <i class="fas fa-arrow-left mr-2"></i>Back to Settings
                    </a>
                </div>
            </div>
        </header>

        <!-- Content -->
        <main class="p-6">
            <!-- Success/Error Messages -->
            <?php if ($success_message): ?>
                <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6">
                    <i class="fas fa-check-circle mr-2"></i>
                    <?php echo $success_message; ?>
                </div>
            <?php endif; ?>

            <?php if ($error_message): ?>
                <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
                    <i class="fas fa-exclamation-triangle mr-2"></i>
                    <?php echo $error_message; ?>
                </div>
            <?php endif; ?>

            <div class="max-w-4xl mx-auto">
                <div class="bg-white rounded-lg shadow p-6">
                    <div class="mb-6">
                        <h2 class="text-xl font-semibold text-gray-900 mb-2">
                            <i class="fas fa-map-marker-alt mr-2 text-blue-600"></i>GPS Location Database Migration
                        </h2>
                        <p class="text-gray-600">
                            This migration adds GPS location tracking fields to the attendance_records table.
                        </p>
                    </div>

                    <!-- Migration Status -->
                    <div class="mb-6 p-4 rounded-lg <?php echo $gpsFieldsExist ? 'bg-green-50 border border-green-200' : 'bg-yellow-50 border border-yellow-200'; ?>">
                        <div class="flex items-center">
                            <i class="fas <?php echo $gpsFieldsExist ? 'fa-check-circle text-green-600' : 'fa-exclamation-triangle text-yellow-600'; ?> mr-3"></i>
                            <div>
                                <h3 class="font-semibold <?php echo $gpsFieldsExist ? 'text-green-900' : 'text-yellow-900'; ?>">
                                    Migration Status: <?php echo $gpsFieldsExist ? 'Completed' : 'Pending'; ?>
                                </h3>
                                <p class="text-sm <?php echo $gpsFieldsExist ? 'text-green-700' : 'text-yellow-700'; ?>">
                                    <?php echo $gpsFieldsExist ? 'GPS location fields are already present in the database.' : 'GPS location fields need to be added to the database.'; ?>
                                </p>
                            </div>
                        </div>
                    </div>

                    <!-- Migration Details -->
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-900 mb-3">Fields to be Added:</h3>
                        <div class="bg-gray-50 rounded-lg p-4">
                            <ul class="space-y-2 text-sm">
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">latitude</code>
                                    <span class="ml-2 text-gray-600">- GPS latitude coordinate (DECIMAL 10,8)</span>
                                </li>
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">longitude</code>
                                    <span class="ml-2 text-gray-600">- GPS longitude coordinate (DECIMAL 11,8)</span>
                                </li>
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">location_accuracy</code>
                                    <span class="ml-2 text-gray-600">- GPS accuracy in meters (FLOAT)</span>
                                </li>
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">location_timestamp</code>
                                    <span class="ml-2 text-gray-600">- When location was captured (TIMESTAMP)</span>
                                </li>
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">location_address</code>
                                    <span class="ml-2 text-gray-600">- Reverse geocoded address (TEXT)</span>
                                </li>
                                <li class="flex items-center">
                                    <i class="fas fa-plus text-green-600 mr-2"></i>
                                    <code class="bg-gray-200 px-2 py-1 rounded">INDEX</code>
                                    <span class="ml-2 text-gray-600">- Location-based query optimization</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <!-- Migration Action -->
                    <?php if (!$gpsFieldsExist): ?>
                    <form method="POST" class="border-t pt-6">
                        <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                        
                        <div class="flex items-center justify-between">
                            <div>
                                <h3 class="text-lg font-semibold text-gray-900">Run Migration</h3>
                                <p class="text-sm text-gray-600">This will add GPS location fields to your database.</p>
                            </div>
                            <button type="submit" name="migrate" 
                                    class="bg-primary text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition duration-300"
                                    onclick="return confirm('Are you sure you want to run this migration? This will modify your database structure.')">
                                <i class="fas fa-database mr-2"></i>Run Migration
                            </button>
                        </div>
                    </form>
                    <?php else: ?>
                    <div class="border-t pt-6">
                        <div class="text-center">
                            <i class="fas fa-check-circle text-green-600 text-4xl mb-4"></i>
                            <h3 class="text-lg font-semibold text-gray-900 mb-2">Migration Complete</h3>
                            <p class="text-gray-600 mb-4">GPS location fields are ready to use.</p>
                            <a href="../attendance/form.php?program=1" 
                               class="bg-green-600 text-white px-6 py-2 rounded-lg hover:bg-green-700 transition duration-300">
                                <i class="fas fa-map-marker-alt mr-2"></i>Test GPS Location
                            </a>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

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