• 1.
    /**
  • 2.
     * Gravity Wiz // Gravity Forms // Rounding by Increment
  • 3.
     *
  • 4.
     * Provides a variety of rounding functions for Gravity Form Number fields powered by the CSS class setting for each field. Functions include:
  • 5.
     *
  • 6.
     *  + rounding to an increment            (i.e. increment of 100 would round 1 to 100, 149 to 100, 150 to 200, etc) | class: 'gw-round-100'
  • 7.
     *  + rounding up by an increment         (i.e. increment of 50 would round 1 to 50, 51 to 100, 149 to 150, etc)    | class: 'gw-round-up-50'
  • 8.
     *  + rounding down by an increment       (i.e. increment of 25 would round 1 to 0, 26 to 25, 51 to 50, etc)        | class: 'gw-round-down-25'
  • 9.
     *  + rounding up to a specific minimum   (i.e. min of 50 would round 1 to 50, 51 and greater would not be rounded) | class: 'gw-round-min-50'
  • 10.
     *  + rounding down to a specific maximum (i.e. max of 25 would round 26 to 25, 25 and below would not be rounded)  | class: 'gw-round-max-25'
  • 11.
     *
  • 12.
     * @version 1.11
  • 13.
     * @author  David Smith <[email protected]>
  • 14.
     * @license GPL-2.0+
  • 15.
     * @link    http://gravitywiz.com/rounding-increments-gravity-forms/
  • 16.
     *
  • 17.
     * Plugin Name:  Gravity Forms Rounding
  • 18.
     * Plugin URI:   http://gravitywiz.com/rounding-increments-gravity-forms/
  • 19.
     * Description:  Round your field values (including calculations) up, down, by an increment, or to a specific minimum or maximum value.
  • 20.
     * Author:       Gravity Wiz
  • 21.
     * Version:      1.11
  • 22.
     * Author URI:   http://gravitywiz.com
  • 23.
     */
  • 24.
    class GW_Rounding {
  • 25.
     
  • 26.
    	private static $instance = null;
  • 27.
     
  • 28.
    	protected static $is_script_output = false;
  • 29.
     
  • 30.
    	protected $class_regex = 'gw-round-(\w+)-?(\w+)?';
  • 31.
     
  • 32.
    	public static function get_instance() {
  • 33.
    		if( null == self::$instance )
  • 34.
    			self::$instance = new self;
  • 35.
    		return self::$instance;
  • 36.
    	}
  • 37.
     
  • 38.
    	private function __construct( $args = array() ) {
  • 39.
     
  • 40.
    		// make sure we're running the required minimum version of Gravity Forms
  • 41.
    		if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) )
  • 42.
    			return;
  • 43.
     
  • 44.
    		// time for hooks
  • 45.
    		add_filter( 'gform_pre_render',            array( $this, 'prepare_form_and_load_script' ), 10, 2 );
  • 46.
    		add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );
  • 47.
    		add_filter( 'gform_enqueue_scripts',       array( $this, 'enqueue_form_scripts' ) );
  • 48.
     
  • 49.
    		add_action( 'gform_pre_submission',     array( $this, 'override_submitted_value' ), 10, 5 );
  • 50.
    		add_filter( 'gform_calculation_result', array( $this, 'override_submitted_calculation_value' ), 10, 5 );
  • 51.
     
  • 52.
    	}
  • 53.
     
  • 54.
    	public function prepare_form_and_load_script( $form, $is_ajax_enabled ) {
  • 55.
     
  • 56.
    		if( ! $this->is_applicable_form( $form ) ) {
  • 57.
    			return $form;
  • 58.
    		}
  • 59.
     
  • 60.
    		if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
  • 61.
    			add_action( 'wp_footer',            array( $this, 'output_script' ) );
  • 62.
    			add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
  • 63.
    			add_action( 'admin_footer',         array( $this, 'output_script' ) );
  • 64.
    		}
  • 65.
     
  • 66.
    		foreach( $form['fields'] as &$field ) {
  • 67.
    			if( preg_match( $this->get_class_regex(), $field['cssClass'] ) ) {
  • 68.
    				$field['cssClass'] .= ' gw-rounding';
  • 69.
    			}
  • 70.
    		}
  • 71.
     
  • 72.
    		return $form;
  • 73.
    	}
  • 74.
     
  • 75.
    	public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
  • 76.
    		return class_exists( 'GFFormDisplay' ) && isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled;
  • 77.
    	}
  • 78.
     
  • 79.
    	function output_script() {
  • 80.
    		?>
  • 81.
     
  • 82.
    		<script type="text/javascript">
  • 83.
     
  • 84.
    			var GWRounding;
  • 85.
     
  • 86.
    			( function( $ ) {
  • 87.
     
  • 88.
    				GWRounding = function( args ) {
  • 89.
     
  • 90.
    					var self = this;
  • 91.
     
  • 92.
    					// copy all args to current object: (list expected props)
  • 93.
    					for( prop in args ) {
  • 94.
    						if( args.hasOwnProperty( prop ) )
  • 95.
    							self[prop] = args[prop];
  • 96.
    					}
  • 97.
     
  • 98.
    					self.init = function() {
  • 99.
     
  • 100.
    						self.fieldElems = $( '#gform_wrapper_' + self.formId + ' .gw-rounding' );
  • 101.
     
  • 102.
    						self.parseElemActions( self.fieldElems );
  • 103.
     
  • 104.
    						self.bindEvents();
  • 105.
     
  • 106.
    					};
  • 107.
     
  • 108.
    					self.parseElemActions = function( elems ) {
  • 109.
     
  • 110.
    						elems.each( function() {
  • 111.
     
  • 112.
    							var cssClasses      = $( this ).attr( 'class' ),
  • 113.
    								roundingActions = self.parseActions( cssClasses );
  • 114.
     
  • 115.
    							$( this ).data( 'gw-rounding', roundingActions );
  • 116.
     
  • 117.
    						} );
  • 118.
     
  • 119.
    					};
  • 120.
     
  • 121.
    					self.parseActions = function( str ) {
  • 122.
     
  • 123.
    						var matches         = getMatchGroups( String( str ), new RegExp( self.classRegex.replace( /\\/g, '\\' ), 'i' ) ),
  • 124.
    							roundingActions = [];
  • 125.
     
  • 126.
    						for( var i = 0; i < matches.length; i++ ) {
  • 127.
     
  • 128.
    							var action      = matches[i][1],
  • 129.
    								actionValue = matches[i][2];
  • 130.
     
  • 131.
    							if( typeof actionValue == 'undefined' ) {
  • 132.
    								actionValue = action;
  • 133.
    								action = 'round';
  • 134.
    							}
  • 135.
     
  • 136.
    							var roundingAction = {
  • 137.
    								'action':      action,
  • 138.
    								'actionValue': actionValue
  • 139.
    							};
  • 140.
     
  • 141.
    							roundingActions.push( roundingAction );
  • 142.
     
  • 143.
    						}
  • 144.
     
  • 145.
    						return roundingActions;
  • 146.
    					};
  • 147.
     
  • 148.
    					self.bindEvents = function() {
  • 149.
     
  • 150.
    						self.fieldElems.each( function() {
  • 151.
     
  • 152.
    							var $targets;
  • 153.
     
  • 154.
    							// Attempt to target only the quantity input of a Single Product field.
  • 155.
    							if( $( this ).hasClass( 'gfield_price' ) ) {
  • 156.
    								$targets = $( this ).find( '.ginput_quantity' );
  • 157.
    							}
  • 158.
     
  • 159.
    							// Otherwise, target all inputs of the given field.
  • 160.
    							if( ! $targets.length ) {
  • 161.
    								$targets = $( this ).find( 'inputs' );
  • 162.
    							}
  • 163.
     
  • 164.
    							$targets.each( function() {
  • 165.
    								self.applyRoundingActions( $( this ) );
  • 166.
    							} ).blur( function() {
  • 167.
    								self.applyRoundingActions( $( this ) );
  • 168.
    							} );
  • 169.
     
  • 170.
    						} );
  • 171.
     
  • 172.
    						gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) {
  • 173.
     
  • 174.
    							var $input = $( '#input_' + formId + '_' + formulaField.field_id )
  • 175.
    							$field = $input.parents( '.gfield' );
  • 176.
     
  • 177.
    							if( $field.hasClass( 'gw-rounding' ) ) {
  • 178.
    								result = self.getRoundedValue( $input, result );
  • 179.
    							}
  • 180.
     
  • 181.
    							return result;
  • 182.
    						} );
  • 183.
     
  • 184.
    					};
  • 185.
     
  • 186.
    					self.applyRoundingActions = function( $input ) {
  • 187.
    						var value = self.getRoundedValue( $input );
  • 188.
    						if( $input.val() != value ) {
  • 189.
    							$input.val( value ).change();
  • 190.
    						}
  • 191.
    					};
  • 192.
     
  • 193.
    					self.getRoundedValue = function( $input, value ) {
  • 194.
     
  • 195.
    						var $field  = $input.parents( '.gfield' ),
  • 196.
    							actions = $field.data( 'gw-rounding' );
  • 197.
     
  • 198.
    						// allows setting the 'gw-rounding' data for an element to null and it will be reparsed
  • 199.
    						if( actions === null ) {
  • 200.
    							self.parseElemActions( $field );
  • 201.
    							actions = $field.data( 'gw-rounding' );
  • 202.
    						}
  • 203.
     
  • 204.
    						if( typeof actions == 'undefined' || actions === false || actions.length <= 0 ) {
  • 205.
    							return;
  • 206.
    						}
  • 207.
     
  • 208.
    						if( typeof value == 'undefined' ) {
  • 209.
     
  • 210.
    							value = gformToNumber( $input.val() );
  • 211.
     
  • 212.
    							var currency = new Currency(gf_global.gf_currency_config);
  • 213.
    							value = gformCleanNumber( value, currency.currency.symbol_right, currency.currency.symbol_left, currency.currency.decimal_separator );
  • 214.
     
  • 215.
    						}
  • 216.
     
  • 217.
    						if( value != '' ) {
  • 218.
    							for( var i = 0; i < actions.length; i++ ) {
  • 219.
    								value = GWRounding.round( value, actions[i].actionValue, actions[i].action );
  • 220.
    							}
  • 221.
    						}
  • 222.
     
  • 223.
    						return isNaN( value ) ? '' : value;
  • 224.
    					};
  • 225.
     
  • 226.
    					GWRounding.round = function( value, actionValue, action ) {
  • 227.
     
  • 228.
    						var interval, base, min, max;
  • 229.
     
  • 230.
    						value = parseFloat( value );
  • 231.
    						actionValue = parseInt( actionValue );
  • 232.
     
  • 233.
    						switch( action ) {
  • 234.
    							case 'min':
  • 235.
    								min = actionValue;
  • 236.
    								if( value < min ) {
  • 237.
    									value = min;
  • 238.
    								}
  • 239.
    								break;
  • 240.
    							case 'max':
  • 241.
    								max = actionValue;
  • 242.
    								if( value > max ) {
  • 243.
    									value = max;
  • 244.
    								}
  • 245.
    								break;
  • 246.
    							case 'up':
  • 247.
    								interval = actionValue;
  • 248.
    								base     = Math.ceil( value / interval );
  • 249.
    								value    = base * interval;
  • 250.
    								break;
  • 251.
    							case 'down':
  • 252.
    								interval = actionValue;
  • 253.
    								base     = Math.floor( value / interval );
  • 254.
    								value    = base * interval;
  • 255.
    								break;
  • 256.
    							default:
  • 257.
    								interval = actionValue;
  • 258.
    								base     = Math.round( value / interval );
  • 259.
    								value    = base * interval;
  • 260.
    								break;
  • 261.
    						}
  • 262.
     
  • 263.
    						return parseInt( value );
  • 264.
    					};
  • 265.
     
  • 266.
    					self.init();
  • 267.
     
  • 268.
    				}
  • 269.
     
  • 270.
    			} )( jQuery );
  • 271.
     
  • 272.
    		</script>
  • 273.
     
  • 274.
    		<?php
  • 275.
    	}
  • 276.
     
  • 277.
    	function add_init_script( $form ) {
  • 278.
     
  • 279.
    		if( ! $this->is_applicable_form( $form ) ) {
  • 280.
    			return;
  • 281.
    		}
  • 282.
     
  • 283.
    		$args = array(
  • 284.
    			'formId'    => $form['id'],
  • 285.
    			'classRegex' => $this->class_regex
  • 286.
    		);
  • 287.
    		$script = 'new GWRounding( ' . json_encode( $args ) . ' );';
  • 288.
     
  • 289.
    		GFFormDisplay::add_init_script( $form['id'], 'gw_rounding', GFFormDisplay::ON_PAGE_RENDER, $script );
  • 290.
     
  • 291.
    	}
  • 292.
     
  • 293.
    	function enqueue_form_scripts( $form ) {
  • 294.
     
  • 295.
    		if( $this->is_applicable_form( $form ) ) {
  • 296.
    			wp_enqueue_script( 'gform_gravityforms' );
  • 297.
    		}
  • 298.
     
  • 299.
    	}
  • 300.
     
  • 301.
    	function override_submitted_value( $form ) {
  • 302.
     
  • 303.
    		foreach( $form['fields'] as $field ) {
  • 304.
    			if( $this->is_applicable_field( $field ) && ! is_a( $field, 'GF_Field_Calculation' ) ) {
  • 305.
     
  • 306.
    				$value = rgpost( "input_{$field['id']}" );
  • 307.
     
  • 308.
    				$is_currency = $field->get_input_type() == 'number' && $field->numberFormat == 'currency';
  • 309.
    				if( $is_currency ) {
  • 310.
    					$value = GFCommon::to_number( $value );
  • 311.
    				}
  • 312.
     
  • 313.
    				$value = $this->process_rounding_actions( $value, $this->get_rounding_actions( $field ) );
  • 314.
     
  • 315.
    				$_POST[ "input_{$field['id']}" ] = $value;
  • 316.
     
  • 317.
    			}
  • 318.
    		}
  • 319.
     
  • 320.
    	}
  • 321.
     
  • 322.
    	function override_submitted_calculation_value( $result, $formula, $field, $form, $entry ) {
  • 323.
     
  • 324.
    		if( $this->is_applicable_field( $field ) ) {
  • 325.
    			$result = $this->process_rounding_actions( $result, $this->get_rounding_actions( $field ) );
  • 326.
    		}
  • 327.
     
  • 328.
    		return $result;
  • 329.
    	}
  • 330.
     
  • 331.
    	function process_rounding_actions( $value, $actions ) {
  • 332.
     
  • 333.
    		foreach( $actions as $action ) {
  • 334.
    			$value = $this->round( $value, $action['action'], $action['action_value'] );
  • 335.
    		}
  • 336.
     
  • 337.
    		return $value;
  • 338.
    	}
  • 339.
     
  • 340.
    	function round( $value, $action, $action_value ) {
  • 341.
     
  • 342.
    		$value = floatval( $value );
  • 343.
    		$action_value = intval( $action_value );
  • 344.
     
  • 345.
    		switch( $action ) {
  • 346.
    			case 'min':
  • 347.
    				$min = $action_value;
  • 348.
    				if( $value < $min ) {
  • 349.
    					$value = $min;
  • 350.
    				}
  • 351.
    				break;
  • 352.
    			case 'max':
  • 353.
    				$max = $action_value;
  • 354.
    				if( $value > $max ) {
  • 355.
    					$value = $max;
  • 356.
    				}
  • 357.
    				break;
  • 358.
    			case 'up':
  • 359.
    				$interval = $action_value;
  • 360.
    				$base     = ceil( $value / $interval );
  • 361.
    				$value    = $base * $interval;
  • 362.
    				break;
  • 363.
    			case 'down':
  • 364.
    				$interval = $action_value;
  • 365.
    				$base     = floor( $value / $interval );
  • 366.
    				$value    = $base * $interval;
  • 367.
    				break;
  • 368.
    			default:
  • 369.
    				$interval = $action_value;
  • 370.
    				$base     = round( $value / $interval );
  • 371.
    				$value    = $base * $interval;
  • 372.
    				break;
  • 373.
    		}
  • 374.
     
  • 375.
    		return intval( $value );
  • 376.
    	}
  • 377.
     
  • 378.
    	// # HELPERS
  • 379.
     
  • 380.
    	function is_applicable_form( $form ) {
  • 381.
     
  • 382.
    		foreach( $form['fields'] as $field ) {
  • 383.
    			if( $this->is_applicable_field( $field ) ) {
  • 384.
    				return true;
  • 385.
    			}
  • 386.
    		}
  • 387.
     
  • 388.
    		return false;
  • 389.
    	}
  • 390.
     
  • 391.
    	function is_applicable_field( $field ) {
  • 392.
    		return preg_match( $this->get_class_regex(), rgar( $field, 'cssClass' ) ) == true;
  • 393.
    	}
  • 394.
     
  • 395.
    	function get_class_regex() {
  • 396.
    		return "/{$this->class_regex}/";
  • 397.
    	}
  • 398.
     
  • 399.
    	function get_rounding_actions( $field ) {
  • 400.
     
  • 401.
    		$actions = array();
  • 402.
     
  • 403.
    		preg_match_all( $this->get_class_regex(), rgar( $field, 'cssClass' ), $matches, PREG_SET_ORDER );
  • 404.
     
  • 405.
    		foreach( $matches as $match ) {
  • 406.
     
  • 407.
    			list( $full_match, $action, $action_value ) = array_pad( $match, 3, false );
  • 408.
     
  • 409.
    			if( $action_value === false ) {
  • 410.
    				$action_value = $action;
  • 411.
    				$action = 'round';
  • 412.
    			}
  • 413.
     
  • 414.
    			$action = array(
  • 415.
    				'action'       => $action,
  • 416.
    				'action_value' => $action_value
  • 417.
    			);
  • 418.
     
  • 419.
    			$actions[] = $action;
  • 420.
     
  • 421.
    		}
  • 422.
     
  • 423.
    		return $actions;
  • 424.
    	}
  • 425.
     
  • 426.
    }
  • 427.
     
  • 428.
    function gw_rounding() {
  • 429.
    	return GW_Rounding::get_instance();
  • 430.
    }
  • 431.
     
  • 432.
    gw_rounding();
/** * Gravity Wiz // Gravity Forms // Rounding by Increment * * Provides a variety of rounding functions for Gravity Form Number fields powered by the CSS class setting for each field. Functions include: * * + rounding to an increment (i.e. increment of 100 would round 1 to 100, 149 to 100, 150 to 200, etc) | class: 'gw-round-100' * + rounding up by an increment (i.e. increment of 50 would round 1 to 50, 51 to 100, 149 to 150, etc) | class: 'gw-round-up-50' * + rounding down by an increment (i.e. increment of 25 would round 1 to 0, 26 to 25, 51 to 50, etc) | class: 'gw-round-down-25' * + rounding up to a specific minimum (i.e. min of 50 would round 1 to 50, 51 and greater would not be rounded) | class: 'gw-round-min-50' * + rounding down to a specific maximum (i.e. max of 25 would round 26 to 25, 25 and below would not be rounded) | class: 'gw-round-max-25' * * @version 1.11 * @author David Smith <[email protected]> * @license GPL-2.0+ * @link http://gravitywiz.com/rounding-increments-gravity-forms/ * * Plugin Name: Gravity Forms Rounding * Plugin URI: http://gravitywiz.com/rounding-increments-gravity-forms/ * Description: Round your field values (including calculations) up, down, by an increment, or to a specific minimum or maximum value. * Author: Gravity Wiz * Version: 1.11 * Author URI: http://gravitywiz.com */ class GW_Rounding { private static $instance = null; protected static $is_script_output = false; protected $class_regex = 'gw-round-(\w+)-?(\w+)?'; public static function get_instance() { if( null == self::$instance ) self::$instance = new self; return self::$instance; } private function __construct( $args = array() ) { // make sure we're running the required minimum version of Gravity Forms if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) return; // time for hooks add_filter( 'gform_pre_render', array( $this, 'prepare_form_and_load_script' ), 10, 2 ); add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ) ); add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) ); add_action( 'gform_pre_submission', array( $this, 'override_submitted_value' ), 10, 5 ); add_filter( 'gform_calculation_result', array( $this, 'override_submitted_calculation_value' ), 10, 5 ); } public function prepare_form_and_load_script( $form, $is_ajax_enabled ) { if( ! $this->is_applicable_form( $form ) ) { return $form; } if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { add_action( 'wp_footer', array( $this, 'output_script' ) ); add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); add_action( 'admin_footer', array( $this, 'output_script' ) ); } foreach( $form['fields'] as &$field ) { if( preg_match( $this->get_class_regex(), $field['cssClass'] ) ) { $field['cssClass'] .= ' gw-rounding'; } } return $form; } public function is_ajax_submission( $form_id, $is_ajax_enabled ) { return class_exists( 'GFFormDisplay' ) && isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled; } function output_script() { ?> <script type="text/javascript"> var GWRounding; ( function( $ ) { GWRounding = function( args ) { var self = this; // copy all args to current object: (list expected props) for( prop in args ) { if( args.hasOwnProperty( prop ) ) self[prop] = args[prop]; } self.init = function() { self.fieldElems = $( '#gform_wrapper_' + self.formId + ' .gw-rounding' ); self.parseElemActions( self.fieldElems ); self.bindEvents(); }; self.parseElemActions = function( elems ) { elems.each( function() { var cssClasses = $( this ).attr( 'class' ), roundingActions = self.parseActions( cssClasses ); $( this ).data( 'gw-rounding', roundingActions ); } ); }; self.parseActions = function( str ) { var matches = getMatchGroups( String( str ), new RegExp( self.classRegex.replace( /\\/g, '\\' ), 'i' ) ), roundingActions = []; for( var i = 0; i < matches.length; i++ ) { var action = matches[i][1], actionValue = matches[i][2]; if( typeof actionValue == 'undefined' ) { actionValue = action; action = 'round'; } var roundingAction = { 'action': action, 'actionValue': actionValue }; roundingActions.push( roundingAction ); } return roundingActions; }; self.bindEvents = function() { self.fieldElems.each( function() { var $targets; // Attempt to target only the quantity input of a Single Product field. if( $( this ).hasClass( 'gfield_price' ) ) { $targets = $( this ).find( '.ginput_quantity' ); } // Otherwise, target all inputs of the given field. if( ! $targets.length ) { $targets = $( this ).find( 'inputs' ); } $targets.each( function() { self.applyRoundingActions( $( this ) ); } ).blur( function() { self.applyRoundingActions( $( this ) ); } ); } ); gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) { var $input = $( '#input_' + formId + '_' + formulaField.field_id ) $field = $input.parents( '.gfield' ); if( $field.hasClass( 'gw-rounding' ) ) { result = self.getRoundedValue( $input, result ); } return result; } ); }; self.applyRoundingActions = function( $input ) { var value = self.getRoundedValue( $input ); if( $input.val() != value ) { $input.val( value ).change(); } }; self.getRoundedValue = function( $input, value ) { var $field = $input.parents( '.gfield' ), actions = $field.data( 'gw-rounding' ); // allows setting the 'gw-rounding' data for an element to null and it will be reparsed if( actions === null ) { self.parseElemActions( $field ); actions = $field.data( 'gw-rounding' ); } if( typeof actions == 'undefined' || actions === false || actions.length <= 0 ) { return; } if( typeof value == 'undefined' ) { value = gformToNumber( $input.val() ); var currency = new Currency(gf_global.gf_currency_config); value = gformCleanNumber( value, currency.currency.symbol_right, currency.currency.symbol_left, currency.currency.decimal_separator ); } if( value != '' ) { for( var i = 0; i < actions.length; i++ ) { value = GWRounding.round( value, actions[i].actionValue, actions[i].action ); } } return isNaN( value ) ? '' : value; }; GWRounding.round = function( value, actionValue, action ) { var interval, base, min, max; value = parseFloat( value ); actionValue = parseInt( actionValue ); switch( action ) { case 'min': min = actionValue; if( value < min ) { value = min; } break; case 'max': max = actionValue; if( value > max ) { value = max; } break; case 'up': interval = actionValue; base = Math.ceil( value / interval ); value = base * interval; break; case 'down': interval = actionValue; base = Math.floor( value / interval ); value = base * interval; break; default: interval = actionValue; base = Math.round( value / interval ); value = base * interval; break; } return parseInt( value ); }; self.init(); } } )( jQuery ); </script> <?php } function add_init_script( $form ) { if( ! $this->is_applicable_form( $form ) ) { return; } $args = array( 'formId' => $form['id'], 'classRegex' => $this->class_regex ); $script = 'new GWRounding( ' . json_encode( $args ) . ' );'; GFFormDisplay::add_init_script( $form['id'], 'gw_rounding', GFFormDisplay::ON_PAGE_RENDER, $script ); } function enqueue_form_scripts( $form ) { if( $this->is_applicable_form( $form ) ) { wp_enqueue_script( 'gform_gravityforms' ); } } function override_submitted_value( $form ) { foreach( $form['fields'] as $field ) { if( $this->is_applicable_field( $field ) && ! is_a( $field, 'GF_Field_Calculation' ) ) { $value = rgpost( "input_{$field['id']}" ); $is_currency = $field->get_input_type() == 'number' && $field->numberFormat == 'currency'; if( $is_currency ) { $value = GFCommon::to_number( $value ); } $value = $this->process_rounding_actions( $value, $this->get_rounding_actions( $field ) ); $_POST[ "input_{$field['id']}" ] = $value; } } } function override_submitted_calculation_value( $result, $formula, $field, $form, $entry ) { if( $this->is_applicable_field( $field ) ) { $result = $this->process_rounding_actions( $result, $this->get_rounding_actions( $field ) ); } return $result; } function process_rounding_actions( $value, $actions ) { foreach( $actions as $action ) { $value = $this->round( $value, $action['action'], $action['action_value'] ); } return $value; } function round( $value, $action, $action_value ) { $value = floatval( $value ); $action_value = intval( $action_value ); switch( $action ) { case 'min': $min = $action_value; if( $value < $min ) { $value = $min; } break; case 'max': $max = $action_value; if( $value > $max ) { $value = $max; } break; case 'up': $interval = $action_value; $base = ceil( $value / $interval ); $value = $base * $interval; break; case 'down': $interval = $action_value; $base = floor( $value / $interval ); $value = $base * $interval; break; default: $interval = $action_value; $base = round( $value / $interval ); $value = $base * $interval; break; } return intval( $value ); } // # HELPERS function is_applicable_form( $form ) { foreach( $form['fields'] as $field ) { if( $this->is_applicable_field( $field ) ) { return true; } } return false; } function is_applicable_field( $field ) { return preg_match( $this->get_class_regex(), rgar( $field, 'cssClass' ) ) == true; } function get_class_regex() { return "/{$this->class_regex}/"; } function get_rounding_actions( $field ) { $actions = array(); preg_match_all( $this->get_class_regex(), rgar( $field, 'cssClass' ), $matches, PREG_SET_ORDER ); foreach( $matches as $match ) { list( $full_match, $action, $action_value ) = array_pad( $match, 3, false ); if( $action_value === false ) { $action_value = $action; $action = 'round'; } $action = array( 'action' => $action, 'action_value' => $action_value ); $actions[] = $action; } return $actions; } } function gw_rounding() { return GW_Rounding::get_instance(); } gw_rounding();

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter