spivurno
10/9/2013 - 7:37 PM

Gravity Wiz // Price Range Generator

Gravity Wiz // Price Range Generator

<?php
/**
* Price Range Generator: Generate a price range based on a calculated number.
* http://gravitywiz.com
*/
class GWPriceRangeGenerator {
    
    private static $_is_script_output;
    
    function __construct( $args ) {
        
        $this->_args = wp_parse_args( $args, array( 
            'form_id'        => false,
            'base_field_id'  => false,
            'element_id'     => '',
            'range'          => array( 'min' => 0, 'max' => 0 )
        ) );
        
        $this->_args['range'] = $this->prepare_range_setting( $this->_args['range'] );
        
        extract( $this->_args ); // gives us $form_id, $base_field_id
        
        if( ! $form_id || ! $base_field_id )
            return;
        
        add_filter( 'gform_pre_render_' . $form_id, array( $this, 'pre_render' ) );
        
    }
    
    function pre_render( $form ) {
        
        add_filter( 'gform_register_init_scripts', array( $this, 'register_init_script' ) );
        
        if( ! self::$_is_script_output )
            add_action( 'wp_footer', array( $this, 'output_script' ), 21 );
        
        return $form;
    }
    
    function register_init_script( $form ) {
        
        $this->enqueue_gravityforms_js();
        
        // remove this function from the filter otherwise it will be called for every other form on the page
        remove_filter( 'gform_register_init_scripts', array( $this, 'register_init_script' ) );
                
        $args = array(
            'formId'      => $this->_args['form_id'],
            'baseFieldId' => $this->_args['base_field_id'],
            'elementId'   => $this->_args['element_id'],
            'range'       => $this->_args['range']
            );
        
        $script = "new gwprg(" . json_encode( $args ) . ");";
        $key = $args['formId'] . '_' . $args['baseFieldId'];
        
        GFFormDisplay::add_init_script( $form['id'], 'gwprg_' . $key , GFFormDisplay::ON_PAGE_RENDER, $script );
        
    }
    
    function output_script() {
        ?>
        
        <script type="text/javascript">
            
        window.gwprg;
        
        (function($){
        
            gwprg = function( args ) {
                
                this.formId      = args.formId;
                this.baseFieldId = args.baseFieldId;
                this.elementId   = args.elementId;
                this.range       = args.range;
                
                this.init = function() {
                    
                    var gwprg     = this,
                        elem      = $( this.elementId ),
                        baseInput = $( '#input_' + this.formId + '_' + this.baseFieldId );
                    
                    // update price range on page load
                    this.updatePriceRange( elem, baseInput );
                    
                    // update price range anytime base value is updated
                    baseInput.change(function(){
                        gwprg.updatePriceRange( elem, baseInput );
                    });
                    
                }
                
                this.updatePriceRange = function( elem, baseInput ) {
                    
                    var value = gformToNumber( baseInput.val() ),
                        min   = this.calculateMinRange( value ),
                        max   = this.calculateMaxRange( value );
                    
                    console.log( typeof this.range.max );
                    
                    console.log( [ value, min, max ] );
                    
                    elem.text( gformFormatMoney( min ) + ' - ' + gformFormatMoney( max ) );
                    
                }
                
                this.calculateMinRange = function( value ) {
                    
                    var subtract = this.range.min;
                    
                    console.log( this.isPercentage( subtract ) );
                    
                    if( this.isPercentage( subtract ) )
                        subtract = this.calculatePercentage( value, subtract );
                    
                    value -= subtract;
                    
                    return value;
                }
                
                this.calculateMaxRange = function( value ) {
                    
                    var add = this.range.max;
                    if( this.isPercentage( add ) )
                        add = this.calculatePercentage( value, add );
                    
                    value += parseFloat( add );
                    
                    return value;
                }
                
                this.calculatePercentage = function( value, percentage ) {
                    
                    var percentage = percentage.replace( '%', '' ),
                        percentageValue = ( value * percentage ) / 100;
                    
                    return percentageValue;
                }
                
                this.isPercentage = function( value ) {
                    return typeof value == 'string' && value.search( '%' ) != -1;
                }
                
                this.init();
                
            }
            
        })(jQuery);
        
        </script>
        
        <?php
    }
    
    function prepare_range_setting( $range ) {
        
        if( ! is_array( $range ) ) {
            
            $range = array( 'min' => $range, 'max' => $range );
            
        } else {
            
            $range = wp_parse_args( $range, array( 
                'min' => 0,
                'max' => 0
            ) );
            
        }
        
        return $range;
    }
    
    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 GWPriceRangeGenerator( array( 
    'form_id' => 365,
    'base_field_id' => 32,
    'element_id' => '#my-price-range',
    'range' => array( 'max' => '50%' )
) );