Sindbad~EG File Manager
<?php
require_once '../config/config.php';
require_admin();
$database = new Database();
$conn = $database->getConnection();
$news = new News($conn);
$user = new User($conn);
$report_type = $_GET['type'] ?? 'overview';
$date_range = $_GET['range'] ?? 'monthly';
$export_format = $_GET['export'] ?? '';
// Calculate date ranges
$end_date = date('Y-m-d');
switch ($date_range) {
case 'daily':
$start_date = date('Y-m-d');
break;
case 'weekly':
$start_date = date('Y-m-d', strtotime('-7 days'));
break;
case 'monthly':
$start_date = date('Y-m-d', strtotime('-30 days'));
break;
case 'quarterly':
$start_date = date('Y-m-d', strtotime('-90 days'));
break;
case '6months':
$start_date = date('Y-m-d', strtotime('-180 days'));
break;
case 'yearly':
$start_date = date('Y-m-d', strtotime('-365 days'));
break;
default:
$start_date = date('Y-m-d', strtotime('-30 days'));
}
// Get report data based on type
$report_data = [];
if ($report_type === 'overview') {
// Overview statistics
$report_data['total_users'] = $user->getTotalCount();
$report_data['total_news'] = $news->getTotalCount();
$report_data['published_news'] = $news->getTotalCount('published');
$report_data['draft_news'] = $news->getTotalCount('draft');
// News by location
$location_stats = $news->getStatsByLocation();
$report_data['location_stats'] = $location_stats;
// Recent activity
$activity_query = "SELECT DATE(created_at) as date, COUNT(*) as count
FROM news
WHERE created_at >= ? AND created_at <= ?
GROUP BY DATE(created_at)
ORDER BY date DESC LIMIT 30";
$stmt = $conn->prepare($activity_query);
$stmt->execute([$start_date, $end_date]);
$report_data['daily_activity'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'users') {
// User statistics
$user_stats_query = "SELECT
account_type,
location_type,
COUNT(*) as count
FROM users
WHERE status = 'active'
GROUP BY account_type, location_type
ORDER BY account_type, location_type";
$stmt = $conn->prepare($user_stats_query);
$stmt->execute();
$report_data['user_stats'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
// User registration trends
$registration_query = "SELECT DATE(created_at) as date, COUNT(*) as count
FROM users
WHERE created_at >= ? AND created_at <= ?
GROUP BY DATE(created_at)
ORDER BY date DESC";
$stmt = $conn->prepare($registration_query);
$stmt->execute([$start_date, $end_date]);
$report_data['registration_trends'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'content') {
// Content statistics
$content_stats_query = "SELECT
c.name as category,
COUNT(n.id) as article_count,
SUM(n.views) as total_views
FROM categories c
LEFT JOIN news n ON c.id = n.category_id
WHERE c.status = 'active'
GROUP BY c.id, c.name
ORDER BY article_count DESC";
$stmt = $conn->prepare($content_stats_query);
$stmt->execute();
$report_data['content_stats'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Top articles
$top_articles_query = "SELECT title, views, location, created_at
FROM news
WHERE status = 'published' AND created_at >= ? AND created_at <= ?
ORDER BY views DESC LIMIT 10";
$stmt = $conn->prepare($top_articles_query);
$stmt->execute([$start_date, $end_date]);
$report_data['top_articles'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Handle export
if ($export_format && !empty($report_data)) {
$filename = "cop_news_report_" . $report_type . "_" . date('Y-m-d') . "." . $export_format;
if ($export_format === 'csv') {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$output = fopen('php://output', 'w');
// Export based on report type
if ($report_type === 'overview') {
fputcsv($output, ['Metric', 'Value']);
fputcsv($output, ['Total Users', $report_data['total_users']]);
fputcsv($output, ['Total News Articles', $report_data['total_news']]);
fputcsv($output, ['Published Articles', $report_data['published_news']]);
fputcsv($output, ['Draft Articles', $report_data['draft_news']]);
fputcsv($output, []);
fputcsv($output, ['Location', 'Article Count']);
foreach ($report_data['location_stats'] as $stat) {
fputcsv($output, [$stat['location'], $stat['count']]);
}
}
fclose($output);
exit;
}
}
$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>Reports - 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">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</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="../news/index.php"><i class="fas fa-newspaper"></i> News</a></li>
<li><a href="index.php"><i class="fas fa-cog"></i> Admin</a></li>
<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-chart-bar"></i> Reports & Analytics</h1>
</div>
<div class="card-body">
<!-- Report Controls -->
<form method="GET" class="mb-4">
<div class="grid grid-3">
<div class="form-group">
<label for="type" class="form-label">Report Type</label>
<select id="type" name="type" class="form-control form-select">
<option value="overview" <?php echo $report_type === 'overview' ? 'selected' : ''; ?>>Overview</option>
<option value="users" <?php echo $report_type === 'users' ? 'selected' : ''; ?>>Users</option>
<option value="content" <?php echo $report_type === 'content' ? 'selected' : ''; ?>>Content</option>
</select>
</div>
<div class="form-group">
<label for="range" class="form-label">Date Range</label>
<select id="range" name="range" class="form-control form-select">
<option value="daily" <?php echo $date_range === 'daily' ? 'selected' : ''; ?>>Today</option>
<option value="weekly" <?php echo $date_range === 'weekly' ? 'selected' : ''; ?>>Last 7 Days</option>
<option value="monthly" <?php echo $date_range === 'monthly' ? 'selected' : ''; ?>>Last 30 Days</option>
<option value="quarterly" <?php echo $date_range === 'quarterly' ? 'selected' : ''; ?>>Last 3 Months</option>
<option value="6months" <?php echo $date_range === '6months' ? 'selected' : ''; ?>>Last 6 Months</option>
<option value="yearly" <?php echo $date_range === 'yearly' ? 'selected' : ''; ?>>Last Year</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-chart-bar"></i> Generate
</button>
<a href="?type=<?php echo $report_type; ?>&range=<?php echo $date_range; ?>&export=csv"
class="btn btn-success">
<i class="fas fa-download"></i> Export CSV
</a>
</div>
</div>
</form>
<!-- Report Content -->
<?php if ($report_type === 'overview'): ?>
<!-- Overview Report -->
<div class="dashboard-stats mb-4">
<div class="stat-card">
<div class="stat-number"><?php echo $report_data['total_users']; ?></div>
<div class="stat-label">Total Users</div>
</div>
<div class="stat-card">
<div class="stat-number"><?php echo $report_data['total_news']; ?></div>
<div class="stat-label">Total Articles</div>
</div>
<div class="stat-card">
<div class="stat-number"><?php echo $report_data['published_news']; ?></div>
<div class="stat-label">Published</div>
</div>
<div class="stat-card">
<div class="stat-number"><?php echo $report_data['draft_news']; ?></div>
<div class="stat-label">Drafts</div>
</div>
</div>
<div class="grid grid-2">
<!-- Location Statistics -->
<div class="card">
<div class="card-header">
<h3>Articles by Location</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['location_stats'])): ?>
<canvas id="locationChart" width="400" height="200"></canvas>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No data available</p>
<?php endif; ?>
</div>
</div>
<!-- Daily Activity -->
<div class="card">
<div class="card-header">
<h3>Daily Activity (<?php echo ucfirst($date_range); ?>)</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['daily_activity'])): ?>
<canvas id="activityChart" width="400" height="200"></canvas>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No data available</p>
<?php endif; ?>
</div>
</div>
</div>
<?php elseif ($report_type === 'users'): ?>
<!-- Users Report -->
<div class="grid grid-2">
<div class="card">
<div class="card-header">
<h3>User Statistics</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['user_stats'])): ?>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Account Type</th>
<th>Location Type</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<?php foreach ($report_data['user_stats'] as $stat): ?>
<tr>
<td><?php echo ucfirst($stat['account_type']); ?></td>
<td><?php echo ucfirst($stat['location_type']); ?></td>
<td><strong><?php echo $stat['count']; ?></strong></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No data available</p>
<?php endif; ?>
</div>
</div>
<div class="card">
<div class="card-header">
<h3>Registration Trends</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['registration_trends'])): ?>
<canvas id="registrationChart" width="400" height="200"></canvas>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No registrations in selected period</p>
<?php endif; ?>
</div>
</div>
</div>
<?php elseif ($report_type === 'content'): ?>
<!-- Content Report -->
<div class="grid grid-2">
<div class="card">
<div class="card-header">
<h3>Content by Category</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['content_stats'])): ?>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Category</th>
<th>Articles</th>
<th>Total Views</th>
</tr>
</thead>
<tbody>
<?php foreach ($report_data['content_stats'] as $stat): ?>
<tr>
<td><?php echo htmlspecialchars($stat['category']); ?></td>
<td><strong><?php echo $stat['article_count']; ?></strong></td>
<td><?php echo number_format($stat['total_views']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No content data available</p>
<?php endif; ?>
</div>
</div>
<div class="card">
<div class="card-header">
<h3>Top Articles (<?php echo ucfirst($date_range); ?>)</h3>
</div>
<div class="card-body">
<?php if (!empty($report_data['top_articles'])): ?>
<?php foreach ($report_data['top_articles'] as $article): ?>
<div class="top-article-item">
<h5><?php echo htmlspecialchars($article['title']); ?></h5>
<div style="font-size: 0.9rem; color: var(--primary-grey);">
<i class="fas fa-eye"></i> <?php echo number_format($article['views']); ?> views
<span class="ml-3">
<i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($article['location']); ?>
</span>
<span class="ml-3">
<i class="fas fa-calendar"></i> <?php echo date('M j, Y', strtotime($article['created_at'])); ?>
</span>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-center" style="color: var(--primary-grey);">No articles in selected period</p>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</main>
<script>
// Chart configurations
<?php if ($report_type === 'overview' && !empty($report_data['location_stats'])): ?>
// Location Chart
const locationCtx = document.getElementById('locationChart').getContext('2d');
new Chart(locationCtx, {
type: 'doughnut',
data: {
labels: <?php echo json_encode(array_column($report_data['location_stats'], 'location')); ?>,
datasets: [{
data: <?php echo json_encode(array_column($report_data['location_stats'], 'count')); ?>,
backgroundColor: [
'#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#06B6D4'
]
}]
},
options: {
responsive: true,
plugins: {
legend: {
position: 'bottom'
}
}
}
});
<?php endif; ?>
<?php if ($report_type === 'overview' && !empty($report_data['daily_activity'])): ?>
// Activity Chart
const activityCtx = document.getElementById('activityChart').getContext('2d');
new Chart(activityCtx, {
type: 'line',
data: {
labels: <?php echo json_encode(array_reverse(array_column($report_data['daily_activity'], 'date'))); ?>,
datasets: [{
label: 'Articles Created',
data: <?php echo json_encode(array_reverse(array_column($report_data['daily_activity'], 'count'))); ?>,
borderColor: '#3B82F6',
backgroundColor: 'rgba(59, 130, 246, 0.1)',
tension: 0.4
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
<?php endif; ?>
<?php if ($report_type === 'users' && !empty($report_data['registration_trends'])): ?>
// Registration Chart
const registrationCtx = document.getElementById('registrationChart').getContext('2d');
new Chart(registrationCtx, {
type: 'bar',
data: {
labels: <?php echo json_encode(array_reverse(array_column($report_data['registration_trends'], 'date'))); ?>,
datasets: [{
label: 'New Registrations',
data: <?php echo json_encode(array_reverse(array_column($report_data['registration_trends'], 'count'))); ?>,
backgroundColor: '#10B981'
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
<?php endif; ?>
</script>
<style>
.top-article-item {
padding: 1rem 0;
border-bottom: 1px solid var(--light-grey);
}
.top-article-item:last-child {
border-bottom: none;
}
.top-article-item h5 {
margin-bottom: 0.5rem;
color: var(--dark-grey);
}
.ml-3 {
margin-left: 1rem;
}
</style>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists