Forked from spivurno/gw-gravity-forms-populate-entries.php
Created
December 2, 2017 19:41
-
-
Save SqueezyDee/618de3903fee198dc66ceb101edd2aaa to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms Populate With Entries
This file contains 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 | |
/** | |
* 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 <[email protected]> | |
* @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, | |
'value' => false, // can be a field ID or a 'template' string with merge tag support | |
'label' => false, // can be a field ID or a 'template' string with merge tag support | |
'query_args' => array() | |
) ); | |
$this->_args['query_args'] = wp_parse_args( $this->_args['query_args'], array( | |
'status' => 'active', | |
'order' => 'asc', | |
'order_by' => 'date_created', | |
'nopaging' => true, | |
'offset' => 0, | |
'page_size' => 20, | |
'field_id' => false, | |
'field_value' => '', | |
'field_filters' => array() | |
) ); | |
if( $this->_args['query_args']['field_id'] ) { | |
$this->_args['query_args']['field_filters'] = array( | |
array( | |
'key' => $this->_args['query_args']['field_id'], | |
'value' => $this->_args['query_args']['field_value'] | |
) | |
); | |
} | |
$is_no_paging = $this->_args['query_args']['nopaging'] == true; | |
$this->_args['paging'] = array( | |
'offset' => $is_no_paging ? 0 : $this->_args['query_args']['offset'], | |
'page_size' => $is_no_paging ? 999 : $this->_args['query_args']['page_size'] | |
); | |
$this->_args['sorting'] = array( | |
'key' => $this->_args['query_args']['order_by'], | |
'direction' => $this->_args['query_args']['order'] | |
); | |
// time for hooks | |
add_action( "gform_pre_render_{$this->_args['target_form_id']}", array( $this, 'populate' ) ); | |
add_action( "gform_admin_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['value'] ); | |
$label = $this->_args['label'] ? $this->get_entry_value( $entry, $this->_args['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'], $this->_args['sorting'], $this->_args['paging'] ); | |
} | |
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_field_id' => 1, // the field to be populated with entry data; must be a choice-based field (drop down, radio, checkbox) | |
'target_form_id' => 449, // the form that contains the field to be populated | |
'source_form_id' => 414, // the form from which entry data should be retrieved | |
'value' => '{entry_id}', // specifies the data that should be populated for the choice's 'value'; can be a field ID (i.e. 12) or a 'template' string with merge tag support (i.e. '{entry_id}') | |
'label' => '{:1.3} {:1.6} ({entry_id})', // same as 'value' but used to populate the label of the choice | |
'query_args' => array( // specifies which entries should be pulled for display | |
'status' => 'trash', | |
'field_id' => '1.3', | |
'field_value' => 'test', | |
'order' => 'asc', | |
'order_by' => '1.3', | |
'nopaging' => true | |
) | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment