Checkbox.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Fields_Checkbox
  4. */
  5. class NF_Fields_Checkbox extends NF_Abstracts_Input
  6. {
  7. protected $_name = 'checkbox';
  8. protected $_nicename = 'Checkbox';
  9. protected $_section = 'common';
  10. protected $_icon = 'check-square-o';
  11. protected $_type = 'checkbox';
  12. protected $_templates = 'checkbox';
  13. protected $_test_value = 0;
  14. protected $_settings = array( 'checkbox_default_value', 'checkbox_values', 'checked_calc_value', 'unchecked_calc_value' );
  15. protected $_settings_exclude = array( 'default', 'placeholder', 'input_limit_set' );
  16. /**
  17. * NF_Fields_Checkbox constructor.
  18. * @since 3.0
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->_nicename = __( 'Single Checkbox', 'ninja-forms' );
  24. $this->_settings[ 'label_pos' ][ 'value' ] = 'right';
  25. add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 2 );
  26. add_filter( 'ninja_forms_merge_tag_value_' . $this->_name, array( $this, 'filter_merge_tag_value' ), 10, 2 );
  27. add_filter( 'ninja_forms_merge_tag_calc_value_' . $this->_name, array( $this, 'filter_merge_tag_value_calc' ), 10, 2 );
  28. add_filter( 'ninja_forms_subs_export_field_value_' . $this->_type, array( $this, 'export_value' ), 10, 2 );
  29. }
  30. /**
  31. * Admin Form Element
  32. * Display the checkbox on the edit submissions area.
  33. * @since 3.0
  34. *
  35. * @param $id Field ID.
  36. * @param $value Field value.
  37. * @return string HTML used for display of checkbox.
  38. */
  39. public function admin_form_element( $id, $value )
  40. {
  41. // If the checkboxes value is 1 or on...
  42. if( 'on' == $value || 1 == $value ) {
  43. // ...this variable to checked.
  44. $checked = 'checked';
  45. } else {
  46. // ...else leave the variable empty.
  47. $checked = '';
  48. }
  49. // Return HTML to be output to the submission edit page.
  50. return "<input type='hidden' name='fields[$id]' value='0' >
  51. <input type='checkbox' name='fields[$id]' id='' $checked>";
  52. }
  53. /**
  54. * Custom Columns
  55. * Creates what is displayed in the columns on the submissions page.
  56. * @since 3.0
  57. *
  58. * @param $value checkbox value
  59. * @param $field field model.
  60. * @return $value string|void
  61. */
  62. public function custom_columns( $value, $field )
  63. {
  64. // If the field type is equal to checkbox...
  65. if( 'checkbox' == $field->get_setting( 'type' ) ) {
  66. // Backwards compatibility check for the new checked value setting.
  67. if( null == $field->get_setting( 'checked_value' ) && 1 == $value || 'on' == $value ) {
  68. return __( 'Checked', 'ninja-forms' );
  69. } elseif( null == $field->get_setting( 'unchecked_value' ) && 0 == $value ) {
  70. return __( 'Unchecked', 'ninja-forms');
  71. }
  72. // If the field value is set to 1....
  73. if( 1 == $value || 'on' == $value) {
  74. // Set the value to the checked value setting.
  75. $value = $field->get_setting( 'checked_value' );
  76. } else {
  77. // Else set the value to the unchecked value setting.
  78. $value = $field->get_setting( 'unchecked_value' );
  79. }
  80. }
  81. return $value;
  82. }
  83. /**
  84. * Filter Merge Tag Value
  85. * This is what provides the merge tag with the fields value.
  86. * @since 3.0
  87. *
  88. * @param $value Field value
  89. * @param $field field model
  90. * @return string|void
  91. */
  92. public function filter_merge_tag_value( $value, $field )
  93. {
  94. // If value is true, return checked value setting.
  95. if( $value ) return $field[ 'settings' ][ 'checked_value' ];
  96. // Else return unchecked value setting.
  97. return $field[ 'settings' ][ 'unchecked_value' ];;
  98. }
  99. /**
  100. * Filter Merge Tag Value Calc
  101. * Provides the calculation value when the merge tag is used.
  102. * @since 3.0
  103. *
  104. * @param $value checkbox value
  105. * @param $field field model
  106. * @return $field
  107. */
  108. public function filter_merge_tag_value_calc( $value, $field )
  109. {
  110. // If value is equal to 1...
  111. if ( 1 == $value ) {
  112. // ...return the checked calc value of the field model.
  113. return $field[ 'checked_calc_value' ];
  114. } else {
  115. // ...else return the unchecked calc value of the field model.
  116. return $field[ 'unchecked_calc_value' ];
  117. }
  118. }
  119. /**
  120. * Export Value
  121. * Determines the value to send to submission export.
  122. * @since 3.0
  123. *
  124. * @param $value checkbox field value
  125. * @param $field checkbox field model
  126. * @return string|void
  127. */
  128. public function export_value( $value, $field )
  129. {
  130. // If value is equal to checked or unchecked return the value
  131. if ( __( 'checked', 'ninja-forms' ) == $value ||
  132. __( 'unchecked', 'ninja-forms' ) == $value ) return $value;
  133. // Creating settings variables for our check.
  134. if( is_array( $field ) ) {
  135. // The email action sends teh field variable as an array
  136. $checked_setting = $field[ 'setting' ][ 'checked_value' ];
  137. $unchecked_setting = $field[ 'setting' ][ 'unchecked_value' ];
  138. } else {
  139. $checked_setting = $field->get_setting( 'checked_value' );
  140. $unchecked_setting = $field->get_setting( 'unchecked_value' );
  141. }
  142. // If the the value and check to see if we have checked and unchecked settings...
  143. if ( 1 == $value && ! empty( $checked_setting ) ) {
  144. // ...if we do return checked setting
  145. return $checked_setting;
  146. } elseif ( 0 == $value && ! empty( $unchecked_setting ) ) {
  147. // ...else return unchecked setting.
  148. return $unchecked_setting;
  149. /*
  150. * These checks are for checkbox fields that were created before version 3.2.7.
  151. */
  152. } elseif ( 1 == $value ) {
  153. return __( 'checked', 'ninja-forms' );
  154. } elseif ( 0 == $value ) {
  155. return __( 'unchecked', 'ninja-forms' );
  156. }
  157. }
  158. }