PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/gravityforms 2/includes/fields/class-gf-field-radio.php

https://gitlab.com/plusplusminus/isissoftware
PHP | 230 lines | 172 code | 55 blank | 3 comment | 58 complexity | 4b94c40e102898e2447f29ea65144d0e MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'GFForms' ) ) {
  3. die();
  4. }
  5. class GF_Field_Radio extends GF_Field {
  6. public $type = 'radio';
  7. public function get_form_editor_field_title() {
  8. return __( 'Radio Buttons', 'gravityforms' );
  9. }
  10. function get_form_editor_field_settings() {
  11. return array(
  12. 'conditional_logic_field_setting',
  13. 'prepopulate_field_setting',
  14. 'error_message_setting',
  15. 'label_setting',
  16. 'label_placement_setting',
  17. 'admin_label_setting',
  18. 'choices_setting',
  19. 'rules_setting',
  20. 'visibility_setting',
  21. 'duplicate_setting',
  22. 'description_setting',
  23. 'css_class_setting',
  24. 'other_choice_setting',
  25. );
  26. }
  27. public function is_conditional_logic_supported() {
  28. return true;
  29. }
  30. public function validate( $value, $form ) {
  31. if ( $this->enableOtherChoice && $value == 'gf_other_choice' ) {
  32. $value = rgpost( "input_{$this->id}_other" );
  33. }
  34. if ( $this->isRequired && $this->enableOtherChoice && $value == GFCommon::get_other_choice_value() ) {
  35. $this->failed_validation = true;
  36. $this->validation_message = empty( $this->errorMessage ) ? __( 'This field is required.', 'gravityforms' ) : $this->errorMessage;
  37. }
  38. }
  39. public function get_first_input_id( $form ){
  40. return '';
  41. }
  42. public function get_field_input( $form, $value = '', $entry = null ) {
  43. $form_id = $form['id'];
  44. $is_entry_detail = $this->is_entry_detail();
  45. $is_form_editor = $this->is_form_editor();
  46. $id = $this->id;
  47. $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
  48. $disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
  49. return sprintf( "<div class='ginput_container'><ul class='gfield_radio' id='%s'>%s</ul></div>", $field_id, $this->get_radio_choices( $value, $disabled_text, $form_id ) );
  50. }
  51. public function get_radio_choices( $value = '', $disabled_text, $form_id = 0 ) {
  52. $choices = '';
  53. $is_entry_detail = $this->is_entry_detail();
  54. $is_form_editor = $this->is_form_editor();
  55. $is_admin = $is_entry_detail || $is_form_editor;
  56. if ( is_array( $this->choices ) ) {
  57. $choice_id = 0;
  58. $other_default_value = '';
  59. // add 'other' choice to choices if enabled
  60. if ( $this->enableOtherChoice ) {
  61. $other_default_value = GFCommon::get_other_choice_value();
  62. $this->choices[] = array( 'text' => $other_default_value, 'value' => 'gf_other_choice', 'isSelected' => false, 'isOtherChoice' => true );
  63. }
  64. $logic_event = $this->get_conditional_logic_event( 'click' );
  65. $count = 1;
  66. foreach ( $this->choices as $choice ) {
  67. if ( $is_entry_detail || $is_form_editor || $form_id == 0 ){
  68. $id = $this->id . '_' . $choice_id ++;
  69. } else {
  70. $id = $form_id . '_' . $this->id . '_' . $choice_id ++;
  71. }
  72. $field_value = ! empty( $choice['value'] ) || $this->enableChoiceValue ? $choice['value'] : $choice['text'];
  73. if ( $this->enablePrice ) {
  74. $price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) );
  75. $field_value .= '|' . $price;
  76. }
  77. if ( rgblank( $value ) && RG_CURRENT_VIEW != 'entry' ) {
  78. $checked = rgar( $choice, 'isSelected' ) ? "checked='checked'" : '';
  79. } else {
  80. $checked = RGFormsModel::choice_value_match( $this, $choice, $value ) ? "checked='checked'" : '';
  81. }
  82. $tabindex = $this->get_tabindex();
  83. $label = sprintf( "<label for='choice_%s' id='label_%s'>%s</label>", $id, $id, $choice['text'] );
  84. $input_focus = '';
  85. // handle 'other' choice
  86. if ( rgar( $choice, 'isOtherChoice' ) ) {
  87. $onfocus = ! $is_admin ? 'jQuery(this).prev("input").attr("checked", true); if(jQuery(this).val() == "' . $other_default_value . '") { jQuery(this).val(""); }' : '';
  88. $onblur = ! $is_admin ? 'if(jQuery(this).val().replace(" ", "") == "") { jQuery(this).val("' . $other_default_value . '"); }' : '';
  89. $onkeyup = $this->get_conditional_logic_event( 'keyup' );
  90. $input_focus = ! $is_admin ? "onfocus=\"jQuery(this).next('input').focus();\"" : '';
  91. $value_exists = RGFormsModel::choices_value_match( $this, $this->choices, $value );
  92. if ( $value == 'gf_other_choice' && rgpost( "input_{$this->id}_other" ) ) {
  93. $other_value = rgpost( "input_{$this->id}_other" );
  94. } else if ( ! $value_exists && ! empty( $value ) ) {
  95. $other_value = $value;
  96. $value = 'gf_other_choice';
  97. $checked = "checked='checked'";
  98. } else {
  99. $other_value = $other_default_value;
  100. }
  101. $label = "<input id='input_{$this->formId}_{$this->id}_other' name='input_{$this->id}_other' type='text' value='" . esc_attr( $other_value ) . "' onfocus='$onfocus' onblur='$onblur' $tabindex $onkeyup $disabled_text />";
  102. }
  103. $choices .= sprintf( "<li class='gchoice_$id'><input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex %s $logic_event %s />%s</li>", $this->id, esc_attr( $field_value ), $checked, $id, $disabled_text, $input_focus, $label );
  104. if ( $is_form_editor && $count >= 5 ) {
  105. break;
  106. }
  107. $count ++;
  108. }
  109. $total = sizeof( $this->choices );
  110. if ( $count < $total ) {
  111. $choices .= "<li class='gchoice_total'>" . sprintf( __( '%d of %d items shown. Edit field to view all', 'gravityforms' ), $count, $total ) . '</li>';
  112. }
  113. }
  114. return apply_filters( 'gform_field_choices_' . $this->formId, apply_filters( 'gform_field_choices', $choices, $this ), $this );
  115. }
  116. public function get_value_default(){
  117. return $this->is_form_editor() ? $this->defaultValue : GFCommon::replace_variables_prepopulate( $this->defaultValue );
  118. }
  119. public function get_value_submission( $field_values, $get_from_post_global_var = true ) {
  120. $value = $this->get_input_value_submission( 'input_' . $this->id, $this->inputName, $field_values, $get_from_post_global_var );
  121. if ( $value == 'gf_other_choice' ) {
  122. //get value from text box
  123. $value = $this->get_input_value_submission( 'input_' . $this->id . '_other', $this->inputName, $field_values, $get_from_post_global_var );
  124. }
  125. return $value;
  126. }
  127. public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
  128. return GFCommon::selection_display( $value, $this, $entry['currency'] );
  129. }
  130. public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
  131. return GFCommon::selection_display( $value, $this, $currency, $use_text );
  132. }
  133. public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format ) {
  134. $use_value = $modifier == 'value';
  135. $use_price = in_array( $modifier, array( 'price', 'currency' ) );
  136. $format_currency = $modifier == 'currency';
  137. if ( is_array( $raw_value ) && (string) intval( $input_id ) != $input_id ) {
  138. $items = array( $input_id => $value ); //float input Ids. (i.e. 4.1 ). Used when targeting specific checkbox items
  139. } else if ( is_array( $raw_value ) ) {
  140. $items = $raw_value;
  141. } else {
  142. $items = array( $input_id => $raw_value );
  143. }
  144. $ary = array();
  145. foreach ( $items as $input_id => $item ) {
  146. if ( $use_value ) {
  147. list( $val, $price ) = rgexplode( '|', $item, 2 );
  148. } else if ( $use_price ) {
  149. list( $name, $val ) = rgexplode( '|', $item, 2 );
  150. if ( $format_currency ) {
  151. $val = GFCommon::to_money( $val, rgar( $entry, 'currency' ) );
  152. }
  153. } else if ( $this->type == 'post_category' ) {
  154. $use_id = strtolower( $modifier ) == 'id';
  155. $item_value = GFCommon::format_post_category( $item, $use_id );
  156. $val = RGFormsModel::is_field_hidden( $form, $this, array(), $entry ) ? '' : $item_value;
  157. } else {
  158. $val = RGFormsModel::is_field_hidden( $form, $this, array(), $entry ) ? '' : RGFormsModel::get_choice_text( $this, $raw_value, $input_id );
  159. }
  160. $ary[] = GFCommon::format_variable_value( $val, $url_encode, $esc_html, $format );
  161. }
  162. return GFCommon::implode_non_blank( ', ', $ary );
  163. }
  164. public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) {
  165. if ( $this->enableOtherChoice && $value == 'gf_other_choice' ) {
  166. $value = rgpost( "input_{$this->id}_other" );
  167. }
  168. $value = $this->sanitize_entry_value( $value, $form['id'] );
  169. return $value;
  170. }
  171. public function allow_html(){
  172. return true;
  173. }
  174. }
  175. GF_Fields::register( new GF_Field_Radio() );