spivurno of Gravity Wiz
4/29/2014 - 2:56 PM

Gravity Wiz // Gravity Forms Populate With Entries

Gravity Wiz // Gravity Forms Populate With Entries

<?php
/**
* Gravity Wiz // Gravity Forms Populate With Entries
*
* Populate choice-based fields (i.e. drop downs, radios, checkboxes) with data from Gravity Form entries.
*
* @version	 1.0
* @author    David Smith <david@gravitywiz.com>
* @license   GPL-2.0+
* @link      http://gravitywiz.com/...
* @copyright 2013 Gravity Wiz
*/
class GW_Populate_Entries {

    public 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;

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'target_form_id'  => false,
            'target_field_id' => false, // must be a choice-based field (drop down, radio, checkbox)
            'source_form_id'  => false,
            'source_value'    => false, // can be a field ID or a 'template' string with merge tag support
            'source_label'    => false, // can be a field ID or a 'template' string with merge tag support
            'query_args'      => array(
                'status' => 'active'
            )
        ) );

        // time for hooks
        add_action( "gform_pre_render_{$this->_args['target_form_id']}",            array( $this, 'populate' ) );
        add_action( "gform_pre_submission_filter_{$this->_args['target_form_id']}", array( $this, 'populate' ) );

    }

    public function populate( $form ) {

        foreach( $form['fields'] as &$field ) {

            if( $field['id'] != $this->_args['target_field_id'] )
                continue;

            $entries = $this->get_entries_for_population( $this->_args['source_form_id'] );
            $choices = array();

            foreach( $entries as $entry ) {

                $value = $this->get_entry_value( $entry, $this->_args['source_value'] );
                $label = $this->_args['source_label'] ? $this->get_entry_value( $entry, $this->_args['source_label'] ) : $value;

                $choices[] = array(
                    'text' => $label,
                    'value' => $value
                );

            }

            if( empty( $choices ) ) {
                $choices[] = array(
                    'text' => __( 'There are no options available currently.' ),
                    'value' => ''
                );
            }

            $field['choices'] = $choices;

            if( GFFormsModel::get_input_type( $field ) != 'checkbox' )
                continue;

            $field['inputs'] = array();

            foreach( $choices as $id => $choice ) {
                $field['inputs'][] = array(
                    'id' => sprintf( '%d.%d', $field['id'], $id + 1 ),
                    'label' => $choice['text'],
                    'name' => ''
                );
            }

        }

        return $form;
    }

    public function get_entries_for_population( $form_id ) {
        return GFAPI::get_entries( $form_id, $this->_args['query_args'] );
    }

    public function get_entry_value( $entry, $template ) {

        $field_id = intval( $template );
        if( $field_id )
            return rgar( $entry, $field_id );

        $form = GFAPI::get_form( $entry['form_id'] );
        $template = GFCommon::replace_variables( $template, $form, $entry, false, true, false, 'html' );

        return $template;
    }

}

# Configuration

new GW_Populate_Entries( array(
    'target_form_id' => 449,
    'target_field_id' => 1, // must be a choice-based field (drop down, radio, checkbox)
    'source_form_id' => 448,
    //'source_value' => 51, // can be a field ID or a 'template' string with merge tag support
    'source_value' => '{entry_id}', // can be a field ID or a 'template' string with merge tag support
    'source_label' => '{:51} ({:41}) / {:64}',
    'query_args' => array(
        'status' => 'active',
        'field_filters' => array(
            array( 'key' => 66, 'value' => 'Approved' )
        )
    )
) );