Sindbad~EG File Manager
<?php
require_once '../config/config.php';
// Verify access code
$access_code = $_GET['access_code'] ?? '';
if (empty($access_code)) {
die('Access code required');
}
$db = new Database();
$conn = $db->getConnection();
// Verify special code
$query = "SELECT * FROM special_codes WHERE code = ? AND is_active = 1
AND (expires_at IS NULL OR expires_at > NOW())";
$stmt = $conn->prepare($query);
$stmt->execute([$access_code]);
$code_info = $stmt->fetch();
if (!$code_info) {
die('Invalid access code');
}
$format = $_GET['format'] ?? 'csv';
$type = $_GET['type'] ?? 'summary';
$program = $_GET['program'] ?? '';
$district = $_GET['district'] ?? '';
$officer_type = $_GET['officer_type'] ?? '';
$group_by = $_GET['group_by'] ?? 'district';
$from_date = $_GET['from_date'] ?? '';
$to_date = $_GET['to_date'] ?? '';
// Build WHERE clause
$where_conditions = [];
$params = [];
if (!empty($program)) {
$where_conditions[] = "ar.program_id = ?";
$params[] = $program;
}
if (!empty($district)) {
$where_conditions[] = "ar.district_id = ?";
$params[] = $district;
}
if (!empty($officer_type)) {
$where_conditions[] = "ar.officer_type = ?";
$params[] = $officer_type;
}
if (!empty($from_date)) {
$where_conditions[] = "DATE(ar.submitted_at) >= ?";
$params[] = $from_date;
}
if (!empty($to_date)) {
$where_conditions[] = "DATE(ar.submitted_at) <= ?";
$params[] = $to_date;
}
$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';
if ($type === 'summary') {
// Summary export
$query = "SELECT
p.name as program_name,
COUNT(ar.id) as total_attendees,
COUNT(DISTINCT ar.district_id) as districts,
COUNT(CASE WHEN ar.officer_type IS NOT NULL THEN 1 END) as officer_count,
COUNT(CASE WHEN ar.officer_type = 'Pastor' THEN 1 END) as pastor_count,
COUNT(CASE WHEN ar.officer_type = 'Elder' THEN 1 END) as elder_count
FROM attendance_records ar
JOIN programs p ON ar.program_id = p.id
$where_clause
GROUP BY ar.program_id, p.name
ORDER BY total_attendees DESC";
} elseif ($type === 'extra') {
// Extra export with GPS data
$group_field = '';
switch ($group_by) {
case 'officer_type':
$group_field = 'ar.officer_type';
break;
case 'gps_location':
$group_field = 'CONCAT(ROUND(ar.latitude, 3), ",", ROUND(ar.longitude, 3))';
break;
default: // district
$group_field = 'ld.name';
break;
}
$query = "SELECT
ar.full_name,
ar.email,
ar.telephone,
ar.officer_type,
p.name as program_name,
ld.name as district_name,
la.name as assembly_name,
CASE
WHEN ar.latitude IS NOT NULL AND ar.longitude IS NOT NULL
THEN CONCAT(ar.latitude, ', ', ar.longitude)
ELSE 'Not available'
END as gps_coordinates,
CASE
WHEN ar.location_accuracy IS NOT NULL
THEN CONCAT(ar.location_accuracy, 'm')
ELSE 'Unknown'
END as gps_accuracy,
ar.location_address,
DATE(ar.submitted_at) as attendance_date,
TIME(ar.submitted_at) as attendance_time,
$group_field as group_value
FROM attendance_records ar
JOIN programs p ON ar.program_id = p.id
LEFT JOIN locations ld ON ar.district_id = ld.id
LEFT JOIN locations la ON ar.assembly_id = la.id
$where_clause
ORDER BY group_value, ar.full_name";
} elseif ($type === 'districts') {
// Districts export with grouped data
$group_field = '';
switch ($group_by) {
case 'officer_type':
$group_field = 'ar.officer_type';
break;
default: // district
$group_field = 'ld.name';
break;
}
$query = "SELECT
$group_field as group_label,
COUNT(ar.id) as count,
GROUP_CONCAT(DISTINCT ar.full_name ORDER BY ar.full_name SEPARATOR ', ') as names,
GROUP_CONCAT(DISTINCT ar.email ORDER BY ar.email SEPARATOR ', ') as emails,
GROUP_CONCAT(DISTINCT ar.telephone ORDER BY ar.telephone SEPARATOR ', ') as phones
FROM attendance_records ar
JOIN programs p ON ar.program_id = p.id
LEFT JOIN locations ld ON ar.district_id = ld.id
LEFT JOIN locations la ON ar.assembly_id = la.id
$where_clause
GROUP BY $group_field
ORDER BY count DESC, group_label ASC";
} else {
// Details export
$query = "SELECT
ar.full_name,
ar.email,
ar.telephone,
ar.officer_type,
p.name as program_name,
ld.name as district_name,
la.name as assembly_name,
DATE(ar.submitted_at) as attendance_date,
TIME(ar.submitted_at) as attendance_time,
ar.tracking_code
FROM attendance_records ar
JOIN programs p ON ar.program_id = p.id
LEFT JOIN locations ld ON ar.district_id = ld.id
LEFT JOIN locations la ON ar.assembly_id = la.id
$where_clause
ORDER BY ar.submitted_at DESC";
}
$stmt = $conn->prepare($query);
$stmt->execute($params);
$data = $stmt->fetchAll();
$filename = 'special_attendance_' . $type . '_' . date('Y-m-d_H-i-s');
if ($format === 'csv') {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
$output = fopen('php://output', 'w');
if (!empty($data)) {
if ($type === 'extra') {
// Handle grouped export for extra reports
$headers = array_keys($data[0]);
fputcsv($output, $headers);
$currentGroup = null;
$groupCounts = [];
// Count records per group
foreach ($data as $row) {
$groupValue = $row['group_value'] ?: 'Not Specified';
$groupCounts[$groupValue] = ($groupCounts[$groupValue] ?? 0) + 1;
}
foreach ($data as $row) {
// Add group header when group changes
if ($row['group_value'] !== $currentGroup) {
$currentGroup = $row['group_value'];
$groupLabel = '';
$groupCount = $groupCounts[$currentGroup ?: 'Not Specified'];
switch ($group_by) {
case 'officer_type':
$groupLabel = 'Officer Type: ' . ($currentGroup ?: 'Not Specified');
break;
case 'gps_location':
$groupLabel = 'GPS Location: ' . ($currentGroup ?: 'No GPS Data');
break;
default:
$groupLabel = 'District: ' . ($currentGroup ?: 'Not Specified');
break;
}
// Write group header row
$groupRow = array_fill(0, count($headers), '');
$groupRow[0] = $groupLabel . ' (' . $groupCount . ' records)';
fputcsv($output, $groupRow);
}
// Write data row
fputcsv($output, $row);
}
} else {
// Regular export for summary and details
fputcsv($output, array_keys($data[0]));
foreach ($data as $row) {
fputcsv($output, $row);
}
}
}
fclose($output);
} elseif ($format === 'excel') {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
echo '<table border="1">';
if (!empty($data)) {
if ($type === 'extra') {
// Handle grouped export for extra reports
$headers = array_keys($data[0]);
// Write headers
echo '<tr>';
foreach ($headers as $header) {
echo '<th>' . htmlspecialchars(ucwords(str_replace('_', ' ', $header))) . '</th>';
}
echo '</tr>';
$currentGroup = null;
$groupCounts = [];
// Count records per group
foreach ($data as $row) {
$groupValue = $row['group_value'] ?: 'Not Specified';
$groupCounts[$groupValue] = ($groupCounts[$groupValue] ?? 0) + 1;
}
foreach ($data as $row) {
// Add group header when group changes
if ($row['group_value'] !== $currentGroup) {
$currentGroup = $row['group_value'];
$groupLabel = '';
$groupCount = $groupCounts[$currentGroup ?: 'Not Specified'];
switch ($group_by) {
case 'officer_type':
$groupLabel = 'Officer Type: ' . ($currentGroup ?: 'Not Specified');
break;
case 'gps_location':
$groupLabel = 'GPS Location: ' . ($currentGroup ?: 'No GPS Data');
break;
default:
$groupLabel = 'District: ' . ($currentGroup ?: 'Not Specified');
break;
}
// Write group header row
echo '<tr style="background-color: #dbeafe; font-weight: bold;">';
echo '<td colspan="' . count($headers) . '">' . htmlspecialchars($groupLabel . ' (' . $groupCount . ' records)') . '</td>';
echo '</tr>';
}
// Write data row
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell ?? '') . '</td>';
}
echo '</tr>';
}
} else {
// Regular export for summary and details
echo '<tr>';
foreach (array_keys($data[0]) as $header) {
echo '<th>' . htmlspecialchars(ucwords(str_replace('_', ' ', $header))) . '</th>';
}
echo '</tr>';
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell ?? '') . '</td>';
}
echo '</tr>';
}
}
}
echo '</table>';
} elseif ($format === 'pdf') {
header('Content-Type: text/html');
echo '<!DOCTYPE html>
<html>
<head>
<title>Special Attendance Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; font-size: 12px; }
th { background-color: #f2f2f2; font-weight: bold; }
.header { text-align: center; margin-bottom: 20px; }
.group-header { background-color: #dbeafe; font-weight: bold; color: #1e40af; }
@media print {
body { margin: 0; }
.no-print { display: none; }
}
</style>
</head>
<body>
<div class="header">
<h2>Special Attendance Report - ' . ucfirst($type) . '</h2>
<p>Generated on: ' . date('F j, Y g:i A') . '</p>
</div>
<table>';
if (!empty($data)) {
if ($type === 'extra') {
// Handle grouped export for extra reports
$headers = array_keys($data[0]);
// Write headers
echo '<tr>';
foreach ($headers as $header) {
echo '<th>' . htmlspecialchars(ucwords(str_replace('_', ' ', $header))) . '</th>';
}
echo '</tr>';
$currentGroup = null;
$groupCounts = [];
// Count records per group
foreach ($data as $row) {
$groupValue = $row['group_value'] ?: 'Not Specified';
$groupCounts[$groupValue] = ($groupCounts[$groupValue] ?? 0) + 1;
}
foreach ($data as $row) {
// Add group header when group changes
if ($row['group_value'] !== $currentGroup) {
$currentGroup = $row['group_value'];
$groupLabel = '';
$groupCount = $groupCounts[$currentGroup ?: 'Not Specified'];
switch ($group_by) {
case 'officer_type':
$groupLabel = 'Officer Type: ' . ($currentGroup ?: 'Not Specified');
break;
case 'gps_location':
$groupLabel = 'GPS Location: ' . ($currentGroup ?: 'No GPS Data');
break;
default:
$groupLabel = 'District: ' . ($currentGroup ?: 'Not Specified');
break;
}
// Write group header row
echo '<tr class="group-header">';
echo '<td colspan="' . count($headers) . '">' . htmlspecialchars($groupLabel . ' (' . $groupCount . ' records)') . '</td>';
echo '</tr>';
}
// Write data row
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell ?? '') . '</td>';
}
echo '</tr>';
}
} else {
// Regular export for summary and details
echo '<tr>';
foreach (array_keys($data[0]) as $header) {
echo '<th>' . htmlspecialchars(ucwords(str_replace('_', ' ', $header))) . '</th>';
}
echo '</tr>';
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell ?? '') . '</td>';
}
echo '</tr>';
}
}
}
echo '</table>
<script>
setTimeout(function() {
window.print();
}, 1000);
</script>
</body>
</html>';
}
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists