52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?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()
|
|
]);
|
|
}
|