Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/news/
Upload File :
Current File : /home/copmadinaarea/.trash/news/index.php

<?php
require_once '../config/config.php';
require_login();

$database = new Database();
$conn = $database->getConnection();
$news = new News($conn);
$category = new Category($conn);

// Pagination
$page = max(1, intval($_GET['page'] ?? 1));
$limit = 10;
$offset = ($page - 1) * $limit;

// Get filters from query parameters
$filters = [
    'status' => 'published',
    'user_location_type' => $_SESSION['location_type'] ?? '',
    'user_location_name' => $_SESSION['location_name'] ?? ''
];

// Apply location filtering for all users except admins/superusers
if (in_array($_SESSION['account_type'] ?? '', ['admin', 'superuser'])) {
    // Admins and superusers can see all news, so remove location filters
    unset($filters['user_location_type']);
    unset($filters['user_location_name']);
}

if (!empty($_GET['category'])) {
    $filters['category_id'] = $_GET['category'];
}

if (!empty($_GET['location'])) {
    $filters['location'] = $_GET['location'];
}

if (!empty($_GET['search'])) {
    $filters['search'] = $_GET['search'];
}

$news_list = $news->getAll($filters);

// Filters
$status_filter = $_GET['status'] ?? '';
$category_filter = $_GET['category'] ?? '';
$user_filter = $_GET['user'] ?? '';
$search = $_GET['search'] ?? '';

// Build conditions
$conditions = [];
$params = [];

if ($status_filter) {
    $conditions[] = "n.status = ?";
    $params[] = $status_filter;
}

if ($category_filter) {
    $conditions[] = "n.category_id = ?";
    $params[] = $category_filter;
}

if ($user_filter) {
    $conditions[] = "n.user_id = ?";
    $params[] = $user_filter;
}

if ($search) {
    $conditions[] = "(n.title LIKE ? OR n.description LIKE ? OR n.content LIKE ?)";
    $search_term = "%$search%";
    $params[] = $search_term;
    $params[] = $search_term;
    $params[] = $search_term;
}

// For regular users, only show their own articles and published articles
if (($_SESSION['account_type'] ?? '') === 'user') {
    $conditions[] = "(n.user_id = ? OR n.status = 'published')";
    $params[] = $_SESSION['user_id'];
}

// Get news articles
$query = "SELECT n.id, n.title, n.location, n.description, n.status, n.views, n.created_at,
                 c.name as category_name, u.name as author_name
          FROM news n
          LEFT JOIN categories c ON n.category_id = c.id
          LEFT JOIN users u ON n.user_id = u.id";

if (!empty($conditions)) {
    $query .= " WHERE " . implode(' AND ', $conditions);
}

$query .= " ORDER BY n.created_at DESC LIMIT ? OFFSET ?";
$params[] = $limit;
$params[] = $offset;

$stmt = $conn->prepare($query);
$stmt->execute($params);
$news_articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get total count for pagination
$count_query = "SELECT COUNT(*) as total FROM news n 
                LEFT JOIN categories c ON n.category_id = c.id
                LEFT JOIN users u ON n.user_id = u.id";

if (!empty($conditions)) {
    $count_params = array_slice($params, 0, -2); // Remove limit and offset
    $count_query .= " WHERE " . implode(' AND ', $conditions);
    $count_stmt = $conn->prepare($count_query);
    $count_stmt->execute($count_params);
} else {
    $count_stmt = $conn->prepare($count_query);
    $count_stmt->execute();
}

$total_records = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
$total_pages = ceil($total_records / $limit);

$categories = $category->getAll();
$flash = get_flash_message();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Articles - COP News Portal</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <header class="header">
        <nav class="navbar">
            <a href="../dashboard.php" class="logo">
                <i class="fas fa-church"></i>
                COP News Portal
            </a>
            <ul class="nav-links">
                <li><a href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="index.php" class="active"><i class="fas fa-newspaper"></i> News</a></li>
                <li><a href="create.php"><i class="fas fa-plus"></i> Add News</a></li>
                <?php if (($_SESSION['account_type'] ?? '') === 'admin' || ($_SESSION['account_type'] ?? '') === 'superuser'): ?>
                    <li><a href="../admin/"><i class="fas fa-cog"></i> Admin</a></li>
                <?php endif; ?>
                <li><a href="../profile.php"><i class="fas fa-user"></i> Profile</a></li>
                <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </nav>
    </header>

    <main class="container" style="margin-top: 2rem;">
        <?php if ($flash): ?>
            <div class="alert alert-<?php echo $flash['type']; ?>">
                <i class="fas fa-info-circle"></i> <?php echo $flash['message']; ?>
            </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-header">
                <h1><i class="fas fa-newspaper"></i> News Articles</h1>
            </div>
            <div class="card-body">
                <!-- Filters and Search -->
                <form method="GET" class="mb-4">
                    <div class="grid grid-2">
                        <div class="form-group">
                            <label for="search" class="form-label">Search</label>
                            <input type="text" id="search" name="search" class="form-control" 
                                   value="<?php echo htmlspecialchars($search); ?>" 
                                   placeholder="Search news articles...">
                        </div>
                        <div class="form-group">
                            <label for="category" class="form-label">Category</label>
                            <select id="category" name="category" class="form-control form-select">
                                <option value="">All Categories</option>
                                <?php foreach ($categories as $cat): ?>
                                    <option value="<?php echo $cat['id']; ?>" 
                                            <?php echo $category_filter == $cat['id'] ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($cat['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="status" class="form-label">Status</label>
                            <select id="status" name="status" class="form-control form-select">
                                <option value="">All Status</option>
                                <option value="published" <?php echo $status_filter === 'published' ? 'selected' : ''; ?>>Published</option>
                                <option value="draft" <?php echo $status_filter === 'draft' ? 'selected' : ''; ?>>Draft</option>
                                <option value="archived" <?php echo $status_filter === 'archived' ? 'selected' : ''; ?>>Archived</option>
                            </select>
                        </div>
                        <div class="form-group" style="display: flex; align-items: end; gap: 1rem;">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> Search
                            </button>
                            <a href="index.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Clear
                            </a>
                            <a href="create.php" class="btn btn-success">
                                <i class="fas fa-plus"></i> Add News
                            </a>
                        </div>
                    </div>
                </form>

                <!-- News Articles List -->
                <?php if (empty($news_articles)): ?>
                    <div class="text-center p-4">
                        <i class="fas fa-newspaper" style="font-size: 4rem; color: var(--light-grey); margin-bottom: 1rem;"></i>
                        <h3>No news articles found</h3>
                        <p style="color: var(--primary-grey);">
                            <?php if ($search || $status_filter || $category_filter): ?>
                                Try adjusting your search criteria or filters.
                            <?php else: ?>
                                Start by creating your first news article.
                            <?php endif; ?>
                        </p>
                        <a href="create.php" class="btn btn-primary mt-2">
                            <i class="fas fa-plus"></i> Create News Article
                        </a>
                    </div>
                <?php else: ?>
                    <div class="news-list">
                        <?php foreach ($news_articles as $article): ?>
                            <div class="news-card mb-3">
                                <div class="news-card-content">
                                    <div class="flex justify-between items-start mb-2">
                                        <h3 class="news-card-title">
                                            <a href="view.php?id=<?php echo $article['id']; ?>" 
                                               style="text-decoration: none; color: inherit;">
                                                <?php echo htmlspecialchars($article['title']); ?>
                                            </a>
                                        </h3>
                                        <span class="badge badge-<?php echo $article['status']; ?>">
                                            <?php echo ucfirst($article['status']); ?>
                                        </span>
                                    </div>
                                    
                                    <div class="news-card-meta mb-2">
                                        <div>
                                            <i class="fas fa-user"></i> <?php echo htmlspecialchars($article['author_name']); ?>
                                            <span class="ml-3">
                                                <i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($article['location']); ?>
                                            </span>
                                            <?php if ($article['category_name']): ?>
                                                <span class="ml-3">
                                                    <i class="fas fa-tag"></i> <?php echo htmlspecialchars($article['category_name']); ?>
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                        <div>
                                            <i class="fas fa-eye"></i> <?php echo $article['views']; ?> views
                                            <span class="ml-3">
                                                <i class="fas fa-calendar"></i> 
                                                <?php echo date('M j, Y', strtotime($article['created_at'])); ?>
                                            </span>
                                        </div>
                                    </div>
                                    
                                    <?php if ($article['description']): ?>
                                        <p class="news-card-description mb-3">
                                            <?php echo htmlspecialchars(substr($article['description'], 0, 200)); ?>
                                            <?php if (strlen($article['description']) > 200): ?>...<?php endif; ?>
                                        </p>
                                    <?php endif; ?>
                                    
                                    <div class="flex gap-2">
                                        <a href="view.php?id=<?php echo $article['id']; ?>" class="btn btn-primary btn-sm">
                                            <i class="fas fa-eye"></i> View
                                        </a>
                                        
                                        <?php if (($article['user_id'] ?? 0) == ($_SESSION['user_id'] ?? 0) || ($_SESSION['account_type'] ?? 'user') !== 'user'): ?>
                                            <a href="edit.php?id=<?php echo $article['id']; ?>" class="btn btn-secondary btn-sm">
                                                <i class="fas fa-edit"></i> Edit
                                            </a>
                                            <a href="delete.php?id=<?php echo $article['id']; ?>" 
                                               class="btn btn-danger btn-sm delete-btn"
                                               data-title="<?php echo htmlspecialchars($article['title']); ?>">
                                                <i class="fas fa-trash"></i> Delete
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                        <div class="pagination-wrapper text-center mt-4">
                            <div class="pagination">
                                <?php if ($page > 1): ?>
                                    <a href="?page=<?php echo $page - 1; ?>&<?php echo http_build_query(array_filter($_GET, function($k) { return $k !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                       class="btn btn-secondary btn-sm">
                                        <i class="fas fa-chevron-left"></i> Previous
                                    </a>
                                <?php endif; ?>
                                
                                <span class="pagination-info">
                                    Page <?php echo $page; ?> of <?php echo $total_pages; ?> 
                                    (<?php echo $total_records; ?> total articles)
                                </span>
                                
                                <?php if ($page < $total_pages): ?>
                                    <a href="?page=<?php echo $page + 1; ?>&<?php echo http_build_query(array_filter($_GET, function($k) { return $k !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                       class="btn btn-secondary btn-sm">
                                        Next <i class="fas fa-chevron-right"></i>
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <style>
        .active {
            color: var(--primary-blue) !important;
            font-weight: 600;
        }
        .pagination {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 1rem;
        }
        .pagination-info {
            color: var(--primary-grey);
            font-size: 0.9rem;
        }
        .news-list {
            display: flex;
            flex-direction: column;
        }
        .gap-2 {
            gap: 0.5rem;
        }
    </style>

    <script>
        function confirmDelete(event, element) {
            console.log('Delete button clicked, href:', element.href);
            event.preventDefault();
            
            const title = element.getAttribute('data-title');
            console.log('Article title:', title);
            
            const message = `Are you sure you want to delete the article "${title}"?\n\nThis action cannot be undone.`;
            console.log('Showing confirmation dialog');
            
            const confirmed = confirm(message);
            console.log('User confirmation result:', confirmed);
            
            if (confirmed) {
                console.log('Delete confirmed, navigating to:', element.href);
                // Force navigation
                try {
                    window.location.href = element.href;
                } catch (error) {
                    console.error('Navigation error:', error);
                    // Fallback: try opening in new window
                    window.open(element.href, '_self');
                }
                return true;
            } else {
                console.log('Delete cancelled by user');
                return false;
            }
        }
        
        // Also add click event listener as backup
        document.addEventListener('DOMContentLoaded', function() {
            const deleteButtons = document.querySelectorAll('.delete-btn');
            console.log('Found', deleteButtons.length, 'delete buttons');
            
            deleteButtons.forEach(function(button, index) {
                console.log('Delete button', index, 'href:', button.href);
            });
        });
    </script>
</body>
</html>

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