/**
 * Generates the possible syndication groups taxonomy list on the post create/edit screen.
 *
 * @return Unordered list of checkboxes showing terms available for assignment.
 */
function ccgn_related_group_checkboxes($group_id, $post_id)
{
    // Get terms that can be used by this group
    $possibly_related_groups = ccgn_get_categories($group_id);
    //Get ids of terms that _are_ related to this post
    //TODO: If mutiple groups can edit a post, you could lose "related groups" association if group 2 can't associate with group 1. Editing should always take place in the origin group.
    $related_groups = wp_get_post_terms($post_id, 'ccgn_related_groups', array("fields" => "ids"));
    if (empty($possibly_related_groups)) {
        _e('This group has no categories associated with it. To post to group blog, first associate one or more categories with it.', 'bcg');
        return;
    } else {
        // Get this group's term; we'll always want it to be checked for UI transparency
        $home_group_term_id = ccgn_get_group_term_id($group_id);
        ?>
        <ul class="ccgn-related-groups">
        <?php 
        foreach ((array) $possibly_related_groups as $possible_relation) {
            $checked = !empty($related_groups) && in_array($possible_relation, $related_groups) || $possible_relation == $home_group_term_id ? true : false;
            $term = get_term($possible_relation, 'ccgn_related_groups');
            ?>
            <li>
                <label for="related-group-<?php 
            echo $possible_relation;
            ?>
"> <input type="checkbox" name="related-groups[]" id="related-group-<?php 
            echo $possible_relation;
            ?>
" value="<?php 
            echo $possible_relation;
            ?>
" <?php 
            if ($checked) {
                echo 'checked="checked"';
            }
            ?>
 /> <?php 
            echo $term->name;
            ?>
</label>
            </li>
            <?php 
        }
        ?>
        </ul>
        <?php 
    }
}
        public function settings_screen($group_id = null)
        {
            // $group_id = bp_get_group_id( $group_id );
            $is_enabled = ccgn_is_enabled($group_id);
            $tab_label = ccgn_get_tab_label($group_id);
            $slug = ccgn_get_slug($group_id);
            $level_to_post = ccgn_get_level_to_post($group_id);
            // Needed to use wp_terms_checklist()
            require_once ABSPATH . 'wp-admin/includes/template.php';
            ?>

				<p>
					Hub Narratives allows your hub to share stories and interact via comments with visitors to your hub space.
				</p>
				<p>
					<label for="ccgn_is_enabled"> <input type="checkbox" name="ccgn_is_enabled" id="ccgn_is_enabled" value="1" <?php 
            checked($is_enabled, true);
            ?>
 /> Enable hub narratives.</label>
				</p>

				<?php 
            // Only show the other settings once the plugin has been enabled for this group
            // This is necessary because the term for associating posts to this group is only created upon submit of the "enable" checkbox
            if ($is_enabled) {
                ?>
					<div id="ccgn_settings_details">

					<p>
							<label for='ccgn_tab_label'>Change the BuddyPress hub tab label from 'Narratives' to whatever you'd like.</label>
							<input type="text" name="ccgn_tab_label" id="ccgn_tab_label" value="<?php 
                echo esc_html($tab_label);
                ?>
" />
					 </p>
					 <?php 
                /* ?>
                		 <p>
                			<label for='ccgn_url_slug'>Change the slug to something other than 'narratives'.</label>
                			<input type="text" name="ccgn_url_slug" id="ccgn_url_slug" value="<?php echo esc_html( $slug ); ?>" />
                		 </p>
                		 <?php */
                ?>
					<p>
							<label for='ccgn_level_to_post'>Who should be able to create new posts?</label>
							<select name="ccgn_level_to_post" id="ccgn_level_to_post">
								<option <?php 
                selected($level_to_post, "admin");
                ?>
 value="admin">Hub Admins Only</option>
								<option <?php 
                selected($level_to_post, "mod");
                if (empty($level_to_post)) {
                    echo 'selected="selected"';
                }
                ?>
 value="mod">Hub Admins and Moderators</option>
								<option <?php 
                selected($level_to_post, "member");
                ?>
 value="member">Any Hub Member</option>
							</select>
					 </p>

					 <p>Select other groups that narratives published in this group could be syndicated to.</p>
					 <?php 
                // @TODO: Maybe only site admins should be able to add other groups
                // Handle group associations, which are a taxonomy
                // Get the syndicated groups selected for this group, add this group's term.
                $selected_terms = ccgn_add_this_group_term(ccgn_get_categories($group_id), ccgn_get_group_term_id($group_id));
                $tax_args = array('hide_empty' => false);
                $terms = get_terms('ccgn_related_groups', $tax_args);
                // TODO: This should be filtered by groups that don't have the plugin enabled.
                // get_terms either returns an array of terms or a WP_Error_Object if there's a problem
                if (!empty($terms) && !is_wp_error($terms)) {
                    $args = array('descendants_and_self' => 0, 'selected_cats' => $selected_terms, 'popular_cats' => false, 'walker' => null, 'taxonomy' => 'ccgn_related_groups', 'checked_ontop' => false);
                    wp_terms_checklist(0, $args);
                } else {
                    ?>
					<div class="error">
							<p>Looks like no group terms are set up yet. Check back later.</p>
					</div>
					<?php 
                }
                ?>
				</div> <!-- End #bcg_settings_details -->

			<?php 
            }
            // ends the "enabled" check
            ?>

				<div class="clear"></div>
				<script type="text/javascript">
					jQuery(document).ready(function() {
						//match visibility on page load
						if ( jQuery('#ccgn_is_enabled').is(':checked') ) {
									jQuery('#ccgn_settings_details').show();
							} else {
									jQuery('#ccgn_settings_details').hide();
							}
						//update visibility on change
						jQuery('#ccgn_is_enabled').click(function() {
							if ( jQuery(this).is(':checked') ) {
									jQuery('#ccgn_settings_details').show();
							} else {
									jQuery('#ccgn_settings_details').hide();
							}
						});
					});
				</script>

		<?php 
        }