index.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Get search query
  7. $search = $_GET['search'] ?? '';
  8. $category = $_GET['category'] ?? '';
  9. $page = max(1, (int)($_GET['page'] ?? 1));
  10. $limit = 10;
  11. $offset = ($page - 1) * $limit;
  12. // Get publications
  13. if ($search) {
  14. $publications = $publication->search($search, 'published');
  15. $totalPublications = count($publications);
  16. } elseif ($category) {
  17. $publications = $publication->getByCategory($category, 'published');
  18. $totalPublications = count($publications);
  19. } else {
  20. $publications = $publication->getAll('published', $limit, $offset);
  21. $totalPublications = $publication->db->fetch("SELECT COUNT(*) as count FROM publications WHERE status = 'published'")['count'];
  22. }
  23. // Get categories for sidebar
  24. $categories = $publication->getCategories();
  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><?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">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" class="search-form">
  53. <input type="text" name="search" placeholder="Search publications..."
  54. value="<?php echo htmlspecialchars($search); ?>">
  55. <button type="submit" class="btn btn-sm">Search</button>
  56. </form>
  57. </div>
  58. <div class="sidebar-section">
  59. <h3>Categories</h3>
  60. <ul class="category-list">
  61. <li>
  62. <a href="index.php" <?php echo empty($category) ? 'class="active"' : ''; ?>>
  63. All Categories
  64. </a>
  65. </li>
  66. <?php foreach ($categories as $cat): ?>
  67. <li>
  68. <a href="index.php?category=<?php echo urlencode($cat['name']); ?>"
  69. <?php echo $category === $cat['name'] ? 'class="active"' : ''; ?>>
  70. <?php echo htmlspecialchars($cat['name']); ?>
  71. <span class="count">(<?php echo $cat['publication_count']; ?>)</span>
  72. </a>
  73. </li>
  74. <?php endforeach; ?>
  75. </ul>
  76. </div>
  77. </aside>
  78. <main class="main-content">
  79. <?php if ($search): ?>
  80. <h2>Search Results for "<?php echo htmlspecialchars($search); ?>"</h2>
  81. <p class="results-count">Found <?php echo $totalPublications; ?> publications</p>
  82. <?php elseif ($category): ?>
  83. <h2>Category: <?php echo htmlspecialchars($category); ?></h2>
  84. <p class="results-count"><?php echo $totalPublications; ?> publications</p>
  85. <?php else: ?>
  86. <h2>Latest Publications</h2>
  87. <?php endif; ?>
  88. <?php if (empty($publications)): ?>
  89. <p>No publications found.</p>
  90. <?php else: ?>
  91. <div class="publication-list">
  92. <?php foreach ($publications as $pub): ?>
  93. <article class="publication-summary">
  94. <h3>
  95. <a href="publication.php?id=<?php echo $pub['id']; ?>">
  96. <?php echo htmlspecialchars($pub['title']); ?>
  97. </a>
  98. </h3>
  99. <?php if ($pub['summary']): ?>
  100. <p class="summary"><?php echo htmlspecialchars($pub['summary']); ?></p>
  101. <?php endif; ?>
  102. <div class="meta">
  103. <span class="author">By <?php echo htmlspecialchars($pub['author']); ?></span>
  104. <span class="date"><?php echo date('F j, Y', strtotime($pub['created_at'])); ?></span>
  105. <?php if ($pub['categories']): ?>
  106. <span class="categories">
  107. <?php foreach (explode(',', $pub['categories']) as $cat): ?>
  108. <a href="index.php?category=<?php echo urlencode(trim($cat)); ?>" class="category-tag">
  109. <?php echo htmlspecialchars(trim($cat)); ?>
  110. </a>
  111. <?php endforeach; ?>
  112. </span>
  113. <?php endif; ?>
  114. </div>
  115. </article>
  116. <?php endforeach; ?>
  117. </div>
  118. <?php if (!$search && !$category && $totalPages > 1): ?>
  119. <div class="pagination">
  120. <?php if ($page > 1): ?>
  121. <a href="?page=<?php echo $page - 1; ?>" class="pagination-link">« Previous</a>
  122. <?php endif; ?>
  123. <?php for ($i = 1; $i <= $totalPages; $i++): ?>
  124. <?php if ($i == $page): ?>
  125. <span class="pagination-current"><?php echo $i; ?></span>
  126. <?php else: ?>
  127. <a href="?page=<?php echo $i; ?>" class="pagination-link"><?php echo $i; ?></a>
  128. <?php endif; ?>
  129. <?php endfor; ?>
  130. <?php if ($page < $totalPages): ?>
  131. <a href="?page=<?php echo $page + 1; ?>" class="pagination-link">Next »</a>
  132. <?php endif; ?>
  133. </div>
  134. <?php endif; ?>
  135. <?php endif; ?>
  136. </main>
  137. </div>
  138. </div>
  139. <footer class="site-footer">
  140. <div class="container">
  141. <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_TITLE; ?>. All rights reserved.</p>
  142. </div>
  143. </footer>
  144. </body>
  145. </html>