Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GW_List_Field_As_Choices {
- function __construct( $args ) {
- $this->_args = wp_parse_args( $args, array(
- 'form_id' => false,
- 'list_field_id' => false,
- 'choice_field_ids' => false,
- 'label_template' => '{0}',
- 'sort' => false
- ) );
- if( ! is_array( $this->_args['choice_field_ids'] ) )
- $this->_args['choice_field_ids'] = array( $this->_args['choice_field_ids'] );
- extract( $this->_args ); // gives us $form_id, $list_field_id, $choices_field_id
- add_filter( 'gform_pre_render', array( $this, 'populate_choice_fields' ), 9 );
- add_filter( 'gform_pre_validation', array( $this, 'populate_choice_fields' ), 9 );
- add_filter( 'gform_pre_submission_filter_' . $form_id, array( $this, 'populate_choice_fields' ) );
- }
- function populate_choice_fields( $form ) {
- if( $form['id'] != $this->_args['form_id'] )
- return $form;
- $list_field = GFFormsModel::get_field( $form, $this->_args['list_field_id'] );
- $values = GFFormsModel::get_field_value( $list_field );
- // if list field doesn't have any values, let's ditch this party
- if( ! is_array( $values ) )
- return $form;
- $choices = $this->get_list_choices( $values );
- foreach( $form['fields'] as &$field ) {
- if( ! $this->is_applicable_field( $field ) )
- continue;
- // set 'choices' for choice fields
- $field['choices'] = $choices;
- // only set inputs for 'checkbox' choice fields
- if( GFFormsModel::get_input_type( $field ) == 'checkbox' ) {
- $inputs = array();
- foreach( $choices as $index => $choice ) {
- $inputs[] = array(
- 'label' => $choice['text'],
- 'id' => $field['id'] . '.' . ( $index + 1 )
- );
- }
- $field['inputs'] = $inputs;
- }
- }
- return $form;
- }
- function get_list_choices( $values ) {
- $choices = array();
- foreach( $values as $row ) {
- $label = $this->replace_template( $this->_args['label_template'], $row );
- $value = isset( $this->_args['value_template'] ) ? $this->replace_template( $this->_args['value_template'], $row ) : $label;
- $choices[] = array(
- 'text' => $label,
- 'value' => $value
- );
- }
- if( $this->_args['sort'] == true )
- usort( $choices, create_function( '$a, $b', 'return strnatcasecmp( $a["text"], $b["text"] );' ) );
- return $choices;
- }
- function replace_template( $template, $row ) {
- // combine our templates so we can find all matches at once
- preg_match_all( '/{(\w+)}/', $template, $matches, PREG_SET_ORDER );
- if( is_array( $row ) ) {
- $mega_row = array_merge( $row, array_values( $row ) );
- foreach( $matches as $match ) {
- $template = str_replace( $match[0], rgar( $mega_row, $match[1] ), $template );
- }
- } else {
- foreach( $matches as $match ) {
- $template = str_replace( $match[0], $row, $template );
- }
- }
- return $template;
- }
- function is_applicable_field( $field ) {
- $is_choice_field = is_array( rgar( $field, 'choices' ) );
- $is_registered_field = in_array( $field['id'], $this->_args['choice_field_ids'] );
- return $is_choice_field && $is_registered_field;
- }
- }
- /**
- * Uncomment the code below by removing the pound symbols (#) in front of each line. See @link in the comment section
- * at the top for additional usage instructions.
- */
- # Basic Usage
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 485,
- 'choice_field_ids' => array( 477, 492 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 486,
- 'choice_field_ids' => array( 495, 496 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 487,
- 'choice_field_ids' => array( 498, 499 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 488,
- 'choice_field_ids' => array( 484, 500, 501 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 489,
- 'choice_field_ids' => array( 503, 504, 505, 506 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
- new GW_List_Field_As_Choices( array(
- 'form_id' => 4,
- 'list_field_id' => 490,
- 'choice_field_ids' => array( 508, 509 ),
- 'value_template' => '{First Name} | 95.00'
- ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement