Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. class GW_List_Field_As_Choices {
  2.     function __construct( $args ) {
  3.         $this->_args = wp_parse_args( $args, array(
  4.             'form_id'           => false,
  5.             'list_field_id'     => false,
  6.             'choice_field_ids'  => false,
  7.             'label_template'    => '{0}',
  8.             'sort'              => false
  9.         ) );
  10.         if( ! is_array( $this->_args['choice_field_ids'] ) )
  11.             $this->_args['choice_field_ids'] = array( $this->_args['choice_field_ids'] );
  12.         extract( $this->_args ); // gives us $form_id, $list_field_id, $choices_field_id
  13.         add_filter( 'gform_pre_render', array( $this, 'populate_choice_fields' ), 9 );
  14.         add_filter( 'gform_pre_validation', array( $this, 'populate_choice_fields' ), 9 );
  15.         add_filter( 'gform_pre_submission_filter_' . $form_id, array( $this, 'populate_choice_fields' ) );
  16.     }
  17.     function populate_choice_fields( $form ) {
  18.        
  19.         if( $form['id'] != $this->_args['form_id'] )
  20.             return $form;
  21.            
  22.         $list_field = GFFormsModel::get_field( $form, $this->_args['list_field_id'] );
  23.         $values = GFFormsModel::get_field_value( $list_field );
  24.         // if list field doesn't have any values, let's ditch this party
  25.         if( ! is_array( $values ) )
  26.             return $form;
  27.         $choices = $this->get_list_choices( $values );
  28.         foreach( $form['fields'] as &$field ) {
  29.             if( ! $this->is_applicable_field( $field ) )
  30.                 continue;
  31.             // set 'choices' for choice fields
  32.             $field['choices'] = $choices;
  33.             // only set inputs for 'checkbox' choice fields
  34.             if( GFFormsModel::get_input_type( $field ) == 'checkbox' ) {
  35.                 $inputs = array();
  36.                 foreach( $choices as $index => $choice ) {
  37.                     $inputs[] = array(
  38.                         'label' => $choice['text'],
  39.                         'id'    => $field['id'] . '.' . ( $index + 1 )
  40.                     );
  41.                 }
  42.                 $field['inputs'] = $inputs;
  43.             }
  44.            
  45.         }
  46.         return $form;
  47.     }
  48.    
  49.     function get_list_choices( $values ) {
  50.        
  51.         $choices = array();
  52.        
  53.         foreach( $values as $row ) {
  54.            
  55.             $label = $this->replace_template( $this->_args['label_template'], $row );
  56.             $value = isset( $this->_args['value_template'] ) ? $this->replace_template( $this->_args['value_template'], $row ) : $label;
  57.            
  58.             $choices[] = array(
  59.                 'text' => $label,
  60.                 'value' => $value
  61.             );
  62.            
  63.         }
  64.         if( $this->_args['sort'] == true )
  65.             usort( $choices, create_function( '$a, $b', 'return strnatcasecmp( $a["text"], $b["text"] );' ) );
  66.         return $choices;
  67.     }
  68.    
  69.     function replace_template( $template, $row ) {
  70.        
  71.         // combine our templates so we can find all matches at once
  72.         preg_match_all( '/{(\w+)}/', $template, $matches, PREG_SET_ORDER );
  73.        
  74.         if( is_array( $row ) ) {
  75.             $mega_row = array_merge( $row, array_values( $row ) );
  76.             foreach( $matches as $match ) {
  77.                 $template = str_replace( $match[0], rgar( $mega_row, $match[1] ), $template );
  78.             }
  79.            
  80.         } else {
  81.             foreach( $matches as $match ) {
  82.                 $template = str_replace( $match[0], $row, $template );
  83.             }
  84.            
  85.         }
  86.        
  87.         return $template;
  88.     }
  89.    
  90.     function is_applicable_field( $field ) {
  91.        
  92.         $is_choice_field = is_array( rgar( $field, 'choices' ) );
  93.         $is_registered_field = in_array( $field['id'], $this->_args['choice_field_ids'] );
  94.        
  95.         return $is_choice_field && $is_registered_field;
  96.     }
  97.    
  98. }
  99. /**
  100. * Uncomment the code below by removing the pound symbols (#) in front of each line. See @link in the comment section
  101. * at the top for additional usage instructions.
  102. */
  103. # Basic Usage
  104. new GW_List_Field_As_Choices( array(
  105.     'form_id' => 4,
  106.     'list_field_id' => 485,
  107.     'choice_field_ids' => array( 477, 492  ),
  108.     'value_template' => '{First Name} | 95.00'
  109. ) );
  110.  
  111. new GW_List_Field_As_Choices( array(
  112.     'form_id' => 4,
  113.     'list_field_id' => 486,
  114.     'choice_field_ids' => array( 495, 496  ),
  115.     'value_template' => '{First Name} | 95.00'
  116. ) );
  117.  
  118. new GW_List_Field_As_Choices( array(
  119.     'form_id' => 4,
  120.     'list_field_id' => 487,
  121.     'choice_field_ids' => array( 498, 499  ),
  122.     'value_template' => '{First Name} | 95.00'
  123. ) );
  124.  
  125. new GW_List_Field_As_Choices( array(
  126.     'form_id' => 4,
  127.     'list_field_id' => 488,
  128.     'choice_field_ids' => array( 484, 500, 501  ),
  129.     'value_template' => '{First Name} | 95.00'
  130. ) );
  131.  
  132. new GW_List_Field_As_Choices( array(
  133.     'form_id' => 4,
  134.     'list_field_id' => 489,
  135.     'choice_field_ids' => array( 503, 504, 505, 506  ),
  136.     'value_template' => '{First Name} | 95.00'
  137. ) );
  138.  
  139. new GW_List_Field_As_Choices( array(
  140.     'form_id' => 4,
  141.     'list_field_id' => 490,
  142.     'choice_field_ids' => array( 508, 509  ),
  143.     'value_template' => '{First Name} | 95.00'
  144. ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement