File "export.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/squads/export.php
File size: 5.3 KiB (5432 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
header('Content-Type: application/json');
function respond($ok, $msg, $extra = []) {
echo json_encode(($ok ? ['status' => 'success'] : ['status' => 'error']) + ['message' => $msg] + $extra);
exit;
}
try {
$export = $_GET['export'] ?? '';
$examDate = $_GET['exam_date'] ?? '';
$session = $_GET['session'] ?? '';
$staffId = $_GET['staff_id'] ?? '';
$status = $_GET['status'] ?? '';
$where = [];
$params = [];
if ($examDate) {
$where[] = "s.exam_date = ?";
$params[] = $examDate;
}
if ($session) {
$where[] = "s.session = ?";
$params[] = $session;
}
if ($staffId) {
$where[] = "s.staff_id = ?";
$params[] = $staffId;
}
if ($status) {
$where[] = "s.status = ?";
$params[] = $status;
}
$whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : '';
$sql = "
SELECT s.*,
st.first_name, st.last_name, st.staff_code
FROM squads s
LEFT JOIN staff st ON s.staff_id = st.staff_id
$whereClause
ORDER BY s.exam_date DESC, s.session, s.squad_name
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($export === 'excel') {
// Set headers for Excel download
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="squad_report_' . date('Y-m-d') . '.xls"');
echo "<table border='1'>";
echo "<tr>";
echo "<th>Sl.No</th>";
echo "<th>Squad Name</th>";
echo "<th>Exam Date</th>";
echo "<th>Session</th>";
echo "<th>Staff Name</th>";
echo "<th>Staff Code</th>";
echo "<th>Status</th>";
echo "</tr>";
foreach ($data as $index => $row) {
echo "<tr>";
echo "<td>" . ($index + 1) . "</td>";
echo "<td>" . htmlspecialchars($row['squad_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['exam_date']) . "</td>";
echo "<td>" . htmlspecialchars($row['session']) . "</td>";
echo "<td>" . htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['staff_code']) . "</td>";
echo "<td>" . htmlspecialchars($row['status']) . "</td>";
echo "</tr>";
}
echo "</table>";
exit;
} elseif ($export === 'print') {
// Set headers for print
header('Content-Type: text/html');
echo "<!DOCTYPE html>";
echo "<html><head><title>Squad Report</title>";
echo "<style>";
echo "body { font-family: Arial, sans-serif; margin: 20px; }";
echo "table { width: 100%; border-collapse: collapse; margin-top: 20px; }";
echo "th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }";
echo "th { background-color: #f2f2f2; font-weight: bold; }";
echo ".header { text-align: center; margin-bottom: 20px; }";
echo ".badge { padding: 2px 6px; border-radius: 3px; font-size: 12px; }";
echo ".bg-primary { background-color: #007bff; color: white; }";
echo ".bg-warning { background-color: #ffc107; color: black; }";
echo ".bg-success { background-color: #28a745; color: white; }";
echo ".bg-danger { background-color: #dc3545; color: white; }";
echo "</style></head><body>";
echo "<div class='header'>";
echo "<h2>Squad Report</h2>";
echo "<p>Generated on: " . date('Y-m-d H:i:s') . "</p>";
if ($examDate) echo "<p>Exam Date: " . htmlspecialchars($examDate) . "</p>";
if ($session) echo "<p>Session: " . htmlspecialchars($session) . "</p>";
echo "</div>";
echo "<table>";
echo "<tr>";
echo "<th>Sl.No</th>";
echo "<th>Squad Name</th>";
echo "<th>Exam Date</th>";
echo "<th>Session</th>";
echo "<th>Staff Name</th>";
echo "<th>Staff Code</th>";
echo "<th>Status</th>";
echo "</tr>";
foreach ($data as $index => $row) {
echo "<tr>";
echo "<td>" . ($index + 1) . "</td>";
echo "<td>" . htmlspecialchars($row['squad_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['exam_date']) . "</td>";
echo "<td><span class='badge " . ($row['session'] === 'FN' ? 'bg-primary' : 'bg-warning') . "'>" . htmlspecialchars($row['session']) . "</span></td>";
echo "<td>" . htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['staff_code']) . "</td>";
echo "<td><span class='badge " . ($row['status'] === 'Active' ? 'bg-success' : 'bg-danger') . "'>" . htmlspecialchars($row['status']) . "</span></td>";
echo "</tr>";
}
echo "</table>";
echo "</body></html>";
exit;
} else {
respond(true, 'Data retrieved successfully', ['data' => $data]);
}
} catch (Exception $e) {
respond(false, $e->getMessage());
}
?>