churchtube/index.php

103 lines
4.5 KiB
PHP

<?php
if (!file_exists('includes/config.php')) {
header('Location: install.php');
exit;
}
require_once 'includes/db.php';
require_once 'includes/settings_helper.php';
require_once 'includes/header.php';
$page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$query = "SELECT DISTINCT v.*, u.username as uploader FROM videos v
LEFT JOIN users u ON v.uploader_id = u.id
LEFT JOIN video_tags vt ON v.id = vt.video_id
LEFT JOIN tags t ON vt.tag_id = t.id";
$search = $_GET['q'] ?? '';
$tag_filter = $_GET['tag'] ?? '';
$params = [];
$where_clauses = [];
if ($search) {
$where_clauses[] = "(v.title LIKE ? OR v.description LIKE ? OR t.name LIKE ?)";
$params = array_merge($params, ["%$search%", "%$search%", "%$search%"]);
}
if ($tag_filter) {
$where_clauses[] = "t.name = ?";
$params[] = $tag_filter;
}
if (!empty($where_clauses)) {
$query .= " WHERE " . implode(" AND ", $where_clauses);
}
// Count total for pagination
$count_query = "SELECT COUNT(DISTINCT v.id) FROM videos v " .
"LEFT JOIN video_tags vt ON v.id = vt.video_id " .
"LEFT JOIN tags t ON vt.tag_id = t.id " .
(!empty($where_clauses) ? " WHERE " . implode(" AND ", $where_clauses) : "");
$total_stmt = $pdo->prepare($count_query);
$total_stmt->execute($params);
$total_count = $total_stmt->fetchColumn();
$total_pages = ceil($total_count / $limit);
$query .= " ORDER BY v.release_date DESC, v.created_at DESC LIMIT $limit OFFSET $offset";
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$videos = $stmt->fetchAll();
// Get popular tags for chips
$popular_tags = $pdo->query("SELECT name FROM tags LIMIT 10")->fetchAll(PDO::FETCH_COLUMN);
?>
<div class="video-grid">
<?php if (empty($videos)): ?>
<div style="grid-column: 1/-1; text-align: center; padding: 100px; color: var(--text-muted);">
<i class="fas fa-video-slash" style="font-size: 3rem; margin-bottom: 20px; display: block;"></i>
<h3>No sermons found.</h3>
<?php if (isEditor()): ?>
<p>Click <a href="admin/add_video.php" style="color: var(--primary-color);">here</a> to add your first video.</p>
<?php endif; ?>
</div>
<?php else: ?>
<?php foreach ($videos as $video): ?>
<a href="watch.php?id=<?= $video['id'] ?>" class="video-card">
<div class="video-thumbnail" style="background-image: url('<?= $video['thumbnail_url'] ?: 'assets/images/default_thumb.png' ?>');">
</div>
<div class="video-info">
<h3 class="video-title"><?= htmlspecialchars($video['title']) ?></h3>
<div style="font-size: 0.8rem; color: var(--text-muted); margin: 8px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;">
<?= htmlspecialchars($video['description']) ?>
</div>
<div class="video-meta">
<span><?= htmlspecialchars($video['uploader']) ?></span> •
<span><?= number_format($video['views']) ?> views</span> •
<span><?= date('M d, Y', strtotime($video['release_date'])) ?></span>
</div>
</div>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php if ($total_pages > 1): ?>
<div style="display: flex; justify-content: center; gap: 12px; margin: 40px 0;">
<?php if ($page > 1): ?>
<a href="?p=<?= $page - 1 ?><?= $search ? "&q=".urlencode($search) : '' ?><?= $tag_filter ? "&tag=".urlencode($tag_filter) : '' ?>" class="btn" style="background: var(--glass);"><i class="fas fa-chevron-left"></i> Previous</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<a href="?p=<?= $i ?><?= $search ? "&q=".urlencode($search) : '' ?><?= $tag_filter ? "&tag=".urlencode($tag_filter) : '' ?>" class="btn" style="background: <?= $i === $page ? 'var(--primary-color)' : 'var(--glass)' ?>; min-width: 40px; justify-content: center;"><?= $i ?></a>
<?php endfor; ?>
<?php if ($page < $total_pages): ?>
<a href="?p=<?= $page + 1 ?><?= $search ? "&q=".urlencode($search) : '' ?><?= $tag_filter ? "&tag=".urlencode($tag_filter) : '' ?>" class="btn" style="background: var(--glass);">Next <i class="fas fa-chevron-right"></i></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php require_once 'includes/footer.php'; ?>