/**
 * Handle field stuff
 */
function bum_manage_fields()
{
    $action = isset($_POST['action']) ? $_POST['action'] : $_GET['action'];
    switch ($action) {
        case 'add_edit':
            global $wp_roles, $wp_user_fields;
            if (isset($_POST['fbJson'])) {
                //the fields are seperated by '&frmb'
                $form_fields = array();
                $fields = explode('&frmb', $_POST['fbJson']);
                foreach ($fields as $field) {
                    if ($field != '') {
                        //get info
                        list($the_key, $the_value) = explode('=', $field);
                        preg_match_all('/\\[(.+?)]/', $the_key, $keys);
                        $keys = $keys[1];
                        //checkboxes show up as 'undefined' if unchecked or 'checked', should be saved as bool
                        $the_value = $the_value == 'undefined' ? 'false' : urldecode($the_value);
                        $the_value = $the_value == 'checked' ? 'true' : $the_value;
                        //this is to deal with different depths of values
                        if (count($keys) == 2) {
                            $form_fields[$keys[0]][$keys[1]] = $the_value;
                        } elseif (count($keys) == 3) {
                            $form_fields[$keys[0]][$keys[1]][$keys[2]] = $the_value;
                        } elseif (count($keys) == 4) {
                            $form_fields[$keys[0]][$keys[1]][$keys[2]][$keys[3]] = $the_value;
                        }
                    }
                }
                //generate the json for this form
                $form = new Formbuilder($form_fields);
                $vals = $form->store();
                $json = $form->generate_json();
                $hash = $vals['form_hash'];
                //update or insert the json into hidden taxonomy
                $term = get_term_by('slug', sanitize_title($_GET['ptab']), BUM_HIDDEN_FIELDS);
                if ($term) {
                    wp_update_term($term->term_id, BUM_HIDDEN_FIELDS, array('description' => $json));
                } else {
                    wp_insert_term($_GET['ptab'], BUM_HIDDEN_FIELDS, array('description' => $json));
                }
            }
            break;
    }
    echo bum_get_show_view('bum-manage-fields');
}