publication.php 16 KB

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