<?php
function getDateStatus($date, $week)
{
$now = new DateTime(); // Current date
$startDate = new DateTime($date); // Start date
$endDate = (clone $startDate)->modify("+$week week"); // End date is start + week
$windowStart = (clone $endDate)->modify("-1 week"); // 1 week before start
$isActive = $now >= $windowStart && $now <= $endDate;
$isPassed = $now > $endDate;
if ($isActive || $isPassed) {
return "deactivated"; // Cannot be deleted or modified
} else {
return "active"; // Can be deleted or modified
}
}
function createPlan($pdo, $fromDate, $week, $isActive)
{
$stmt = $pdo->prepare("INSERT INTO Plan (fromDate, week, is_active) " .
"VALUES (?,?)");
return $stmt->execute([$fromDate, $week, $isActive]);
}
function getPlan($pdo, $id)
{
$stmt = $pdo->prepare("SELECT * FROM Plan where pk_plan = ?");
$stmt->execute([$id]);
$plan = $stmt->fetch(PDO::FETCH_ASSOC);
return $plan ?: null;
}
function getAllActivePlan($pdo): array
{
$stmt = $pdo->query("SELECT * FROM Plan where is_active = 1");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function updatePlan($pdo, $id, $fromDate, $week, $isActive): bool
{
$stmt = $pdo->prepare("UPDATE Plan SET fromDate=?, week=?, is_active=? WHERE pk_plan = ?");
return $stmt->execute([$fromDate, $week, $isActive, $id]);
}
function setActivePlan($pdo, $id)
{
$stmt = $pdo->prepare("UPDATE Plan SET is_active=1 WHERE pk_plan = ?");
return $stmt->execute([$id]);
}
function deletePlan($pdo, $id)
{
$stmt = $pdo->prepare("DELETE FROM Plan WHERE pk_plan = ? AND is_active = 1");
return $stmt->execute([$id]);
}
?>