Podcast-server/admin/dashboard.php

142 lines
7.0 KiB
PHP

<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
requireAdmin();
$is_admin = hasRole('admin');
// Delete episode logic
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
// Get file name to delete it from disk
$stmt = $pdo->prepare("SELECT audio_file FROM episodes WHERE id = ?");
$stmt->execute([$id]);
$file = $stmt->fetchColumn();
if ($file) {
$filePath = "../assets/uploads/audio/" . $file;
if (file_exists($filePath)) unlink($filePath);
$stmt = $pdo->prepare("DELETE FROM episodes WHERE id = ?");
$stmt->execute([$id]);
logActivity($_SESSION['admin_id'], 'EPISODE_DELETE', "Deleted episode ID: $id (File: $file)");
}
header("Location: dashboard.php");
exit;
}
// Reset stats logic
if (isset($_POST['reset_stats'])) {
requireRole('admin');
$pdo->exec("TRUNCATE TABLE plays");
logActivity($_SESSION['admin_id'], 'ANALYTICS_RESET', "Reset all play statistics.");
header("Location: dashboard.php?reset=success");
exit;
}
$sql = "SELECT e.*, COUNT(p.id) as play_count
FROM episodes e
LEFT JOIN plays p ON e.id = p.episode_id
GROUP BY e.id
ORDER BY release_date DESC";
$stmt = $pdo->query($sql);
$episodes = $stmt->fetchAll();
// Calculate summary stats
$totalPlays = $pdo->query("SELECT COUNT(*) FROM plays")->fetchColumn();
$totalDurationRaw = $pdo->query("SELECT SUM(duration) FROM plays")->fetchColumn();
$totalDurationMin = floor($totalDurationRaw / 60);
$mostPopular = $pdo->query("
SELECT e.title, COUNT(p.id) as play_count
FROM episodes e
JOIN plays p ON e.id = p.episode_id
GROUP BY e.id
ORDER BY play_count DESC
LIMIT 1
")->fetch();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - <?php echo getSetting($pdo, 'site_title'); ?></title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<nav>
<a href="<?php echo PROJECT_ROOT_URL; ?>/" class="logo">Admin Dashboard</a>
<div class="nav-links">
<a href="dashboard.php" style="color: var(--primary-color);">Episodes</a>
<a href="upload.php">Upload New</a>
<?php if ($is_admin): ?>
<a href="settings.php">Site Settings</a>
<a href="users.php">Manage Users</a>
<a href="system.php">System</a>
<?php endif; ?>
<a href="logout.php">Logout</a>
</div>
</nav>
<div class="container" style="margin-top: 3rem;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1.5rem; margin-bottom: 3rem;">
<div class="episode-card" style="margin-bottom: 0; padding: 1.5rem; text-align: center;">
<div class="episode-meta">Total Plays</div>
<div style="font-size: 2rem; font-weight: 700; color: var(--primary-color);"><?php echo number_format($totalPlays); ?></div>
</div>
<div class="episode-card" style="margin-bottom: 0; padding: 1.5rem; text-align: center;">
<div class="episode-meta">Listen Time</div>
<div style="font-size: 2rem; font-weight: 700; color: var(--primary-color);"><?php echo $totalDurationMin; ?> <span style="font-size: 1rem; font-weight: 400;">min</span></div>
</div>
<div class="episode-card" style="margin-bottom: 0; padding: 1.5rem; text-align: center;">
<div class="episode-meta">Most Popular</div>
<div style="font-size: 1.1rem; font-weight: 600; margin-top: 0.5rem;"><?php echo $mostPopular ? htmlspecialchars($mostPopular['title']) : 'N/A'; ?></div>
<?php if($mostPopular): ?>
<div style="font-size: 0.8rem; color: var(--text-muted);"><?php echo $mostPopular['play_count']; ?> plays</div>
<?php endif; ?>
</div>
</div>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
<h2>Podcast Episodes</h2>
<div style="display: flex; gap: 1rem;">
<?php if ($is_admin): ?>
<form method="POST" onsubmit="return confirm('WARNING: This will permanently delete all play statistics. Are you sure?')">
<button type="submit" name="reset_stats" class="btn" style="background: rgba(239, 68, 68, 0.1); color: #ef4444;">Reset All Analytics</button>
</form>
<?php endif; ?>
<a href="upload.php" class="btn btn-primary">+ Upload New Episode</a>
</div>
</div>
<div class="episode-list">
<?php foreach ($episodes as $episode): ?>
<div class="episode-card" style="display: flex; justify-content: space-between; align-items: center;">
<div style="display: flex; align-items: center; gap: 1.5rem;">
<div class="episode-icon" style="background: var(--primary-color); width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center;">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path><path d="M19 10v2a7 7 0 0 1-14 0v-2"></path><line x1="12" y1="19" x2="12" y2="23"></line><line x1="8" y1="23" x2="16" y2="23"></line></svg>
</div>
<div>
<div class="episode-meta"><?php echo formatDate($episode['release_date']); ?> &bull; <?php echo number_format($episode['play_count']); ?> plays</div>
<h3 class="episode-title" style="font-size: 1.25rem; margin-bottom: 0.2rem;">
<a href="<?php echo PROJECT_ROOT_URL; ?>/#episode-<?php echo $episode['id']; ?>" target="_blank" style="color: inherit; text-decoration: none;"><?php echo htmlspecialchars($episode['title']); ?></a>
</h3>
</div>
</div>
<div style="display: flex; gap: 1rem;">
<a href="edit_episode.php?id=<?php echo $episode['id']; ?>" class="btn" style="background: rgba(99, 102, 241, 0.1); color: var(--primary-color);">Edit</a>
<a href="?delete=<?php echo $episode['id']; ?>" class="btn" style="background: rgba(239, 68, 68, 0.1); color: #ef4444;" onclick="return confirm('Are you sure you want to delete this episode?')">Delete</a>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($episodes)): ?>
<p style="text-align: center; color: var(--text-muted);">No episodes found. Start by uploading one!</p>
<?php endif; ?>
</div>
</div>
<?php include '../includes/footer.php'; ?>
</body>
</html>