publication.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/comment.php';
  10. require_once '../includes/captcha.php';
  11. require_once '../includes/translation.php';
  12. // Translation system is auto-initialized when translation.php is included
  13. $publication = new Publication();
  14. $comment = new Comment();
  15. $id = (int)($_GET['id'] ?? 0);
  16. $pub = $publication->getById($id);
  17. if (!$pub || $pub['status'] !== 'published') {
  18. http_response_code(404);
  19. echo '<h1>' . t('error_publication_not_found') . '</h1>';
  20. echo '<p>' . t('error_publication_not_found_description') . '</p>';
  21. exit;
  22. }
  23. // Get related publications
  24. $relatedPublications = [];
  25. if ($pub['categories_array']) {
  26. $firstCategory = $pub['categories_array'][0];
  27. $relatedPublications = $publication->getByCategory($firstCategory, 'published');
  28. // Remove current publication from related list
  29. $relatedPublications = array_filter($relatedPublications, function($p) use ($id) {
  30. return $p['id'] != $id;
  31. });
  32. // Limit to 3 related publications
  33. $relatedPublications = array_slice($relatedPublications, 0, 3);
  34. }
  35. // Get approved comments for this publication
  36. $comments = $comment->getApprovedByPublication($id);
  37. $commentCount = $comment->getCountByPublication($id);
  38. ?>
  39. <!DOCTYPE html>
  40. <html lang="en">
  41. <head>
  42. <meta charset="UTF-8">
  43. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  44. <title><?php echo htmlspecialchars($pub['title']); ?> - <?php echo SITE_TITLE; ?></title>
  45. <link rel="stylesheet" href="../css/style.css">
  46. <link rel="stylesheet" href="../css/comments.css">
  47. </head>
  48. <body>
  49. <header class="site-header">
  50. <div class="container">
  51. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  52. <nav class="main-nav">
  53. <a href="index.php"><?php echo t('nav_home'); ?></a>
  54. <a href="categories.php"><?php echo t('nav_categories'); ?></a>
  55. <a href="search.php"><?php echo t('nav_search'); ?></a>
  56. </nav>
  57. <?php echo Translation::getLanguageSwitcher('publication.php?id=' . $id); ?>
  58. </div>
  59. </header>
  60. <div class="container">
  61. <article class="publication-full">
  62. <header class="publication-header">
  63. <h1><?php echo htmlspecialchars($pub['title']); ?></h1>
  64. <div class="publication-meta">
  65. <span class="author">By <?php echo htmlspecialchars($pub['author']); ?></span>
  66. <span class="date"><?php echo date('F j, Y', strtotime($pub['created_at'])); ?></span>
  67. <?php if ($pub['published_at']): ?>
  68. <span class="published-date">Published <?php echo date('F j, Y', strtotime($pub['published_at'])); ?></span>
  69. <?php endif; ?>
  70. </div>
  71. <?php if ($pub['categories_array']): ?>
  72. <div class="publication-categories">
  73. <?php foreach ($pub['categories_array'] as $category): ?>
  74. <a href="index.php?category=<?php echo urlencode(trim($category)); ?>" class="category-tag">
  75. <?php echo htmlspecialchars(trim($category)); ?>
  76. </a>
  77. <?php endforeach; ?>
  78. </div>
  79. <?php endif; ?>
  80. </header>
  81. <div class="publication-content">
  82. <?php if ($pub['summary']): ?>
  83. <div class="publication-summary">
  84. <p><?php echo htmlspecialchars($pub['summary']); ?></p>
  85. </div>
  86. <?php endif; ?>
  87. <div class="publication-body">
  88. <?php echo $pub['content']; ?>
  89. </div>
  90. </div>
  91. </article>
  92. <!-- Comments Section -->
  93. <section class="comments-section">
  94. <h3><?php echo t('comments'); ?> (<?php echo $commentCount; ?>)</h3>
  95. <?php if (!empty($comments)): ?>
  96. <div class="comments-list">
  97. <?php foreach ($comments as $comment_item): ?>
  98. <div class="comment <?php echo $comment_item['admin_reply'] ? 'admin-reply' : ''; ?>"
  99. data-comment-id="<?php echo $comment_item['id']; ?>">
  100. <div class="comment-header">
  101. <div class="comment-author">
  102. <strong><?php echo htmlspecialchars($comment_item['name']); ?></strong>
  103. <?php if ($comment_item['admin_reply']): ?>
  104. <span class="admin-badge"><?php echo t('admin'); ?></span>
  105. <?php endif; ?>
  106. </div>
  107. <div class="comment-date">
  108. <?php echo date('M j, Y g:i A', strtotime($comment_item['created_at'])); ?>
  109. </div>
  110. </div>
  111. <div class="comment-content">
  112. <?php echo nl2br(htmlspecialchars($comment_item['content'])); ?>
  113. </div>
  114. <?php if ($comment_item['replied_by_username']): ?>
  115. <div class="reply-info">
  116. <small><?php echo t('replied_by'); ?> <?php echo htmlspecialchars($comment_item['replied_by_username']); ?></small>
  117. </div>
  118. <?php endif; ?>
  119. </div>
  120. <?php endforeach; ?>
  121. </div>
  122. <?php else: ?>
  123. <p class="no-comments"><?php echo t('no_comments_yet'); ?></p>
  124. <?php endif; ?>
  125. <!-- Comment Form -->
  126. <div class="comment-form-container">
  127. <h4><?php echo t('leave_a_comment'); ?></h4>
  128. <form id="comment-form" class="comment-form">
  129. <input type="hidden" name="publication_id" value="<?php echo $id; ?>">
  130. <input type="hidden" name="parent_id" value="">
  131. <div class="form-group">
  132. <label for="comment-name"><?php echo t('name'); ?> *</label>
  133. <input type="text" id="comment-name" name="name" required maxlength="100">
  134. </div>
  135. <div class="form-group">
  136. <label for="comment-email"><?php echo t('email'); ?></label>
  137. <input type="email" id="comment-email" name="email" maxlength="255">
  138. <small><?php echo t('email_optional'); ?></small>
  139. </div>
  140. <div class="form-group">
  141. <label for="comment-content"><?php echo t('comment'); ?> *</label>
  142. <textarea id="comment-content" name="content" required maxlength="2000" rows="4"></textarea>
  143. </div>
  144. <?php echo Captcha::getHtml(); ?>
  145. <div class="form-actions">
  146. <button type="submit" class="btn btn-primary"><?php echo t('submit_comment'); ?></button>
  147. <button type="button" class="btn btn-secondary cancel-reply" style="display: none;"><?php echo t('cancel'); ?></button>
  148. </div>
  149. </form>
  150. </div>
  151. </section>
  152. <?php if (!empty($relatedPublications)): ?>
  153. <section class="related-publications">
  154. <h2><?php echo t('related_publications'); ?></h2>
  155. <div class="publication-grid">
  156. <?php foreach ($relatedPublications as $related): ?>
  157. <article class="publication-card">
  158. <h3>
  159. <a href="publication.php?id=<?php echo $related['id']; ?>">
  160. <?php echo htmlspecialchars($related['title']); ?>
  161. </a>
  162. </h3>
  163. <?php if ($related['summary']): ?>
  164. <p class="summary"><?php echo htmlspecialchars($related['summary']); ?></p>
  165. <?php endif; ?>
  166. <div class="meta">
  167. <span class="author"><?php echo htmlspecialchars($related['author']); ?></span>
  168. <span class="date"><?php echo date('M j, Y', strtotime($related['created_at'])); ?></span>
  169. </div>
  170. </article>
  171. <?php endforeach; ?>
  172. </div>
  173. </section>
  174. <?php endif; ?>
  175. <div class="publication-actions">
  176. <a href="index.php" class="btn"><?php echo t('back_to_publications'); ?></a>
  177. </div>
  178. </div>
  179. <footer class="site-footer">
  180. <div class="container">
  181. <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_TITLE; ?>. All rights reserved.</p>
  182. </div>
  183. </footer>
  184. <script>
  185. // Comment System JavaScript
  186. document.addEventListener('DOMContentLoaded', function() {
  187. const commentForm = document.getElementById('comment-form');
  188. const cancelReplyBtn = document.querySelector('.cancel-reply');
  189. const parent_id_input = commentForm.querySelector('input[name="parent_id"]');
  190. let isReplying = false;
  191. // Handle comment submission
  192. commentForm.addEventListener('submit', function(e) {
  193. e.preventDefault();
  194. const formData = new FormData(commentForm);
  195. const submitBtn = commentForm.querySelector('button[type="submit"]');
  196. const originalText = submitBtn.textContent;
  197. // Disable submit button
  198. submitBtn.disabled = true;
  199. submitBtn.textContent = '<?php echo t('submitting'); ?>...';
  200. // Submit comment via AJAX
  201. fetch('submit_comment.php', {
  202. method: 'POST',
  203. body: formData
  204. })
  205. .then(response => response.json())
  206. .then(data => {
  207. if (data.success) {
  208. // Show success message
  209. showMessage(data.message, 'success');
  210. // Reset form
  211. commentForm.reset();
  212. cancelReply();
  213. // Update comment count
  214. const commentCountElement = document.querySelector('.comments-section h3');
  215. if (commentCountElement) {
  216. commentCountElement.textContent = '<?php echo t('comments'); ?> (' + data.comment_count + ')';
  217. }
  218. // Reload comments after a short delay
  219. setTimeout(() => {
  220. window.location.reload();
  221. }, 1500);
  222. } else {
  223. showMessage(data.message, 'error');
  224. }
  225. })
  226. .catch(error => {
  227. console.error('Error:', error);
  228. showMessage('<?php echo t('comment_submit_error'); ?>', 'error');
  229. })
  230. .finally(() => {
  231. // Re-enable submit button
  232. submitBtn.disabled = false;
  233. submitBtn.textContent = originalText;
  234. });
  235. });
  236. // Handle reply buttons
  237. document.addEventListener('click', function(e) {
  238. if (e.target.classList.contains('reply-btn')) {
  239. e.preventDefault();
  240. const commentId = e.target.dataset.commentId;
  241. const commentElement = document.querySelector(`[data-comment-id="${commentId}"]`);
  242. const commentName = commentElement.querySelector('.comment-author strong').textContent;
  243. // Set parent ID
  244. parent_id_input.value = commentId;
  245. // Update form title
  246. const formTitle = document.querySelector('.comment-form-container h4');
  247. formTitle.textContent = '<?php echo t('replying_to'); ?> ' + commentName;
  248. // Show cancel button
  249. cancelReplyBtn.style.display = 'inline-block';
  250. // Scroll to form
  251. commentForm.scrollIntoView({ behavior: 'smooth' });
  252. isReplying = true;
  253. }
  254. });
  255. // Handle cancel reply
  256. cancelReplyBtn.addEventListener('click', cancelReply);
  257. function cancelReply() {
  258. parent_id_input.value = '';
  259. const formTitle = document.querySelector('.comment-form-container h4');
  260. formTitle.textContent = '<?php echo t('leave_a_comment'); ?>';
  261. cancelReplyBtn.style.display = 'none';
  262. isReplying = false;
  263. }
  264. // Show message function
  265. function showMessage(message, type) {
  266. const messageDiv = document.createElement('div');
  267. messageDiv.className = `alert alert-${type}`;
  268. messageDiv.textContent = message;
  269. const formContainer = document.querySelector('.comment-form-container');
  270. formContainer.insertBefore(messageDiv, formContainer.firstChild);
  271. // Remove message after 5 seconds
  272. setTimeout(() => {
  273. messageDiv.remove();
  274. }, 5000);
  275. }
  276. // Refresh captcha on error
  277. function refreshCaptcha() {
  278. const captchaContainer = document.querySelector('.captcha-container');
  279. if (captchaContainer) {
  280. fetch('submit_comment.php?action=refresh_captcha')
  281. .then(response => response.json())
  282. .then(data => {
  283. if (data.question) {
  284. const questionElement = captchaContainer.querySelector('.captcha-numbers');
  285. const operatorElement = captchaContainer.querySelector('.captcha-operator');
  286. if (questionElement) {
  287. const parts = data.question.split(' ');
  288. questionElement.textContent = parts[0] + ' ' + parts[2];
  289. operatorElement.textContent = parts[1];
  290. }
  291. }
  292. })
  293. .catch(error => console.error('Error refreshing captcha:', error));
  294. }
  295. }
  296. });
  297. </script>
  298. </body>
  299. </html>