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

    if (language() == 'french')
    {
        define('SHOW_COMMUNITY_COMMUNITY', 'Les Communaut�s');
        define('SHOW_COMMUNITY_THREADS', 'Threads');
        define('SHOW_COMMUNITY_MESSAGES', 'Messages');
        define('SHOW_COMMUNITY_NEW', 'nouveau');
        define('SHOW_COMMUNITY_TOTAL', 'totaux');
        define('SHOW_COMMUNITY_SMILEYS', 'Smileys');
        define('SHOW_COMMUNITY_HELP', 'Aide');
        define('SHOW_COMMUNITY_MARK_ALL_AS_VIEWED', 'Marquer Tout Comme Regard�');    
    }
    else
    {
        define('SHOW_COMMUNITY_COMMUNITY', 'Communities');
        define('SHOW_COMMUNITY_THREADS', 'Threads');
        define('SHOW_COMMUNITY_MESSAGES', 'Messages');
        define('SHOW_COMMUNITY_NEW', 'new');
        define('SHOW_COMMUNITY_TOTAL', 'total');
        define('SHOW_COMMUNITY_SMILEYS', 'Smileys');
        define('SHOW_COMMUNITY_HELP', 'Help');
        define('SHOW_COMMUNITY_MARK_ALL_AS_VIEWED', 'Mark All As Viewed');
    }

    $community = mysqli_query_logged("SELECT * FROM community WHERE community_id = '" . $_id . "'");
    if (!$community_row = mysqli_fetch_assoc($community))
    {
        header('Location: ./?s=communities');
        die;        
    }
    
    require_once('include/functions/community_banned.php');
    community_banned($community_row['community_id']);
    
    include('include/parts/header.php');
    
    echo box_outside_top('<a href="?s=communities">' . SHOW_COMMUNITY_COMMUNITY . '</a> - ' . htmlentities($community_row['community_name']), '[ <a href="?s=community_smileys">' . SHOW_COMMUNITY_SMILEYS . '</a> | <a href="?s=community_help">' . SHOW_COMMUNITY_HELP . '</a>' . ($GLOBALS['auth']['id'] ? ' | <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '">My Forums</a> | <a href="?a=community_mark_community&i=' . $community_row['community_id'] . '">' . SHOW_COMMUNITY_MARK_ALL_AS_VIEWED . '</a>' : '') . ' ]');
    echo box_inside_top();
    echo 'Welcome to the <b>' . htmlentities($community_row['community_name']) . '</b> community.';
    echo box_inside_bottom();
    echo box_outside_bottom();
    
    $i = 0;
    $community_sections = mysqli_query_logged("SELECT * FROM community_sections WHERE community_id = '" . $community_row['community_id'] . "' AND section_deleted = '0' ORDER BY section_order_id");
    while ($community_sections_row = mysqli_fetch_assoc($community_sections))
    {
        echo box_outside_top(htmlentities($community_sections_row['section_name_' . language()]));
        $j = 0;
        $community_forums = mysqli_query_logged("SELECT * FROM community_forums WHERE section_id = '" . $community_sections_row['section_id'] . "' AND forum_deleted = '0' ORDER BY forum_order_id");
        while ($community_forums_row = mysqli_fetch_assoc($community_forums))
        {
            if ($j++ > 0)
            {
                echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            }
            echo box_inside_top();
            echo '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
            echo '<td>';
            echo '<span style="font-size: 14px; font-weight: bold;"><a href="?s=community_forum&i=' . $community_forums_row['forum_id'] . '">' . htmlentities($community_forums_row['forum_name_' . language()]) . '</a>' . ($community_forums_row['forum_locked'] ? ' <span class="notice_attention">Locked</span>' : '') . '</span><br />';
            echo '<span style="font-size: 11px;">' . htmlentities($community_forums_row['forum_description_' . language()]) . '</span>';
            echo '</td>';
            echo '<td width="100" align="right" style="padding-right: 5px;">' . SHOW_COMMUNITY_THREADS . ':<br />' . SHOW_COMMUNITY_MESSAGES . ':</td>';
            echo '<td width="110">';
            echo '<b>' . $community_forums_row['forum_threads'] . '</b> ' . SHOW_COMMUNITY_TOTAL . '<br />';
            echo '<b>' . $community_forums_row['forum_messages'] . '</b> ' . SHOW_COMMUNITY_TOTAL;
            echo '</td>';
            echo '<td width="110">';
            if ($GLOBALS['auth']['id'])
            {
                echo '<br />';
                $new_messages = get_new_messages($community_forums_row['forum_id']);
                echo ($new_messages ? '<div class="notice_attention">' : '');
                echo '<b>' . $new_messages . '</b> ' . SHOW_COMMUNITY_NEW;
                echo ($new_messages ? '</div>' : '');
            }
            else
            {
                echo '<b>0</b> ' . SHOW_COMMUNITY_NEW . '<br />';    
            }        
            echo '</td>';
            echo '</tr></table>';
            echo box_inside_bottom();
        }
        echo box_outside_bottom();
    }
    
    echo box_outside_top('Moderators');
    echo box_inside_top();
    $arr = array();
    $community_permissions = mysqli_query_logged("SELECT DISTINCT(user_id) FROM community_permissions WHERE community_id = '" . $community_row['community_id'] . "'");
    while ($community_permissions_row = mysqli_fetch_assoc($community_permissions))
    {
        $username = get_username($community_permissions_row['user_id']);
        $arr[] = '<a href="?s=profile&u=' . $username . '">' . $username . '</a>';
    }    
    echo implode(', ', $arr);
    echo box_inside_bottom();
    echo box_outside_bottom();
    
    include('include/parts/footer.php');
    
    function get_new_messages($forum_id)
    {
        $new = 0;
        $community_threads = mysqli_query_logged("SELECT * FROM community_threads WHERE forum_id = '" . $forum_id . "' ORDER BY thread_sticky DESC, thread_last_posted_on DESC LIMIT " . COMMUNITY_THREADS_PER_PAGE * 2);
        while ($community_threads_row = mysqli_fetch_assoc($community_threads))
        {
            $community_threads_pointers = mysqli_query_logged("SELECT * FROM community_threads_pointers WHERE thread_id = '" . $community_threads_row['thread_id'] . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
            if ($community_threads_pointers_row = mysqli_fetch_assoc($community_threads_pointers))
            {
                $community_messages = mysqli_query_logged("SELECT COUNT(*) AS count FROM community_messages WHERE thread_id = '" . $community_threads_row['thread_id'] . "' AND message_id > '" . $community_threads_pointers_row['message_id'] . "' AND message_id <= '" . $community_threads_row['thread_last_message_id'] . "'");
                if ($community_messages_row = mysqli_fetch_assoc($community_messages))
                {
                    $new = $new + $community_messages_row['count'];
                }
            }
            else
            {
                $new = $new + $community_threads_row['thread_messages'];
            }
        }
        return $new;
    }
?>