| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- // Start session for language preference
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- require_once '../includes/publication.php';
- require_once '../includes/comment.php';
- require_once '../includes/captcha.php';
- require_once '../includes/translation.php';
- require_once '../includes/auth.php';
- // Translation system is auto-initialized when translation.php is included
- $publication = new Publication();
- $comment = new Comment();
- $auth = new Auth();
- $id = (int)($_GET['id'] ?? 0);
- $pub = $publication->getById($id);
- if (!$pub || $pub['status'] !== 'published') {
- http_response_code(404);
- echo '<h1>' . t('error_publication_not_found') . '</h1>';
- echo '<p>' . t('error_publication_not_found_description') . '</p>';
- exit;
- }
- // Get related publications
- $relatedPublications = [];
- if ($pub['categories_array']) {
- $firstCategory = $pub['categories_array'][0];
- $relatedPublications = $publication->getByCategory($firstCategory, 'published');
- // Remove current publication from related list
- $relatedPublications = array_filter($relatedPublications, function($p) use ($id) {
- return $p['id'] != $id;
- });
- // Limit to 3 related publications
- $relatedPublications = array_slice($relatedPublications, 0, 3);
- }
- // Get approved comments for this publication
- $comments = $comment->getApprovedByPublication($id);
- $commentCount = $comment->getCountByPublication($id);
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title><?php echo htmlspecialchars($pub['title']); ?> - <?php echo SITE_TITLE; ?></title>
- <link rel="stylesheet" href="../css/style.css">
- <link rel="stylesheet" href="../css/comments.css">
- </head>
- <body>
- <header class="site-header">
- <div class="container">
- <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
- <nav class="main-nav">
- <a href="index.php"><?php echo t('nav_home'); ?></a>
- <a href="categories.php"><?php echo t('nav_categories'); ?></a>
- <a href="search.php"><?php echo t('nav_search'); ?></a>
- </nav>
- <?php echo Translation::getLanguageSwitcher('publication.php?id=' . $id); ?>
- </div>
- </header>
- <div class="container">
- <article class="publication-full">
- <header class="publication-header">
- <div class="publication-title-section">
- <h1><?php echo htmlspecialchars($pub['title']); ?></h1>
- <?php if ($auth->isAdmin()): ?>
- <a href="../admin/edit.php?id=<?php echo $id; ?>" class="admin-edit-btn" title="<?php echo t('admin_edit_publication'); ?>">
- <i class="icon-edit">✏️</i> <?php echo t('edit'); ?>
- </a>
- <?php endif; ?>
- </div>
-
- <div class="publication-meta">
- <span class="author">By <?php echo htmlspecialchars($pub['author']); ?></span>
- <span class="date"><?php echo date('F j, Y', strtotime($pub['created_at'])); ?></span>
- <?php if ($pub['published_at']): ?>
- <span class="published-date">Published <?php echo date('F j, Y', strtotime($pub['published_at'])); ?></span>
- <?php endif; ?>
- </div>
- <?php if ($pub['categories_array']): ?>
- <div class="publication-categories">
- <?php foreach ($pub['categories_array'] as $category): ?>
- <a href="index.php?category=<?php echo urlencode(trim($category)); ?>" class="category-tag">
- <?php echo htmlspecialchars(trim($category)); ?>
- </a>
- <?php endforeach; ?>
- </div>
- <?php endif; ?>
- </header>
- <div class="publication-content">
- <?php if ($pub['summary']): ?>
- <div class="publication-summary">
- <p><?php echo htmlspecialchars($pub['summary']); ?></p>
- </div>
- <?php endif; ?>
- <div class="publication-body">
- <?php echo $pub['content']; ?>
- </div>
- </div>
- </article>
- <!-- Comments Section -->
- <section class="comments-section">
- <h3><?php echo t('comments'); ?> (<?php echo $commentCount; ?>)</h3>
-
- <?php if (!empty($comments)): ?>
- <div class="comments-list">
- <?php foreach ($comments as $comment_item): ?>
- <div class="comment <?php echo $comment_item['admin_reply'] ? 'admin-reply' : ''; ?>"
- data-comment-id="<?php echo $comment_item['id']; ?>">
- <div class="comment-header">
- <div class="comment-author">
- <strong><?php echo htmlspecialchars($comment_item['name']); ?></strong>
- <?php if ($comment_item['admin_reply']): ?>
- <span class="admin-badge"><?php echo t('admin'); ?></span>
- <?php endif; ?>
- </div>
- <div class="comment-date">
- <?php echo date('M j, Y g:i A', strtotime($comment_item['created_at'])); ?>
- </div>
- </div>
- <div class="comment-content">
- <?php echo nl2br(htmlspecialchars($comment_item['content'])); ?>
- </div>
- <?php if ($comment_item['replied_by_username']): ?>
- <div class="reply-info">
- <small><?php echo t('replied_by'); ?> <?php echo htmlspecialchars($comment_item['replied_by_username']); ?></small>
- </div>
- <?php endif; ?>
- </div>
- <?php endforeach; ?>
- </div>
- <?php else: ?>
- <p class="no-comments"><?php echo t('no_comments_yet'); ?></p>
- <?php endif; ?>
-
- <!-- Comment Form -->
- <div class="comment-form-container">
- <h4><?php echo t('leave_a_comment'); ?></h4>
- <form id="comment-form" class="comment-form">
- <input type="hidden" name="publication_id" value="<?php echo $id; ?>">
- <input type="hidden" name="parent_id" value="">
-
- <div class="form-group">
- <label for="comment-name"><?php echo t('name'); ?> *</label>
- <input type="text" id="comment-name" name="name" required maxlength="100">
- </div>
-
- <div class="form-group">
- <label for="comment-email"><?php echo t('email'); ?></label>
- <input type="email" id="comment-email" name="email" maxlength="255">
- <small><?php echo t('email_optional'); ?></small>
- </div>
-
- <div class="form-group">
- <label for="comment-content"><?php echo t('comment'); ?> *</label>
- <textarea id="comment-content" name="content" required maxlength="2000" rows="4"></textarea>
- </div>
-
- <?php echo Captcha::getHtml(); ?>
-
- <div class="form-actions">
- <button type="submit" class="btn btn-primary"><?php echo t('submit_comment'); ?></button>
- <button type="button" class="btn btn-secondary cancel-reply" style="display: none;"><?php echo t('cancel'); ?></button>
- </div>
- </form>
- </div>
- </section>
- <?php if (!empty($relatedPublications)): ?>
- <section class="related-publications">
- <h2><?php echo t('related_publications'); ?></h2>
- <div class="publication-grid">
- <?php foreach ($relatedPublications as $related): ?>
- <article class="publication-card">
- <h3>
- <a href="publication.php?id=<?php echo $related['id']; ?>">
- <?php echo htmlspecialchars($related['title']); ?>
- </a>
- </h3>
-
- <?php if ($related['summary']): ?>
- <p class="summary"><?php echo htmlspecialchars($related['summary']); ?></p>
- <?php endif; ?>
-
- <div class="meta">
- <span class="author"><?php echo htmlspecialchars($related['author']); ?></span>
- <span class="date"><?php echo date('M j, Y', strtotime($related['created_at'])); ?></span>
- </div>
- </article>
- <?php endforeach; ?>
- </div>
- </section>
- <?php endif; ?>
- <div class="publication-actions">
- <a href="index.php" class="btn"><?php echo t('back_to_publications'); ?></a>
- </div>
- </div>
- <footer class="site-footer">
- <div class="container">
- <p>© <?php echo date('Y'); ?> <?php echo SITE_TITLE; ?>. All rights reserved.</p>
- </div>
- </footer>
- <script>
- // Comment System JavaScript
- document.addEventListener('DOMContentLoaded', function() {
- const commentForm = document.getElementById('comment-form');
- const cancelReplyBtn = document.querySelector('.cancel-reply');
- const parent_id_input = commentForm.querySelector('input[name="parent_id"]');
- let isReplying = false;
-
- // Handle comment submission
- commentForm.addEventListener('submit', function(e) {
- e.preventDefault();
-
- const formData = new FormData(commentForm);
- const submitBtn = commentForm.querySelector('button[type="submit"]');
- const originalText = submitBtn.textContent;
-
- // Disable submit button
- submitBtn.disabled = true;
- submitBtn.textContent = '<?php echo t('submitting'); ?>...';
-
- // Submit comment via AJAX
- fetch('submit_comment.php', {
- method: 'POST',
- body: formData
- })
- .then(response => response.json())
- .then(data => {
- if (data.success) {
- // Show success message
- showMessage(data.message, 'success');
-
- // Reset form
- commentForm.reset();
- cancelReply();
-
- // Update comment count
- const commentCountElement = document.querySelector('.comments-section h3');
- if (commentCountElement) {
- commentCountElement.textContent = '<?php echo t('comments'); ?> (' + data.comment_count + ')';
- }
-
- // Reload comments after a short delay
- setTimeout(() => {
- window.location.reload();
- }, 1500);
- } else {
- showMessage(data.message, 'error');
- }
- })
- .catch(error => {
- console.error('Error:', error);
- showMessage('<?php echo t('comment_submit_error'); ?>', 'error');
- })
- .finally(() => {
- // Re-enable submit button
- submitBtn.disabled = false;
- submitBtn.textContent = originalText;
- });
- });
-
- // Handle reply buttons
- document.addEventListener('click', function(e) {
- if (e.target.classList.contains('reply-btn')) {
- e.preventDefault();
-
- const commentId = e.target.dataset.commentId;
- const commentElement = document.querySelector(`[data-comment-id="${commentId}"]`);
- const commentName = commentElement.querySelector('.comment-author strong').textContent;
-
- // Set parent ID
- parent_id_input.value = commentId;
-
- // Update form title
- const formTitle = document.querySelector('.comment-form-container h4');
- formTitle.textContent = '<?php echo t('replying_to'); ?> ' + commentName;
-
- // Show cancel button
- cancelReplyBtn.style.display = 'inline-block';
-
- // Scroll to form
- commentForm.scrollIntoView({ behavior: 'smooth' });
-
- isReplying = true;
- }
- });
-
- // Handle cancel reply
- cancelReplyBtn.addEventListener('click', cancelReply);
-
- function cancelReply() {
- parent_id_input.value = '';
- const formTitle = document.querySelector('.comment-form-container h4');
- formTitle.textContent = '<?php echo t('leave_a_comment'); ?>';
- cancelReplyBtn.style.display = 'none';
- isReplying = false;
- }
-
- // Show message function
- function showMessage(message, type) {
- const messageDiv = document.createElement('div');
- messageDiv.className = `alert alert-${type}`;
- messageDiv.textContent = message;
-
- const formContainer = document.querySelector('.comment-form-container');
- formContainer.insertBefore(messageDiv, formContainer.firstChild);
-
- // Remove message after 5 seconds
- setTimeout(() => {
- messageDiv.remove();
- }, 5000);
- }
-
- // Refresh captcha on error
- function refreshCaptcha() {
- const captchaContainer = document.querySelector('.captcha-container');
- if (captchaContainer) {
- fetch('submit_comment.php?action=refresh_captcha')
- .then(response => response.json())
- .then(data => {
- if (data.question) {
- const questionElement = captchaContainer.querySelector('.captcha-numbers');
- const operatorElement = captchaContainer.querySelector('.captcha-operator');
- if (questionElement) {
- const parts = data.question.split(' ');
- questionElement.textContent = parts[0] + ' ' + parts[2];
- operatorElement.textContent = parts[1];
- }
- }
- })
- .catch(error => console.error('Error refreshing captcha:', error));
- }
- }
- });
- </script>
- </body>
- </html>
|