Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/news/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/news/index.php

<?php
session_start();
require_once '../config/config.php';
require_once '../config/database.php';
require_once '../classes/News.php';
require_once '../classes/Category.php';
require_once '../classes/Location.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../index.php');
    exit();
}

// Initialize database connection
$database = new Database();
$pdo = $database->getConnection();

// Initialize classes
$news = new News($pdo);
$category = new Category($pdo);
$location = new Location($pdo);

// Get filter parameters
$search = $_GET['search'] ?? '';
$category_filter = $_GET['category'] ?? '';
$status_filter = $_GET['status'] ?? '';
$page = max(1, intval($_GET['page'] ?? 1));
$per_page = 10;

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

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

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

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

// Add user restriction for non-admin users
if (($_SESSION['account_type'] ?? 'user') === 'user') {
    $where_conditions[] = "(n.status = 'published' OR n.user_id = ?)";
    $params[] = $_SESSION['user_id'];
}

$where_clause = $where_conditions ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

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

$count_stmt = $pdo->prepare($count_sql);
$count_stmt->execute($params);
$total_articles = $count_stmt->fetchColumn();
$total_pages = ceil($total_articles / $per_page);

// Get news articles with pagination
$offset = ($page - 1) * $per_page;
$sql = "SELECT n.*, c.name as category_name, l.name as location, u.name as author_name 
        FROM news n 
        LEFT JOIN categories c ON n.category_id = c.id 
        LEFT JOIN locations l ON n.location_id = l.id 
        LEFT JOIN users u ON n.user_id = u.id 
        $where_clause 
        ORDER BY n.created_at DESC 
        LIMIT $per_page OFFSET $offset";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$news_articles = $stmt->fetchAll();

// Get categories for filter
$categories = $category->getAll();

// Flash message handling
$flash = null;
if (isset($_SESSION['flash'])) {
    $flash = $_SESSION['flash'];
    unset($_SESSION['flash']);
}

// Set page title and navigation
$page_title = 'News Articles';
$css_path = '../assets/css/style.css';
$home_path = '../dashboard.php';
$dashboard_path = '../dashboard.php';
$news_path = 'index.php';
$create_path = 'create.php';
$editorial_path = '../editorial/dashboard.php';
$admin_path = '../admin/';
$profile_path = '../profile.php';
$logout_path = '../logout.php';

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

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

        <div class="card mb-6">
            <div class="card-header">
                <h2 class="text-xl font-semibold text-cop-dark flex items-center">
                    <i class="fas fa-newspaper mr-2"></i> News Articles
                </h2>
            </div>
            <div class="card-body">
                <!-- Filters and Search -->
                <form method="GET" action="" class="mb-6">
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
                        <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>
                    <div class="flex flex-wrap gap-3">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search mr-2"></i> Search
                        </button>
                        <a href="index.php" class="btn btn-secondary">
                            <i class="fas fa-times mr-2"></i> Clear
                        </a>
                        <a href="create.php" class="btn btn-success">
                            <i class="fas fa-plus mr-2"></i> Add News
                        </a>
                    </div>
                </form>

                <!-- News Articles List -->
                <?php if (empty($news_articles)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-newspaper text-6xl text-gray-300 mb-4"></i>
                        <h3 class="text-xl font-semibold text-gray-700 mb-2">No news articles found</h3>
                        <p class="text-gray-500 mb-6">
                            <?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">
                            <i class="fas fa-plus mr-2"></i> Create News Article
                        </a>
                    </div>
                <?php else: ?>
                    <div class="space-y-4">
                        <?php foreach ($news_articles as $article): ?>
                            <div class="card">
                                <div class="card-body">
                                    <div class="flex justify-between items-start mb-3">
                                        <h4 class="text-lg font-semibold text-cop-dark">
                                            <a href="view.php?id=<?php echo $article['id']; ?>" 
                                               class="text-cop-dark hover:text-cop-blue transition-colors">
                                                <?php echo htmlspecialchars($article['title']); ?>
                                            </a>
                                        </h4>
                                        <span class="badge badge-<?php echo $article['status']; ?>">
                                            <?php echo ucfirst($article['status']); ?>
                                        </span>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <div class="flex flex-wrap gap-4 text-sm text-gray-600">
                                            <span class="flex items-center">
                                                <i class="fas fa-user mr-1"></i> <?php echo htmlspecialchars($article['author_name']); ?>
                                            </span>
                                            <span class="flex items-center">
                                                <i class="fas fa-map-marker-alt mr-1"></i> <?php echo htmlspecialchars($article['location']); ?>
                                            </span>
                                            <?php if ($article['category_name']): ?>
                                                <span class="flex items-center">
                                                    <i class="fas fa-tag mr-1"></i> <?php echo htmlspecialchars($article['category_name']); ?>
                                                </span>
                                            <?php endif; ?>
                                            <span class="flex items-center">
                                                <i class="fas fa-eye mr-1"></i> <?php echo $article['views']; ?> views
                                            </span>
                                            <span class="flex items-center">
                                                <i class="fas fa-calendar mr-1"></i> 
                                                <?php echo date('M j, Y', strtotime($article['created_at'])); ?>
                                            </span>
                                        </div>
                                    </div>
                                    
                                    <?php if ($article['description']): ?>
                                        <p class="text-gray-700 mb-4">
                                            <?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 mr-1"></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 mr-1"></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 mr-1"></i> Delete
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                        <div class="pagination-wrapper text-center mt-6">
                            <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; ?>
                                
                                <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                                    <?php if ($i == $page): ?>
                                        <span class="btn btn-primary btn-sm"><?php echo $i; ?></span>
                                    <?php else: ?>
                                        <a href="?page=<?php echo $i; ?>&<?php echo http_build_query(array_filter($_GET, function($k) { return $k !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                           class="btn btn-secondary btn-sm"><?php echo $i; ?></a>
                                    <?php endif; ?>
                                <?php endfor; ?>
                                
                                <?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>

    <script>
        // Delete confirmation
        document.addEventListener('DOMContentLoaded', function() {
            const deleteButtons = document.querySelectorAll('.delete-btn');
            deleteButtons.forEach(button => {
                button.addEventListener('click', function(e) {
                    e.preventDefault();
                    const title = this.getAttribute('data-title');
                    if (confirm(`Are you sure you want to delete "${title}"? This action cannot be undone.`)) {
                        window.location.href = this.href;
                    }
                });
            });
        });
    </script>

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

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