categories.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. require_once '../includes/config.php';
  3. require_once '../includes/database.php';
  4. require_once '../includes/publication.php';
  5. $publication = new Publication();
  6. $category = $_GET['category'] ?? '';
  7. $page = max(1, (int)($_GET['page'] ?? 1));
  8. $limit = 10;
  9. $offset = ($page - 1) * $limit;
  10. // Get categories for sidebar
  11. $categories = $publication->getCategories();
  12. // Get publications for selected category or all categories
  13. if ($category) {
  14. $publications = $publication->getByCategory($category, 'published');
  15. $totalPublications = count($publications);
  16. // Apply pagination
  17. $publications = array_slice($publications, $offset, $limit);
  18. $currentCategory = $category;
  19. } else {
  20. // Show all publications when no specific category is selected
  21. $publications = $publication->getAll('published', $limit, $offset);
  22. $totalPublications = $publication->db->fetch("SELECT COUNT(*) as count FROM publications WHERE status = 'published'")['count'];
  23. $currentCategory = '';
  24. }
  25. // Calculate pagination
  26. $totalPages = ceil($totalPublications / $limit);
  27. ?>
  28. <!DOCTYPE html>
  29. <html lang="en">
  30. <head>
  31. <meta charset="UTF-8">
  32. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  33. <title>Categories - <?php echo SITE_TITLE; ?></title>
  34. <link rel="stylesheet" href="../css/style.css">
  35. </head>
  36. <body>
  37. <header class="site-header">
  38. <div class="container">
  39. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  40. <nav class="main-nav">
  41. <a href="index.php">Home</a>
  42. <a href="categories.php" class="active">Categories</a>
  43. <a href="search.php">Search</a>
  44. </nav>
  45. </div>
  46. </header>
  47. <div class="container">
  48. <div class="content-layout">
  49. <aside class="sidebar">
  50. <div class="sidebar-section">
  51. <h3>Search</h3>
  52. <form method="get" action="search.php" class="search-form">
  53. <input type="text" name="q" placeholder="Search publications...">
  54. <button type="submit" class="btn btn-sm">Search</button>
  55. </form>
  56. </div>
  57. <div class="sidebar-section">
  58. <h3>Categories</h3>
  59. <ul class="category-list">
  60. <li>
  61. <a href="categories.php" <?php echo empty($currentCategory) ? 'class="active"' : ''; ?>>
  62. All Categories
  63. </a>
  64. </li>
  65. <?php foreach ($categories as $cat): ?>
  66. <li>
  67. <a href="categories.php?category=<?php echo urlencode($cat['name']); ?>"
  68. <?php echo $currentCategory === $cat['name'] ? 'class="active"' : ''; ?>>
  69. <?php echo htmlspecialchars($cat['name']); ?>
  70. <span class="count">(<?php echo $cat['publication_count']; ?>)</span>
  71. </a>
  72. </li>
  73. <?php endforeach; ?>
  74. </ul>
  75. </div>
  76. <div class="sidebar-section">
  77. <h3>Category Stats</h3>
  78. <div class="category-stats">
  79. <div class="stat-item">
  80. <span class="stat-label">Total Categories:</span>
  81. <span class="stat-value"><?php echo count($categories); ?></span>
  82. </div>
  83. <div class="stat-item">
  84. <span class="stat-label">Total Publications:</span>
  85. <span class="stat-value">
  86. <?php echo array_sum(array_column($categories, 'publication_count')); ?>
  87. </span>
  88. </div>
  89. </div>
  90. </div>
  91. </aside>
  92. <main class="main-content">
  93. <div class="page-header">
  94. <h2>
  95. <?php if ($currentCategory): ?>
  96. Category: <?php echo htmlspecialchars($currentCategory); ?>
  97. <?php else: ?>
  98. All Categories
  99. <?php endif; ?>
  100. </h2>
  101. <?php if ($currentCategory): ?>
  102. <a href="categories.php" class="btn btn-secondary">View All Categories</a>
  103. <?php endif; ?>
  104. </div>
  105. <?php if ($currentCategory): ?>
  106. <div class="category-info">
  107. <?php
  108. $categoryInfo = array_filter($categories, function($cat) use ($currentCategory) {
  109. return $cat['name'] === $currentCategory;
  110. });
  111. $categoryInfo = reset($categoryInfo);
  112. ?>
  113. <?php if ($categoryInfo && $categoryInfo['description']): ?>
  114. <div class="category-description">
  115. <p><?php echo htmlspecialchars($categoryInfo['description']); ?></p>
  116. </div>
  117. <?php endif; ?>
  118. <p class="results-count">
  119. <?php echo $totalPublications; ?> publications in this category
  120. </p>
  121. </div>
  122. <?php else: ?>
  123. <div class="category-overview">
  124. <h3>Category Overview</h3>
  125. <div class="category-grid">
  126. <?php foreach ($categories as $cat): ?>
  127. <?php if ($cat['publication_count'] > 0): ?>
  128. <div class="category-card">
  129. <h4>
  130. <a href="categories.php?category=<?php echo urlencode($cat['name']); ?>">
  131. <?php echo htmlspecialchars($cat['name']); ?>
  132. </a>
  133. </h4>
  134. <?php if ($cat['description']): ?>
  135. <p class="category-description">
  136. <?php echo htmlspecialchars(substr($cat['description'], 0, 150)) . (strlen($cat['description']) > 150 ? '...' : ''); ?>
  137. </p>
  138. <?php endif; ?>
  139. <div class="category-meta">
  140. <span class="publication-count"><?php echo $cat['publication_count']; ?> publications</span>
  141. <a href="categories.php?category=<?php echo urlencode($cat['name']); ?>" class="view-link">
  142. View Publications
  143. </a>
  144. </div>
  145. </div>
  146. <?php endif; ?>
  147. <?php endforeach; ?>
  148. </div>
  149. </div>
  150. <?php endif; ?>
  151. <?php if ($currentCategory && !empty($publications)): ?>
  152. <div class="publication-list">
  153. <h3>Publications in <?php echo htmlspecialchars($currentCategory); ?></h3>
  154. <?php foreach ($publications as $pub): ?>
  155. <article class="publication-summary">
  156. <h3>
  157. <a href="publication.php?id=<?php echo $pub['id']; ?>">
  158. <?php echo htmlspecialchars($pub['title']); ?>
  159. </a>
  160. </h3>
  161. <?php if ($pub['summary']): ?>
  162. <p class="summary"><?php echo htmlspecialchars($pub['summary']); ?></p>
  163. <?php endif; ?>
  164. <div class="meta">
  165. <span class="author">By <?php echo htmlspecialchars($pub['author']); ?></span>
  166. <span class="date"><?php echo date('F j, Y', strtotime($pub['created_at'])); ?></span>
  167. <?php if ($pub['categories']): ?>
  168. <span class="categories">
  169. <?php foreach (explode(',', $pub['categories']) as $cat): ?>
  170. <a href="categories.php?category=<?php echo urlencode(trim($cat)); ?>" class="category-tag">
  171. <?php echo htmlspecialchars(trim($cat)); ?>
  172. </a>
  173. <?php endforeach; ?>
  174. </span>
  175. <?php endif; ?>
  176. </div>
  177. </article>
  178. <?php endforeach; ?>
  179. </div>
  180. <?php if ($totalPages > 1): ?>
  181. <div class="pagination">
  182. <?php if ($page > 1): ?>
  183. <a href="?category=<?php echo urlencode($currentCategory); ?>&page=<?php echo $page - 1; ?>" class="pagination-link">« Previous</a>
  184. <?php endif; ?>
  185. <?php for ($i = 1; $i <= $totalPages; $i++): ?>
  186. <?php if ($i == $page): ?>
  187. <span class="pagination-current"><?php echo $i; ?></span>
  188. <?php else: ?>
  189. <a href="?category=<?php echo urlencode($currentCategory); ?>&page=<?php echo $i; ?>" class="pagination-link"><?php echo $i; ?></a>
  190. <?php endif; ?>
  191. <?php endfor; ?>
  192. <?php if ($page < $totalPages): ?>
  193. <a href="?category=<?php echo urlencode($currentCategory); ?>&page=<?php echo $page + 1; ?>" class="pagination-link">Next »</a>
  194. <?php endif; ?>
  195. </div>
  196. <?php endif; ?>
  197. <?php elseif ($currentCategory): ?>
  198. <p>No publications found in this category.</p>
  199. <?php endif; ?>
  200. </main>
  201. </div>
  202. </div>
  203. <footer class="site-footer">
  204. <div class="container">
  205. <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_TITLE; ?>. All rights reserved.</p>
  206. </div>
  207. </footer>
  208. <style>
  209. .page-header {
  210. display: flex;
  211. justify-content: space-between;
  212. align-items: center;
  213. margin-bottom: 2rem;
  214. }
  215. .category-info {
  216. background: #f8f9fa;
  217. padding: 1.5rem;
  218. border-radius: 0.5rem;
  219. margin-bottom: 2rem;
  220. border-left: 4px solid #007bff;
  221. }
  222. .category-description {
  223. margin-bottom: 1rem;
  224. }
  225. .category-description p {
  226. margin: 0;
  227. color: #495057;
  228. }
  229. .category-overview {
  230. margin-bottom: 2rem;
  231. }
  232. .category-grid {
  233. display: grid;
  234. grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  235. gap: 1.5rem;
  236. margin-top: 1.5rem;
  237. }
  238. .category-card {
  239. background: white;
  240. padding: 1.5rem;
  241. border-radius: 0.5rem;
  242. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  243. border: 1px solid #dee2e6;
  244. }
  245. .category-card h4 {
  246. margin-bottom: 1rem;
  247. }
  248. .category-card h4 a {
  249. color: #007bff;
  250. text-decoration: none;
  251. }
  252. .category-card h4 a:hover {
  253. text-decoration: underline;
  254. }
  255. .category-card .category-description {
  256. color: #6c757d;
  257. margin-bottom: 1rem;
  258. font-size: 0.875rem;
  259. }
  260. .category-meta {
  261. display: flex;
  262. justify-content: space-between;
  263. align-items: center;
  264. font-size: 0.875rem;
  265. }
  266. .publication-count {
  267. color: #6c757d;
  268. font-weight: 500;
  269. }
  270. .view-link {
  271. color: #007bff;
  272. text-decoration: none;
  273. }
  274. .view-link:hover {
  275. text-decoration: underline;
  276. }
  277. .category-stats {
  278. background: white;
  279. padding: 1rem;
  280. border-radius: 0.375rem;
  281. border: 1px solid #dee2e6;
  282. }
  283. .stat-item {
  284. display: flex;
  285. justify-content: space-between;
  286. margin-bottom: 0.5rem;
  287. }
  288. .stat-item:last-child {
  289. margin-bottom: 0;
  290. }
  291. .stat-label {
  292. color: #6c757d;
  293. font-size: 0.875rem;
  294. }
  295. .stat-value {
  296. font-weight: 600;
  297. color: #495057;
  298. }
  299. @media (max-width: 768px) {
  300. .page-header {
  301. flex-direction: column;
  302. gap: 1rem;
  303. align-items: stretch;
  304. }
  305. .category-grid {
  306. grid-template-columns: 1fr;
  307. }
  308. .category-meta {
  309. flex-direction: column;
  310. gap: 0.5rem;
  311. align-items: flex-start;
  312. }
  313. }
  314. </style>
  315. </body>
  316. </html>