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,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;
}
?>