Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/classes/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/classes/Notification.php

<?php
/**
 * Notification Management Class for COP News Portal
 */

class Notification {
    private $conn;
    private $table_name = "notifications";

    public function __construct($db) {
        $this->conn = $db;
    }

    public function create($data) {
        $query = "INSERT INTO " . $this->table_name . " 
                  (user_id, title, message, type) 
                  VALUES (?, ?, ?, ?)";
        
        $stmt = $this->conn->prepare($query);
        
        $result = $stmt->execute([
            $data['user_id'],
            $data['title'],
            $data['message'],
            $data['type'] ?? 'info'
        ]);

        if ($result) {
            $notification_id = $this->conn->lastInsertId();
            log_audit('CREATE', 'notifications', $notification_id, null, $data);
            return $notification_id;
        }
        
        return false;
    }

    public function getByUserId($user_id, $limit = 20, $offset = 0, $unread_only = false) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE user_id = ?";
        $params = [$user_id];
        
        if ($unread_only) {
            $query .= " AND is_read = 0";
        }
        
        $query .= " ORDER BY created_at DESC LIMIT ? OFFSET ?";
        $params[] = $limit;
        $params[] = $offset;
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute($params);
        
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function markAsRead($id, $user_id = null) {
        $query = "UPDATE " . $this->table_name . " SET is_read = 1 WHERE id = ?";
        $params = [$id];
        
        if ($user_id) {
            $query .= " AND user_id = ?";
            $params[] = $user_id;
        }
        
        $stmt = $this->conn->prepare($query);
        return $stmt->execute($params);
    }

    public function markAllAsRead($user_id) {
        $query = "UPDATE " . $this->table_name . " SET is_read = 1 WHERE user_id = ? AND is_read = 0";
        $stmt = $this->conn->prepare($query);
        return $stmt->execute([$user_id]);
    }

    public function getUnreadCount($user_id) {
        $query = "SELECT COUNT(*) as count FROM " . $this->table_name . " WHERE user_id = ? AND is_read = 0";
        $stmt = $this->conn->prepare($query);
        $stmt->execute([$user_id]);
        
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return $result['count'];
    }

    public function delete($id, $user_id = null) {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        $params = [$id];
        
        if ($user_id) {
            $query .= " AND user_id = ?";
            $params[] = $user_id;
        }
        
        $stmt = $this->conn->prepare($query);
        return $stmt->execute($params);
    }

    public function createForAllUsers($title, $message, $type = 'info', $exclude_user_id = null) {
        // Get all active users
        $user_query = "SELECT id FROM users WHERE status = 'active'";
        $params = [];
        
        if ($exclude_user_id) {
            $user_query .= " AND id != ?";
            $params[] = $exclude_user_id;
        }
        
        $user_stmt = $this->conn->prepare($user_query);
        $user_stmt->execute($params);
        $users = $user_stmt->fetchAll(PDO::FETCH_ASSOC);
        
        $created_count = 0;
        foreach ($users as $user) {
            if ($this->create([
                'user_id' => $user['id'],
                'title' => $title,
                'message' => $message,
                'type' => $type
            ])) {
                $created_count++;
            }
        }
        
        return $created_count;
    }

    public function createForUsersByLocation($location_type, $location_name, $title, $message, $type = 'info') {
        // Get users by location
        $user_query = "SELECT id FROM users WHERE location_type = ? AND location_name = ? AND status = 'active'";
        $user_stmt = $this->conn->prepare($user_query);
        $user_stmt->execute([$location_type, $location_name]);
        $users = $user_stmt->fetchAll(PDO::FETCH_ASSOC);
        
        $created_count = 0;
        foreach ($users as $user) {
            if ($this->create([
                'user_id' => $user['id'],
                'title' => $title,
                'message' => $message,
                'type' => $type
            ])) {
                $created_count++;
            }
        }
        
        return $created_count;
    }
}
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists