Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,66 @@
<?php
header('Content-Type: application/json; charset=utf-8');
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
$holidays = [];
function getThaiHolidayName($title) {
$title = strtolower($title);
if (strpos($title, "new year's day") !== false) return "วันขึ้นปีใหม่";
if (strpos($title, "makha bucha") !== false) return "วันมาฆบูชา";
if (strpos($title, "chakri") !== false) return "วันจักรี";
if (strpos($title, "songkran") !== false) return "เทศกาลสงกรานต์";
if (strpos($title, "labour") !== false || strpos($title, "labor") !== false) return "วันแรงงานแห่งชาติ";
if (strpos($title, "coronation") !== false) return "วันฉัตรมงคล";
if (strpos($title, "visakha bucha") !== false) return "วันวิสาขบูชา";
if (strpos($title, "queen suthida") !== false) return "วันเฉลิมพระชนมพรรษา สมเด็จพระนางเจ้าฯ พระบรมราชินี";
if (strpos($title, "maha vajiralongkorn") !== false) return "วันเฉลิมพระชนมพรรษา พระบาทสมเด็จพระเจ้าอยู่หัว";
if (strpos($title, "asanha bucha") !== false) return "วันอาสาฬหบูชา";
if (strpos($title, "buddhist lent") !== false || strpos($title, "khao phansa") !== false) return "วันเข้าพรรษา";
if (strpos($title, "queen sirikit") !== false || strpos($title, "mother's day") !== false) return "วันเฉลิมพระชนมพรรษา สมเด็จพระบรมราชชนนีพันปีหลวง และวันแม่แห่งชาติ";
if (strpos($title, "bhumibol adulyadej the great memorial") !== false || strpos($title, "navamindra") !== false) return "วันนวมินทรมหาราช";
if (strpos($title, "chulalongkorn") !== false) return "วันปิยมหาราช";
if (strpos($title, "bhumibol adulyadej's birthday") !== false || strpos($title, "father's day") !== false) return "วันคล้ายวันพระบรมราชสมภพ ร.9 และวันพ่อแห่งชาติ";
if (strpos($title, "constitution") !== false) return "วันรัฐธรรมนูญ";
if (strpos($title, "new year's eve") !== false) return "วันสิ้นปี";
if (strpos($title, "royal ploughing") !== false) return "วันพืชมงคล";
if (strpos($title, "special") !== false) return "วันหยุดพิเศษ";
return $title; // fallback
}
try {
$apiUrl = "https://thailandformats.com/api/v1/holidays/" . $year;
$ctx = stream_context_create(array('http' => array('timeout' => 5)));
$holidayJson = @file_get_contents($apiUrl, false, $ctx);
if ($holidayJson) {
$holidayData = json_decode($holidayJson, true);
if (isset($holidayData['holidays'])) {
foreach ($holidayData['holidays'] as $h) {
$startDate = new DateTime($h['start_date']);
$endDate = new DateTime($h['end_date']);
$titleEn = $h['title'];
$isSub = (stripos($titleEn, 'substitution') !== false || stripos($titleEn, 'observed') !== false);
$name = getThaiHolidayName($titleEn);
if ($isSub) {
$name = 'ชดเชย' . str_replace('ชดเชย', '', $name);
}
for ($d = $startDate; $d <= $endDate; $d->modify('+1 day')) {
$dateStr = $d->format('Y-m-d');
$holidays[$dateStr] = $name;
}
}
}
}
echo json_encode(['status' => 'success', 'data' => $holidays]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>
@@ -0,0 +1,51 @@
<?php
require_once '../config.php';
header('Content-Type: application/json; charset=utf-8');
try {
// Auto-create table
$pdo->exec("CREATE TABLE IF NOT EXISTS `monthly_locks` (
`year` int(4) NOT NULL,
`month` int(2) NOT NULL,
`is_locked` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`year`,`month`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$year = isset($_GET['year']) ? intval($_GET['year']) : date('Y');
$month = isset($_GET['month']) ? intval($_GET['month']) : date('n');
$stmt = $pdo->prepare("SELECT is_locked FROM monthly_locks WHERE year = ? AND month = ?");
$stmt->execute([$year, $month]);
$row = $stmt->fetch();
echo json_encode([
'status' => 'success',
'is_locked' => $row ? (bool)$row['is_locked'] : false
]);
exit;
}
if ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
$year = intval($data['year']);
$month = intval($data['month']);
$is_locked = empty($data['is_locked']) ? 0 : 1;
$stmt = $pdo->prepare("INSERT INTO monthly_locks (year, month, is_locked) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE is_locked = VALUES(is_locked)");
$stmt->execute([$year, $month, $is_locked]);
echo json_encode([
'status' => 'success',
'is_locked' => (bool)$is_locked
]);
exit;
}
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => $e->getMessage()
]);
}
@@ -0,0 +1,140 @@
<?php
require_once __DIR__ . '/../config.php';
// Telegram Bot Configuration - Please change these
define('TELEGRAM_BOT_TOKEN', 'YOUR_TELEGRAM_BOT_TOKEN_HERE');
define('TELEGRAM_CHAT_ID', 'YOUR_TELEGRAM_CHAT_ID_HERE');
// Optionally get date from query param, default to today
$dateStr = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$date = new DateTime($dateStr);
$year = (int)$date->format('Y');
$month = (int)$date->format('n');
$day = (int)$date->format('j');
// Thai Date formatting
$thaiMonths = [
1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน',
5 => 'พฤษภาคม', 6 => 'มิถุนายน', 7 => 'กรกฎาคม', 8 => 'สิงหาคม',
9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม'
];
$thaiDays = [
'Sunday' => 'อาทิตย์', 'Monday' => 'จันทร์', 'Tuesday' => 'อังคาร',
'Wednesday' => 'พุธ', 'Thursday' => 'พฤหัสบดี', 'Friday' => 'ศุกร์', 'Saturday' => 'เสาร์'
];
$thaiDateStr = "วัน" . $thaiDays[$date->format('l')] . "ที่ " . $day . " " . $thaiMonths[$month] . " " . ($year + 543);
try {
// Get schedules for the target date
$stmt = $pdo->prepare("
SELECT s.shift_type, st.name, st.phone
FROM schedules s
JOIN staff st ON s.staff_id = st.id
WHERE s.year = :year AND s.month = :month AND s.day = :day
");
$stmt->execute([':year' => $year, ':month' => $month, ':day' => $day]);
$schedules = $stmt->fetchAll();
if (empty($schedules)) {
// No schedule for today, maybe send nothing or send a specific message
$message = "\n📅 <b>ประกาศตารางเวร</b>\n$thaiDateStr\n\n✨ <i>วันนี้ไม่มีผู้เข้าเวรพักผ่อนให้เต็มที่ครับ</i> ✨";
} else {
$morning = [];
$afternoon = [];
$both = [];
$leave = [];
foreach ($schedules as $row) {
$nameInfo = $row['name'];
if (!empty($row['phone'])) {
$nameInfo .= " (📞 " . $row['phone'] . ")";
}
if ($row['shift_type'] === 'ช') {
$morning[] = $nameInfo;
} elseif ($row['shift_type'] === 'บ') {
$afternoon[] = $nameInfo;
} elseif ($row['shift_type'] === 'ช/บ') {
$both[] = $nameInfo;
} elseif ($row['shift_type'] === 'ลา') {
$leave[] = $nameInfo;
}
}
// Build beautiful message using HTML tags for Telegram
$message = "📢 <b>แจ้งเตือนตารางเวรประจำวัน</b> 📢\n";
$message .= "🗓️ <b>$thaiDateStr</b>\n";
$message .= str_repeat("", 12) . "\n\n";
if (!empty($morning) || !empty($both)) {
$message .= "🌅 <b>เวรเช้า (08:30 - 16:30 น.)</b>\n";
foreach ($both as $name) {
$message .= " 🔹 " . $name . "\n";
}
foreach ($morning as $name) {
$message .= " 🔹 " . $name . "\n";
}
$message .= "\n";
}
if (!empty($afternoon) || !empty($both)) {
$message .= "🌇 <b>เวรบ่าย (16:30 - 00:30 น.)</b>\n";
foreach ($both as $name) {
$message .= " 🔸 " . $name . "\n";
}
foreach ($afternoon as $name) {
$message .= " 🔸 " . $name . "\n";
}
$message .= "\n";
}
if (!empty($leave)) {
$message .= "⛔ <b>ลาพัก/ลากิจ</b>\n";
foreach ($leave as $name) {
$message .= "" . $name . "\n";
}
$message .= "\n";
}
$message .= "💡 <i>ศูนย์คอมพิวเตอร์ ฝ่ายคอมพิวเตอร์และสารสนเทศ</i>";
}
// Only send to Telegram if token is set and not default
if (TELEGRAM_BOT_TOKEN !== 'YOUR_TELEGRAM_BOT_TOKEN_HERE' && !empty(TELEGRAM_BOT_TOKEN)) {
$url = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/sendMessage";
$postData = [
'chat_id' => TELEGRAM_CHAT_ID,
'text' => $message,
'parse_mode' => 'HTML'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo json_encode([
'status' => 'success',
'message' => 'Notification sent successfully',
'response' => json_decode($result),
'text' => $message // For preview
]);
} else {
echo json_encode([
'status' => 'success',
'message' => 'Preview mode (No Telegram Token)',
'text' => $message // For preview
]);
}
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>
@@ -0,0 +1,69 @@
<?php
require_once '../config.php';
$method = $_SERVER['REQUEST_METHOD'];
// Ensure table exists
try {
$pdo->exec("
CREATE TABLE IF NOT EXISTS `oncall_days` (
`day_of_week` int(11) NOT NULL,
`staff_id` int(11) NOT NULL,
PRIMARY KEY (`day_of_week`),
CONSTRAINT `fk_oncall_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
} catch (PDOException $e) {}
switch ($method) {
case 'GET':
try {
$stmt = $pdo->query("SELECT day_of_week, staff_id FROM oncall_days");
$data = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); // returns [day_of_week => staff_id]
// If empty, return defaults 1->1, 2->2...
if (empty($data)) {
$data = [1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5];
}
echo json_encode(['status' => 'success', 'data' => $data]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
case 'POST':
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['day_of_week']) || !isset($input['staff_id'])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
exit;
}
try {
$stmt = $pdo->prepare("
INSERT INTO oncall_days (day_of_week, staff_id)
VALUES (:day_of_week, :staff_id)
ON DUPLICATE KEY UPDATE staff_id = :staff_id_update
");
$stmt->execute([
':day_of_week' => $input['day_of_week'],
':staff_id' => $input['staff_id'],
':staff_id_update' => $input['staff_id']
]);
echo json_encode(['status' => 'success', 'message' => 'On-call updated successfully']);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
default:
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
break;
}
?>
@@ -0,0 +1,130 @@
<?php
require_once '../config.php';
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
// Get schedules for a specific month and year
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
$month = isset($_GET['month']) ? (int)$_GET['month'] : date('n');
try {
$stmt = $pdo->prepare("SELECT staff_id, day, shift_type FROM schedules WHERE year = :year AND month = :month");
$stmt->execute([':year' => $year, ':month' => $month]);
$schedules = $stmt->fetchAll();
// Format data for frontend: { staffId: { day: 'shift_type' } }
$formattedData = [];
foreach ($schedules as $row) {
$staffId = $row['staff_id'];
$day = $row['day'];
$shiftType = $row['shift_type'];
if (!isset($formattedData[$staffId])) {
$formattedData[$staffId] = [];
}
$formattedData[$staffId][$day] = $shiftType;
}
echo json_encode(['status' => 'success', 'data' => $formattedData]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
case 'POST':
// Save or update a shift
$input = json_decode(file_get_contents('php://input'), true);
// Check if this is a bulk operation
if (isset($input['bulk']) && is_array($input['schedules'])) {
try {
$pdo->beginTransaction();
// Optional: clear existing month data if requested
if (isset($input['clear_month']) && $input['clear_month']) {
$stmt = $pdo->prepare("DELETE FROM schedules WHERE year = :year AND month = :month");
$stmt->execute([
':year' => $input['year'],
':month' => $input['month']
]);
}
$insertStmt = $pdo->prepare("INSERT INTO schedules (staff_id, year, month, day, shift_type) VALUES (:staff_id, :year, :month, :day, :shift_type) ON DUPLICATE KEY UPDATE shift_type = :shift_type");
foreach ($input['schedules'] as $schedule) {
if ($schedule['shift_type'] !== '') {
$insertStmt->execute([
':staff_id' => $schedule['staff_id'],
':year' => $input['year'],
':month' => $input['month'],
':day' => $schedule['day'],
':shift_type' => $schedule['shift_type']
]);
}
}
$pdo->commit();
echo json_encode(['status' => 'success', 'message' => 'Bulk schedules saved']);
} catch (PDOException $e) {
$pdo->rollBack();
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
exit;
}
if (!isset($input['staff_id']) || !isset($input['year']) || !isset($input['month']) || !isset($input['day'])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
exit;
}
$staffId = $input['staff_id'];
$year = $input['year'];
$month = $input['month'];
$day = $input['day'];
$shiftType = isset($input['shift_type']) ? $input['shift_type'] : '';
try {
if ($shiftType === '') {
// If shift_type is empty, delete the record
$stmt = $pdo->prepare("DELETE FROM schedules WHERE staff_id = :staff_id AND year = :year AND month = :month AND day = :day");
$stmt->execute([
':staff_id' => $staffId,
':year' => $year,
':month' => $month,
':day' => $day
]);
} else {
// Insert or update (upsert)
$stmt = $pdo->prepare("
INSERT INTO schedules (staff_id, year, month, day, shift_type)
VALUES (:staff_id, :year, :month, :day, :shift_type)
ON DUPLICATE KEY UPDATE shift_type = :shift_type_update
");
$stmt->execute([
':staff_id' => $staffId,
':year' => $year,
':month' => $month,
':day' => $day,
':shift_type' => $shiftType,
':shift_type_update' => $shiftType
]);
}
echo json_encode(['status' => 'success', 'message' => 'Schedule updated successfully']);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
default:
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
break;
}
?>
@@ -0,0 +1,101 @@
<?php
require_once '../config.php';
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
// Get all staff
try {
// Also try to add phone and preferences columns if they don't exist
try { $pdo->exec("ALTER TABLE staff ADD COLUMN phone VARCHAR(20) DEFAULT NULL"); } catch (Exception $e) {}
try { $pdo->exec("ALTER TABLE staff ADD COLUMN pref_weekdays VARCHAR(50) DEFAULT NULL"); } catch (Exception $e) {}
try { $pdo->exec("ALTER TABLE staff ADD COLUMN pref_weekend VARCHAR(10) DEFAULT NULL"); } catch (Exception $e) {}
$stmt = $pdo->query("SELECT id, name, phone, pref_weekdays, pref_weekend FROM staff ORDER BY id ASC");
$staff = $stmt->fetchAll();
echo json_encode(['status' => 'success', 'data' => $staff]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
case 'PUT':
case 'POST':
// Add or Update staff name, phone, and preferences
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['name'])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing name']);
exit;
}
try {
$phone = isset($input['phone']) ? $input['phone'] : null;
$pref_weekdays = isset($input['pref_weekdays']) ? $input['pref_weekdays'] : null;
$pref_weekend = isset($input['pref_weekend']) ? $input['pref_weekend'] : null;
if (isset($input['id']) && $input['id']) {
// Update existing
$stmt = $pdo->prepare("UPDATE staff SET name = :name, phone = :phone, pref_weekdays = :pref_weekdays, pref_weekend = :pref_weekend WHERE id = :id");
$stmt->execute([
':name' => $input['name'],
':phone' => $phone,
':pref_weekdays' => $pref_weekdays,
':pref_weekend' => $pref_weekend,
':id' => $input['id']
]);
echo json_encode(['status' => 'success', 'message' => 'Staff updated successfully']);
} else {
// Insert new
$stmt = $pdo->prepare("INSERT INTO staff (name, phone, pref_weekdays, pref_weekend) VALUES (:name, :phone, :pref_weekdays, :pref_weekend)");
$stmt->execute([
':name' => $input['name'],
':phone' => $phone,
':pref_weekdays' => $pref_weekdays,
':pref_weekend' => $pref_weekend
]);
$newId = $pdo->lastInsertId();
echo json_encode(['status' => 'success', 'message' => 'Staff added successfully', 'id' => $newId]);
}
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
case 'DELETE':
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['id'])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Missing id']);
exit;
}
try {
// Delete staff
$stmt = $pdo->prepare("DELETE FROM staff WHERE id = :id");
$stmt->execute([':id' => $input['id']]);
// Delete related schedules
$stmt = $pdo->prepare("DELETE FROM schedules WHERE staff_id = :id");
$stmt->execute([':id' => $input['id']]);
// Delete related oncall
$stmt = $pdo->prepare("DELETE FROM oncall_days WHERE staff_id = :id");
$stmt->execute([':id' => $input['id']]);
echo json_encode(['status' => 'success', 'message' => 'Staff deleted successfully']);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
break;
default:
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
break;
}
?>