35311-vm/export_residents.php
2025-10-30 00:12:08 +00:00

78 lines
1.9 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Check if user is logged in and has the 'staff' role
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'staff') {
header("HTTP/1.1 403 Forbidden");
exit("Access denied.");
}
$pdo = db();
// -- Filtering (reuse logic from dashboard) --
$program_filter = $_GET['program'] ?? '';
$risk_filter = $_GET['risk_level'] ?? '';
$status_filter = $_GET['status'] ?? '';
$where_clauses = [];
$params = [];
if ($program_filter) {
$where_clauses[] = "program = ?";
$params[] = $program_filter;
}
if ($risk_filter) {
$where_clauses[] = "risk_level = ?";
$params[] = $risk_filter;
}
if ($status_filter) {
$where_clauses[] = "status = ?";
$params[] = $status_filter;
}
$sql = "SELECT * FROM residents"; // Select all columns for export
if (!empty($where_clauses)) {
$sql .= " WHERE " . implode(' AND ', $where_clauses);
}
$sql .= " ORDER BY last_name, first_name";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
// -- CSV Generation --
$filename = "continuum_residents_" . date('Y-m-d') . ".csv";
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $filename);
$output = fopen('php://output', 'w');
// Add header row
fputcsv($output, [
'ID', 'First Name', 'Last Name', 'Email', 'Phone Number', 'Date of Birth',
'Program', 'Status', 'Risk Level', 'Health Progress', 'Housing Progress', 'Employment Progress', 'Created At'
]);
// Add data rows
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
fputcsv($output, [
$row['id'],
$row['first_name'],
$row['last_name'],
$row['email'],
$row['phone_number'],
$row['date_of_birth'],
$row['program'],
$row['status'],
$row['risk_level'],
$row['health_progress'],
$row['housing_progress'],
$row['employment_progress'],
$row['created_at']
]);
}
fclose($output);
exit;