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

    $_id = isset($_GET['i']) ? trim($_GET['i']) : '';

    $community = mysqli_query_logged("SELECT * FROM community_forums, community_sections, community WHERE community_forums.forum_id = " . sq($_id) . " AND community_forums.forum_deleted = '0' AND community_forums.section_id = community_sections.section_id AND community_sections.section_deleted = '0' AND community_sections.community_id = community.community_id");
    if (!$community_row = mysqli_fetch_assoc($community))
    {
        make_cookie('notice', 'Sorry, this forum does not exist.');
        header('Location: ./?s=community_create');
        die;
    }
    
    require_once('include/functions/community_permissions.php');
    community_permissions($community_row['community_id']);
    
    if (!$GLOBALS['auth']['community']['administration'])
    {
        make_cookie('notice', 'Sorry, you don\'t have permission to modify this forum.');
        header('Location: ./?s=community_create');
        die;
    }

    include('include/parts/header.php');
    
    echo box_outside_top('<a href="?s=community_modify&i=' . $community_row['community_id'] . '">Modify Your Community</a> - <a href="?s=community_section_modify&i=' . $community_row['section_id'] . '">Modify Section</a> - Modify Forum');
    echo box_inside_top();
?>

<form action="?a=community_forum_modify&i=<?php echo $community_row['forum_id']; ?>" method="post" class="form">
<b>Forum Name</b> (<?php echo COMMUNITY_FORUM_NAME_MIN ?> to <?php echo COMMUNITY_FORUM_NAME_MAX ?> characters in length.)
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="text" name="name" maxlength="<?php echo COMMUNITY_FORUM_NAME_MAX; ?>" value="<?php echo htmlentities($community_row['forum_name_english']); ?>" class="input_text" style="width: 400px;">
<div style="padding: 5px 0px 0px 0px;"></div>
<b>Forum Description</b> (<?php echo COMMUNITY_FORUM_DESC_MIN ?> to <?php echo COMMUNITY_FORUM_DESC_MAX ?> characters in length.)
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="text" name="description" maxlength="<?php echo COMMUNITY_FORUM_DESC_MAX; ?>" value="<?php echo htmlentities($community_row['forum_description_english']) ?>" class="input_text" style="width: 400px;">
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Modify Forum" class="input_submit">                    
</form>

<?php        
    echo box_inside_bottom();
    echo box_outside_bottom();
    
    echo box_outside_top('Forum Moderators');
    echo box_inside_top();

    $i = 0;
    $community_forums_permissions = mysqli_query_logged("SELECT DISTINCT(user_id) FROM community_forums_permissions WHERE forum_id = '" . $community_row['forum_id'] . "'");
    if (mysqli_num_rows($community_forums_permissions))
    {
        while ($community_forums_permissions_row = mysqli_fetch_assoc($community_forums_permissions))
        {
            if ($i++)
            {
                echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            }
            echo '[' . $community_forums_permissions_row['user_id'] . '] <span style="font-size: 14px; font-weight: bold;">';
            echo '<a href="?s=community_admin_modify_forum&i=' . $community_row['forum_id'] . '&admin_name=' . get_username($community_forums_permissions_row['user_id']) . '">' . get_username($community_forums_permissions_row['user_id']) . '</a></span>';
            echo ' [<a href="?a=community_admin_delete_forum&i=' . $community_row['forum_id'] . '&admin_name=' . get_username($community_forums_permissions_row['user_id']) . '" onClick="return confirm(\'Are you sure you want to remove this admin?\')">remove</a>]';
            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            $j = 0;
            $permissions = mysqli_query_logged("SELECT permission FROM community_forums_permissions WHERE forum_id = '" . $community_row['forum_id'] . "' AND user_id = '" . $community_forums_permissions_row['user_id'] . "' ORDER BY permission");
            while ($permissions_row = mysqli_fetch_array($permissions))
            {
                if ($j++)
                {
                    echo ', ';
                }
                echo $permissions_row['permission'];
            }
        }
    }
    else
    {
        echo 'There are no forum moderators.';
    }
    
    echo box_inside_bottom();
    echo '<div style="padding: 5px 0px 0px 0px;"></div>';
    echo box_inside_top();
?>

<form action="?a=community_admin_add_forum&i=<?php echo $community_row['forum_id'] ?>" method="post" class="form">
<b>Add A Moderator</b> (enter username)
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="text" name="admin_name" class="input_text" style="width: 400px;">
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Add Moderator" class="input_submit">
</form>

<?php
    echo box_inside_bottom();
    echo box_outside_bottom();

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