GFPDFHelperFieldsField_Survey

Controls the display and output of a Gravity Form field.

Defined (1)

The class is defined in the following location(s).

/src/helper/fields/Field_Survey.php  
  1. class Field_Survey extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Check the appropriate variables are parsed in send to the parent construct 
  5. * @param object $field The GF_Field_* Object 
  6. * @param array $entry The Gravity Forms Entry 
  7. * @param \GFPDF\Helper\Helper_Abstract_Form $gform 
  8. * @param \GFPDF\Helper\Helper_Misc $misc 
  9. * @throws Exception 
  10. * @since 4.0 
  11. */ 
  12. public function __construct( $field, $entry, Helper_Abstract_Form $gform, Helper_Misc $misc ) { 
  13.  
  14. /** call our parent method */ 
  15. parent::__construct( $field, $entry, $gform, $misc ); 
  16.  
  17. /** 
  18. * Survey Field can be any of the following: 
  19. * single line text, paragraph, dropdown, select, checkbox,  
  20. * likert, rank or rating 
  21. */ 
  22. $class = $this->misc->get_field_class( $field->inputType ); 
  23.  
  24. try { 
  25. /** check load our class */ 
  26. if ( class_exists( $class ) ) { 
  27.  
  28. /** See https://gravitypdf.com/documentation/v4/gfpdf_field_class/ for more details about these filters */ 
  29. $this->fieldObject = apply_filters( 'gfpdf_field_class', new $class( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  30. $this->fieldObject = apply_filters( 'gfpdf_field_class_' . $field->inputType, $this->fieldObject, $field, $entry, $this->form ); 
  31. } else { 
  32. throw new Exception(); 
  33. } catch ( Exception $e ) { 
  34. /** Exception thrown. Load generic field loader */ 
  35. $this->fieldObject = apply_filters( 'gfpdf_field_default_class', new Field_Default( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  36.  
  37. /** force the fieldObject value cache */ 
  38. $this->value(); 
  39.  
  40. /** 
  41. * Get the $form_data object 
  42. * Survey field uses multiple field types so we need to account for that 
  43. * @return array 
  44. * @since 4.0 
  45. */ 
  46. private function get_form_data() { 
  47. if ( method_exists( $this->fieldObject, 'form_data' ) ) { 
  48. return $this->fieldObject->form_data(); 
  49.  
  50. return parent::form_data(); 
  51.  
  52. /** 
  53. * Used to check if the current field has a value 
  54. * @since 4.0 
  55. */ 
  56. public function is_empty() { 
  57. return $this->fieldObject->is_empty(); 
  58.  
  59. /** 
  60. * Return the HTML form data 
  61. * @return array 
  62. * @since 4.0 
  63. */ 
  64. public function form_data() { 
  65.  
  66. $data = []; 
  67. $field_id = (int) $this->field->id; 
  68.  
  69. /** Provide backwards compatibility fixes to certain fields */ 
  70. switch ( $this->field->inputType ) { 
  71. case 'radio': 
  72. case 'select': 
  73.  
  74. $data = $this->get_form_data(); 
  75. $value = $data['field'][ $this->field->id . '_name' ]; 
  76.  
  77. /** Overriding survey radio values with name */ 
  78. array_walk( $data['field'], function ( &$item, $key, $value ) { 
  79. $item = esc_html( $value ); 
  80. }, $value ); 
  81.  
  82. break; 
  83.  
  84. case 'checkbox': 
  85. $value = $this->get_value(); 
  86.  
  87. /** Convert survey ID to real value */ 
  88. foreach ( $this->field->choices as $choice ) { 
  89.  
  90. if ( ( $key = array_search( $choice['value'], $value ) ) !== false ) { 
  91. $value[ $key ] = esc_html( $choice['text'] ); 
  92.  
  93. $value = [ $value ]; 
  94. $label = GFFormsModel::get_label( $this->field ); 
  95.  
  96. /** Gravity PDF v3 backwards compatibility. Check if nothing is selected and return blank */ 
  97. if ( 0 === sizeof( array_filter( $value[0] ) ) ) { 
  98. $value = ''; 
  99.  
  100. $data[ $field_id . '.' . $label ] = $value; 
  101. $data[ $field_id ] = $value; 
  102. $data[ $label ] = $value; 
  103.  
  104. $data = [ 'field' => $data ]; 
  105.  
  106. break; 
  107.  
  108. default: 
  109. $data = $this->get_form_data(); 
  110. break; 
  111.  
  112. return $data; 
  113.  
  114.  
  115. /** 
  116. * Display the HTML version of this field 
  117. * @param string $value 
  118. * @param bool $label 
  119. * @return string 
  120. * @since 4.0 
  121. */ 
  122. public function html( $value = '', $label = true ) { 
  123.  
  124. /** Return early to prevent unwanted details being displayed when the plugin isn't enabled */ 
  125. if ( ! class_exists( 'GFSurvey' ) ) { 
  126. return parent::html( '' ); 
  127.  
  128. return $this->fieldObject->html(); 
  129.  
  130. /** 
  131. * Get the standard GF value of this field 
  132. * @return string|array 
  133. * @since 4.0 
  134. */ 
  135. public function value() { 
  136. if ( $this->fieldObject->has_cache() ) { 
  137. return $this->fieldObject->cache(); 
  138.  
  139. $value = $this->fieldObject->value(); 
  140.  
  141. $this->fieldObject->cache( $value ); 
  142.  
  143. return $this->fieldObject->cache();