spivurno
5/17/2016 - 6:09 PM

Gravity Wiz // Gravity Forms // Populate List Field

Gravity Wiz // Gravity Forms // Populate List Field

<?php
/**
 * Gravity Wiz // Gravity Forms // Populate List Field
 *
 * Populate a List field from a Multi-select field. The List field must be on a later page than then Multi-select field.
 *
 * @version   0.1
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/...
 *
 * Plugin Name: Gravity Forms Populate List Field
 * Plugin URI: http://gravitywiz.com/...
 * Description: Populate a List field from a Multi-select field. The List field must be on a later page than then Multi-select field.
 * Author: Gravity Wiz
 * Version: 1.0
 * Author URI: http://gravitywiz.com
 */
class GW_Populate_List_Field {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'  => false,
            'field_id' => false
        ) );

        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
            return;
        }

        // carry on
        add_filter( 'gform_pre_validation', array( $this, 'populate_list_field' ) );
	    add_action( 'gform_pre_render', array( $this, 'add_classes' ) );
	    add_action( 'gform_register_init_scripts', array( $this, 'register_init_script' ), 10, 2 );

    }

	public function add_classes( $form ) {

		if( ! $this->is_applicable_form( $form ) ) {
			return $form;
		}

		foreach( $form['fields'] as &$field ) {
			if( $this->is_target_field( $field ) ) {
				$field->cssClass .= ' gwplf-locked';
			}
		}

		$this->output_styles();

		return $form;
	}

	public function output_styles() {
		?>

		<style type="text/css">
			.gwplf-locked .gfield_list_icons {
				display: none;
			}
		</style>

		<?php
	}

	function register_init_script( $form ) {

		if( ! $this->is_applicable_form( $form ) ) {
			return;
		}

		$script = 'jQuery( ".gwplf-locked tr.gfield_list_group td:first-child input" ).prop( "readonly", true );';
		GFFormDisplay::add_init_script( $this->_args['form_id'], 'gw_populate_list_field', GFFormDisplay::ON_PAGE_RENDER, $script );

	}

    public function is_applicable_form( $form ) {

        $form_id = isset( $form['id'] ) ? $form['id'] : $form;

        return $form_id == $this->_args['form_id'];
    }

	public function is_target_field( $field ) {

		$field_id = isset( $field->id ) ? $field->id : $field;

		return $field_id == $this->_args['target_field_id'];
	}

	public function populate_list_field( $form ) {

		if( ! $this->is_applicable_form( $form ) ) {
			return $form;
		}

		$values = $this->get_source_values();
		if( empty( $values ) ) {
			return $form;
		}

		$list_field = GFFormsModel::get_field( $form, $this->_args['target_field_id'] );

		if ( $list_field->enableColumns ) {

			$list_value   = array();
			$column_count = count( $list_field->choices );

			foreach( $values as $value ) {
				$list_value = array_merge( $list_value, array_pad( array( $value ), $column_count, '' ) );
			}

		} else {

			$list_value = $values;

		}

		$_POST[ "input_{$this->_args['target_field_id']}" ] = $list_value;

		return $form;
	}

	public function get_source_values() {

		$values = rgpost( "input_{$this->_args['source_field_id']}" );

		if( empty( $values ) ) {
			$values = array();
		}

		if( ! is_array( $values ) ) {
			$values = array( $values );
		}

		return $values;
	}

}

# Configuration

new GW_Populate_List_Field( array(
    'form_id' => 1332,
    'source_field_id' => 1,
	'target_field_id' => 3
) );

new GW_Populate_List_Field( array(
	'form_id' => 1332,
	'source_field_id' => 4,
	'target_field_id' => 5
) );