public static function add_categories_as_choices($field, $value)
 {
     $choices = $inputs = array();
     $is_post = isset($_POST['gform_submit']);
     $has_placeholder = $field->categoryInitialItemEnabled && RGFormsModel::get_input_type($field) == 'select';
     if ($has_placeholder) {
         $choices[] = array('text' => $field->categoryInitialItem, 'value' => '', 'isSelected' => true);
     }
     $display_all = $field->displayAllCategories;
     $args = array('hide_empty' => false, 'orderby' => 'name');
     if (!$display_all) {
         foreach ($field->choices as $field_choice_to_include) {
             $args['include'][] = $field_choice_to_include['value'];
         }
     }
     $args = gf_apply_filters('gform_post_category_args', $field->id, $args, $field);
     $terms = get_terms('category', $args);
     $terms_copy = unserialize(serialize($terms));
     // deep copy the terms to avoid repeating GFCategoryWalker on previously cached terms.
     $walker = new GFCategoryWalker();
     $categories = $walker->walk($terms_copy, 0, array(0));
     // 3rd parameter prevents notices triggered by $walker::display_element() function which checks $args[0]
     foreach ($categories as $category) {
         if ($display_all) {
             $selected = $value == $category->term_id || empty($value) && get_option('default_category') == $category->term_id && RGFormsModel::get_input_type($field) == 'select' && !$is_post && !$has_placeholder;
             $choices[] = array('text' => $category->name, 'value' => $category->term_id, 'isSelected' => $selected);
         } else {
             foreach ($field->choices as $field_choice) {
                 if ($field_choice['value'] == $category->term_id) {
                     $choices[] = array('text' => $category->name, 'value' => $category->term_id);
                     break;
                 }
             }
         }
     }
     if (empty($choices)) {
         $choices[] = array('text' => 'You must select at least one category.', 'value' => '');
     }
     $choice_number = 1;
     foreach ($choices as $choice) {
         if ($choice_number % 10 == 0) {
             //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10
             $choice_number++;
         }
         $input_id = $field->id . '.' . $choice_number;
         $inputs[] = array('id' => $input_id, 'label' => $choice['text'], 'name' => '');
         $choice_number++;
     }
     $field->choices = $choices;
     $is_form_editor = GFCommon::is_form_editor();
     $is_entry_detail = GFCommon::is_entry_detail();
     $is_admin = $is_form_editor || $is_entry_detail;
     $form_id = $is_admin ? rgget('id') : $field->formId;
     $field->choices = gf_apply_filters('gform_post_category_choices', array($form_id, $field->id), $field->choices, $field, $form_id);
     if (RGFormsModel::get_input_type($field) == 'checkbox') {
         $field->inputs = $inputs;
     }
     return $field;
 }
 public static function add_categories_as_choices($field, $value)
 {
     $choices = $inputs = array();
     $is_post = isset($_POST["gform_submit"]);
     $has_placeholder = rgar($field, 'categoryInitialItemEnabled') && RGFormsModel::get_input_type($field) == 'select';
     if ($has_placeholder) {
         $choices[] = array('text' => rgar($field, 'categoryInitialItem'), 'value' => '', 'isSelected' => true);
     }
     if (rgar($field, "displayAllCategories")) {
         $args = array('hide_empty' => false, 'orderby' => 'name');
         $args = apply_filters("gform_post_category_args_{$field["id"]}", apply_filters("gform_post_category_args", $args, $field), $field);
         $terms = get_terms('category', $args);
         $walker = new GFCategoryWalker();
         $categories = $walker->walk($terms, 0, array(0));
         // 3rd parameter prevents notices triggered by $walker::display_element() function which checks $args[0]
         foreach ($categories as $category) {
             $selected = $value == $category->term_id || empty($value) && get_option('default_category') == $category->term_id && RGFormsModel::get_input_type($field) == 'select' && !$is_post && !$has_placeholder;
             $choices[] = array('text' => $category->name, 'value' => $category->term_id, 'isSelected' => $selected);
         }
     } else {
         $choices = array_merge($choices, $field['choices']);
     }
     if (empty($choices)) {
         $choices[] = array('text' => 'You must select at least one category.', 'value' => '');
     }
     $choice_number = 1;
     foreach ($choices as $choice) {
         if ($choice_number % 10 == 0) {
             //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10
             $choice_number++;
         }
         $input_id = $field["id"] . '.' . $choice_number;
         $inputs[] = array('id' => $input_id, 'label' => $choice['text'], 'name' => '');
         $choice_number++;
     }
     $field['choices'] = $choices;
     if (RGFormsModel::get_input_type($field) == 'checkbox') {
         $field['inputs'] = $inputs;
     }
     return $field;
 }
Beispiel #3
0
    public static function add_categories_as_choices($field, $value) {

        $choices = $inputs = array();
        $is_post = isset($_POST["gform_submit"]);
        $has_placeholder = rgar($field, 'categoryInitialItemEnabled') && RGFormsModel::get_input_type($field) == 'select';

        if($has_placeholder)
            $choices[] = array('text' => rgar($field, 'categoryInitialItem'), 'value' => '', 'isSelected' => true);

        $display_all = rgar($field, "displayAllCategories");

        $args = array('hide_empty' => false, 'orderby' => 'name');

        if(!$display_all){
            foreach($field["choices"] as $field_choice_to_include){
                $args["include"][] = $field_choice_to_include["value"];
            }
        }

        $args = apply_filters("gform_post_category_args_{$field["id"]}", apply_filters("gform_post_category_args", $args, $field), $field);
        $terms = get_terms('category', $args);

        $terms_copy = unserialize(serialize($terms)); // deep copy the terms to avoid repeating GFCategoryWalker on previously cached terms.
        $walker = new GFCategoryWalker();
        $categories = $walker->walk($terms_copy, 0, array(0)); // 3rd parameter prevents notices triggered by $walker::display_element() function which checks $args[0]

        foreach($categories as $category) {
            if($display_all) {
                $selected = $value == $category->term_id ||
                            (
                                empty($value) &&
                                get_option('default_category') == $category->term_id &&
                                RGFormsModel::get_input_type($field) == 'select' && // only preselect default category on select fields
                                !$is_post &&
                                !$has_placeholder
                            );
                $choices[] = array('text' => $category->name, 'value' => $category->term_id, 'isSelected' => $selected);
            } else {
                foreach($field["choices"] as $field_choice){
                    if($field_choice["value"] == $category->term_id){
                        $choices[] = array('text' => $category->name, 'value' => $category->term_id);
                        break;
                    }
                }
            }
        }

        if(empty($choices))
            $choices[] = array('text' => 'You must select at least one category.', 'value' => '');

        $choice_number = 1;
        foreach($choices as $choice) {

            if($choice_number % 10 == 0) //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10
                $choice_number++;

            $input_id = $field["id"] . '.' . $choice_number;
            $inputs[] = array('id' => $input_id, 'label' => $choice['text'], 'name' => '');
            $choice_number++;
        }

        $field['choices'] = $choices;

        $form_id = IS_ADMIN ? rgget("id") : rgar($field,"formId");

        $field['choices'] = apply_filters("gform_post_category_choices", $field["choices"], $field, $form_id);
        $field['choices'] = apply_filters("gform_post_category_choices_{$form_id}_{$field["id"]}", $field["choices"], $field, $form_id);

        if(RGFormsModel::get_input_type($field) == 'checkbox')
            $field['inputs'] = $inputs;

        return $field;
    }