Spaces:
Configuration error

File size: 5,630 Bytes
8df07b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
```php
<?php
include 'includes/auth.php';
redirectIfNotLoggedIn();
include 'config/database.php';

$db = new Database();
$connection = $db->getConnection();

$userId = isAdmin() ? null : $_SESSION['user_id'];

// Get notifications
if (isAdmin()) {
    $query = "SELECT n.*, u.name as userName FROM notifications n 
              LEFT JOIN users u ON n.userId = u.userId 
              ORDER BY n.createdAt DESC";
    $stmt = $connection->prepare($query);
} else {
    $query = "SELECT * FROM notifications WHERE userId = :userId ORDER BY createdAt DESC";
    $stmt = $connection->prepare($query);
    $stmt->bindParam(':userId', $userId);
}
$stmt->execute();
$notifications = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Mark notifications as read
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['mark_read'])) {
    if (isAdmin()) {
        $updateQuery = "UPDATE notifications SET readFlag = 1";
        $updateStmt = $connection->prepare($updateQuery);
    } else {
        $updateQuery = "UPDATE notifications SET readFlag = 1 WHERE userId = :userId";
        $updateStmt = $connection->prepare($updateQuery);
        $updateStmt->bindParam(':userId', $userId);
    }
    $updateStmt->execute();
    header("Location: notifications.php");
    exit();
}

// Get unread count
if (isAdmin()) {
    $countQuery = "SELECT COUNT(*) as count FROM notifications WHERE readFlag = 0";
    $countStmt = $connection->prepare($countQuery);
} else {
    $countQuery = "SELECT COUNT(*) as count FROM notifications WHERE userId = :userId AND readFlag = 0";
    $countStmt = $connection->prepare($countQuery);
    $countStmt->bindParam(':userId', $userId);
}
$countStmt->execute();
$unreadCount = $countStmt->fetch(PDO::FETCH_ASSOC)['count'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notifications - Lost & Found</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <div class="container">
            <nav>
                <div class="logo">Lost & Found Pro</div>
                <ul class="nav-links">
                    <li><a href="home.php">Home</a></li>
                    <li><a href="items.php">Browse Items</a></li>
                    <li><a href="dashboard.php">Dashboard</a></li>
                    <?php if (isAdmin()): ?>
                        <li><a href="admin.php">Admin</a></li>
                        <li><a href="verifications.php">Verifications</a></li>
                        <li><a href="storage.php">Storage</a></li>
                    <?php endif; ?>
                    <?php if (isFinder()): ?>
                        <li><a href="report_item.php">Report Item</a></li>
                    <?php endif; ?>
                    <li><a href="notifications.php">Notifications</a></li>
                    <li><a href="profile.php">Profile</a></li>
                    <li><a href="logout.php">Logout</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <main class="container">
        <div class="card">
            <div class="card-header d-flex justify-between align-center">
                <div>
                    <h1>Notifications</h1>
                    <p>You have <?php echo $unreadCount; ?> unread notifications</p>
                </div>
                <?php if ($unreadCount > 0): ?>
                    <form method="POST">
                        <button type="submit" name="mark_read" class="btn btn-primary">Mark All as Read</button>
                    </form>
                <?php endif; ?>
            </div>
            
            <?php if (empty($notifications)): ?>
                <div class="text-center p-4">
                    <p>No notifications yet.</p>
                </div>
            <?php else: ?>
                <div class="notification-list">
                    <?php foreach ($notifications as $notification): ?>
                        <div class="p-3 border-bottom <?php echo $notification['readFlag'] ? '' : 'bg-blue-50'; ?>">
                            <div class="d-flex justify-between align-center">
                                <div>
                                    <p class="<?php echo $notification['readFlag'] ? '' : 'font-semibold'; ?>">
                                        <?php echo $notification['message']; ?>
                                    </p>
                                    <small class="text-gray-600">
                                        <?php echo date('M j, Y g:i A', strtotime($notification['createdAt'])); ?>
                                        <?php if (isAdmin() && isset($notification['userName'])): ?>
                                            - For: <?php echo $notification['userName']; ?>
                                        <?php endif; ?>
                                    </small>
                                </div>
                                <?php if (!$notification['readFlag']): ?>
                                    <span class="badge badge-primary">New</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </main>

    <footer>
        <div class="container">
            <div class="footer-content">
                <div class="logo">Lost & Found Pro</div>
                <p>&copy; 2024 Lost & Found Management System. All rights reserved.</p>
            </div>
        </div>
    </footer>
</body>
</html>
```