<?php
// -----------------------------------------------------------------------
//
// $Id: index.php 177 2012-03-30 15:09:49Z raoul $
//
// Copyright (C) 2003-2012 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
//
// -----------------------------------------------------------------------
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// -----------------------------------------------------------------------
include('./../includes/common.php');
page_header($lang['COMMENTS_INDEX_TITLE']);
// Check query
if (!empty($_GET['news_id']))
{
$sql->query('SELECT news_id
FROM ' . TABLE_NEWS . '
WHERE news_id = \'' . $_GET['news_id'] . '\'');
$table_news = $sql->fetch();
// Check news
if (!$table_news['news_id'])
{
error_template($lang['COMMENTS_INDEX_ERROR1']);
}
else
{
// Query string - ?news_id=&comment_id=&story_id=&page=&list=
if (!empty($_GET['comment_id']))
{
$clause = 'WHERE ' . TABLE_COMMENTS . '.comment_id = \'' . $_GET['comment_id'] . '\'';
$query = 'comment_id=' . $_GET['comment_id'];
}
if (!empty($_GET['story_id']))
{
if (!empty($_GET['comment_id']))
{
$clause .= ' AND ' . TABLE_COMMENTS . '.story_id = \'' . $_GET['story_id'] . '\'';
$query .= '&story_id=' . $_GET['story_id'];
}
else
{
$clause = 'WHERE ' . TABLE_COMMENTS . '.story_id = \'' . $_GET['story_id'] . '\'';
$query = 'story_id=' . $_GET['story_id'];
}
}
if (empty($_GET['comment_id']) && empty($_GET['story_id']))
{
$clause = 'WHERE';
$query = '';
}
else
{
$clause .= ' AND';
$query .= '&';
}
// Create pages list
if (empty($_GET['page']))
{
$_GET['page'] = 1;
}
// Complete story ?
if (!empty($_GET['story_id']))
{
$comments_per_page = $settings['comments_per_page'];
$comments_offset = ($_GET['page'] - 1) * $comments_per_page;
$last_comment[$_GET['story_id']] = NULL;
// stories_per_page = comments_per_story (see below)
$sql->query('SELECT comment_id
FROM ' . TABLE_COMMENTS . '
' . $clause . ' news_id = \'' . $_GET['news_id'] . '\'');
$stories_per_page = $sql->num_rows();
}
else
{
$comments_offset = $comments_per_page = $stories_per_page = 0;
$last_comment = array();
$sql->query('SELECT COUNT(*) AS comments_per_story, MAX(comment_creation) AS last_comment, story_id
FROM ' . TABLE_COMMENTS . '
' . $clause . ' news_id = \'' . $_GET['news_id'] . '\'
GROUP BY story_id
ORDER BY story_id');
while ($table_comments = $sql->fetch())
{
$last_comment[$table_comments['story_id']] = $table_comments['last_comment'];
$stories_per_page++;
// Offset
if ($stories_per_page <= ($_GET['page'] - 1) * $settings['comments_per_page'])
{
$comments_offset += $table_comments['comments_per_story'];
}
// Number of rows to return
if (($_GET['page'] - 1) * $settings['comments_per_page'] < $stories_per_page)
{
if ($stories_per_page <= $settings['comments_per_page'] + (($_GET['page'] - 1) * $settings['comments_per_page']))
{
$comments_per_page += $table_comments['comments_per_story'];
}
}
}
}
$num_pages = ceil($stories_per_page / $settings['comments_per_page']);
$pages_list = get_pages_list('./../comments/index.php?news_id=' . $_GET['news_id'] . '&' . $query, $num_pages);
// ----------------------------------------------------------------
$previous_story = '';
$date_format = get_date_format();
$date_offset = get_date_offset();
$template->set_file('index', 'comments/index.htpl');
$template->set_block('index', 'COMMENTS_BLOCK', 'comments');
$template->set_block('index', 'STORIES_BLOCK', 'stories');
$sql->query('SELECT ' . TABLE_COMMENTS . '.comment_creation, ' . TABLE_COMMENTS . '.comment_edition, ' . TABLE_COMMENTS . '.comment_id, ' . TABLE_COMMENTS . '.story_id, ' . TABLE_COMMENTS . '.comment_subject, ' . TABLE_COMMENTS . '.comment_text, ' . TABLE_USERS . '.user_avatar, ' . TABLE_USERS . '.user_id, ' . TABLE_USERS . '.user_name
FROM ' . TABLE_COMMENTS . ', ' . TABLE_USERS . '
' . $clause . ' ' . TABLE_COMMENTS . '.news_id = \'' . $_GET['news_id'] . '\'
AND ' . TABLE_COMMENTS . '.user_id = ' . TABLE_USERS . '.user_id
ORDER BY story_id, comment_creation
LIMIT ' . $comments_offset . ', ' . $comments_per_page);
while ($table_comments = $sql->fetch())
{
$comment_creation = date($date_format, ($table_comments['comment_creation'] + $date_offset));
$comment_edition = date($date_format, ($table_comments['comment_edition'] + $date_offset));
// First comment in a story or complete story ?
if ($previous_story != $table_comments['story_id'] || !empty($_GET['story_id']))
{
if ($table_comments['comment_edition'])
{
$comment_edition = sprintf($lang['COMMENTS_INDEX_EDITION'], $comment_edition);
}
else
{
$comment_edition = '';
}
// User logged ?
if ($_SESSION['user_id'] != 0)
{
// Administrator
if ($users['user_level'] == 4)
{
$comments_index_edit = '<a href="./../admin/comments.php?comment_id=' . $table_comments['comment_id'] . '"><img src="./../images/comments/edit.png" alt="." title="' . $lang['COMMENTS_INDEX_EDIT'] . '" /></a>';
}
// Poster
elseif ($table_comments['user_id'] == $_SESSION['user_id'])
{
$comments_index_edit = '<a href="./../comments/edit.php?comment_id=' . $table_comments['comment_id'] . '"><img src="./../images/comments/edit.png" alt="." title="' . $lang['COMMENTS_INDEX_EDIT'] . '" /></a>';
}
else
{
$comments_index_edit = '';
}
$comments_index_reply = '<a href="./../comments/reply.php?comment_id=' . $table_comments['comment_id'] . '"><img src="./../images/comments/reply.png" alt="." title="' . $lang['COMMENTS_INDEX_REPLY'] . '" /></a>';
// New comments since last visit
if ($table_comments['comment_creation'] > $users['user_lastvisit'])
{
$subject_class = 'newItem';
}
else
{
$subject_class = 'oldItem';
}
}
else
{
$comments_index_edit = '';
$comments_index_reply = '';
$subject_class = 'oldItem';
}
// Comment links
if (!empty($_GET['comment_id']))
{
$comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&story_id='. $table_comments['story_id'] .'#c'. $table_comments['comment_id'] .'" title="−">−</a> ';
}
elseif (!empty($_GET['story_id']))
{
if ($previous_story != $table_comments['story_id'])
{
$comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '" title="−">−</a> ';
}
else
{
$comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&story_id='. $table_comments['story_id'] .'#c'. $table_comments['story_id'] .'" title="^">^</a> ';
}
}
else
{
$comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&story_id='. $table_comments['story_id'] .'#c'. $table_comments['story_id'] .'" title="+">+</a> ';
}
$comment_links .= '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&story_id='. $table_comments['story_id'] .'&comment_id='. $table_comments['comment_id'] .'" title="#">#</a>';
// First comment
if ($table_comments['comment_id'] == $table_comments['story_id'] || empty($_GET['story_id']))
{
$comment_subject = $table_comments['comment_subject'];
}
else
{
$comment_subject = $lang['COMMENTS_INDEX_PREVIOUS'] . $table_comments['comment_subject'];
}
$comment_text = undo_escape_sequences($table_comments['comment_text']);
$template->set_var(array('COMMENT_ID' => $table_comments['comment_id'],
'COMMENT_EDITION' => $comment_edition,
'COMMENT_LINKS' => $comment_links,
'COMMENT_RELEASE' => sprintf($lang['COMMENTS_INDEX_RELEASE'], $table_comments['user_id'], $table_comments['user_name'], $comment_creation),
'COMMENT_SUBJECT' => $comment_subject,
'COMMENT_TEXT' => $comment_text,
'COMMENTS_INDEX_EDIT' => $comments_index_edit,
'COMMENTS_INDEX_REPLY' => $comments_index_reply,
'STORY_ID' => $table_comments['story_id'],
'SUBJECT_CLASS' => $subject_class,
'USER_AVATAR' => $table_comments['user_avatar'],
'comments' => ''));
$previous_story = $table_comments['story_id'];
}
// Other comments
else
{
if (empty($_GET['story_id']))
{
// User logged ?
if ($_SESSION['user_id'] != 0)
{
// New comments since last visit
if ($table_comments['comment_creation'] > $users['user_lastvisit'])
{
$subject_class = 'newItem';
}
else
{
$subject_class = 'oldItem';
}
}
else
{
$subject_class = 'oldItem';
}
$template->set_var('COMMENT_RELEASE2', sprintf($lang['COMMENTS_INDEX_RELEASE2'], $_GET['news_id'], $table_comments['story_id'], $table_comments['comment_id'], $subject_class, $comment_subject, $table_comments['user_id'], $table_comments['user_name'], $comment_creation));
$template->parse('COMMENTS_BLOCK', 'comments', true);
}
}
// Last comment in a story or complete story ?
if ($table_comments['comment_creation'] == $last_comment[$table_comments['story_id']] || !empty($_GET['story_id']))
{
$template->parse('STORIES_BLOCK', 'stories', true);
}
$comments_exist = true;
}
if (isset($comments_exist))
{
$template->set_var('COMMENTS_INDEX_PAGES', sprintf($lang['COMMENTS_INDEX_PAGES'], $pages_list));
}
else
{
$template->set_var('COMMENTS_INDEX_PAGES', $lang['COMMENTS_INDEX_ERROR2']);
}
// ----------------------------------------------------------------
$sql->query('SELECT ' . TABLE_CATEGORIES . '.category_id, ' . TABLE_CATEGORIES . '.category_image, ' . TABLE_CATEGORIES . '.category_name, ' . TABLE_NEWS . '.news_date, ' . TABLE_NEWS . '.news_source, ' . TABLE_NEWS . '.news_subject, ' . TABLE_NEWS . '.news_text, ' . TABLE_USERS . '.user_id, ' . TABLE_USERS . '.user_name
FROM ' . TABLE_CATEGORIES . ', ' . TABLE_NEWS . ', ' . TABLE_USERS . '
WHERE ' . TABLE_CATEGORIES . '.category_id = ' . TABLE_NEWS . '.category_id
AND ' . TABLE_CATEGORIES . '.category_level IN (\'0\', \'2\')
AND ' . TABLE_NEWS . '.news_active = \'1\'
AND ' . TABLE_NEWS . '.user_id = ' . TABLE_USERS . '.user_id
AND ' . TABLE_NEWS . '.news_id = \'' . $_GET['news_id'] . '\'');
$table_news = $sql->fetch();
$news_date = date($date_format, ($table_news['news_date'] + $date_offset));
if ($table_news['news_source'])
{
$news_source = undo_escape_sequences($table_news['news_source']);
$news_source = sprintf($lang['NEWS_INDEX_SOURCE'], $news_source);
}
else
{
$news_source = '<!-- empty -->';
}
$news_text = undo_escape_sequences($table_news['news_text']);
if ($settings['allow_html'] == 0)
{
$html_support = $lang['HTML_DISABLED'];
}
else
{
$html_support = $lang['HTML_ENABLED'];
}
$template->set_var(array('ADD' => $lang['ADD'],
'BACK_HOME' => $lang['BACK_HOME'],
'CATEGORY_ID' => $table_news['category_id'],
'CATEGORY_IMAGE' => $table_news['category_image'],
'CATEGORY_NAME' => $table_news['category_name'],
'COMMENTS_INDEX_HEADER' => $lang['COMMENTS_INDEX_HEADER'],
'FORM_COMMENT_SUBJECT' => $lang['FORM_COMMENT_SUBJECT'],
'FORM_COMMENT_TEXT' => $lang['FORM_COMMENT_TEXT'],
'FORM_IN_SUBJECT' => $lang['FORM_IN_SUBJECT'],
'FORM_IN_TEXT' => $lang['FORM_IN_TEXT'],
'HTML_SUPPORT' => $html_support,
'NEWS_ID' => $_GET['news_id'],
'NEWS_INDEX_SEND' => $lang['NEWS_INDEX_SEND'],
'NEWS_RELEASE' => sprintf($lang['NEWS_INDEX_RELEASE'], $table_news['user_id'], $table_news['user_name'], $news_date),
'NEWS_SOURCE' => $news_source,
'NEWS_SUBJECT' => $table_news['news_subject'],
'NEWS_TEXT' => $news_text,
'PREVIEW' => $lang['PREVIEW'],
'SEARCH' => $lang['SEARCH'],
'SMILIES_LIST' => get_smilies_list(0)));
$template->parse('index');
}
}
else
{
error_template($lang['COMMENTS_INDEX_ERROR1']);
}
page_footer();
?>