30 days) try { $pdo->exec("DELETE FROM notifications WHERE is_deleted = 1 AND deleted_at < NOW() - INTERVAL 30 DAY"); } catch(PDOException $e) {} // Auto Move to Trash (Read > 7 days) try { // Karena kita tidak punya timestamp khusus 'kapan dibaca', kita hitung dari waktu notifikasi dibuat (time) + sudah is_read $pdo->exec("UPDATE notifications SET is_deleted = 1, deleted_at = NOW() WHERE is_deleted = 0 AND is_read = 1 AND time < NOW() - INTERVAL 7 DAY"); } catch(PDOException $e) {} // Fetch active notifications $excludeCond = "AND title NOT LIKE '%baterai%' AND title NOT LIKE '%battery%' AND title NOT LIKE '%pesan%' AND title NOT LIKE '%message%' AND title NOT LIKE '%chat%'"; $stmt = $pdo->query("SELECT * FROM notifications WHERE is_deleted = 0 $excludeCond ORDER BY time DESC"); $notifications = $stmt->fetchAll(); // Fetch trash $stmtTrash = $pdo->query("SELECT * FROM notifications WHERE is_deleted = 1 $excludeCond ORDER BY deleted_at DESC"); $trashItems = $stmtTrash->fetchAll(); function timeAgo($dt) { $diff = (new DateTime())->diff(new DateTime($dt)); if ($diff->d > 0) return $diff->d . ' day(s) ago'; if ($diff->h > 0) return $diff->h . ' hour(s) ago'; if ($diff->i > 0) return $diff->i . ' minute(s) ago'; return 'Just now'; } function groupNotifications($records) { $grouped = []; foreach ($records as $n) { $dateObj = new DateTime($n['time']); $today = new DateTime('today'); $yesterday = new DateTime('yesterday'); $dateOnly = $dateObj->format('Y-m-d'); if ($dateOnly === $today->format('Y-m-d')) { $label = 'Today'; } elseif ($dateOnly === $yesterday->format('Y-m-d')) { $label = 'Yesterday'; } else { $label = $dateObj->format('d M Y'); } $grouped[$label][] = $n; } return $grouped; } $grouped = groupNotifications($notifications); $groupedTrash = groupNotifications($trashItems); $unreadCount = count(array_filter($notifications, fn($n) => !$n['is_read'])); $dangerCount = count(array_filter($notifications, fn($n) => $n['is_danger'] || stripos($n['title'], 'terbuka') !== false || stripos($n['message'], 'terbuka') !== false)); $totalCount = count($notifications); $trashCount = count($trashItems); include 'header.php'; ?>