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

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

    $_id = isset($_GET['i']) ? intval($_GET['i']) : null;
    
    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');
        define('SHOW_COMMUNITY_FORUM_AUTOMATED', 'This forum is automated. No new messages are allowed.');
    }
    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');
        define('SHOW_COMMUNITY_FORUM_AUTOMATED', 'This forum is automated. No new messages are allowed.');
    }

    $community_forums = mysqli_query_logged("SELECT * FROM community_forums, community_sections, community WHERE community_forums.forum_id = '" . $_id . "' AND community_forums.forum_deleted = '0' AND community_forums.section_id = community_sections.section_id AND community_sections.community_id = community.community_id");
    if (!$community_forums_row = mysqli_fetch_assoc($community_forums))    
    {
        header('Location: ./?s=communities');
        die;        
    }

    require_once('include/functions/community_banned.php');
    community_banned($community_forums_row['community_id']);    
    require_once('include/functions/community_permissions.php');    
    community_permissions($community_forums_row['community_id'], $community_forums_row['section_id'], $community_forums_row['forum_id']);

    include('include/parts/header.php');

    echo box_outside_top('<a href="?s=community">Communities</a> - <a href="?s=community&i=' . $community_forums_row['community_id'] . '">' . $community_forums_row['community_name'] . '</a> - ' . $community_forums_row['forum_name_' . language()], '[ <a href="?s=community_my_forum&i=' . $community_forums_row['community_id'] . '&type=new">New Threads</a> | <a href="?s=community_my_forum&i=' . $community_forums_row['community_id'] . '&type=my_new">Participating New Threads</a> ]');
    echo box_inside_top();
    echo '<span style="font-size: 14px; font-weight: bold;">' . htmlentities($community_forums_row['forum_name_' . language()]);
    if ($community_forums_row['forum_locked'] == 1)
    {
        echo ' <span class="notice_attention">Locked</span>';
    }
    echo '</span><br /><span style="font-size: 12px;">' . htmlentities($community_forums_row['forum_description_' . language()]) . '</span>';
    echo box_inside_bottom();
    echo box_outside_bottom();
    
    $page_count = ceil($community_forums_row['forum_threads'] / COMMUNITY_THREADS_PER_PAGE);
    $pages = '<span class="pages">Page: ' . pages('?s=community_forum&i=' . $community_forums_row['forum_id'], $_page, $page_count, true) . '</span>';

    $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 " . (($_page - 1) * COMMUNITY_THREADS_PER_PAGE . ", " . COMMUNITY_THREADS_PER_PAGE));
    if (mysqli_num_rows($community_threads))
    {
        echo box_outside_top('<a href="?s=community">Communities</a> - <a href="?s=community&i=' . $community_forums_row['community_id'] . '">' . $community_forums_row['community_name'] . '</a> - ' . $community_forums_row['forum_name_' . language()] . '<br />' . $pages, '[ <a href="?s=smileys">Smileys</a> | <a href="?s=community_help">Help</a>' . ($GLOBALS['auth']['id'] ? ' | <a href="?a=community_mark_forum&i=' . $community_forums_row['forum_id'] . '">Mark All As Viewed</a>' : '') . ' ]');
        $i = 0;
        $stickies_header = 0;
        $normal_header = 0;
        while ($community_threads_row = mysqli_fetch_assoc($community_threads))
        {
            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>';
            }
            $new_reply_count = 0;
            $new_thread = 0;
            if ($GLOBALS['auth']['id'])
            {
                $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;
                }
            }
            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 ($community_threads_row['thread_sticky'])
            {
                echo '<td width="40" class="notice_error" style="font-weight: bold; text-align: center;">sticky</td>';
            }
            if ($new_thread)
            {
                echo '<td width="40" class="notice_attention" style="font-weight: bold; text-align: center;">' . SHOW_COMMUNITY_FORUM_NEW . '</td>';
            }
            if ($GLOBALS['auth']['id'])
            {
                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"));
                }
            }
            else
            {
                $my_reply_count['num'] = 0;
            }
            $newtitle = '';
            if ($community_threads_row['thread_tile'])
            {
                $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE tile_id = '" . $community_threads_row['thread_tile'] . "'");
                if ($tiles_row = mysqli_fetch_assoc($tiles))
                {
                    if ($tiles_row['comment'])
                    {
                        $newtitle = substr($tiles_row['comment'], 0, 50);
                    }
                }
                else
                {
                    $newtitle = 'Removed Tile';
                }
            }
            $newquilt = '';
            if ($community_threads_row['thread_quilt'])
            {
                $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = '" . $community_threads_row['thread_quilt'] . "'");
                if ($quilts_row = mysqli_fetch_assoc($quilts))
                {
                    if ($quilts_row['name'])
                    {
                        $newquilt = substr($quilts_row['name'], 0, 50);
                    }
                }
                else
                {
                    $newquilt = 'Removed Quilt';
                }
            }
            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>&nbsp;<div style="font-weight: bold; font-size: 12px; display: inline;"><a href="' . ($community_threads_row['thread_tile'] ? '?s=tile&i=' . $community_threads_row['thread_tile'] : ($community_threads_row['thread_quilt'] ? '?s=quilt&i=' . $community_threads_row['thread_quilt'] : '?s=community_thread&i=' . $community_threads_row['thread_id'])) . '#l">' . htmlentities($newtitle ? $newtitle : ($newquilt ? $newquilt : $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_error">' : '') . 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();
        }    
        echo box_outside_bottom();
    }
    else
    {
        echo box_outside_top(SHOW_COMMUNITY_FORUM_MESSAGES);
        echo box_inside_top();
        echo SHOW_COMMUNITY_FORUM_ERROR_NO_THREADS_YET;
        echo box_inside_bottom();
        echo box_outside_bottom();
    }    

    if (mysqli_num_rows($community_threads) > 5)
    {
        echo make_pages('<a href="?s=community">Communities</a> - <a href="?s=community&i=' . $community_forums_row['community_id'] . '">' . $community_forums_row['community_name'] . '</a> - ' . $community_forums_row['forum_name_' . language()] . '<br />' . $pages);
    }

    echo box_outside_top(SHOW_COMMUNITY_FORUM_POST_NEW_MESSAGE);
    echo box_inside_top();
    if ($GLOBALS['auth']['id'] && ($community_forums_row['forum_locked'] == 0 || $GLOBALS['auth']['community']['forum_lock_post']) && ($community_forums_row['forum_automated'] == 0 || $GLOBALS['auth']['community']['forum_automate_post']))
    {
?>
        <form action="?a=community_thread_add&i=<?php echo $community_forums_row['forum_id']; ?>" method="post" class="form">
        <b><?php echo SHOW_COMMUNITY_FORUM_SUBJECT ?></b>
        <div style="padding: 5px 0px 0px 0px;"></div>
        <input type="text" name="title" maxlength="<?php echo COMMUNITY_THREAD_TITLE_MAX; ?>" class="input_text" style="width: 90%;"><br />
        <div style="padding: 5px 0px 0px 0px;"></div>
        <b><?php echo SHOW_COMMUNITY_FORUM_BODY ?></b>
        <div style="padding: 5px 0px 0px 0px;"></div>
        <textarea name="body" class="input_text" style="height: 120px; width: 90%;"></textarea>
        <div style="padding: 5px 0px 0px 0px;"></div>
        <input type="submit" value="<?php echo SHOW_COMMUNITY_FORUM_POST_NEW_MESSAGE ?>" class="input_submit">
        </form>
<?php
    }
    elseif ($GLOBALS['auth']['id'])
    {
        if ($community_forums_row['forum_locked'])
        {
            echo SHOW_COMMUNITY_FORUM_LOCKED;
        }
        elseif ($community_forums_row['forum_automated'])
        {
            echo SHOW_COMMUNITY_FORUM_AUTOMATED;
        }
    }
    else
    {
        echo SHOW_COMMUNITY_FORUM_MUST_BE_LOGGED_IN;
    }
    echo box_inside_bottom();
    echo box_outside_bottom();
    
    if ($GLOBALS['auth']['community']['forum_lock'])
    {
        echo box_outside_top(SHOW_COMMUNITY_FORUM_MODERATOR_TOOLS);
        echo box_inside_top();
        if ($community_forums_row['forum_locked'])
        {
            echo '<a href="?a=community_forum_lock&i=' . $community_forums_row['forum_id'] . '&unlock" onClick="return confirm(\'Are you sure you want to unlock this forum?\r\n Any member will be able to post again in it.\');">' . SHOW_COMMUNITY_FORUM_UNLOCK_THIS_FORUM . '</a><br />';
        }
        else
        {
            echo '<a href="?a=community_forum_lock&i=' . $community_forums_row['forum_id'] . '&lock" onClick="return confirm(\'Are you sure you want to lock this forum?\r\n No further posting will be allowed (except by authorised moderators).\');">' . SHOW_COMMUNITY_FORUM_LOCK_THIS_FORUM . '</a><br />';                
        }
        echo box_inside_bottom();                
        echo box_outside_bottom();
    }

    include('include/parts/footer.php');
?>