Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/notifications.php

<?php
session_start();
require_once '../includes/functions.php';

// Check if user is logged in and has appropriate role
if (!isLoggedIn()) {
    header('Location: ../login.php');
    exit();
}

$user = getCurrentUser();
if (!hasRole(['superuser'])) {
    header('Location: index.php');
    exit();
}

// Handle notification actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'mark_read') {
        $notification_id = $_POST['notification_id'];
        $stmt = executeQuery("UPDATE notifications SET is_read = 1 WHERE id = ?", [$notification_id]);
        if ($stmt) {
            addNotification('success', 'Notification marked as read.');
        }
    } elseif ($action === 'mark_all_read') {
        $stmt = executeQuery("UPDATE notifications SET is_read = 1 WHERE user_id = ?", [$user['id']]);
        if ($stmt) {
            addNotification('success', 'All notifications marked as read.');
        }
    } elseif ($action === 'delete') {
        $notification_id = $_POST['notification_id'];
        $stmt = executeQuery("DELETE FROM notifications WHERE id = ?", [$notification_id]);
        if ($stmt) {
            addNotification('success', 'Notification deleted.');
        }
    }
    
    header('Location: notifications.php');
    exit();
}

// Get notifications with pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 20;
$offset = ($page - 1) * $limit;

$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';

$where_clause = '';
$params = [$user['id']];

if ($filter === 'unread') {
    $where_clause = 'AND is_read = 0';
} elseif ($filter === 'read') {
    $where_clause = 'AND is_read = 1';
}

$query = "
    SELECT * FROM notifications 
    WHERE user_id = ? $where_clause
    ORDER BY created_at DESC
    LIMIT $limit OFFSET $offset
";

$stmt = executeQuery($query, $params);
$notifications = $stmt ? $stmt->fetchAll() : [];

// Get total count
$count_query = "SELECT COUNT(*) as total FROM notifications WHERE user_id = ? $where_clause";
$count_stmt = executeQuery($count_query, $params);
$total_records = $count_stmt ? $count_stmt->fetch()['total'] : 0;
$total_pages = ceil($total_records / $limit);

// Get unread count
$unread_stmt = executeQuery("SELECT COUNT(*) as unread FROM notifications WHERE user_id = ? AND is_read = 0", [$user['id']]);
$unread_count = $unread_stmt ? $unread_stmt->fetch()['unread'] : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notifications - COP Madina Conference</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gradient-to-br from-slate-50 to-blue-50 min-h-screen">
    <div class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/admin_sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden ml-72">
            <!-- Header -->
            <?php include 'includes/admin_header.php'; ?>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-8">
                <!-- Stats Cards -->
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-rose-100">
                                <i class="fas fa-bell text-rose-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo $total_records; ?></p>
                                <p class="text-slate-600">Total Notifications</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-orange-100">
                                <i class="fas fa-exclamation-circle text-orange-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo $unread_count; ?></p>
                                <p class="text-slate-600">Unread</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6">
                        <div class="flex items-center">
                            <div class="p-3 rounded-full bg-green-100">
                                <i class="fas fa-check-circle text-green-600 text-xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-2xl font-bold text-slate-800"><?php echo $total_records - $unread_count; ?></p>
                                <p class="text-slate-600">Read</p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Filters and Actions -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6 mb-8">
                    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
                        <div class="flex space-x-2">
                            <a href="?filter=all" class="px-4 py-2 rounded-lg font-medium transition-colors <?php echo $filter === 'all' ? 'bg-rose-600 text-white' : 'bg-slate-100 text-slate-700 hover:bg-slate-200'; ?>">
                                All
                            </a>
                            <a href="?filter=unread" class="px-4 py-2 rounded-lg font-medium transition-colors <?php echo $filter === 'unread' ? 'bg-rose-600 text-white' : 'bg-slate-100 text-slate-700 hover:bg-slate-200'; ?>">
                                Unread (<?php echo $unread_count; ?>)
                            </a>
                            <a href="?filter=read" class="px-4 py-2 rounded-lg font-medium transition-colors <?php echo $filter === 'read' ? 'bg-rose-600 text-white' : 'bg-slate-100 text-slate-700 hover:bg-slate-200'; ?>">
                                Read
                            </a>
                        </div>
                        
                        <?php if ($unread_count > 0): ?>
                        <form method="POST" class="inline">
                            <input type="hidden" name="action" value="mark_all_read">
                            <button type="submit" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition-colors">
                                <i class="fas fa-check-double mr-2"></i>
                                Mark All Read
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Notifications List -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 overflow-hidden">
                    <div class="p-6 border-b border-slate-200/50">
                        <h2 class="text-xl font-bold text-slate-800">Notifications</h2>
                        <p class="text-slate-600 mt-1">Your system notifications and alerts</p>
                    </div>
                    
                    <div class="divide-y divide-slate-200">
                        <?php if (empty($notifications)): ?>
                        <div class="p-12 text-center text-slate-500">
                            <i class="fas fa-bell-slash text-4xl mb-4"></i>
                            <p>No notifications found</p>
                        </div>
                        <?php else: ?>
                        <?php foreach ($notifications as $notification): ?>
                        <div class="p-6 hover:bg-slate-50 transition-colors <?php echo !$notification['is_read'] ? 'bg-rose-50/50' : ''; ?>">
                            <div class="flex items-start justify-between">
                                <div class="flex-1">
                                    <div class="flex items-center mb-2">
                                        <?php if (!$notification['is_read']): ?>
                                        <div class="w-2 h-2 bg-rose-500 rounded-full mr-3"></div>
                                        <?php endif; ?>
                                        
                                        <?php
                                        $type_icons = [
                                            'success' => 'fas fa-check-circle text-green-600',
                                            'error' => 'fas fa-exclamation-circle text-red-600',
                                            'warning' => 'fas fa-exclamation-triangle text-yellow-600',
                                            'info' => 'fas fa-info-circle text-blue-600'
                                        ];
                                        $icon_class = $type_icons[$notification['type']] ?? 'fas fa-bell text-slate-600';
                                        ?>
                                        <i class="<?php echo $icon_class; ?> mr-3"></i>
                                        
                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full <?php echo $notification['type'] === 'success' ? 'bg-green-100 text-green-800' : ($notification['type'] === 'error' ? 'bg-red-100 text-red-800' : ($notification['type'] === 'warning' ? 'bg-yellow-100 text-yellow-800' : 'bg-blue-100 text-blue-800')); ?>">
                                            <?php echo ucfirst($notification['type']); ?>
                                        </span>
                                        
                                        <span class="text-sm text-slate-500 ml-3">
                                            <?php echo date('M j, Y g:i A', strtotime($notification['created_at'])); ?>
                                        </span>
                                    </div>
                                    
                                    <p class="text-slate-800 <?php echo !$notification['is_read'] ? 'font-semibold' : ''; ?>">
                                        <?php echo htmlspecialchars($notification['message']); ?>
                                    </p>
                                </div>
                                
                                <div class="flex space-x-2 ml-4">
                                    <?php if (!$notification['is_read']): ?>
                                    <form method="POST" class="inline">
                                        <input type="hidden" name="action" value="mark_read">
                                        <input type="hidden" name="notification_id" value="<?php echo $notification['id']; ?>">
                                        <button type="submit" class="p-2 text-slate-400 hover:text-green-600 transition-colors" title="Mark as read">
                                            <i class="fas fa-check"></i>
                                        </button>
                                    </form>
                                    <?php endif; ?>
                                    
                                    <form method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete this notification?')">
                                        <input type="hidden" name="action" value="delete">
                                        <input type="hidden" name="notification_id" value="<?php echo $notification['id']; ?>">
                                        <button type="submit" class="p-2 text-slate-400 hover:text-red-600 transition-colors" title="Delete">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                    <div class="px-6 py-4 border-t border-slate-200/50 flex items-center justify-between">
                        <div class="text-sm text-slate-700">
                            Showing <?php echo (($page - 1) * $limit) + 1; ?> to <?php echo min($page * $limit, $total_records); ?> of <?php echo $total_records; ?> results
                        </div>
                        <div class="flex space-x-2">
                            <?php if ($page > 1): ?>
                            <a href="?page=<?php echo $page - 1; ?>&filter=<?php echo urlencode($filter); ?>" 
                               class="px-3 py-2 text-sm bg-white border border-slate-300 rounded-lg hover:bg-slate-50">
                                Previous
                            </a>
                            <?php endif; ?>
                            
                            <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                            <a href="?page=<?php echo $i; ?>&filter=<?php echo urlencode($filter); ?>" 
                               class="px-3 py-2 text-sm <?php echo $i === $page ? 'bg-rose-600 text-white' : 'bg-white border border-slate-300 hover:bg-slate-50'; ?> rounded-lg">
                                <?php echo $i; ?>
                            </a>
                            <?php endfor; ?>
                            
                            <?php if ($page < $total_pages): ?>
                            <a href="?page=<?php echo $page + 1; ?>&filter=<?php echo urlencode($filter); ?>" 
                               class="px-3 py-2 text-sm bg-white border border-slate-300 rounded-lg hover:bg-slate-50">
                                Next
                            </a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </main>
        </div>
    </div>
</body>
</html>

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