Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpnf-override-parent-merge-tag-on-submission.php: Fixed an issue with parent merge tags not updating Time value on entry edit. #905

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion gp-nested-forms/gpnf-override-parent-merge-tag-on-submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,16 @@ function override_parent_merge_tags( $entry, $form ) {
}

foreach ( $inputs as $input ) {
$this->override_child_entry_input_value( $entry, $field, $child_form, $input['id'], rgar( $input, 'defaultValue' ) );
switch ( $child_field->type ) {
case 'time':
$default_value = preg_replace( '/(\d+)\.\d+/', '$1', rgar( $child_field['inputs'][0], 'defaultValue' ) );
break;
default:
$default_value = rgar( $input, 'defaultValue' );
break;
}

$this->override_child_entry_input_value( $entry, $field, $child_form, $input['id'], $default_value );
}
}
}
Expand All @@ -122,9 +131,40 @@ function override_child_entry_input_value( $entry, $field, $child_form, $input_i
$child_entry = GFAPI::get_entry( $child_entry_id );
$value = GFCommon::replace_variables( $default_value, $child_form, $child_entry );
GFAPI::update_entry_field( $child_entry_id, $input_id, $value );

// If because of the field update, any formula evaluation should be done on the entry.
$this->reprocess_form_calculations( $child_entry_id );
}

}

function reprocess_form_calculations( $entry_id ) {
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $entry['form_id'] );

foreach ( $form['fields'] as $field ) {
// Only process calculation fields
if ( $field->enableCalculation && $field->calculationFormula ) {
$field_id = $field->id;

// Retrieve the formula for this field and process. First check for GPDTC calculations because of ':' calculations.
if ( is_callable( array( gp_date_time_calculator(), 'modify_calculation_formula' ) ) ) {
$formula = gp_date_time_calculator()->modify_calculation_formula( $field->calculationFormula, $field, $form, $entry );
}

// Process any other filter customization over it.
$formula = apply_filters( 'gform_calculation_formula', $formula, $form, $field, $entry );

// Process/Calculate the formula
$parsed_formula = GFCommon::replace_variables( $formula, $form, $entry, false, false, false, 'text' );
// phpcs:ignore Squiz.PHP.Eval.Discouraged
$calculated_value = eval( 'return ' . $parsed_formula . ';' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly callign the eval seems suspicious. We should run that through existing logic.

Here's how we recalc with another snippet: https://github.com/gravitywiz/snippet-library/blob/master/experimental/gpdtc-recalc.php


// Update the entry with the recalculated value
GFAPI::update_entry_field( $entry_id, $field_id, $calculated_value );
}
}
}
}

# -------------------------------------------------
Expand Down
Loading