getUnreadCount()]); exit; } } /** * Get notifications for current user */ function getNotifications($limit = 20) { global $class_database, $cfg; $usr_id = isset($_SESSION['USER_ID']) ? (int) $_SESSION['USER_ID'] : 0; if ($usr_id == 0) { return ['notifications' => [], 'unread_count' => 0]; } $sql = "SELECT n.*, u.usr_user, u.usr_dname, u.usr_key FROM `db_notifications` n LEFT JOIN `db_accountuser` u ON n.from_usr_id = u.usr_id WHERE n.to_usr_id = %d ORDER BY n.db_id DESC LIMIT %d"; $result = $class_database->doQuery($sql, $usr_id, $limit); $notifications = []; while ($row = $result->fetch_assoc()) { $notifications[] = [ 'id' => $row['db_id'], 'type' => $row['notif_type'], 'message' => $row['notif_message'], 'from_user' => $row['usr_dname'] ?: 'System', 'from_username' => $row['usr_user'], 'from_key' => $row['usr_key'], 'link' => $row['notif_link'], 'is_read' => $row['notif_read'] == 1, 'date' => $row['notif_date'], 'time_ago' => timeAgo($row['notif_date']) ]; } return [ 'notifications' => $notifications, 'unread_count' => getUnreadCount() ]; } /** * Get unread notification count */ function getUnreadCount() { global $class_database; $usr_id = isset($_SESSION['USER_ID']) ? (int) $_SESSION['USER_ID'] : 0; if ($usr_id == 0) { return 0; } $sql = "SELECT COUNT(*) as count FROM `db_notifications` WHERE `to_usr_id` = %d AND `notif_read` = 0"; $result = $class_database->doQuery($sql, $usr_id); $row = $result->fetch_assoc(); return (int) $row['count']; } /** * Mark notification as read */ function markAsRead($notif_id) { global $class_database; $usr_id = isset($_SESSION['USER_ID']) ? (int) $_SESSION['USER_ID'] : 0; $sql = "UPDATE `db_notifications` SET `notif_read` = 1 WHERE `db_id` = %d AND `to_usr_id` = %d"; $class_database->doQuery($sql, $notif_id, $usr_id); return ['success' => true, 'unread_count' => getUnreadCount()]; } /** * Mark all notifications as read */ function markAllAsRead() { global $class_database; $usr_id = isset($_SESSION['USER_ID']) ? (int) $_SESSION['USER_ID'] : 0; $sql = "UPDATE `db_notifications` SET `notif_read` = 1 WHERE `to_usr_id` = %d AND `notif_read` = 0"; $class_database->doQuery($sql, $usr_id); return ['success' => true, 'unread_count' => 0]; } /** * Format time ago */ function timeAgo($datetime) { $timestamp = strtotime($datetime); $diff = time() - $timestamp; if ($diff < 60) { return 'just now'; } elseif ($diff < 3600) { $mins = floor($diff / 60); return $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago'; } elseif ($diff < 86400) { $hours = floor($diff / 3600); return $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago'; } elseif ($diff < 604800) { $days = floor($diff / 86400); return $days . ' day' . ($days > 1 ? 's' : '') . ' ago'; } elseif ($diff < 2592000) { $weeks = floor($diff / 604800); return $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago'; } else { return date('M d, Y', $timestamp); } } // If not an AJAX request, return the HTML component if (!isset($_POST['action'])): $unread_count = getUnreadCount(); ?>

Notifications

Loading notifications...