categories.php 15 KB

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