/**
 * Handle edit form submission.
 */
function groups_admin_groups_edit_submit()
{
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $group_id = isset($_POST['group-id-field']) ? $_POST['group-id-field'] : null;
    $group = Groups_Group::read($group_id);
    if ($group) {
        $group_id = $group->group_id;
        if ($group->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
            $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
        } else {
            $name = Groups_Registered::REGISTERED_GROUP_NAME;
        }
        $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
        $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
        return Groups_Group::update(compact("group_id", "name", "parent_id", "description"));
    } else {
        return false;
    }
}
/**
 * Handle edit form submission.
 */
function groups_admin_groups_edit_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $group_id = isset($_POST['group-id-field']) ? $_POST['group-id-field'] : null;
    $group = Groups_Group::read($group_id);
    if ($group) {
        $group_id = $group->group_id;
        if ($group->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
            $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
        } else {
            $name = Groups_Registered::REGISTERED_GROUP_NAME;
        }
        $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
        $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
        if (empty($name)) {
            Groups_Admin::add_message(__('The <em>Name</em> must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
            return false;
        }
        if ($other_group = Groups_Group::read_by_name($name)) {
            if ($other_group->group_id != $group_id) {
                Groups_Admin::add_message(sprintf(__('The <em>%s</em> group already exists and cannot be used to name this one.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($other_group->name))), 'error');
                return false;
            }
        }
        $group_id = Groups_Group::update(compact("group_id", "name", "parent_id", "description"));
        if ($group_id) {
            $capability_table = _groups_get_tablename("capability");
            $group_capability_table = _groups_get_tablename("group_capability");
            $group_capabilities = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$capability_table} WHERE capability_id IN ( SELECT capability_id FROM {$group_capability_table} WHERE group_id = %d )", Groups_Utility::id($group_id)));
            $group_capabilities_array = array();
            foreach ($group_capabilities as $group_capability) {
                $group_capabilities_array[] = $group_capability->capability_id;
            }
            $caps = array();
            if (isset($_POST['capability_ids'])) {
                $caps = $_POST['capability_ids'];
            }
            // delete
            foreach ($group_capabilities_array as $group_cap) {
                if (!in_array($group_cap, $caps)) {
                    Groups_Group_Capability::delete($group_id, $group_cap);
                }
            }
            // add
            foreach ($caps as $cap) {
                if (!in_array($cap, $group_capabilities_array)) {
                    Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $cap));
                }
            }
        }
        return $group_id;
    } else {
        return false;
    }
}