TKK_E32230273/export_schedule.php

91 lines
3.1 KiB
PHP

<?php
include_once 'koneksi.php';
date_default_timezone_set('Asia/Jakarta');
$start_month = isset($_GET['start_month']) ? (int)$_GET['start_month'] : (int)date('m');
$end_month = isset($_GET['end_month']) && $_GET['end_month'] !== '' ? (int)$_GET['end_month'] : $start_month;
$year = isset($_GET['year']) ? (int)$_GET['year'] : (int)date('Y');
$format = isset($_GET['format']) ? $_GET['format'] : 'excel';
$monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'];
// Ensure validity of month index
$sm_idx = $start_month - 1; if($sm_idx < 0) $sm_idx = 0; if($sm_idx > 11) $sm_idx = 11;
$em_idx = $end_month - 1; if($em_idx < 0) $em_idx = 0; if($em_idx > 11) $em_idx = 11;
$start_name = $monthNames[$sm_idx];
$end_name = $monthNames[$em_idx];
if ($start_month === $end_month) {
$period_title = "Month $start_name $year";
} else {
$period_title = "Months $start_name - $end_name $year";
}
$stmt = $pdo->prepare("SELECT * FROM schedule WHERE (YEAR(date) = :y AND MONTH(date) >= :sm AND MONTH(date) <= :em) ORDER BY date ASC");
$stmt->execute(['y' => $year, 'sm' => $start_month, 'em' => $end_month]);
$data = $stmt->fetchAll();
if ($format === 'excel') {
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Schedule_Export_$year.xls");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Export Schedule Data</title>
<style>
body { font-family: sans-serif; }
table { border-collapse: collapse; width: 100%; }
table, th, td { border: 1px solid #000; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2 style="text-align: center;">Schedule Data (Activities)</h2>
<h4 style="text-align: center;">Period: <?= $period_title ?></h4>
<p style="text-align: center; font-style: italic;">Export Data Activity Description Room</p>
<table border="1">
<thead>
<tr>
<th style="width:50px; text-align:center;">No</th>
<th style="width:150px;">Date</th>
<th style="width:100px;">Time</th>
<th>Activity</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
if(count($data) > 0):
foreach($data as $row):
$dt = new DateTime($row['date']);
?>
<tr>
<td style="text-align:center;"><?= $no++ ?></td>
<td><?= $dt->format('d M Y') ?></td>
<td><?= $dt->format('H:i') ?></td>
<td><?= htmlspecialchars($row['kegiatan']) ?></td>
</tr>
<?php
endforeach;
else:
?>
<tr>
<td colspan="4" style="text-align:center;">No activity data for this period.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php if ($format === 'pdf'): ?>
<script>
window.onload = function() {
window.print();
}
</script>
<?php endif; ?>
</body>
</html>