Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/reports/dashboard/notifications.php

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

if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit();
}

$page_title = 'Notifications';
$page_description = 'Manage your notifications';

$user_id = $_SESSION['user_id'];
$success_message = '';
$error_message = '';

// Handle actions
if ($_POST) {
    if (isset($_POST['mark_read'])) {
        $notification_id = (int)$_POST['notification_id'];
        if (markNotificationRead($notification_id)) {
            $success_message = 'Notification marked as read.';
        } else {
            $error_message = 'Failed to mark notification as read.';
        }
    } elseif (isset($_POST['delete_notification'])) {
        $notification_id = (int)$_POST['notification_id'];
        $delete_query = "DELETE FROM notifications WHERE id = :id AND user_id = :user_id";
        $delete_stmt = $db->prepare($delete_query);
        $delete_stmt->bindParam(':id', $notification_id);
        $delete_stmt->bindParam(':user_id', $user_id);
        
        if ($delete_stmt->execute()) {
            logAudit('DELETE', 'notifications', $notification_id);
            $success_message = 'Notification deleted successfully.';
        } else {
            $error_message = 'Failed to delete notification.';
        }
    } elseif (isset($_POST['mark_all_read'])) {
        $mark_all_query = "UPDATE notifications SET is_read = 1 WHERE user_id = :user_id AND is_read = 0";
        $mark_all_stmt = $db->prepare($mark_all_query);
        $mark_all_stmt->bindParam(':user_id', $user_id);
        
        if ($mark_all_stmt->execute()) {
            logAudit('UPDATE', 'notifications', null, null, ['action' => 'mark_all_read']);
            $success_message = 'All notifications marked as read.';
        } else {
            $error_message = 'Failed to mark all notifications as read.';
        }
    } elseif (isset($_POST['delete_all_read'])) {
        $delete_all_query = "DELETE FROM notifications WHERE user_id = :user_id AND is_read = 1";
        $delete_all_stmt = $db->prepare($delete_all_query);
        $delete_all_stmt->bindParam(':user_id', $user_id);
        
        if ($delete_all_stmt->execute()) {
            logAudit('DELETE', 'notifications', null, null, ['action' => 'delete_all_read']);
            $success_message = 'All read notifications deleted.';
        } else {
            $error_message = 'Failed to delete read notifications.';
        }
    }
}

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

$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';
$where_clause = '';
if ($filter === 'unread') {
    $where_clause = ' AND is_read = 0';
} elseif ($filter === 'read') {
    $where_clause = ' AND is_read = 1';
}

// Count total notifications
$count_query = "SELECT COUNT(*) as total FROM notifications WHERE user_id = :user_id" . $where_clause;
$count_stmt = $db->prepare($count_query);
$count_stmt->bindParam(':user_id', $user_id);
$count_stmt->execute();
$total_notifications = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
$total_pages = ceil($total_notifications / $per_page);

// Get notifications
$notifications_query = "SELECT * FROM notifications WHERE user_id = :user_id" . $where_clause . " ORDER BY created_at DESC LIMIT :offset, :per_page";
$notifications_stmt = $db->prepare($notifications_query);
$notifications_stmt->bindParam(':user_id', $user_id);
$notifications_stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$notifications_stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT);
$notifications_stmt->execute();
$notifications = $notifications_stmt->fetchAll(PDO::FETCH_ASSOC);

// Get notification counts
$counts_query = "SELECT 
    COUNT(*) as total,
    SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) as unread,
    SUM(CASE WHEN is_read = 1 THEN 1 ELSE 0 END) as `read`
    FROM notifications WHERE user_id = :user_id";
$counts_stmt = $db->prepare($counts_query);
$counts_stmt->bindParam(':user_id', $user_id);
$counts_stmt->execute();
$counts = $counts_stmt->fetch(PDO::FETCH_ASSOC);

include '../includes/header.php';
?>

<?php if ($success_message): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6 alert-auto-hide">
    <div class="flex items-center">
        <i class="fas fa-check-circle mr-2"></i>
        <span><?php echo htmlspecialchars($success_message); ?></span>
    </div>
</div>
<?php endif; ?>

<?php if ($error_message): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6 alert-auto-hide">
    <div class="flex items-center">
        <i class="fas fa-exclamation-circle mr-2"></i>
        <span><?php echo htmlspecialchars($error_message); ?></span>
    </div>
</div>
<?php endif; ?>

<!-- Notification Stats -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
    <div class="bg-white rounded-lg shadow-sm p-6 border-l-4 border-blue-500">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <i class="fas fa-bell text-3xl text-blue-500"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-600">Total Notifications</p>
                <p class="text-2xl font-semibold text-gray-900"><?php echo number_format($counts['total']); ?></p>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-lg shadow-sm p-6 border-l-4 border-yellow-500">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <i class="fas fa-exclamation-circle text-3xl text-yellow-500"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-600">Unread</p>
                <p class="text-2xl font-semibold text-gray-900"><?php echo number_format($counts['unread']); ?></p>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-lg shadow-sm p-6 border-l-4 border-green-500">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <i class="fas fa-check-circle text-3xl text-green-500"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-600">Read</p>
                <p class="text-2xl font-semibold text-gray-900"><?php echo number_format($counts['read']); ?></p>
            </div>
        </div>
    </div>
</div>

<!-- Notifications Management -->
<div class="bg-white rounded-lg shadow-sm">
    <div class="p-6 border-b border-gray-200">
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between">
            <h3 class="text-lg font-semibold text-gray-800 mb-4 sm:mb-0">Notifications</h3>
            
            <div class="flex flex-col sm:flex-row space-y-2 sm:space-y-0 sm:space-x-4">
                <!-- Filter Tabs -->
                <div class="flex bg-gray-100 rounded-lg p-1">
                    <a href="?filter=all" 
                       class="px-4 py-2 text-sm font-medium rounded-md transition duration-200 <?php echo $filter === 'all' ? 'bg-white text-cop-blue shadow-sm' : 'text-gray-600 hover:text-gray-800'; ?>">
                        All (<?php echo $counts['total']; ?>)
                    </a>
                    <a href="?filter=unread" 
                       class="px-4 py-2 text-sm font-medium rounded-md transition duration-200 <?php echo $filter === 'unread' ? 'bg-white text-cop-blue shadow-sm' : 'text-gray-600 hover:text-gray-800'; ?>">
                        Unread (<?php echo $counts['unread']; ?>)
                    </a>
                    <a href="?filter=read" 
                       class="px-4 py-2 text-sm font-medium rounded-md transition duration-200 <?php echo $filter === 'read' ? 'bg-white text-cop-blue shadow-sm' : 'text-gray-600 hover:text-gray-800'; ?>">
                        Read (<?php echo $counts['read']; ?>)
                    </a>
                </div>
                
                <!-- Bulk Actions -->
                <div class="flex space-x-2">
                    <?php if ($counts['unread'] > 0): ?>
                    <form method="POST" class="inline">
                        <button type="submit" 
                                name="mark_all_read"
                                onclick="return confirm('Mark all notifications as read?')"
                                class="px-4 py-2 bg-blue-600 text-white text-sm rounded-lg hover:bg-blue-700 transition duration-200">
                            <i class="fas fa-check mr-1"></i>Mark All Read
                        </button>
                    </form>
                    <?php endif; ?>
                    
                    <?php if ($counts['read'] > 0): ?>
                    <form method="POST" class="inline">
                        <button type="submit" 
                                name="delete_all_read"
                                onclick="return confirm('Delete all read notifications? This action cannot be undone.')"
                                class="px-4 py-2 bg-red-600 text-white text-sm rounded-lg hover:bg-red-700 transition duration-200">
                            <i class="fas fa-trash mr-1"></i>Delete Read
                        </button>
                    </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    
    <div class="p-6">
        <?php if (empty($notifications)): ?>
        <div class="text-center text-gray-500 py-12">
            <i class="fas fa-bell-slash text-4xl mb-4"></i>
            <h3 class="text-lg font-medium mb-2">No notifications found</h3>
            <p class="text-gray-400">
                <?php if ($filter === 'unread'): ?>
                You have no unread notifications.
                <?php elseif ($filter === 'read'): ?>
                You have no read notifications.
                <?php else: ?>
                You don't have any notifications yet.
                <?php endif; ?>
            </p>
        </div>
        <?php else: ?>
        <div class="space-y-4">
            <?php foreach ($notifications as $notification): ?>
            <div class="flex items-start space-x-4 p-4 rounded-lg border <?php echo $notification['is_read'] ? 'bg-gray-50 border-gray-200' : 'bg-blue-50 border-blue-200'; ?>">
                <div class="flex-shrink-0">
                    <div class="w-10 h-10 rounded-full flex items-center justify-center <?php 
                        echo $notification['type'] === 'error' ? 'bg-red-100' : 
                            ($notification['type'] === 'warning' ? 'bg-yellow-100' : 
                            ($notification['type'] === 'success' ? 'bg-green-100' : 'bg-blue-100')); ?>">
                        <i class="fas fa-<?php 
                            echo $notification['type'] === 'error' ? 'exclamation-triangle text-red-600' : 
                                ($notification['type'] === 'warning' ? 'exclamation-triangle text-yellow-600' : 
                                ($notification['type'] === 'success' ? 'check-circle text-green-600' : 'info-circle text-blue-600')); ?>"></i>
                    </div>
                </div>
                
                <div class="flex-1 min-w-0">
                    <div class="flex items-start justify-between">
                        <div class="flex-1">
                            <h4 class="font-medium text-gray-900 <?php echo !$notification['is_read'] ? 'font-semibold' : ''; ?>">
                                <?php echo htmlspecialchars($notification['title']); ?>
                                <?php if (!$notification['is_read']): ?>
                                <span class="inline-block w-2 h-2 bg-blue-500 rounded-full ml-2"></span>
                                <?php endif; ?>
                            </h4>
                            <p class="text-gray-600 mt-1"><?php echo htmlspecialchars($notification['message']); ?></p>
                            <p class="text-xs text-gray-500 mt-2">
                                <i class="fas fa-clock mr-1"></i>
                                <?php echo getRelativeTime($notification['created_at']); ?>
                            </p>
                        </div>
                        
                        <div class="flex items-center space-x-2 ml-4">
                            <?php if (!$notification['is_read']): ?>
                            <form method="POST" class="inline">
                                <input type="hidden" name="notification_id" value="<?php echo $notification['id']; ?>">
                                <button type="submit" 
                                        name="mark_read"
                                        class="text-blue-600 hover:text-blue-800 text-sm"
                                        title="Mark as read">
                                    <i class="fas fa-check"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                            
                            <form method="POST" class="inline">
                                <input type="hidden" name="notification_id" value="<?php echo $notification['id']; ?>">
                                <button type="submit" 
                                        name="delete_notification"
                                        onclick="return confirm('Delete this notification?')"
                                        class="text-red-600 hover:text-red-800 text-sm"
                                        title="Delete notification">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
        <div class="flex items-center justify-between mt-8">
            <div class="text-sm text-gray-600">
                Showing <?php echo $offset + 1; ?> to <?php echo min($offset + $per_page, $total_notifications); ?> of <?php echo $total_notifications; ?> notifications
            </div>
            
            <div class="flex space-x-2">
                <?php if ($page > 1): ?>
                <a href="?page=<?php echo $page - 1; ?>&filter=<?php echo $filter; ?>" 
                   class="px-3 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition duration-200">
                    <i class="fas fa-chevron-left"></i>
                </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 $filter; ?>" 
                   class="px-3 py-2 rounded-lg transition duration-200 <?php echo $i === $page ? 'bg-cop-blue text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?>">
                    <?php echo $i; ?>
                </a>
                <?php endfor; ?>
                
                <?php if ($page < $total_pages): ?>
                <a href="?page=<?php echo $page + 1; ?>&filter=<?php echo $filter; ?>" 
                   class="px-3 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition duration-200">
                    <i class="fas fa-chevron-right"></i>
                </a>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>

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