Sindbad~EG File Manager
<?php
require_once '../config/config.php';
header('Content-Type: application/json');
// Verify access code
$input = json_decode(file_get_contents('php://input'), true);
$access_code = $input['access_code'] ?? '';
if (empty($access_code)) {
echo json_encode(['success' => false, 'message' => 'Access code required']);
exit;
}
$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) {
echo json_encode(['success' => false, 'message' => 'Invalid access code']);
exit;
}
try {
// Build WHERE clause based on filters
$where_conditions = [];
$params = [];
if (!empty($input['program'])) {
$where_conditions[] = "ar.program_id = ?";
$params[] = $input['program'];
}
if (!empty($input['district'])) {
$where_conditions[] = "ar.district_id = ?";
$params[] = $input['district'];
}
if (!empty($input['officer_type'])) {
$where_conditions[] = "ar.officer_type = ?";
$params[] = $input['officer_type'];
}
if (!empty($input['from_date'])) {
$where_conditions[] = "DATE(ar.submitted_at) >= ?";
$params[] = $input['from_date'];
}
if (!empty($input['to_date'])) {
$where_conditions[] = "DATE(ar.submitted_at) <= ?";
$params[] = $input['to_date'];
}
$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';
// Determine grouping based on group_by parameter
$group_by = $input['group_by'] ?? 'district';
$group_field = '';
$group_name = '';
switch ($group_by) {
case 'officer_type':
$group_field = 'ar.officer_type';
$group_name = 'officer_type';
break;
default: // district
$group_field = 'ld.name';
$group_name = 'district_name';
break;
}
// Get grouped data for chart
$chart_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
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";
$chart_stmt = $conn->prepare($chart_query);
$chart_stmt->execute($params);
$chart_data = $chart_stmt->fetchAll();
// Get total statistics
$stats_query = "SELECT
COUNT(ar.id) as total_records,
COUNT(DISTINCT $group_field) as unique_groups
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";
$stats_stmt = $conn->prepare($stats_query);
$stats_stmt->execute($params);
$stats = $stats_stmt->fetch();
// Calculate average per group
$average_per_group = $stats['unique_groups'] > 0 ? round($stats['total_records'] / $stats['unique_groups'], 1) : 0;
$stats['average_per_group'] = $average_per_group;
// Process chart data for better display
foreach ($chart_data as &$item) {
// Limit names display to first 5 names for readability
$names_array = explode(', ', $item['names']);
if (count($names_array) > 5) {
$item['names_display'] = implode(', ', array_slice($names_array, 0, 5)) . ' and ' . (count($names_array) - 5) . ' more';
} else {
$item['names_display'] = $item['names'];
}
// Ensure group_label is not null
if (empty($item['group_label'])) {
$item['group_label'] = 'Not Specified';
}
}
echo json_encode([
'success' => true,
'stats' => $stats,
'chart_data' => $chart_data,
'group_by' => $group_by,
'group_name' => $group_name
]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists