<?php
    $GLOBALS['highlight'] = 'forum';

    require_once('include/functions/community_banned.php');
    require_once('include/functions/community_permissions.php');    
    require_once('include/functions/community_thread_overall_rating.php');    
    require_once('include/functions/pages.php');

    $_id = isset($_GET['i']) ? trim($_GET['i']) : '';
    $_type = isset($_GET['type']) ? trim($_GET['type']) : 'new';
    
    if ($_type != 'new' && $_type != 'my_new')
    {
        $_type = 'new';
    }
    
    if (language() == 'french')
    {
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_FORUM', 'D�sol�, ce forum n\'existe pas.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_SECTION', 'D�sol�, ce section n\'existe pas.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_COMMUNITY', 'D�sol�, cette communaut� n\'existe pas.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_THREADS_YET', 'Il n\'y a pas de sujet dans ce forum pourtant!');
        define('SHOW_COMMUNITY_FORUM_MESSAGES', 'Messages');
        define('SHOW_COMMUNITY_FORUM_PAGE', 'Page');
        define('SHOW_COMMUNITY_FORUM_LAST_UPDATED', 'Mis � Jour');
        define('SHOW_COMMUNITY_FORUM_STICKIES', 'Stickies');
        define('SHOW_COMMUNITY_FORUM_MESSAGES_TOTAL', 'threads avec');
        define('SHOW_COMMUNITY_FORUM_POST_NEW_MESSAGE', 'Poster Un Nouveau Message');
        define('SHOW_COMMUNITY_FORUM_SUBJECT', 'Sujet');
        define('SHOW_COMMUNITY_FORUM_BODY', 'Message');
        define('SHOW_COMMUNITY_FORUM_LOCKED', 'This forum is locked. No new messages are allowed.');
        define('SHOW_COMMUNITY_FORUM_MUST_BE_LOGGED_IN', 'You must be logged in to post a message.');
        define('SHOW_COMMUNITY_FORUM_MODERATOR_TOOLS', 'Moderator Tools');
        define('SHOW_COMMUNITY_FORUM_LOCK_THIS_FORUM', 'Lock This Forum');
        define('SHOW_COMMUNITY_FORUM_UNLOCK_THIS_FORUM', 'Unlock This Forum');
        define('SHOW_COMMUNITY_FORUM_BY', 'par');
        define('SHOW_COMMUNITY_FORUM_TOTAL', 'totaux');
        define('SHOW_COMMUNITY_FORUM_NEW', 'nouveau');
        define('SHOW_COMMUNITY_FORUM_VIEWS', 'Vues');
        define('SHOW_COMMUNITY_FORUM_REPLIES', 'R�ponses');
        define('SHOW_COMMUNITY_FORUM_REPLIES_LC', 'message totalisent');
    }
    else
    {
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_FORUM', 'Sorry, that forum does not exist.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_SECTION', 'Sorry, that section does not exist.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_COMMUNITY', 'Sorry, that community does not exist.');
        define('SHOW_COMMUNITY_FORUM_ERROR_NO_THREADS_YET', 'There aren\'t any threads in this forum yet!');
        define('SHOW_COMMUNITY_FORUM_MESSAGES', 'Messages');
        define('SHOW_COMMUNITY_FORUM_PAGE', 'Page');
        define('SHOW_COMMUNITY_FORUM_LAST_UPDATED', 'Last Updated');
        define('SHOW_COMMUNITY_FORUM_STICKIES', 'Stickies');
        define('SHOW_COMMUNITY_FORUM_MESSAGES_TOTAL', 'threads with');
        define('SHOW_COMMUNITY_FORUM_POST_NEW_MESSAGE', 'Post New Message');
        define('SHOW_COMMUNITY_FORUM_SUBJECT', 'Subject');
        define('SHOW_COMMUNITY_FORUM_BODY', 'Body');
        define('SHOW_COMMUNITY_FORUM_LOCKED', 'This forum is locked. No new messages are allowed.');
        define('SHOW_COMMUNITY_FORUM_MUST_BE_LOGGED_IN', 'You must be logged in to post a message.');
        define('SHOW_COMMUNITY_FORUM_MODERATOR_TOOLS', 'Moderator Tools');
        define('SHOW_COMMUNITY_FORUM_LOCK_THIS_FORUM', 'Lock This Forum');
        define('SHOW_COMMUNITY_FORUM_UNLOCK_THIS_FORUM', 'Unlock This Forum');
        define('SHOW_COMMUNITY_FORUM_BY', 'by');
        define('SHOW_COMMUNITY_FORUM_TOTAL', 'total');
        define('SHOW_COMMUNITY_FORUM_NEW', 'new');
        define('SHOW_COMMUNITY_FORUM_VIEWS', 'Views');
        define('SHOW_COMMUNITY_FORUM_REPLIES', 'Replies');
        define('SHOW_COMMUNITY_FORUM_REPLIES_LC', 'messages total');
    }

    $community = mysqli_query_logged("SELECT * FROM community WHERE community_id = " . sq($_id));
    if (!$community_row = mysqli_fetch_assoc($community))    
    {
        header('Location: ./?s=communities');
        die;        
    }

    community_banned($community_row['community_id']);
    community_permissions($community_row['community_id']);

    include('include/parts/header.php');
    
    $j = 0;
    $community_sections = mysqli_query_logged("SELECT * FROM community_sections WHERE community_id = '" . $community_row['community_id'] . "' AND section_deleted = '0'");
    while ($community_sections_row = mysqli_fetch_assoc($community_sections))
    {
        $community_forums = mysqli_query_logged("SELECT * FROM community_forums WHERE section_id = '" . $community_sections_row['section_id'] . "'");
        while ($community_forums_row = mysqli_fetch_assoc($community_forums))
        {
            $community_threads = mysqli_query_logged("SELECT * FROM community_threads WHERE forum_id = '" . $community_forums_row['forum_id'] . "' ORDER BY thread_sticky DESC, thread_last_posted_on DESC LIMIT 0, " . COMMUNITY_THREADS_PER_PAGE);
            if (mysqli_num_rows($community_threads))
            {
                $i = 0;
                $stickies_header = 0;
                $normal_header = 0;
                while ($community_threads_row = mysqli_fetch_assoc($community_threads))
                {
                    $new_reply_count = 0;
                    $new_thread = 0;
                    $community_threads_pointers = mysqli_query_logged("SELECT * FROM community_threads_pointers WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND thread_id = '" . $community_threads_row['thread_id'] . "'");
                    if ($community_threads_pointers_row = mysqli_fetch_assoc($community_threads_pointers))
                    {
                        $community_messages_row = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS num FROM community_messages WHERE thread_id = '" . $community_threads_row['thread_id'] . "' AND message_id > '" . $community_threads_pointers_row['message_id'] . "'"));
                        $new_reply_count = $community_messages_row['num'];
                    }
                    else
                    {
                        $new_reply_count = $community_threads_row['thread_messages'];
                    }
                    if ($new_reply_count)
                    {
                        $new_thread = 1;
                    }
                    if ($community_threads_row['thread_user_id'] == $GLOBALS['auth']['id'])
                    {
                        $my_reply_count['num'] = 1;
                    }
                    else
                    {
                        $my_reply_count = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS num FROM community_messages WHERE message_user_id = '" . $GLOBALS['auth']['id'] . "' AND thread_id = '" . $community_threads_row['thread_id'] . "' LIMIT 1"));
                    }
                    if (($_type == 'new' && $new_thread) || ($_type == 'my_new' && $new_thread && $my_reply_count['num']))
                    {
                        if ($i == 0)
                        {
                            echo box_outside_top('<a href="?s=community">Communities</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . $community_row['community_name'] . '</a> - ' . $community_forums_row['forum_name_' . language()], '[ ' . ($_type == 'my_new' ? '<a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=new">New Threads</a>' : 'New Threads') . ' | ' . ($_type == 'new' ? '<a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=my_new">Participating New Threads</a>' : 'Participating New Threads') . ' | <a href="?a=community_mark_forum&i=' . $community_forums_row['forum_id'] . '">Mark All As Viewed</a> ]');
                        }
                        if ($i++)
                        {
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        }
                        echo box_inside_top();
                        $status = '';
                        if ($community_threads_row['thread_locked'])
                        {
                            $status .= ' <span class="notice_attention">locked</span>';
                        }
                        echo '<div style="cursor: pointer;" onClick="document.location=\'?s=community_thread&i=' . $community_threads_row['thread_id'] . '#l\';">';
                        echo '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
                        if ($new_thread)
                        {
                            echo '<td width="30" class="notice_attention" style="font-weight: bold; text-align: center;">' . SHOW_COMMUNITY_FORUM_NEW . '</td>';
                        }
                        if ($community_threads_row['thread_sticky'])
                        {
                            echo '<td width="30" class="notice_attention" style="font-weight: bold; text-align: center;">STICK</td>';
                        }
                        echo '<td style="padding-left: 8px;"><div style="font-weight: bold; font-size: 12px; display: inline;"' . ($my_reply_count['num'] ? ' class="notice_good"' : ' class="notice_error"') . '">&raquo;</div> <div style="font-weight: bold; display: inline;"><a href="?s=community_thread&i=' . $community_threads_row['thread_id'] . '#l">' . htmlentities($community_threads_row['thread_title']) . '</a>' . ($status ? $status : '') . '</div><div>' . SHOW_COMMUNITY_FORUM_BY . ': ' . get_username($community_threads_row['thread_user_id'], 1) . '</div></td>';
                        echo '<td width="50" align="right" valign="top" style="padding-right: 5px;"' . ($GLOBALS['auth']['id'] ? ' valign="top"' : '') . '>' . SHOW_COMMUNITY_FORUM_VIEWS . ':</td>';
                        echo '<td width="100" valign="top"><b>' . ($community_threads_row['thread_views'] > 1000 ? '<span class="notice_error">' : '') . number_format($community_threads_row['thread_views']) . ($community_threads_row['thread_views'] > 1000 ? '</span>' : '') . '</b><br />' . thread_overall_rating($community_threads_row['thread_id']) . '</td>';
                        echo '<td width="50" align="right" valign="top" style="padding-right: 5px;"' . ($GLOBALS['auth']['id'] ? ' valign="top"' : '') . '>' . SHOW_COMMUNITY_FORUM_REPLIES . ':</td>';
                        echo '<td width="90" valign="top">';
                        if ($GLOBALS['auth']['id'])
                        {    
                            if ($new_reply_count)
                            {
                                echo '<span class="notice_attention"><b>' . number_format($new_reply_count) . '</b> ' . SHOW_COMMUNITY_FORUM_NEW . '</span>';
                            }    
                            else
                            {
                                echo '<b>' . number_format($new_reply_count) . '</b> ' . SHOW_COMMUNITY_FORUM_NEW;
                            }
                            echo '<br />';
                        }
                        echo '<b>' . ($community_threads_row['thread_messages'] > 100 ? '<span class="notice_attention">' : '') . number_format($community_threads_row['thread_messages']) . ($community_threads_row['thread_messages'] > 100 ? '</span>' : '') . '</b> ' . SHOW_COMMUNITY_FORUM_TOTAL;    
                        echo '</td>';
                        echo '<td width="180" valign="top">' . nice_date($community_threads_row['thread_last_posted_on'], 'D M j, y @ g:ia');
                        $community_messages = mysqli_query_logged("SELECT message_user_id FROM community_messages WHERE thread_id = '" . $community_threads_row['thread_id'] . "' ORDER BY message_id DESC LIMIT 1");
                        if ($community_messages_row = mysqli_fetch_assoc($community_messages))
                        {
                            echo '<br />' . SHOW_COMMUNITY_FORUM_BY . ': ' . get_username($community_messages_row['message_user_id'], 1);
                        }
                        echo '</td>';    
                        echo '</tr></table>';
                        echo '</div>';
                        echo box_inside_bottom();
                        $j++;
                    }
                }
                if ($i)
                {
                    echo box_outside_bottom();
                }
            }    
        }
    }
    if ($j == 0)
    {
        echo box_outside_top('<a href="?s=community">Communities</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . $community_row['community_name'] . '</a> - My Forums', '[ <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=new">All New Messages</a> | <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=my_new">Participating New Messages</a> ]');
        echo box_inside_top();
        echo 'There are no threads matching what you want to view...';
        echo box_inside_bottom();
        echo box_outside_bottom();
    }
    
    include('include/parts/footer.php');
?>