Location: PHPKode > projects > Gnew > Gnew-2013.1/comments/index.php
<?php
// -----------------------------------------------------------------------------
// $Id: index.php 689 2013-03-29 09:24:15Z raoul $
//
// Copyright (C) 2013 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.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

include('./../includes/common.php');

page_header($lang['COMMENTS_INDEX']);

// Comments in news enabled?
if ($settings['news_commenting'] == 0)
{
    error_template($lang['ERROR_COMMENTING_DISABLED']);
}
// Check query
elseif (!empty($_GET['news_id']) && string_is_int($_GET['news_id']))
{
    $sql->query('SELECT t1.category_id, t1.user_id, t1.news_source, t1.news_subject, t1.news_text, t1.news_creation, t1.news_edition, t1.edition_id, t1.edition_name, t2.category_image, t2.category_name, t3.user_name
                 FROM ' . TABLE_NEWS . ' AS t1
                 INNER JOIN ' . TABLE_CATEGORIES . ' AS t2
                             ON (t2.category_id = t1.category_id AND t2.category_level IN (\'2\', \'4\'))
                 INNER JOIN ' . TABLE_USERS . ' AS t3
                             ON (t3.user_id = t1.user_id)
                 WHERE t1.news_active = \'1\'
                 AND t1.news_id = \'' . $_GET['news_id'] . '\'');
    $table_news = $sql->fetch();
    // Check news
    if (!$table_news['category_id'])
    {
        error_template($lang['ERROR_NO_DATA']);
    }
    else
    {
        /********
         * News *
         ********/
        if ($table_news['news_edition'])
        {
            $news_edited = sprintf($lang['EDITED_BY'], $table_news['edition_id'], $table_news['edition_name'], format_date($table_news['news_edition']));
        }
        else
        {
            $news_edited = '';
        }
        if ($table_news['news_source'])
        {
            $news_source = undo_escape_sequences($table_news['news_source']);
            $news_source = sprintf($lang['SOURCE'], $news_source);
        }
        else
        {
            $news_source = '<!-- empty -->';
        }
        $news_text = undo_escape_sequences($table_news['news_text']);
        $news_text = cut_text($news_text, './../news/index.php?news_id=' . $_GET['news_id']);
        if ($settings['allow_html'])
        {
            $html_support = $lang['HTML_ENABLED'];
        }
        else
        {
            $html_support = $lang['HTML_DISABLED'];
        }
        $template->set_var(array('CATEGORY_ID' => $table_news['category_id'],
                                 'CATEGORY_IMAGE' => $table_news['category_image'],
                                 'CATEGORY_NAME' => $table_news['category_name'],
                                 'EMOTICONS_LIST' => get_emoticons_list(0),
                                 'HTML_SUPPORT' => $html_support,
                                 'NEWS_EDITED' => $news_edited,
                                 'NEWS_ID' => $_GET['news_id'],
                                 'NEWS_POSTED' => sprintf($lang['POSTED_BY'], $table_news['user_id'], $table_news['user_name'], format_date($table_news['news_creation'])),
                                 'NEWS_SOURCE' => $news_source,
                                 'NEWS_SUBJECT' => $table_news['news_subject'],
                                 'NEWS_TEXT' => $news_text));
        /************
         * Comments *
         ************/
        // Query string - ?news_id=&story_id=&comment_id=&page=&list=
        if (!empty($_GET['story_id']) && string_is_int($_GET['story_id']))
        {
            $clause = 'WHERE t1.story_id = \'' . $_GET['story_id'] . '\'';
            $query = 'story_id=' . $_GET['story_id'];
        }
        if (!empty($_GET['comment_id']) && string_is_int($_GET['comment_id']))
        {
            if (!empty($_GET['story_id']) && string_is_int($_GET['story_id']))
            {
                $clause .= ' AND t1.comment_id = \'' . $_GET['comment_id'] . '\'';
                $query .= '&amp;comment_id=' . $_GET['comment_id'];
            }
            else
            {
                $clause = 'WHERE t1.comment_id = \'' . $_GET['comment_id'] . '\'';
                $query = 'comment_id=' . $_GET['comment_id'];
            }
        }
        if ((empty($_GET['story_id']) || !string_is_int($_GET['story_id'])) && (empty($_GET['comment_id']) || !string_is_int($_GET['comment_id'])))
        {
            $clause = 'WHERE';
            $query = '';
        }
        else
        {
            $clause .= ' AND';
            $query .= '&amp;';
        }

        // Pages list creation - start
        if (empty($_GET['page']) || !string_is_int($_GET['page']))
        {
            $_GET['page'] = 1;
        }
        $comments_per_story = array();
        // Unfolded view
        if (!empty($_GET['story_id']) && string_is_int($_GET['story_id']))
        {
            $comments_per_page = $settings['comments_per_page'];
            $comments_offset = ($_GET['page'] - 1) * $comments_per_page;
            $sql->query('SELECT comment_id
                         FROM ' . TABLE_COMMENTS . ' AS t1
                         ' . $clause . ' news_id = \'' . $_GET['news_id'] . '\'');
            $num_comments = $sql->num_rows();
            $comments_per_story[$_GET['story_id']] = $num_comments;
            $num_pages = ceil($num_comments / $comments_per_page);
        }
        // Folded view
        else
        {
            $comments_per_page = $comments_offset = $num_stories = 0;
            $stories_per_page = $settings['stories_per_page'];
            $sql->query('SELECT story_id, COUNT(*) AS comments_per_story
                         FROM ' . TABLE_COMMENTS . ' AS t1
                         ' . $clause . ' news_id = \'' . $_GET['news_id'] . '\'
                         GROUP BY story_id
                         ORDER BY story_id');
            while ($table_comments = $sql->fetch())
            {
                $num_stories++;
                // LIMIT
                if ((($_GET['page'] - 1) * $stories_per_page) < $num_stories)
                {
                    if ($num_stories <= $stories_per_page + (($_GET['page'] - 1) * $stories_per_page))
                    {
                        $comments_per_page += $table_comments['comments_per_story'];
                    }
                }
                // OFFSET
                if ($num_stories <= ($_GET['page'] - 1) * $stories_per_page)
                {
                    $comments_offset += $table_comments['comments_per_story'];
                }
                $comments_per_story[$table_comments['story_id']] = $table_comments['comments_per_story'];
            }
            $num_pages = ceil($num_stories / $stories_per_page);
        }
        // Pages list creation - end
        $pages_list = get_pages_list('./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;' . $query, $num_pages);

        // 1st pass
        $sql->query('SELECT t1.comment_id, t1.story_id, t1.reply_id, t1.user_id, t1.comment_active, t1.comment_subject, t1.comment_text, t1.comment_creation, t1.comment_edition, t1.edition_id, t1.edition_name, t2.user_avatar, t2.user_name, t2.user_signature
                     FROM ' . TABLE_COMMENTS . ' AS t1
                     INNER JOIN ' . TABLE_USERS . ' AS t2
                                 ON (t2.user_id = t1.user_id)
                     ' . $clause . ' t1.news_id = \'' . $_GET['news_id'] . '\'
                     ORDER BY t1.story_id, t1.reply_id, t1.' . $settings['comments_sorting'] . ' ' . $settings['comments_order']);
        $table_comments_tmp = $sql->fetch_all();

        // 2nd pass: sorting $table_comments_tmp
        // Margins, pages and lists are allocated to comments
        $num_stories = 0;
        $articles_margins = $replies_margins = $table_comments = $table_lists = $table_pages = array();
        if (is_array($table_comments_tmp))
        {
            // Used by recursive_replies()
            $replies_per_comment = array();
            $sql->query('SELECT reply_id, COUNT(*) AS replies_per_comment
                         FROM ' . TABLE_COMMENTS . ' AS t1
                         ' . $clause . ' news_id = \'' . $_GET['news_id'] . '\'
                         AND reply_id <> \'0\'
                         GROUP BY reply_id
                         ORDER BY reply_id');
            while ($table_comments = $sql->fetch())
            {
                $replies_per_comment[$table_comments['reply_id']] = $table_comments['replies_per_comment'];
            }

            $num_comments_tmp = count($table_comments_tmp);
            for ($key = 0; $key < $num_comments_tmp; $key++)
            {
                if (($table_comments_tmp[$key]['reply_id'] == 0) || (!empty($_GET['comment_id']) && string_is_int($_GET['comment_id'])))
                {
                    $num_comments = $num_replies = 0;
                    $num_comments++;
                    $num_stories++;
                    $table_comments[$key] = $table_comments_tmp[$key];
                    $articles_margins[$key] = 0;
                    $replies_margins[$key] = '';
                    $table_pages[$table_comments[$key]['comment_id']] = ceil($num_stories / $settings['stories_per_page']);
                }
                else
                {
                    $num_comments++;
                    if ($table_comments_tmp[$key]['reply_id'] == $table_comments_tmp[$key]['story_id'])
                    {
                        $table_comments[($key + $num_replies)] = $table_comments_tmp[$key];
                        $articles_margins[($key + $num_replies)] = 1;
                        $replies_margins[($key + $num_replies)] = '<img src="./../images/comments/tree.png" alt="" />';
                        recursive_replies($key);
                    }
                    $table_pages[$table_comments[$key]['comment_id']] = ceil($num_comments / $settings['comments_per_page']);
                }
                $table_lists[$table_comments[$key]['comment_id']] = ceil($table_pages[$table_comments[$key]['comment_id']] / $settings['pages_limit']);
            }
        }

        // LIMIT and OFFSET for the $table_comments loop
        if (($comments_offset + $comments_per_page) <= count($table_comments))
        {
            $comments_limit = $comments_offset + $comments_per_page;
        }
        else
        {
            $comments_limit = count($table_comments);
        }

        $num_comments = 0;
        $template->set_file('index', 'comments/index.htpl');
        $template->set_block('index', 'COMMENTS_BLOCK', 'comments');
        $template->set_block('index', 'STORIES_BLOCK', 'stories');
        for ($key = $comments_offset; $key < $comments_limit; $key++)
        {
            $num_comments++;
            // Folded (first comment) or unfolded view (all comments)
            if (($table_comments[$key]['reply_id'] == 0) || (!empty($_GET['story_id']) && string_is_int($_GET['story_id'])))
            {
                if ($table_comments[$key]['comment_edition'])
                {
                    $comment_edited = sprintf($lang['EDITED_BY'], $table_comments[$key]['edition_id'], $table_comments[$key]['edition_name'], format_date($table_comments[$key]['comment_edition']));
                }
                else
                {
                    $comment_edited = '';
                }

                // User logged?
                if ($_SESSION['user_id'] > 0)
                {
                    // Administrator?
                    if ($users['user_level'] == 4)
                    {
                        $edit_link = '<a href="./../admin/comments.php?comment_id=' . $table_comments[$key]['comment_id'] . '" title="' . $lang['EDIT'] . '">' . $lang['EDIT'] . '</a>';
                    }
                    // Normal user?
                    elseif (($table_comments[$key]['user_id'] == $_SESSION['user_id']) && ($table_comments[$key]['comment_active']))
                    {
                        $edit_link = '<a href="./../comments/edit.php?comment_id=' . $table_comments[$key]['comment_id'] . '" title="' . $lang['EDIT'] . '">' . $lang['EDIT'] . '</a>';
                    }
                    else
                    {
                        $edit_link = '';
                    }

                    // Hidden comment?
                    if ($table_comments[$key]['comment_active'])
                    {
                        if (($users['user_level'] == 4) || ($table_comments[$key]['user_id'] == $_SESSION['user_id']))
                        {
                            $edit_link .= ' | ';
                        }
                        $reply_link = '<a href="./../comments/reply.php?comment_id=' . $table_comments[$key]['comment_id'] . '" title="' . $lang['REPLY'] . '">' . $lang['REPLY'] . '</a>';
                    }
                    else
                    {
                        $reply_link = '';
                    }

                    // New comments since last visit?
                    if ($table_comments[$key]['comment_creation'] > $users['user_last_visit'])
                    {
                        $subject_class = 'new';
                    }
                    else
                    {
                        $subject_class = 'old';
                    }
                }
                else
                {
                    $edit_link = '';
                    $reply_link = '';
                    $subject_class = 'old';
                }

                if (!empty($_GET['comment_id']) && string_is_int($_GET['comment_id']))
                {
                    $comment_links = '<a href="javascript:history.back()"><img src="./../images/comments/minus.png" alt="&minus;"  title="&minus;" /></a>';
                }
                elseif (!empty($_GET['story_id']) && string_is_int($_GET['story_id']))
                {
                    if ($table_comments[$key]['reply_id'] == 0)
                    {
                        $comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '"><img src="./../images/comments/minus.png" alt="&minus;"  title="&minus;" /></a>';
                    }
                    else
                    {
                        $comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '&amp;page=' . $table_pages[$table_comments[$key]['reply_id']] . '&amp;list=' . $table_lists[$table_comments[$key]['reply_id']] . '#c' . $table_comments[$key]['reply_id'] . '"><img src="./../images/comments/parent.png" alt="' . $lang['PARENT_COMMENT'] . '" title="' . $lang['PARENT_COMMENT'] . '" /></a>';
                    }
                    $comment_links .= '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '&amp;page=' . $table_pages[$table_comments[$key]['comment_id']] . '&amp;list=' . $table_lists[$table_comments[$key]['comment_id']] . '#c' . $table_comments[$key]['comment_id'] . '"><img src="./../images/comments/anchor.png" alt="' . $lang['DIRECT_LINK'] . '" title="' . $lang['DIRECT_LINK'] . '" /></a>';

                    $template->set_var(array('HIDDEN_COMMENT_ID' => '<input type="hidden" name="comment_id" value="' . $table_comments[$key]['story_id'] . '" />',
                                             'HIDDEN_STORY_ID' => '<input type="hidden" name="story_id" value="' . $table_comments[$key]['story_id'] . '" />'));
                }
                else
                {
                    $comment_links = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '"><img src="./../images/comments/plus.png" alt="+" title="+" /></a>';
                }

                if (($table_comments[$key]['comment_active']) || (!empty($_GET['comment_id']) && string_is_int($_GET['comment_id'])))
                {
                    $comment_text = undo_escape_sequences($table_comments[$key]['comment_text']);
                    $comment_text = cut_text($comment_text, './../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '&amp;comment_id=' . $table_comments[$key]['comment_id'], 'comment_id');
                    $user_avatar = $table_comments[$key]['user_avatar'];
                    if (!empty($table_comments[$key]['user_signature']))
                    {
                        $user_signature = '<br />&#45;&#45;<br />' . $table_comments[$key]['user_signature'];
                    }
                    else
                    {
                        $user_signature = '';
                    }
                }
                else
                {
                    $comment_text = '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '&amp;comment_id=' . $table_comments[$key]['comment_id'] . '" title="' . $lang['HIDDEN_COMMENT'] . '">' . $lang['HIDDEN_COMMENT'] . '</a>';
                    $user_avatar = './../images/avatars/empty.png';
                    $user_signature = '';
                }

                $template->set_var(array('ARTICLE_MARGIN' => $articles_margins[$key],
                                         'COMMENT_EDITED' => $comment_edited,
                                         'COMMENT_ID' => $table_comments[$key]['comment_id'],
                                         'COMMENT_LINKS' => $comment_links,
                                         'COMMENT_POSTED' => sprintf($lang['POSTED_BY'], $table_comments[$key]['user_id'], $table_comments[$key]['user_name'], format_date($table_comments[$key]['comment_creation'])),
                                         'COMMENT_SUBJECT' => $table_comments[$key]['comment_subject'],
                                         'COMMENT_TEXT' => $comment_text,
                                         'EDIT_LINK' => $edit_link,
                                         'REPLY_LINK' => $reply_link,
                                         'STORY_ID' => $table_comments[$key]['story_id'],
                                         'SUBJECT_CLASS' => $subject_class,
                                         'USER_AVATAR' => $user_avatar,
                                         'USER_SIGNATURE' => $user_signature,
                                         'comments' => ''));
            }
            // Replies (folded view)
            else
            {
                // User logged?
                if ($_SESSION['user_id'] > 0)
                {
                    // New comments since last visit
                    if ($table_comments[$key]['comment_creation'] > $users['user_last_visit'])
                    {
                        $subject_class = 'new';
                    }
                    else
                    {
                        $subject_class = 'old';
                    }
                }
                else
                {
                    $subject_class = 'old';
                }
                $template->set_var(array('COMMENT_REPLY' => '<a href="./../comments/index.php?news_id=' . $_GET['news_id'] . '&amp;story_id=' . $table_comments[$key]['story_id'] . '&amp;page=' . $table_pages[$table_comments[$key]['comment_id']] . '&amp;list=' . $table_lists[$table_comments[$key]['comment_id']] . '#c' . $table_comments[$key]['comment_id'] . '" title="' . $table_comments[$key]['comment_subject'] . '"><span class="' . $subject_class . '">' . $table_comments[$key]['comment_subject'] . '</span></a> ' . $lang['BY'] . ' <a href="./../users/info.php?user_id=' . $table_comments[$key]['user_id'] . '" title="' . $table_comments[$key]['user_name'] . '">' . $table_comments[$key]['user_name'] . '</a> ' . format_date($table_comments[$key]['comment_creation']),
                                         'REPLY_MARGIN' => $replies_margins[$key]));
                $template->parse('COMMENTS_BLOCK', 'comments', true);
            }
            // Last comment
            if (($num_comments == $comments_per_story[$table_comments[$key]['story_id']]) || (!empty($_GET['story_id']) && string_is_int($_GET['story_id'])))
            {
                $num_comments = 0;
                $template->parse('STORIES_BLOCK', 'stories', true);
            }
            $comments_exist = true;
        }
        if (isset($comments_exist))
        {
            $template->set_var('FOOTER', sprintf($lang['PAGES'], $pages_list));
        }
        else
        {
            $template->set_var('FOOTER', $lang['ERROR_NO_COMMENT']);
        }
        $template->parse('index', null, false, array(TABLE_CATEGORIES, TABLE_COMMENTS, TABLE_EMOTICONS, TABLE_NEWS, TABLE_USERS));
    }
}
else
{
    error_template($lang['ERROR_NO_DATA']);
}

page_footer();

?>
Return current item: Gnew