Court Status
‘auto’, // auto | open | closed | caution
‘message’ => ”, // Custom message (empty = use default)
‘updatedBy’ => ”, // Who made the change
‘updatedAt’ => ”, // Timestamp
‘hours’ => ‘Mon–Sun 10am–3pm’, // Current season hours
‘leagueNote’=> ”, // e.g. “League play starts at 5:30pm”
];
// ============================
// READ CURRENT STATUS
// ============================
function getStatus($file, $default) {
if (file_exists($file)) {
$data = json_decode(file_get_contents($file), true);
if ($data) return array_merge($default, $data);
}
return $default;
}
// ============================
// HANDLE GET — Return status
// ============================
if ($_SERVER[‘REQUEST_METHOD’] === ‘GET’) {
$status = getStatus($STATUS_FILE, $default);
// Don’t expose PINs
echo json_encode($status);
exit;
}
// ============================
// HANDLE POST — Update status
// ============================
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
$input = json_decode(file_get_contents(‘php://input’), true);
if (!$input) {
http_response_code(400);
echo json_encode([‘error’ => ‘Invalid JSON’]);
exit;
}
// Verify PIN
$pin = $input[‘pin’] ?? ”;
if ($pin !== $ADMIN_PIN && $pin !== $STAFF_PIN) {
http_response_code(403);
echo json_encode([‘error’ => ‘Invalid PIN’]);
exit;
}
$role = ($pin === $ADMIN_PIN) ? ‘Admin’ : ‘Staff’;
// Build new status
$current = getStatus($STATUS_FILE, $default);
if (isset($input[‘status’])) {
$allowed = [‘auto’, ‘open’, ‘closed’, ‘caution’];
if (in_array($input[‘status’], $allowed)) {
$current[‘status’] = $input[‘status’];
}
}
if (isset($input[‘message’])) {
$current[‘message’] = substr(strip_tags($input[‘message’]), 0, 300);
}
if (isset($input[‘hours’])) {
$current[‘hours’] = substr(strip_tags($input[‘hours’]), 0, 100);
}
if (isset($input[‘leagueNote’])) {
$current[‘leagueNote’] = substr(strip_tags($input[‘leagueNote’]), 0, 200);
}
$current[‘updatedBy’] = $role . ‘ (‘ . ($input[‘name’] ?? ‘Unknown’) . ‘)’;
$current[‘updatedAt’] = date(‘c’);
// Save
$written = file_put_contents($STATUS_FILE, json_encode($current, JSON_PRETTY_PRINT));
if ($written === false) {
http_response_code(500);
echo json_encode([‘error’ => ‘Could not save status. Check file permissions.’]);
exit;
}
echo json_encode([‘success’ => true, ‘status’ => $current]);
exit;
}
http_response_code(405);
echo json_encode([‘error’ => ‘Method not allowed’]);