Last active
October 20, 2021 10:14
-
-
Save spivurno/3708820 to your computer and use it in GitHub Desktop.
Gravity Wiz // Format Fields as Currency via jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Format Fields as Currency via jQuery | |
| * http://gravitywiz.com/format-field-as-currency/ | |
| */ | |
| class GWFormatMoney { | |
| /** | |
| * Stores a copy of the original field values so once the money-formatted numbers have been cleaned for validation | |
| * they can be restored after being validated. | |
| * | |
| */ | |
| var $_field_values = array(); | |
| function __construct() { | |
| add_filter( 'gform_pre_render', array( $this, 'format_money_js' ) ); | |
| add_filter( 'gform_pre_validation', array( $this, 'clean_value_for_number_fields' ) ); | |
| add_filter( 'gform_validation', array( $this, 'restore_value_for_number_fields' ) ); | |
| add_action( 'gform_get_field_value', array( $this, 'format_money' ), 10, 3 ); | |
| } | |
| function format_money_js($form) { | |
| $this->enqueue_gravityforms_js(); | |
| $script = '(function($){' . | |
| '$(".gf_money input").each( function() { ' . | |
| '$(this).val( gformFormatMoney( $(this).val() ) ); ' . | |
| '}).change( function( event ) { ' . | |
| '$(this).val( gformFormatMoney( $(this).val() ) ); ' . | |
| '}); ' . | |
| 'gform.addFilter( "gform_calculation_format_result", "gwMaybeFormatMoney" ); ' . | |
| 'window.gwMaybeFormatMoney = function( returnVal, origResult, formulaField, formId ) { ' . | |
| 'var field = jQuery("#field_" + formId + "_" + formulaField.field_id); ' . | |
| 'return field.hasClass( "gf_money" ) ? gformFormatMoney( origResult ) : returnVal; ' . | |
| '} ' . | |
| '})(jQuery);'; | |
| GFFormDisplay::add_init_script( $form['id'], 'format_money', GFFormDisplay::ON_PAGE_RENDER, $script ); | |
| return $form; | |
| } | |
| function format_money( $value, $lead, $field ) { | |
| $css_classes = explode( ' ', rgar( $field, 'cssClass' ) ); | |
| if( ! in_array( 'gf_money', $css_classes ) ) | |
| return $value; | |
| require_once( GFCommon::get_base_path() . '/currency.php' ); | |
| $currency = new RGCurrency( GFCommon::get_currency() ); | |
| $value = $currency->to_money( $value ); | |
| return $value; | |
| } | |
| function clean_value_for_number_fields( $form ) { | |
| foreach( $form['fields'] as $field ) { | |
| $is_number_field = GFFormsModel::get_input_type( $field ) == 'number'; | |
| $has_gf_money_class = in_array( 'gf_money', explode( ' ', rgar( $field, 'cssClass' ) ) ); | |
| if( ! $is_number_field || ! $has_gf_money_class ) | |
| continue; | |
| $value = $_POST["input_{$field['id']}"]; | |
| if( ! $value ) | |
| continue; | |
| // save original value to be restored later | |
| $this->_field_values[$field['id']] = $value; | |
| require_once(GFCommon::get_base_path() . '/currency.php'); | |
| $currency = new RGCurrency(GFCommon::get_currency()); | |
| $_POST["input_{$field['id']}"] = $currency->to_number( $value ); | |
| } | |
| return $form; | |
| } | |
| function restore_value_for_number_fields( $validation_result ) { | |
| foreach( $validation_result['form']['fields'] as $field ) { | |
| if( ! isset( $this->_field_values[$field['id']] ) ) | |
| continue; | |
| $value = $this->_field_values[$field['id']]; | |
| // @REMOVE: since the original value will already be in the money format, no need to reformat here | |
| // leaving in place for a few weeks to ensure no issues come up with the removal | |
| // require_once(GFCommon::get_base_path() . '/currency.php'); | |
| // $currency = new RGCurrency(GFCommon::get_currency()); | |
| // $currency->to_money( $value ); | |
| $_POST["input_{$field['id']}"] = $value; | |
| } | |
| return $validation_result; | |
| } | |
| function enqueue_gravityforms_js() { | |
| $has_proper_support = version_compare( GFCommon::$version, '1.8.dev4', '>=' ); | |
| $enqueue_script = $has_proper_support ? 'gform_gravityforms' : 'gforms_gravityforms'; | |
| if( ! wp_script_is( $enqueue_script ) ) | |
| wp_enqueue_script( $enqueue_script ); | |
| } | |
| } | |
| new GWFormatMoney(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment