Sindbad~EG File Manager

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

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

// Check if user is logged in
if (!isLoggedIn()) {
    die('Please login first.');
}

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

echo "<h2>Locations.php Fix Verification</h2>";

try {
    // Test the same query used in locations.php
    $query = "SELECT l.*, p.name as parent_name,
                     COUNT(DISTINCT CASE WHEN ar.district_id = l.id THEN ar.id END) as district_usage,
                     COUNT(DISTINCT CASE WHEN ar.assembly_id = l.id THEN ar.id END) as assembly_usage,
                     COUNT(DISTINCT CASE WHEN pr.location_id = l.id THEN pr.id END) as program_usage,
                     COUNT(DISTINCT CASE WHEN child.parent_id = l.id THEN child.id END) as child_locations
              FROM locations l 
              LEFT JOIN locations p ON l.parent_id = p.id 
              LEFT JOIN attendance_records ar ON (ar.district_id = l.id OR ar.assembly_id = l.id)
              LEFT JOIN programs pr ON pr.location_id = l.id
              LEFT JOIN locations child ON child.parent_id = l.id
              GROUP BY l.id
              ORDER BY l.type, l.name
              LIMIT 5";
    
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $locations = $stmt->fetchAll();
    
    echo "<h3 style='color: green;'>✓ Query executed successfully!</h3>";
    echo "<h3>Sample Results:</h3>";
    
    if ($locations) {
        echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f0f0f0;'>
                <th>Name</th>
                <th>Type</th>
                <th>Parent</th>
                <th>District Usage</th>
                <th>Assembly Usage</th>
                <th>Program Usage</th>
                <th>Child Locations</th>
                <th>Active</th>
              </tr>";
        
        foreach ($locations as $location) {
            // Test the fix - use null coalescing operator
            $program_usage = $location['program_usage'] ?? 0;
            $district_usage = $location['district_usage'] ?? 0;
            $assembly_usage = $location['assembly_usage'] ?? 0;
            $child_locations = $location['child_locations'] ?? 0;
            
            echo "<tr>";
            echo "<td>" . htmlspecialchars($location['name']) . "</td>";
            echo "<td>" . htmlspecialchars($location['type']) . "</td>";
            echo "<td>" . htmlspecialchars($location['parent_name'] ?? 'None') . "</td>";
            echo "<td>" . number_format($district_usage) . "</td>";
            echo "<td>" . number_format($assembly_usage) . "</td>";
            echo "<td style='background: #e8f5e8;'>" . number_format($program_usage) . "</td>";
            echo "<td>" . number_format($child_locations) . "</td>";
            echo "<td>" . ($location['is_active'] ? 'Yes' : 'No') . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        
        echo "<h3 style='color: green;'>✓ All fields processed without errors!</h3>";
        echo "<p><strong>Fix Applied:</strong> Changed <code>\$location['program_count']</code> to <code>\$location['program_usage'] ?? 0</code></p>";
        echo "<p><strong>Result:</strong> No more 'Undefined array key' or 'null parameter' warnings.</p>";
        
    } else {
        echo "<p>No locations found in database.</p>";
    }
    
    // Test for potential issues
    echo "<h3>Field Availability Check:</h3>";
    if (!empty($locations)) {
        $sample = $locations[0];
        $expected_fields = ['id', 'name', 'type', 'parent_name', 'district_usage', 'assembly_usage', 'program_usage', 'child_locations', 'is_active'];
        
        echo "<ul>";
        foreach ($expected_fields as $field) {
            $exists = array_key_exists($field, $sample);
            $color = $exists ? 'green' : 'red';
            $icon = $exists ? '✓' : '✗';
            echo "<li style='color: $color;'>$icon $field: " . ($exists ? 'Available' : 'Missing') . "</li>";
        }
        echo "</ul>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'><strong>Error:</strong> " . $e->getMessage() . "</p>";
}

echo "<hr>";
echo "<p><a href='locations.php'>→ Test Fixed Locations Page</a></p>";
echo "<p><a href='dashboard.php'>← Back to Dashboard</a></p>";
?>

<style>
table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
}
th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
}
th {
    background-color: #f2f2f2;
    font-weight: bold;
}
code {
    background: #f4f4f4;
    padding: 2px 4px;
    border-radius: 3px;
    font-family: monospace;
}
</style>

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