Пример #1
0
    private static function get_conditional_logic($form, $field_values = array()){
        $logics = "";
        $dependents = "";
        $fields_with_logic = array();
        $default_values = array();

        foreach($form["fields"] as $field){

            //use section's logic if one exists
            $section = RGFormsModel::get_section($form, $field["id"]);
            $section_logic = !empty($section) ? rgar($section,"conditionalLogic") : null;

            $field_logic = $field["type"] != "page" ? RGForms::get("conditionalLogic", $field) : null; //page break conditional logic will be handled during the next button click

            $next_button_logic = isset($field["nextButton"]) && isset($field["nextButton"]["conditionalLogic"]) ? $field["nextButton"]["conditionalLogic"] : null;

            if(!empty($field_logic) || !empty($next_button_logic)){

                $field_section_logic = array("field" => $field_logic, "nextButton" => $next_button_logic, "section" => $section_logic);

                $logics .= $field["id"] . ": " . GFCommon::json_encode($field_section_logic) . ",";

                $fields_with_logic[] = $field["id"];

                $peers = $field["type"] == "section" ? GFCommon::get_section_fields($form, $field["id"]) : array($field);
                $peer_ids = array();

                foreach ($peers as $peer)
                    $peer_ids[] = $peer["id"];

                $dependents .= $field["id"] . ": " . GFCommon::json_encode($peer_ids) . ",";
            }

            //-- Saving default values so that they can be restored when toggling conditional logic ---
            $field_val = "";
            $input_type = RGFormsModel::get_input_type($field);

            //get parameter value if pre-populate is enabled
            if(rgar($field, "allowsPrepopulate")){
                if(is_array(rgar($field, "inputs"))){
                    $field_val = array();
                    foreach($field["inputs"] as $input){
                        $field_val["input_{$input["id"]}"] = RGFormsModel::get_parameter_value(rgar($input, "name"), $field_values, $field);
                    }
                }
                else if($input_type == "time"){
                    $parameter_val = RGFormsModel::get_parameter_value(rgar($field, "inputName"), $field_values, $field);
                    if(!empty($parameter_val) && preg_match('/^(\d*):(\d*) ?(.*)$/', $parameter_val, $matches)){
                        $field_val = array();
                        $field_val[] = esc_attr($matches[1]); //hour
                        $field_val[] = esc_attr($matches[2]); //minute
                        $field_val[] = rgar($matches,3); //am or pm
                    }
                }
                else if($input_type == "list"){
                    $parameter_val = RGFormsModel::get_parameter_value(rgar($field, "inputName"), $field_values, $field);
                    $field_val = is_array($parameter_val) ? $parameter_val : explode(",", str_replace("|", ",", $parameter_val));
                }
                else {
                    $field_val = RGFormsModel::get_parameter_value(rgar($field, "inputName"), $field_values, $field);
                }
            }

            //use default value if pre-populated value is empty
            $field_val = self::default_if_empty($field, $field_val);

            if(is_array(rgar($field, "choices")) && $input_type != "list"){

                //radio buttons start at 0 and checkboxes start at 1
                $choice_index = $input_type == "radio" ? 0 : 1;

                foreach($field["choices"] as $choice){

                    if(rgar($choice,"isSelected") && $input_type == "select"){
                        $val = isset($choice["price"]) ? $choice["value"] . "|" . GFCommon::to_number($choice["price"]) :  $choice["value"];
                        $default_values[$field["id"]] = $val;
                    }
                    else if(rgar($choice,"isSelected")){
                        if(!isset($default_values[$field["id"]]))
                            $default_values[$field["id"]] = array();

                        $default_values[$field["id"]][] = "choice_{$field["id"]}_{$choice_index}";
                    }
                    $choice_index++;
                }
            }
            else if(!empty($field_val)){

                if(GFFormsModel::get_input_type($field) == "date"){
                    //format date
                    $format = empty($field["dateFormat"]) ? "mdy" : esc_attr($field["dateFormat"]);
                    $date_info = GFcommon::parse_date($field_val, $format);
                    switch($format){
                        case "mdy":
                            $field_val = $date_info["month"] . "/" . $date_info["day"] . "/" . $date_info["year"];
                            break;
                        case "dmy":
                            $field_val = $date_info["day"] . "/" . $date_info["month"] . "/" . $date_info["year"];
                            break;
                        case "ymd":
                            $field_val = $date_info["year"] . "/" . $date_info["month"] . "/" . $date_info["day"];
                            break;
                    }
                }



                $default_values[$field["id"]] = $field_val;
            }
        }

        $button_conditional_script = "";

        //adding form button conditional logic if enabled
        if(isset($form["button"]["conditionalLogic"])){
            $logics .= "0: " . GFCommon::json_encode(array("field"=>$form["button"]["conditionalLogic"], "section" => null)) . ",";
            $dependents .= "0: " . GFCommon::json_encode(array(0)) . ",";
            $fields_with_logic[] = 0;

            $button_conditional_script = "jQuery('#gform_{$form['id']}').submit(" .
                                            "function(event, isButtonPress){" .
                                            "    var visibleButton = jQuery('.gform_next_button:visible, .gform_button:visible, .gform_image_button:visible');" .
                                            "    return visibleButton.length > 0 || isButtonPress == true;" .
                                            "}" .
                                        ");";
        }

        if(!empty($logics))
            $logics = substr($logics, 0, strlen($logics) - 1); //removing last comma;

        if(!empty($dependents))
            $dependents = substr($dependents, 0, strlen($dependents) - 1); //removing last comma;

        $animation = rgar($form,"enableAnimation") ? "1" : "0";
        global $wp_locale;
        $number_format = $wp_locale->number_format['decimal_point'] == "," ? "decimal_comma" : "decimal_dot";

        $str = "if(window['jQuery']){" .

                    "if(!window['gf_form_conditional_logic'])" .
                        "window['gf_form_conditional_logic'] = new Array();" .
                    "window['gf_form_conditional_logic'][{$form['id']}] = {'logic' : {" . $logics . " }, 'dependents' : {" . $dependents . " }, 'animation' : " . $animation . " , 'defaults' : " . json_encode($default_values) . " }; ".
                    "if(!window['gf_number_format'])" .
                        "window['gf_number_format'] = '" . $number_format . "';" .

                    "jQuery(document).ready(function(){" .
                        "gf_apply_rules({$form['id']}, " . json_encode($fields_with_logic) . ", true);" .
                        "jQuery('#gform_wrapper_{$form['id']}').show();" .
                        "jQuery(document).trigger('gform_post_conditional_logic', [{$form['id']}, null, true]);" .
                        $button_conditional_script .

                    "} );" .

                "} ";

        return $str;
    }
Пример #2
0
 /**
  * Get init script and all necessary data for conditional logic.
  *
  * @todo: Replace much of the field value retrieval with a get_original_value() method in GF_Field class.
  *
  * @param       $form
  * @param array $field_values
  *
  * @return string
  */
 private static function get_conditional_logic($form, $field_values = array())
 {
     $logics = '';
     $dependents = '';
     $fields_with_logic = array();
     $default_values = array();
     foreach ($form['fields'] as $field) {
         /* @var GF_Field $field */
         $field_deps = self::get_conditional_logic_fields($form, $field->id);
         $field_dependents[$field->id] = !empty($field_deps) ? $field_deps : array();
         //use section's logic if one exists
         $section = RGFormsModel::get_section($form, $field->id);
         $section_logic = !empty($section) ? rgar($section, 'conditionalLogic') : null;
         $field_logic = $field->type != 'page' ? $field->conditionalLogic : null;
         //page break conditional logic will be handled during the next button click
         $next_button_logic = !empty($field->nextButton) && !empty($field->nextButton['conditionalLogic']) ? $field->nextButton['conditionalLogic'] : null;
         if (!empty($field_logic) || !empty($next_button_logic)) {
             $field_section_logic = array('field' => $field_logic, 'nextButton' => $next_button_logic, 'section' => $section_logic);
             $logics .= $field->id . ': ' . GFCommon::json_encode($field_section_logic) . ',';
             $fields_with_logic[] = $field->id;
             $peers = $field->type == 'section' ? GFCommon::get_section_fields($form, $field->id) : array($field);
             $peer_ids = array();
             foreach ($peers as $peer) {
                 $peer_ids[] = $peer->id;
             }
             $dependents .= $field->id . ': ' . GFCommon::json_encode($peer_ids) . ',';
         }
         //-- Saving default values so that they can be restored when toggling conditional logic ---
         $field_val = '';
         $input_type = $field->get_input_type();
         $inputs = $field->get_entry_inputs();
         //get parameter value if pre-populate is enabled
         if ($field->allowsPrepopulate) {
             if ($input_type == 'checkbox') {
                 $field_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                 if (!is_array($field_val)) {
                     $field_val = explode(',', $field_val);
                 }
             } elseif (is_array($inputs)) {
                 $field_val = array();
                 foreach ($inputs as $input) {
                     $field_val["input_{$input['id']}"] = RGFormsModel::get_parameter_value(rgar($input, 'name'), $field_values, $field);
                 }
             } elseif ($input_type == 'time') {
                 // maintained for backwards compatibility. The Time field now has an inputs array.
                 $parameter_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                 if (!empty($parameter_val) && preg_match('/^(\\d*):(\\d*) ?(.*)$/', $parameter_val, $matches)) {
                     $field_val = array();
                     $field_val[] = esc_attr($matches[1]);
                     //hour
                     $field_val[] = esc_attr($matches[2]);
                     //minute
                     $field_val[] = rgar($matches, 3);
                     //am or pm
                 }
             } elseif ($input_type == 'list') {
                 $parameter_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                 $field_val = is_array($parameter_val) ? $parameter_val : explode(',', str_replace('|', ',', $parameter_val));
                 if (is_array(rgar($field_val, 0))) {
                     $list_values = array();
                     foreach ($field_val as $row) {
                         $list_values = array_merge($list_values, array_values($row));
                     }
                     $field_val = $list_values;
                 }
             } else {
                 $field_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
             }
         }
         //use default value if pre-populated value is empty
         $field_val = $field->get_value_default_if_empty($field_val);
         if (is_array($field->choices) && $input_type != 'list') {
             //radio buttons start at 0 and checkboxes start at 1
             $choice_index = $input_type == 'radio' ? 0 : 1;
             $is_pricing_field = GFCommon::is_pricing_field($field->type);
             foreach ($field->choices as $choice) {
                 if ($input_type == 'checkbox' && $choice_index % 10 == 0) {
                     $choice_index++;
                 }
                 $is_prepopulated = is_array($field_val) ? in_array($choice['value'], $field_val) : $choice['value'] == $field_val;
                 $is_choice_selected = rgar($choice, 'isSelected') || $is_prepopulated;
                 if ($is_choice_selected && $input_type == 'select') {
                     $price = GFCommon::to_number(rgar($choice, 'price')) == false ? 0 : GFCommon::to_number(rgar($choice, 'price'));
                     $val = $is_pricing_field && $field->type != 'quantity' ? $choice['value'] . '|' . $price : $choice['value'];
                     $default_values[$field->id] = $val;
                 } elseif ($is_choice_selected) {
                     if (!isset($default_values[$field->id])) {
                         $default_values[$field->id] = array();
                     }
                     $default_values[$field->id][] = "choice_{$form['id']}_{$field->id}_{$choice_index}";
                 }
                 $choice_index++;
             }
         } elseif (!empty($field_val)) {
             switch ($input_type) {
                 case 'date':
                     // for date fields; that are multi-input; and where the field value is a string
                     // (happens with prepop, default value will always be an array for multi-input date fields)
                     if (is_array($field->inputs) && (!is_array($field_val) || !isset($field_val['m']))) {
                         $format = empty($field->dateFormat) ? 'mdy' : esc_attr($field->dateFormat);
                         $date_info = GFcommon::parse_date($field_val, $format);
                         // converts date to array( 'm' => 1, 'd' => '13', 'y' => '1987' )
                         $field_val = $field->get_date_array_by_format(array($date_info['month'], $date_info['day'], $date_info['year']));
                     }
                     break;
                 case 'time':
                     if (is_array($field_val)) {
                         $ampm_key = key(array_slice($field_val, -1, 1, true));
                         $field_val[$ampm_key] = strtolower($field_val[$ampm_key]);
                     }
                     break;
                 case 'address':
                     $state_input_id = sprintf('%s.4', $field->id);
                     if (isset($field_val[$state_input_id]) && !$field_val[$state_input_id]) {
                         $field_val[$state_input_id] = $field->defaultState;
                     }
                     $country_input_id = sprintf('%s.6', $field->id);
                     if (isset($field_val[$country_input_id]) && !$field_val[$country_input_id]) {
                         $field_val[$country_input_id] = $field->defaultCountry;
                     }
                     break;
             }
             $default_values[$field->id] = $field_val;
         }
     }
     $button_conditional_script = '';
     //adding form button conditional logic if enabled
     if (isset($form['button']['conditionalLogic'])) {
         $logics .= '0: ' . GFCommon::json_encode(array('field' => $form['button']['conditionalLogic'], 'section' => null)) . ',';
         $dependents .= '0: ' . GFCommon::json_encode(array(0)) . ',';
         $fields_with_logic[] = 0;
         $button_conditional_script = "jQuery('#gform_{$form['id']}').submit(" . 'function(event, isButtonPress){' . '    var visibleButton = jQuery(".gform_next_button:visible, .gform_button:visible, .gform_image_button:visible");' . '    return visibleButton.length > 0 || isButtonPress == true;' . '}' . ');';
     }
     if (!empty($logics)) {
         $logics = substr($logics, 0, strlen($logics) - 1);
     }
     //removing last comma;
     if (!empty($dependents)) {
         $dependents = substr($dependents, 0, strlen($dependents) - 1);
     }
     //removing last comma;
     $animation = rgar($form, 'enableAnimation') ? '1' : '0';
     global $wp_locale;
     $number_format = $wp_locale->number_format['decimal_point'] == ',' ? 'decimal_comma' : 'decimal_dot';
     $str = "if(window['jQuery']){" . "if(!window['gf_form_conditional_logic'])" . "window['gf_form_conditional_logic'] = new Array();" . "window['gf_form_conditional_logic'][{$form['id']}] = { logic: { {$logics} }, dependents: { {$dependents} }, animation: {$animation}, defaults: " . json_encode($default_values) . ", fields: " . json_encode($field_dependents) . " }; " . "if(!window['gf_number_format'])" . "window['gf_number_format'] = '" . $number_format . "';" . 'jQuery(document).ready(function(){' . "gf_apply_rules({$form['id']}, " . json_encode($fields_with_logic) . ', true);' . "jQuery('#gform_wrapper_{$form['id']}').show();" . "jQuery(document).trigger('gform_post_conditional_logic', [{$form['id']}, null, true]);" . $button_conditional_script . '} );' . '} ';
     return $str;
 }
Пример #3
0
 private static function get_conditional_logic($form, $field_values = array())
 {
     $logics = '';
     $dependents = '';
     $fields_with_logic = array();
     $default_values = array();
     foreach ($form['fields'] as $field) {
         /* @var GF_Field $field */
         //use section's logic if one exists
         $section = RGFormsModel::get_section($form, $field->id);
         $section_logic = !empty($section) ? rgar($section, 'conditionalLogic') : null;
         $field_logic = $field->type != 'page' ? $field->conditionalLogic : null;
         //page break conditional logic will be handled during the next button click
         $next_button_logic = !empty($field->nextButton) && !empty($field->nextButton['conditionalLogic']) ? $field->nextButton['conditionalLogic'] : null;
         if (!empty($field_logic) || !empty($next_button_logic)) {
             $field_section_logic = array('field' => $field_logic, 'nextButton' => $next_button_logic, 'section' => $section_logic);
             $logics .= $field->id . ': ' . GFCommon::json_encode($field_section_logic) . ',';
             $fields_with_logic[] = $field->id;
             $peers = $field->type == 'section' ? GFCommon::get_section_fields($form, $field->id) : array($field);
             $peer_ids = array();
             foreach ($peers as $peer) {
                 $peer_ids[] = $peer->id;
             }
             $dependents .= $field->id . ': ' . GFCommon::json_encode($peer_ids) . ',';
         }
         //-- Saving default values so that they can be restored when toggling conditional logic ---
         $field_val = '';
         $input_type = RGFormsModel::get_input_type($field);
         //get parameter value if pre-populate is enabled
         if ($field->allowsPrepopulate) {
             if (is_array($field->inputs)) {
                 $field_val = array();
                 foreach ($field->inputs as $input) {
                     $field_val["input_{$input['id']}"] = RGFormsModel::get_parameter_value(rgar($input, 'name'), $field_values, $field);
                 }
             } else {
                 if ($input_type == 'time') {
                     // maintained for backwards compatibility. The Time field now has an inputs array.
                     $parameter_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                     if (!empty($parameter_val) && preg_match('/^(\\d*):(\\d*) ?(.*)$/', $parameter_val, $matches)) {
                         $field_val = array();
                         $field_val[] = esc_attr($matches[1]);
                         //hour
                         $field_val[] = esc_attr($matches[2]);
                         //minute
                         $field_val[] = rgar($matches, 3);
                         //am or pm
                     }
                 } else {
                     if ($input_type == 'list') {
                         $parameter_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                         $field_val = is_array($parameter_val) ? $parameter_val : explode(',', str_replace('|', ',', $parameter_val));
                     } else {
                         $field_val = RGFormsModel::get_parameter_value($field->inputName, $field_values, $field);
                     }
                 }
             }
         }
         //use default value if pre-populated value is empty
         $field_val = $field->get_value_default_if_empty($field_val);
         if (is_array($field->choices) && $input_type != 'list') {
             //radio buttons start at 0 and checkboxes start at 1
             $choice_index = $input_type == 'radio' ? 0 : 1;
             $is_pricing_field = GFCommon::is_pricing_field($field->type);
             foreach ($field->choices as $choice) {
                 if ($input_type == 'checkbox' && $choice_index % 10 == 0) {
                     $choice_index++;
                 }
                 if (rgar($choice, 'isSelected') && $input_type == 'select') {
                     $price = GFCommon::to_number(rgar($choice, 'price')) == false ? 0 : GFCommon::to_number(rgar($choice, 'price'));
                     $val = $is_pricing_field && $field->type != 'quantity' ? $choice['value'] . '|' . $price : $choice['value'];
                     $default_values[$field->id] = $val;
                 } else {
                     if (rgar($choice, 'isSelected')) {
                         if (!isset($default_values[$field->id])) {
                             $default_values[$field->id] = array();
                         }
                         $default_values[$field->id][] = "choice_{$form['id']}_{$field->id}_{$choice_index}";
                     }
                 }
                 $choice_index++;
             }
         } else {
             if (!empty($field_val)) {
                 if (GFFormsModel::get_input_type($field) == 'date') {
                     //format date
                     $format = empty($field->dateFormat) ? 'mdy' : esc_attr($field->dateFormat);
                     $date_info = GFcommon::parse_date($field_val, $format);
                     switch ($format) {
                         case 'mdy':
                             $field_val = $date_info['month'] . '/' . $date_info['day'] . '/' . $date_info['year'];
                             break;
                         case 'dmy':
                             $field_val = $date_info['day'] . '/' . $date_info['month'] . '/' . $date_info['year'];
                             break;
                         case 'ymd':
                             $field_val = $date_info['year'] . '/' . $date_info['month'] . '/' . $date_info['day'];
                             break;
                     }
                 }
                 $default_values[$field->id] = $field_val;
             }
         }
     }
     $button_conditional_script = '';
     //adding form button conditional logic if enabled
     if (isset($form['button']['conditionalLogic'])) {
         $logics .= '0: ' . GFCommon::json_encode(array('field' => $form['button']['conditionalLogic'], 'section' => null)) . ',';
         $dependents .= '0: ' . GFCommon::json_encode(array(0)) . ',';
         $fields_with_logic[] = 0;
         $button_conditional_script = "jQuery('#gform_{$form['id']}').submit(" . "function(event, isButtonPress){" . "    var visibleButton = jQuery('.gform_next_button:visible, .gform_button:visible, .gform_image_button:visible');" . "    return visibleButton.length > 0 || isButtonPress == true;" . "}" . ");";
     }
     if (!empty($logics)) {
         $logics = substr($logics, 0, strlen($logics) - 1);
     }
     //removing last comma;
     if (!empty($dependents)) {
         $dependents = substr($dependents, 0, strlen($dependents) - 1);
     }
     //removing last comma;
     $animation = rgar($form, 'enableAnimation') ? '1' : '0';
     global $wp_locale;
     $number_format = $wp_locale->number_format['decimal_point'] == ',' ? 'decimal_comma' : 'decimal_dot';
     $str = "if(window['jQuery']){" . "if(!window['gf_form_conditional_logic'])" . "window['gf_form_conditional_logic'] = new Array();" . "window['gf_form_conditional_logic'][{$form['id']}] = {'logic' : {" . $logics . " }, 'dependents' : {" . $dependents . " }, 'animation' : " . $animation . " , 'defaults' : " . json_encode($default_values) . ' }; ' . "if(!window['gf_number_format'])" . "window['gf_number_format'] = '" . $number_format . "';" . "jQuery(document).ready(function(){" . "gf_apply_rules({$form['id']}, " . json_encode($fields_with_logic) . ', true);' . "jQuery('#gform_wrapper_{$form['id']}').show();" . "jQuery(document).trigger('gform_post_conditional_logic', [{$form['id']}, null, true]);" . $button_conditional_script . "} );" . "} ";
     return $str;
 }