File "export.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/staff_roles/export.php
File size: 3.02 KiB (3088 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
try {
$exportType = isset($_GET['export']) ? $_GET['export'] : 'excel';
$filterSearch = isset($_GET['filter_search']) ? trim($_GET['filter_search']) : '';
$filterStatus = isset($_GET['filter_status']) ? trim($_GET['filter_status']) : '';
$conditions = [];
$bindings = [];
if ($filterSearch !== '') { $conditions[] = 'role_name LIKE :fs'; $bindings[':fs'] = "%$filterSearch%"; }
if ($filterStatus !== '') { $conditions[] = 'status = :fst'; $bindings[':fst'] = $filterStatus; }
$where = count($conditions) ? ('WHERE ' . implode(' AND ', $conditions)) : '';
$sql = "SELECT role_id, role_name, status, created_at, updated_at FROM staff_role_types $where ORDER BY role_name ASC";
$stmt = $pdo->prepare($sql);
foreach($bindings as $k=>$v){ $stmt->bindValue($k,$v,PDO::PARAM_STR);}
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($exportType === 'print') {
exportToPrint($rows, 'Staff Roles');
} else {
exportToExcel($rows, 'Staff_Roles');
}
} catch (Throwable $e) {
http_response_code(500);
echo 'Error: ' . $e->getMessage();
}
function exportToExcel(array $rows, string $filename): void {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
echo '<table border="1">';
echo '<tr><th>ID</th><th>Role Name</th><th>Status</th><th>Created At</th><th>Updated At</th></tr>';
foreach ($rows as $r) {
echo '<tr>';
echo '<td>' . htmlspecialchars($r['role_id']) . '</td>';
echo '<td>' . htmlspecialchars($r['role_name']) . '</td>';
echo '<td>' . htmlspecialchars($r['status']) . '</td>';
echo '<td>' . htmlspecialchars($r['created_at']) . '</td>';
echo '<td>' . htmlspecialchars($r['updated_at']) . '</td>';
echo '</tr>';
}
echo '</table>';
}
function exportToPrint(array $rows, string $title): void {
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><title>' . $title . '</title>';
echo '<style>body{font-family:Arial,sans-serif;margin:20px}table{border-collapse:collapse;width:100%}th,td{border:1px solid #ddd;padding:6px;text-align:left}th{background:#f2f2f2}</style>';
echo '</head><body>';
echo '<h2>' . $title . '</h2>';
echo '<table>';
echo '<tr><th>ID</th><th>Role Name</th><th>Status</th><th>Created At</th><th>Updated At</th></tr>';
foreach ($rows as $r) {
echo '<tr>';
echo '<td>' . htmlspecialchars($r['role_id']) . '</td>';
echo '<td>' . htmlspecialchars($r['role_name']) . '</td>';
echo '<td>' . htmlspecialchars($r['status']) . '</td>';
echo '<td>' . htmlspecialchars($r['created_at']) . '</td>';
echo '<td>' . htmlspecialchars($r['updated_at']) . '</td>';
echo '</tr>';
}
echo '</table>';
echo '<script>window.print();</script>';
echo '</body></html>';
}
?>