-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.php
More file actions
276 lines (253 loc) · 11.5 KB
/
tag.php
File metadata and controls
276 lines (253 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
require_once 'includes/dbh.inc.php';
require_once 'includes/cache-manager.inc.php';
require_once 'includes/tag-functions.inc.php';
session_set_cookie_params(0, '/');
session_start();
$slug = $_GET['slug'] ?? '';
if (empty($slug)) {
header('Location: /');
exit;
}
// Pagination settings
$postsPerPage = 10;
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($currentPage - 1) * $postsPerPage;
try {
// Get tag information
$stmt = $pdo->prepare("SELECT * FROM Tags WHERE Slug = ?");
$stmt->execute([$slug]);
$tag = $stmt->fetch();
if (!$tag) {
header('HTTP/1.0 404 Not Found');
exit('Tag not found');
}
// Check for search query
$searchTerm = $_GET['q'] ?? '';
if (!empty($searchTerm)) {
// Use the search handler with tag filter
require_once 'includes/search-handler.inc.php';
$searchResults = getSearchResults($pdo, $searchTerm, $currentPage, $postsPerPage, [
'tagId' => $tag['TagID']
]);
$posts = $searchResults['posts'];
$totalPosts = $searchResults['total'];
$totalPages = $searchResults['pages'];
} else {
// Original tag posts query code
$stmt = $pdo->prepare("
SELECT COUNT(DISTINCT p.PostID)
FROM Posts p
JOIN PostTags pt ON p.PostID = pt.PostID
WHERE pt.TagID = ? AND p.Status = 'published'
");
$stmt->execute([$tag['TagID']]);
$totalPosts = $stmt->fetchColumn();
$totalPages = ceil($totalPosts / $postsPerPage);
}
// Get posts for current page
$stmt = $pdo->prepare("
SELECT
p.PostID,
p.Title,
p.CreatedAt,
p.FeaturedImage,
p.FeaturedImageCaption,
LEFT(p.Content, 300) as Excerpt,
u.Username as AuthorName,
GROUP_CONCAT(DISTINCT c.Name) as Categories,
GROUP_CONCAT(DISTINCT t.Name) as Tags
FROM Posts p
LEFT JOIN Users u ON p.UserID = u.UserID
LEFT JOIN PostCategories pc ON p.PostID = pc.PostID
LEFT JOIN Categories c ON pc.CategoryID = c.CategoryID
LEFT JOIN PostTags pt ON p.PostID = pt.PostID
LEFT JOIN Tags t ON pt.TagID = t.TagID
WHERE p.Status = 'published' AND pt.TagID = ?
GROUP BY p.PostID
ORDER BY p.CreatedAt DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$tag['TagID'], $postsPerPage, $offset]);
$posts = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Posts tagged with: <?php echo htmlspecialchars($tag['Name']); ?></title>
<link rel="stylesheet" href="/css/blog-styles.css">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<?php include_once 'header.php'; ?>
<script src="/js/breadcrumbs.js"></script>
<div class="blog-header-container">
<div class="blog-header">
<h1>Posts tagged with: <?php echo htmlspecialchars($tag['Name']); ?></h1>
</div>
</div>
<div class="blog-container">
<!-- Posts Grid -->
<div class="posts-grid">
<?php if (empty($posts)): ?>
<div class="no-posts">
<p>No posts found with this tag.</p>
</div>
<?php else: ?>
<?php foreach ($posts as $post):
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $post['Title'])));
?>
<article class="post-card">
<?php if ($post['FeaturedImage']): ?>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="post-thumbnail-link">
<img src="<?php echo htmlspecialchars($post['FeaturedImage']); ?>"
alt="<?php echo htmlspecialchars($post['Title']); ?>"
class="post-thumbnail">
</a>
<?php endif; ?>
<div class="post-content">
<h2>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>">
<?php echo htmlspecialchars($post['Title']); ?>
</a>
</h2>
<div class="post-meta">
<span class="post-date">
<?php echo date('F j, Y', strtotime($post['CreatedAt'])); ?>
</span>
<span class="post-author">
by <?php echo htmlspecialchars($post['AuthorName']); ?>
</span>
<?php if ($post['Categories']): ?>
<span class="post-categories">
<?php
$categories = explode(',', $post['Categories']);
foreach ($categories as $index => $category):
$categorySlug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $category)));
?>
<a class="article-category-tag" href="/category/<?php echo $categorySlug; ?>">
<?php echo htmlspecialchars(trim($category)); ?>
</a><?php echo $index < count($categories) - 1 ? ' ' : ''; ?>
<?php endforeach; ?>
</span>
<?php endif; ?>
<?php if ($post['Tags']): ?>
<span class="post-tags">
<?php
$tags = explode(',', $post['Tags']);
foreach ($tags as $index => $t):
$tagSlug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $t)));
?>
<a class="tag" href="/tag/<?php echo $tagSlug; ?>">
<?php echo htmlspecialchars(trim($t)); ?>
</a><?php echo $index < count($tags) - 1 ? ' ' : ''; ?>
<?php endforeach; ?>
</span>
<?php endif; ?>
</div>
<div class="post-excerpt">
<?php
$excerpt = strip_tags($post['Excerpt']);
if (strlen($excerpt) >= 300) {
$excerpt = substr($excerpt, 0, strrpos(substr($excerpt, 0, 300), ' ')) . '...';
}
echo $excerpt;
?>
</div>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="read-more">
Read More
</a>
</div>
</article>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Sidebar -->
<div class="blog-sidebar">
<!-- Search -->
<div class="blog-search-container">
<input type="text" id="search-bar" placeholder="Search...">
</div>
<!-- Tags -->
<div class="blog-categories">
<h2>Popular Tags</h2>
<div class="tag-cloud">
<?php
$tags = getTagsWithPostCount($pdo);
foreach ($tags as $t):
$tagSlug = htmlspecialchars($t['Slug']);
$name = htmlspecialchars($t['Name']);
$count = (int)$t['post_count'];
$isActive = $t['TagID'] === $tag['TagID'];
?>
<a href="/tag/<?php echo $tagSlug; ?>"
class="tag <?php echo $isActive ? 'active' : ''; ?>">
<?php echo $name; ?> (<?php echo $count; ?>)
</a>
<?php endforeach; ?>
</div>
</div>
<!-- Categories -->
<div class="blog-categories">
<h2>Categories</h2>
<ul class="blog-categories-list">
<?php
require_once 'includes/category-functions.inc.php';
$categories = getCategoriesWithPostCount($pdo);
foreach ($categories as $category):
$slug = htmlspecialchars($category['Slug']);
$name = htmlspecialchars($category['Name']);
$count = (int)$category['post_count'];
?>
<li class="blog-category">
<a class="category-link" href="/category/<?php echo $slug; ?>">
<span class="category-name"><?php echo $name; ?></span>
<span class="category-count"><?php echo $count; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- Ad Box -->
<div class="sidebar-sponsor-container">
<img src="/img/sponsor_spot.png" alt="Ads support us so much!">
</div>
</div>
</div>
<?php if ($totalPages > 1): ?>
<nav class="pagination">
<?php if ($currentPage > 1): ?>
<a href="/tag/<?php echo $tag['Slug']; ?>/page/<?php echo ($currentPage - 1); ?>" class="page-link">Previous</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<?php if (
$i == 1 ||
$i == $totalPages ||
($i >= $currentPage - 2 && $i <= $currentPage + 2)
): ?>
<a href="/tag/<?php echo $tag['Slug']; ?>/page/<?php echo $i; ?>"
class="page-link <?php echo $i === $currentPage ? 'active' : ''; ?>"><?php echo $i; ?></a>
<?php elseif (
$i == $currentPage - 3 ||
$i == $currentPage + 3
): ?>
<span class="page-ellipsis">...</span>
<?php endif; ?>
<?php endfor; ?>
<?php if ($currentPage < $totalPages): ?>
<a href="/tag/<?php echo $tag['Slug']; ?>/page/<?php echo ($currentPage + 1); ?>" class="page-link">Next</a>
<?php endif; ?>
</nav>
<?php endif; ?>
<?php include_once 'footer.php'; ?>
<script src="/js/search.js"></script>
</body>
</html>
<?php
} catch (PDOException $e) {
error_log("Error in tag.php: " . $e->getMessage());
echo "An error occurred while loading the tagged posts.";
}
?>