notificationModel = new Notification(); } /** * Create a domain expiring notification */ public function notifyDomainExpiring(int $userId, string $domainName, int $daysLeft, int $domainId): void { $this->notificationModel->createNotification( $userId, 'domain_expiring', 'Domain Expiring Soon', "{$domainName} expires in {$daysLeft} day" . ($daysLeft > 1 ? 's' : ''), $domainId ); } /** * Create a domain expired notification */ public function notifyDomainExpired(int $userId, string $domainName, int $domainId): void { $this->notificationModel->createNotification( $userId, 'domain_expired', 'Domain Expired', "{$domainName} has expired - renew immediately", $domainId ); } /** * Create a domain WHOIS updated notification */ public function notifyDomainUpdated(int $userId, string $domainName, int $domainId, string $changes = ''): void { $message = !empty($changes) ? "{$domainName} - {$changes}" : "{$domainName} WHOIS data updated"; $this->notificationModel->createNotification( $userId, 'domain_updated', 'Domain WHOIS Updated', $message, $domainId ); } /** * Create a WHOIS lookup failed notification */ public function notifyWhoisFailed(int $userId, string $domainName, int $domainId, string $reason = ''): void { $message = !empty($reason) ? "Could not refresh {$domainName} - {$reason}" : "Could not refresh {$domainName}"; $this->notificationModel->createNotification( $userId, 'whois_failed', 'WHOIS Lookup Failed', $message, $domainId ); } /** * Create a new login notification */ public function notifyNewLogin(int $userId, string $location, string $ipAddress): void { $this->notificationModel->createNotification( $userId, 'session_new', 'New Login Detected', "Login from {$location} ({$ipAddress})", null ); } /** * Create welcome notification for new users/fresh install */ public function notifyWelcome(int $userId, string $username): void { $this->notificationModel->createNotification( $userId, 'system_welcome', 'Welcome to Domain Monitor! 🎉', "Hi {$username}! Your account is ready. Start by adding your first domain to monitor.", null ); } /** * Create system upgrade notification for admins */ public function notifySystemUpgrade(int $userId, string $fromVersion, string $toVersion, int $migrationsCount): void { $this->notificationModel->createNotification( $userId, 'system_upgrade', 'System Upgraded Successfully', "Domain Monitor upgraded from v{$fromVersion} to v{$toVersion} ({$migrationsCount} migration" . ($migrationsCount > 1 ? 's' : '') . " applied)", null ); } /** * Notify all admins about system upgrade */ public function notifyAdminsUpgrade(string $fromVersion, string $toVersion, int $migrationsCount): void { try { $pdo = \Core\Database::getConnection(); $stmt = $pdo->query("SELECT id FROM users WHERE role = 'admin'"); $admins = $stmt->fetchAll(\PDO::FETCH_ASSOC); foreach ($admins as $admin) { $this->notifySystemUpgrade($admin['id'], $fromVersion, $toVersion, $migrationsCount); } } catch (\Exception $e) { error_log("Failed to notify admins about upgrade: " . $e->getMessage()); } } /** * Delete old read notifications (cleanup) */ public function cleanOldNotifications(int $daysOld = 30): void { try { $pdo = \Core\Database::getConnection(); $stmt = $pdo->prepare( "DELETE FROM user_notifications WHERE is_read = 1 AND read_at < DATE_SUB(NOW(), INTERVAL ? DAY)" ); $stmt->execute([$daysOld]); } catch (\Exception $e) { error_log("Failed to clean old notifications: " . $e->getMessage()); } } }