<?php
    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    $_name = isset($_POST['name']) ? trim(strval($_POST['name'])) : ''; 
    $_description = isset($_POST['description']) ? trim(strval($_POST['description'])) : ''; 
    
    $community = mysqli_query_logged("SELECT * FROM community_sections, community WHERE community_sections.section_id = " . sq($_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, but what you are trying to access 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 community.');
        header('Location: ./?s=community_create');
        die;
    }
    
    require_once('include/functions/community_forum_valid.php');
    if ($error = community_forum_valid($_name, $_description))
    {
        make_cookie('notice', $error);
        header('Location: ./?s=community_modify&i=' . $community_row['community_id']);
        die;
    }

    $community_forums = mysqli_query_logged("SELECT * FROM community_forums WHERE section_id = '" . $community_row['section_id'] . "' ORDER BY forum_order_id DESC LIMIT 1");
    if ($community_forums_row = mysqli_fetch_array($community_forums))
    {
        $order_id = $community_forums_row['forum_order_id'] + 1;
    }
    else
    {
        $order_id = 0;
    }

    mysqli_query_logged("INSERT INTO community_forums SET section_id = '" . $community_row['section_id'] . "', forum_name_english = " . sq($_name) . ", forum_name_french = " . sq($_name) . ", forum_description_english = " . sq($_description) . ", forum_description_french = " . sq($_description) . ", forum_order_id = '" . $order_id . "'");

    header('Location: ./?s=community_section_modify&i=' . $community_row['section_id']);
?>