/**
 * Recursive function will return all communities below the specified community_id, as an array.
 *
 * @param int $community_id
 * @param array $requested_fields
 * @param int $max_generations
 * @param bool $show_inactive
 * @param int $level
 * @return array
 */
function communities_fetch_children($community_id = 0, $requested_fields = false, $max_generations = 0, $show_inactive = false, $output_type = false, $level = 0)
{
    global $db, $COMMUNITIES_FETCH_CHILDREN;
    if ($level > 99) {
        return false;
    }
    if ($output_type && !in_array($output_type, array("array", "select"))) {
        return false;
    }
    if (!is_array($requested_fields) || !count($requested_fields)) {
        $requested_fields = array("community_id", "community_parent", "community_url", "community_title", "community_active");
    }
    $fetched = array();
    $query = "\tSELECT `" . implode("`, `", $requested_fields) . "`\n\t\t\t\tFROM `communities`\n\t\t\t\tWHERE `community_parent` = " . $db->qstr((int) $community_id) . "\n\t\t\t\t" . (!(bool) $show_inactive ? " AND `community_active` = '1'" : "") . "\n\t\t\t\tORDER BY `community_title` ASC";
    $results = $db->GetAll($query);
    if ($results) {
        foreach ($results as $result) {
            $fetched[$result["community_id"]] = $result;
            if ($output_type) {
                $fetched[$result["community_id"]]["indent_level"] = $indent;
            }
            if (!$max_generations || $level < $max_generations) {
                $children = communities_fetch_children($result["community_id"], $requested_fields, $max_generations, $show_inactive, $output_type, $level + 1);
                if (is_array($children) && @count($children)) {
                    $fetched[$result["community_id"]]["community_children"] = $children;
                } else {
                    $fetched[$result["community_id"]]["community_children"] = array();
                }
            } else {
                $fetched[$result["community_id"]]["community_children"] = array();
            }
        }
    }
    switch ($output_type) {
        case "select":
            $html = "";
            if (is_array($fetched) && count($fetched)) {
                foreach ($fetched as $result) {
                    $html .= "<option value=\"" . $result["community_id"] . "\"" . (is_array($COMMUNITIES_FETCH_CHILDREN) && in_array($result["community_id"], $COMMUNITIES_FETCH_CHILDREN) ? " selected=\"selected\"" : "") . ">" . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $result["indent_level"]) . html_encode($result["community_title"]) . "</option>\n";
                }
            }
            return $html;
            break;
        case "array":
        default:
            return $fetched;
            break;
    }
}
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="vertical-align: top"><input type="radio" name="community_registration" id="community_registration_3" value="3" onclick="selectRegistrationOption('3')" style="vertical-align: middle"<?php 
            echo isset($PROCESSED["community_registration"]) && $PROCESSED["community_registration"] == 3 ? " checked=\"checked\"" : "";
            ?>
 /></td>
                                    <td>
                                        <label for="community_registration_3" class="normal-green">Community Registration</label>
                                        <div class="content-small">Only members of the selected Communities can register to be part of this community.</div>
                                        <div id="community_registration_show_communities" style="display: none; padding: 5px 5px 0px 5px">
                                            <select id="community_registration_communities" name="community_registration_communities[]" multiple="multiple" size="10" style="width: 85%; height: 150px">
                                                <?php 
            $COMMUNITIES_FETCH_CHILDREN = isset($community_communities) ? $community_communities : array();
            echo communities_fetch_children(0, false, 0, false, "select");
            ?>
                                            </select>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="vertical-align: top"><input type="radio" name="community_registration" id="community_registration_4" value="4" onclick="selectRegistrationOption('4')" style="vertical-align: middle"<?php 
            echo isset($PROCESSED["community_registration"]) && $PROCESSED["community_registration"] == 4 ? " checked=\"checked\"" : "";
            ?>
 /></td>
                                    <td>
                                        <label for="community_registration_4" class="normal-green">Private Community</label>
                                        <div class="content-small">People cannot register, members are invited only by community administrators.</div>
                                    </td>
                                </tr>